ArduShop.ro

ArduShop.ro
cod Z4A413ZN reducere 5%, pentru vizitatorii paginilor mele !

luni, 31 decembrie 2012

Arduino Uno si un LED multicolor (RGB)

     Un cod adaptat dupa cel de la http://learn.adafruit.com/rgb-led-strips/example-code unde se folosea o banda RGB cu anod comun, iar eu folosesc un LED RGB cu catod comun (OSTA5131A-C, vezi datasheet-ul de la http://www.interelcom.ru/files/doc/osta5131a-c.pdf) este urmatorul:

// color swirl! connect an RGB LED to the PWM pins as indicated
// in the #defines
// public domain, enjoy!
// original sketch from http://learn.adafruit.com/rgb-led-strips/example-code
// adapted sketch by niq_ro ( http://www.tehnic.go.ro )

#define REDPIN 11 
#define GREENPIN 9
#define BLUEPIN 10

#define FADESPEED 30     // make this higher to slow down

void setup() {
  pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
}

void loop() {
  int r, g, b;

// fade from black to white
  for (r = 0; r < 256; r++) { 
    analogWrite(REDPIN, r);
    analogWrite(GREENPIN, r);
    analogWrite(BLUEPIN, r);
    delay(FADESPEED);
  }
// fade from white to black
  for (r = 255; r > 0; r--) { 
    analogWrite(REDPIN, r);
    analogWrite(GREENPIN, r);
    analogWrite(BLUEPIN, r);
    delay(FADESPEED);
  }
// fade from black to red
  for (r = 0; r < 256; r++) { 
    analogWrite(REDPIN, r);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, 0);
    delay(FADESPEED);
  }
// fade from red to black
  for (r = 255; r > 0; r--) { 
    analogWrite(REDPIN, r);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, 0);
    delay(FADESPEED);
  }
// fade from black to green
  for (g = 0; g < 256; g++) { 
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, g);
    analogWrite(BLUEPIN, 0);
    delay(FADESPEED);
  }
// fade from green to black
  for (g = 255; g > 0; g--) { 
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, g);
    analogWrite(BLUEPIN, 0);
    delay(FADESPEED);
  }
// fade from black to blue
  for (b = 0; b < 256; b++) { 
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  }
// fade from blue to black
  for (b = 255; b > 0; b--) { 
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  }

// fade from black to red
  for (r = 0; r < 256; r++) { 
    analogWrite(REDPIN, r);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, 0);
    delay(FADESPEED);
  }
// fade from red to green + red
  for (g = 0; g < 256; g++) { 
    analogWrite(REDPIN, 255);
    analogWrite(GREENPIN, g);
    analogWrite(BLUEPIN, 0);
    delay(FADESPEED);
  }
// fade from green red to green
  for (r = 255; r > 0; r--) { 
    analogWrite(REDPIN, r);
    analogWrite(GREENPIN, 255);
    analogWrite(BLUEPIN, 0);
    delay(FADESPEED);
  }
// fade from green to black
  for (g = 255; g > 0; g--) { 
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, g);
    analogWrite(BLUEPIN, 0);
    delay(FADESPEED);
  }
// fade from black to green
  for (g = 0; g < 256; g++) { 
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, g);
    analogWrite(BLUEPIN, 0);
    delay(FADESPEED);
  }
// fade from green to blue + green
  for (b = 0; b < 256; b++) { 
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, 255);
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  }
// fade from green + blue to blue
  for (g = 255; g > 0; g--) { 
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, g);
    analogWrite(BLUEPIN, 255);
    delay(FADESPEED);
  }
// fade from blue to black
  for (b = 255; b > 0; b--) { 
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  }
// fade from black to blue
  for (b = 0; b < 256; b++) { 
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  }
// fade from blue to red + blue
  for (r = 0; r < 256; r++) { 
    analogWrite(REDPIN, r);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, 255);
    delay(FADESPEED);
  }
// fade from red + blue to red
  for (b = 255; b > 0; b--) { 
    analogWrite(REDPIN, 255);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  }
// fade from red to black
  for (r = 255; r > 0; r--) { 
    analogWrite(REDPIN, r);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, 0);
    delay(FADESPEED);
  }
}

    Un prim filmulet este:

apoi am pus un pahar peste LED si am obtinut:


    Am pus o rezistenta de 180 de ohmi la LED-ul rosu si cate una de 100 de ohmi la cel verde, respectiv albastru sa am cam 17mA prin ele.

    Schema de conectare este urmatoarea:


    Un alt program (sketch) este acela in care culorile de baza "stau" 2 secunde:


// original sketch from http://learn.adafruit.com/rgb-led-strips/example-code
// adapted sketch by niq_ro ( http://www.tehnic.go.ro )

#define REDPIN 11 
#define GREENPIN 9
#define BLUEPIN 10

#define FADESPEED 20
// make this higher to slow down

void setup() {
  pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
}


