duminică, 6 ianuarie 2013

Arduino Uno si un LED multicolor (RGB) - continuare

   Am gasit la http://luckylarry.co.uk/arduino-projects/3-led-crossfade-with-pwm-and-arduino/ un program mult mai simplu cu variante si mai multe de combinatii de culori.

    Programul (sketch-ul) adaptat de mine, dupa cum am conectate culorile LED-ului multicolor si timpii care mi-au convenit, este urmatorul:

// original sketch from http://luckylarry.co.uk/arduino-projects/3-led-crossfade-with-pwm-and-arduino/
// adapted sketch by niq_ro from http://www.tehnic.go.ro
// Output
int redPin = 11; // Red LED, connected to digital pin 11
int greenPin = 9; // Green LED, connected to digital pin 9
int bluePin = 10; // Blue LED, connected to digital pin 10

// Program variables set as integer (number) type
int redLEDValue = 255; // Variables to store the values to send to the pins
int greenLEDValue = 1; // Initial values are Red full, Green and Blue off
int blueLEDValue = 1; // These values get passed to the analogWrite() function.
int i = 0; // Loop counter

//setup the pins/ inputs & outputs
void setup()
{
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}

// Main program, count to 763, a third of the way switch the incrementing of the LEDs
void loop()
{
i += 1; // Increment counter
if (i < 255) // First phase of fades
{
redLEDValue -= 1; // Red down
greenLEDValue += 1; // Green up
blueLEDValue = 1; // Blue low
}
else if (i < 509) // Second phase of fades
{
redLEDValue = 1; // Red low
greenLEDValue -= 1; // Green down
blueLEDValue += 1; // Blue up
}
else if (i < 763) // Third phase of fades
{
redLEDValue += 1; // Red up
greenLEDValue = 1; // Green low
blueLEDValue -= 1; // Blue down
}
else // Re-set the counter, and start the fades again
{
i = 1;
}// analogWrite() expects 2 parameters, the object/pin and a value between 0 (off) and 255 (full on)
analogWrite(redPin, redLEDValue); // Write current values to LED pins
analogWrite(greenPin, greenLEDValue);
analogWrite(bluePin, blueLEDValue);

delay(20); // Pause for xx milliseconds before resuming the loop
}

iar rezultatul este urmatorul:


Niciun comentariu:

Trimiteți un comentariu