共陽極全彩LED彩虹燈

V R G B 接線圖

 
共陽極全彩LED燈模組


//程式碼如下:


// 共陽極全彩LED燈模組的V接5V     R接pin9    G接pin10    B接pin11

// 定義RGB引腳

#define RED_PIN 9

#define GREEN_PIN 10

#define BLUE_PIN 11


void setup() {

  pinMode(RED_PIN, OUTPUT);

  pinMode(GREEN_PIN, OUTPUT);

  pinMode(BLUE_PIN, OUTPUT);

}

void loop() {

  rainbowCycle(70); // 調整速度

}

void rainbowCycle(int wait) {

  for (int i = 255; i > 0; i--) {

    setColorWheel(i);

    delay(wait);

  }

}


void setColorWheel(int pos) {

  if (pos < 85) {

    setColor(pos * 3, 255 - pos * 3, 0);

  } else if (pos < 170) {

    pos -= 85;

    setColor(255 - pos * 3, 0, pos * 3);


  } else {

    pos -= 170;

    setColor(0, pos * 3, 255 - pos * 3);

  }

}


void setColor(int red, int green, int blue) {

  analogWrite(RED_PIN, 255 - red);   // 共陽極

  analogWrite(GREEN_PIN, 255 - green);

  analogWrite(BLUE_PIN, 255 - blue);

}

留言