void loop() {
  int r, g, b;

// fade from black to white
  for (r = 0; r < 256; r++) { 
    analogWrite(REDPIN, r);
    analogWrite(GREENPIN, r);
    analogWrite(BLUEPIN, r);
    delay(FADESPEED);
  }
 // stay in white 2 second
    analogWrite(REDPIN, 255);
    analogWrite(GREENPIN, 255);
    analogWrite(BLUEPIN, 255);
    delay(2000);
// fade from white to black
  for (r = 255; r > 0; r--) { 
    analogWrite(REDPIN, r);
    analogWrite(GREENPIN, r);
    analogWrite(BLUEPIN, r);
    delay(FADESPEED);
  }
// fade from black to red
  for (r = 0; r < 256; r++) { 
    analogWrite(REDPIN, r);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, 0);
    delay(FADESPEED);
  }
 // stay in red 2 second
    analogWrite(REDPIN, 255);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, 0);
    delay(2000);
// fade from red to black
  for (r = 255; r > 0; r--) { 
    analogWrite(REDPIN, r);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, 0);
    delay(FADESPEED);
  }
// fade from black to green
  for (g = 0; g < 256; g++) { 
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, g);
    analogWrite(BLUEPIN, 0);
    delay(FADESPEED);
  }
 // stay in green 2 second
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, 255);
    analogWrite(BLUEPIN, 0);
    delay(2000);
// fade from green to black
  for (g = 255; g > 0; g--) { 
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, g);
    analogWrite(BLUEPIN, 0);
    delay(FADESPEED);
  }
// fade from black to blue
  for (b = 0; b < 256; b++) { 
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  }
 // stay in blue 2 second
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, 255);
    delay(2000);
// fade from blue to black
  for (b = 255; b > 0; b--) { 
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  }
// fade from black to red
  for (r = 0; r < 256; r++) { 
    analogWrite(REDPIN, r);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, 0);
    delay(FADESPEED);
  }
 // stay in red 2 second
    analogWrite(REDPIN, 255);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, 0);
    delay(2000);
// fade from red to green + red
  for (g = 0; g < 256; g++) { 
    analogWrite(REDPIN, 255);
    analogWrite(GREENPIN, g);
    analogWrite(BLUEPIN, 0);
    delay(FADESPEED);
  }
 // stay in white 2 second
    analogWrite(REDPIN, 255);
    analogWrite(GREENPIN, 255);
    analogWrite(BLUEPIN, 0);
    delay(2000);
// fade from green red to green
  for (r = 255; r > 0; r--) { 
    analogWrite(REDPIN, r);
    analogWrite(GREENPIN, 255);
    analogWrite(BLUEPIN, 0);
    delay(FADESPEED);
  }
 // stay in green 2 second
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, 255);
    analogWrite(BLUEPIN, 0);
    delay(2000);
// fade from green to black
  for (g = 255; g > 0; g--) { 
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, g);
    analogWrite(BLUEPIN, 0);
    delay(FADESPEED);
  }
// fade from black to green
  for (g = 0; g < 256; g++) { 
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, g);
    analogWrite(BLUEPIN, 0);
    delay(FADESPEED);
  }
 // stay in green 2 second
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, 255);
    analogWrite(BLUEPIN, 0);
    delay(2000);
// fade from green to blue + green
  for (b = 0; b < 256; b++) { 
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, 255);
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  }
 // stay in blue+green 2 second
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, 255);
    analogWrite(BLUEPIN, 255);
    delay(2000);
// fade from green + blue to blue
  for (g = 255; g > 0; g--) { 
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, g);
    analogWrite(BLUEPIN, 255);
    delay(FADESPEED);
  }
 // stay in blue 2 second
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, 255);
    delay(2000);
// fade from blue to black
  for (b = 255; b > 0; b--) { 
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  }
// fade from black to blue
  for (b = 0; b < 256; b++) { 
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  }
 // stay in blue 2 second
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, 255);
    delay(2000);
// fade from blue to red + blue
  for (r = 0; r < 256; r++) { 
    analogWrite(REDPIN, r);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, 255);
    delay(FADESPEED);
  }
 // stay in red+blue 2 second
    analogWrite(REDPIN, 255);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, 255);
    delay(2000);
// fade from red + blue to red
  for (b = 255; b > 0; b--) { 
    analogWrite(REDPIN, 255);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  }
 // stay in red 2 second
    analogWrite(REDPIN, 255);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, 0);
    delay(2000);
// fade from red to black
  for (r = 255; r > 0; r--) { 
    analogWrite(REDPIN, r);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, 0);
    delay(FADESPEED);
  }
}

   La acestea filmuletele cu functionarea sunt:


  Am modificat putin programul pentru a realiza un joc de lumini mai dinamic si am obtinut:



iar codul este urmatorul:


// original sketch from http://learn.adafruit.com/rgb-led-strips/example-code
// adapted sketch by niq_ro ( http://www.tehnic.go.ro )

