Mission 11 - Color Changing LED
 DuinoKit Essentials   Started by Daniel Alich   2017-06-27 21:32:04 -04:00   Comments: 3    Viewed: 534

  1. Daniel Alich
    Daniel Alich Member Staff Member
    252_1511882146_Mission 11 – Color Chan


    /*
    Demonstrates the use a 16x2 LCD display. The LiquidCrystal
    library works with all LCD displays that are compatible with the
    Hitachi HD44780 driver. There are many of them out there, and you
    can usually tell them by the 16-pin interface.
    This sketch prints "Hello World!" to the LCD
    and shows the time.

    The circuit:
    LCD VSS pin the GND
    LCD R/W pin to GND
    LCD Vcc to 5V
    Potentiometer for contrast is built into DuinoKit
    LCD RS pin to digital pin 12
    LCD E pin to digital pin 11
    LCD D4 pin to digital pin 5
    LCD D5 pin to digital pin 4
    LCD D6 pin to digital pin 3
    LCD D7 pin to digital pin 2
    To hook up LCD BackLight
    LCD A_LED pin to 5V
    LCD K_LED pin to GND

    Library originally added 18 Apr 2008
    by David A. Mellis
    library modified 5 Jul 2009
    by Limor Fried (http://www.ladyada.net)
    example added 9 Jul 2009
    by Tom Igoe
    modified 22 Nov 2010
    by Tom Igoe

    This example code is in the public domain.
    http://www.arduino.cc/en/Tutorial/LiquidCrystal
    */
    // include the library code:
    #include
    // initialize the library with the numbers of the interface pins
    LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);
    // (RS, E, D4, D5, D6, D7)
    // Added for RGB color fader LED
    int redPin = 9; // Red LED, connected to digital pin 9
    int grnPin = 10; // Green LED, connected to digital pin 10
    int bluPin = 6; // Blue LED, connected to digital pin 11
    // Color arrays
    int black[3] = { 0, 0, 0 };
    int white[3] = { 100, 100, 100 };
    int red[3] = { 100, 0, 0 };
    int green[3] = { 0, 100, 0 };
    int blue[3] = { 0, 0, 100 };
    int yellow[3] = { 40, 95, 0 };
    int dimWhite[3] = { 30, 30, 30 };
    // etc.
    // Set initial color
    int redVal = black[0];
    int grnVal = black[1];
    int bluVal = black[2];
    int wait = 10; // 10ms internal crossFade delay; increase for slower fades
    int hold = 0; // Optional hold when a color is complete, before the next crossFade
    int DEBUG = 1; // DEBUG counter; if set to 1, will write values back via serial
    int loopCount = 60; // How often should DEBUG report?
    int repeat = 3000; // How many times should we loop before stopping? (0 for no stop)
    int j = 0; // Loop counter for repeat
    // Initialize color variables
    int prevR = redVal;
    int prevG = grnVal;
    int prevB = bluVal;
    void setup() {
    // set up the LCD's number of columns and rows:
    lcd.begin(16, 2);
    // Print a message to the LCD.
    lcd.print("Inspire & Invent");
    pinMode(redPin, OUTPUT); // sets the pins as output
    pinMode(grnPin, OUTPUT);
    pinMode(bluPin, OUTPUT);
    if (DEBUG) { // If we want to see values for debugging... Serial.begin(9600); // ...set up the serial ouput
    }
    }

    void loop() {
    // set the cursor to column 0, line 1
    // (note: line 1 is the second row, since counting begins with 0):
    // lcd.setCursor(0, 1);
    // print the number of seconds since reset:
    // lcd.print("DuinoKit.com ");
    // lcd.print(millis()/1000);
    // Added for RGB LED
    crossFade(red);
    crossFade(green);
    crossFade(blue);
    crossFade(yellow);
    if (repeat) { // Do we loop a finite number of times?
    j += 1;
    if (j >= repeat) { // Are we there yet?
    exit(j); // If so, stop.
    }
    }
    }
    int calculateStep(int prevValue, int endValue) {
    int step = endValue - prevValue; // What's the overall gap?
    if (step) { // If its non-zero,
    step = 1020/step; // divide by 1020
    }
    return step;
    }
    int calculateVal(int step, int val, int i) {
    if ((step) && i % step == 0) { // If step is non-zero and its time to change a value,
    if (step > 0) { // increment the value if step is positive...
    val += 1;
    }
    else if (step < 0) { // ...or decrement it if step is negative
    val -= 1;
    }
    }
    // Defensive driving: make sure val stays in the range 0-255
    if (val > 255) {
    val = 255;
    }
    else if (val < 0) {
    val = 0;
    }
    return val;
    }
    void crossFade(int color[3]) {
    // Convert to 0-255
    int R = (color[0] * 255) / 100;
    int G = (color[1] * 255) / 100;
    int B = (color[2] * 255) / 100;
    int stepR = calculateStep(prevR, R);
    int stepG = calculateStep(prevG, G);
    int stepB = calculateStep(prevB, B);
    for (int i = 0; i <= 1020; i++) {
    redVal = calculateVal(stepR, redVal, i);
    grnVal = calculateVal(stepG, grnVal, i);
    bluVal = calculateVal(stepB, bluVal, i);
    analogWrite(redPin, redVal); // Write current values to LED pins
    analogWrite(grnPin, grnVal);
    analogWrite(bluPin, bluVal);
    delay(wait); // Pause for 'wait' milliseconds before resuming the loop
    if (DEBUG) { // If we want serial output, print it at the
    if (i == 0 or i % loopCount == 0) { // beginning, and every loopCount times
    Serial.print("Loop/RGB: #");
    Serial.print(i);
    Serial.print(" | ");
    lcd.setCursor(0, 1);
    lcd.print(" ");
    lcd.setCursor(0, 1);
    lcd.print(redVal);
    lcd.setCursor(3, 1);
    lcd.print(" / ");
    lcd.setCursor(6, 1);
    lcd.print(" ");
    lcd.setCursor(6, 1);
    lcd.print(grnVal);
    lcd.setCursor(9, 1);
    lcd.print(" / ");
    lcd.setCursor(12, 1);
    lcd.print(" ");
    lcd.setCursor(12, 1);
    lcd.print(bluVal);
    }
    DEBUG += 1;
    }
    }
    // Update current values for next loop
    prevR = redVal;
    prevG = grnVal;
    prevB = bluVal;
    delay(hold); // Pause for optional 'wait' milliseconds before resuming the loop
    }
    Daniel Alich, 2017-06-27 21:32:04 -04:00
  2. Josh Rawls
    Josh Rawls Member St. Louis, MO
    The wiring digram and the code don't match. The code is correct, the graphic needs to be corrected. Thanks!
    Josh Rawls, 2018-08-01 11:28:31 -04:00
  3. Daniel Alich
    Daniel Alich Member Staff Member
    Can you be more specifict about what in the graphic needs to be corrected?
    Daniel Alich, 2018-08-04 11:45:03 -04:00
  4. (You must log in or sign up to post here)

Report Post

Write your reason

Log in | Sign up
Terms & Conditions!
Help!