Arduino 3: LED - - RGB
共陰極 RGB LED
使用random()來隨機產生數值:
- #define LEDR 9
- #define LEDG 10
- #define LEDB 11
- void setup() {
- pinMode(LEDR, OUTPUT);
- pinMode(LEDG, OUTPUT);
- pinMode(LEDB, OUTPUT);
- }
- int r = 0;
- int g = 0;
- int b = 0;
- void loop() {
- r = random(0, 255);
- g = random(0, 255);
- b = random(0, 255);
- analogWrite(LEDR, r);
- analogWrite(LEDG, g);
- analogWrite(LEDB, b);
- delay(1000);
- }
改用[可變電阻]改變不同的R、G、B數值:
- #define LEDR 9
- #define LEDG 10
- #define LEDB 11
- void setup() {
- pinMode(LEDR, OUTPUT);
- pinMode(LEDG, OUTPUT);
- pinMode(LEDB, OUTPUT);
- Serial.begin(9600);
- }
- int r = 0;
- int g = 0;
- int b = 0;
- void loop() {
- int rREAD=analogRead(0);
- int gREAD=analogRead(1);
- int bREAD=analogRead(2);
- //r = random(0, 255);
- //g = random(0, 255);
- //b = random(0, 255);
- r = map(rREAD, 0,1023, 0,255);
- g = map(gREAD, 0,1023, 0,255);
- b = map(bREAD, 0,1023, 0,255);
-
- Serial.println(r);
- Serial.println(g);
- Serial.println(b);
-
- analogWrite(LEDR, r);
- analogWrite(LEDG, g);
- analogWrite(LEDB, b);
- delay(100);
- }
Attiny85
- // This program is in the public domain.
- // This sketch waits for an IR pulse from a remote control
- // and then randomly changes the color of an RGB LED
- int ledPinRED = 2; // LED on digital pin 2
- int ledPinGREEN = 1; // LED on digital pin 1
- int ledPinBLUE = 4; // LED on digital pin 0
- int inPin = 0; // the input pin for the IR phototransistor
- int randRED = 0;
- int randGREEN = 0;
- int randBLUE = 0;
- void setup() {
- pinMode(inPin, INPUT); // declare IR phototransistor as input
- }
- void loop(){
- while(digitalRead(inPin) != LOW) {}; // read input value
-
- randRED = random(255); // picking a random number
- randGREEN = random(255); // between 1 and 255
- randBLUE = random(255);
-
- analogWrite(ledPinRED, randRED);
- analogWrite(ledPinGREEN, randGREEN);
- analogWrite(ledPinBLUE, randBLUE);
- delay(100); // de-bounces the input so it does not zoom
- // through a zillion colors with every button click
- }
沒有留言:
張貼留言