#define REDPIN 11 
#define GREENPIN 9
#define BLUEPIN 10

#define FADESPEED 2
// make this higher to slow down

#define INTARZIERE 300

void setup() {
  pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
}


void loop() {
  int r, g, b;

 // flasher LED
 for (int i=0; i <= 10; i++){
   delay(INTARZIERE);
    analogWrite(REDPIN, 255);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, b);
  delay(INTARZIERE);
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, 255);
    analogWrite(BLUEPIN, b);
  delay(INTARZIERE);
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, 255);
  delay(INTARZIERE);
    analogWrite(REDPIN, 255);
    analogWrite(GREENPIN, 255);
    analogWrite(BLUEPIN, b);
  delay(INTARZIERE);
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, 255);
    analogWrite(BLUEPIN, 255);
  delay(INTARZIERE);
    analogWrite(REDPIN, 255);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, 255);
  delay(INTARZIERE);
    analogWrite(REDPIN, 255);
    analogWrite(GREENPIN, 255);
    analogWrite(BLUEPIN, 255);
 }

// fade from black to white
  for (r = 0; r < 256; r++) { 
    analogWrite(REDPIN, r);
    analogWrite(GREENPIN, r);
    analogWrite(BLUEPIN, r);
    delay(FADESPEED);
  }
 // stay in white 
    delay(INTARZIERE);
// fade from white to black
  for (r = 255; r > 0; r--) { 
    analogWrite(REDPIN, r);
    analogWrite(GREENPIN, r);
    analogWrite(BLUEPIN, r);
    delay(FADESPEED);
  }
// fade from black to red
  for (r = 0; r < 256; r++) { 
    analogWrite(REDPIN, r);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, 0);
    delay(FADESPEED);
  }
 // stay in red 
     delay(INTARZIERE);
// fade from red to black
  for (r = 255; r > 0; r--) { 
    analogWrite(REDPIN, r);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, 0);
    delay(FADESPEED);
  }
// fade from black to green
  for (g = 0; g < 256; g++) { 
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, g);
    analogWrite(BLUEPIN, 0);
    delay(FADESPEED);
  }
 // stay in green
    delay(INTARZIERE);
// fade from green to black
  for (g = 255; g > 0; g--) { 
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, g);
    analogWrite(BLUEPIN, 0);
    delay(FADESPEED);
  }
// fade from black to blue
  for (b = 0; b < 256; b++) { 
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  }
 // stay in blue
    delay(INTARZIERE);
// fade from blue to black
  for (b = 255; b > 0; b--) { 
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  }
// fade from black to red
  for (r = 0; r < 256; r++) { 
    analogWrite(REDPIN, r);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, 0);
    delay(FADESPEED);
  }
 // stay in red
    delay(INTARZIERE);
// fade from red to green + red
  for (g = 0; g < 256; g++) { 
    analogWrite(REDPIN, 255);
    analogWrite(GREENPIN, g);
    analogWrite(BLUEPIN, 0);
    delay(FADESPEED);
  }
 // stay in white
    delay(INTARZIERE);
// fade from green red to green
  for (r = 255; r > 0; r--) { 
    analogWrite(REDPIN, r);
    analogWrite(GREENPIN, 255);
    analogWrite(BLUEPIN, 0);
    delay(FADESPEED);
  }
 // stay in green
    delay(INTARZIERE);
// fade from green to black
  for (g = 255; g > 0; g--) { 
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, g);
    analogWrite(BLUEPIN, 0);
    delay(FADESPEED);
  }
// fade from black to green
  for (g = 0; g < 256; g++) { 
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, g);
    analogWrite(BLUEPIN, 0);
    delay(FADESPEED);
  }
 // stay in green
    delay(INTARZIERE);
// fade from green to blue + green
  for (b = 0; b < 256; b++) { 
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, 255);
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  }
 // stay in blue+green
    delay(INTARZIERE);
// fade from green + blue to blue
  for (g = 255; g > 0; g--) { 
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, g);
    analogWrite(BLUEPIN, 255);
    delay(FADESPEED);
  }
 // stay in blue
    delay(INTARZIERE);
// fade from blue to black
  for (b = 255; b > 0; b--) { 
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  }
// fade from black to blue
  for (b = 0; b < 256; b++) { 
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  }
 // stay in blue
    delay(INTARZIERE);
// fade from blue to red + blue
  for (r = 0; r < 256; r++) { 
    analogWrite(REDPIN, r);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, 255);
    delay(FADESPEED);
  }
 // stay in red+blue
    delay(INTARZIERE);
// fade from red + blue to red
  for (b = 255; b > 0; b--) { 
    analogWrite(REDPIN, 255);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  }
 // stay in red
    delay(INTARZIERE);
// fade from red to black
  for (r = 255; r > 0; r--) { 
    analogWrite(REDPIN, r);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, 0);
    delay(FADESPEED);
  }
}