// rgb_led.pde - Switch LED colours based on serial input
// ver 0.1
// Jody K Delauney
// jodydelauney@elder-n00b.org
// 2009/04/20
//
// Copyright (c) 2009, Jody K Delauney
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
int incomingByte = 0; // for incoming serial data
byte r_pin = PIN_B7;
byte g_pin = PIN_C5;
byte b_pin = PIN_C6;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
pinMode(PIN_D6, OUTPUT); // Set on board LED pin to output so startup can be monitored
digitalWrite(PIN_D6, LOW);
delay(1000);
digitalWrite(PIN_D6, HIGH);
delay(1000);
digitalWrite(PIN_D6, LOW); // Delays to give host a chance to setup serial port
pinMode(r_pin, OUTPUT); // Set PWM ports to output so we can make the pretty colours
pinMode(g_pin, OUTPUT); //
pinMode(b_pin, OUTPUT); //
Serial.flush(); // Clear out anything that might be lingering in the buffer
digitalWrite(PIN_D6, HIGH); // Turn off on board LED
}
void loop() {
// send data only when you receive data:
incomingByte = Serial.read(); // Read byte from buffer.
if (incomingByte >= 0) { // Would use Serial.available in the if but Teensyduino doesn't like it.
// set colours as follows
// R = full Red t = half Red r = no Red
// G = full Green h = half Green g = no Green
// B = full Blue n = half Blue b = no Blue
if (char(incomingByte) == 'Y') {
analogWrite(r_pin, 255);
analogWrite(g_pin, 125);
}
else if (char(incomingByte) == 'y') {
analogWrite(r_pin, 0);
analogWrite(g_pin, 0);
}
else if (char(incomingByte) == 'u') {
analogWrite(r_pin, 125);
analogWrite(g_pin, 125);
}
else if (char(incomingByte) == 'r') {
analogWrite(r_pin, 0);
}
else if (char(incomingByte) == 'R') {
analogWrite(r_pin, 255);
}
else if (char(incomingByte) == 't') {
analogWrite(r_pin, 125);
}
else if (char(incomingByte) == 'g') {
analogWrite(g_pin, 0);
}
else if (char(incomingByte) == 'G') {
analogWrite(g_pin, 255);
}
else if (char(incomingByte) == 'h') {
analogWrite(g_pin, 125);
}
else if (char(incomingByte) == 'b') {
analogWrite(b_pin, 0);
}
else if (char(incomingByte) == 'B') {
analogWrite(b_pin, 255);
}
else if (char(incomingByte) == 'n') {
analogWrite(b_pin, 125);
}
}
}