關鍵一:
Arduino Mini
腳位接到數位IO腳 3, 5, 6, 9, 10, 11 才能做輸出.. !!!
關鍵二:
此 RGB LED circuit 標示錯誤 - 負極..實際上要接 +正極,
R/G/B 接 Digital Pin, 共4線.. 不須接地線.
影片:
程式碼:
int R_ledPin = 9; int G_ledPin = 10; int B_ledPin = 11; int ledPin = 13; // val 255 = off int R_val=254; int G_val=254; int B_val=254; void setup() { pinMode(ledPin, OUTPUT); pinMode( R_ledPin, OUTPUT); pinMode( G_ledPin, OUTPUT); pinMode( B_ledPin, OUTPUT); Serial.begin ( 9600 ); analogWrite ( R_ledPin, R_val ); analogWrite ( G_ledPin, G_val ); analogWrite ( B_ledPin, B_val ); delay ( 1000 ); } int dir=0; int color = 0; int offset = 5; void loop() { digitalWrite(ledPin, HIGH); switch ( color ) { case 0: analogWrite ( R_ledPin, R_val ); break; case 1: analogWrite ( G_ledPin, R_val ); break; case 2: analogWrite ( B_ledPin, R_val ); break; } if ( dir == 0 ) { if ( R_val - offset > 0 ) { R_val -= offset; } else { R_val = 0; dir = 1; } } if ( dir == 1 ) { if ( R_val + offset < 255 ) { R_val += offset; } else { R_val = 254; dir = 0; if ( color + 1 < 3 ) color ++; else color = 0; } } //delay ( 300 ); clearLCD (); // 清除 LCD 畫面 // 輸出數值到 LCD Serial.print ( "R= "); Serial.print ( R_val, DEC ); Serial.print ( "\n"); } // ===== clear the LCD ===== void clearLCD() { Serial.print(12, BYTE); }
沒有留言:
張貼留言