|
Arduino RGB LED controller
As a fun off season project, I decided to try to make a light controller for out robot. My idea was to use 3 digital outputs from the Digital Side Car to connect to the Arduino, and have 8 lighting combinations. The arduino's 6 PWM's will control r/g/b on two separate lighting zones. Also, the lights on the second zone will be divided into two separate zones, which can be switched on and off with a relay. I've made the circuit in the attached schematic(replaced the solid state relay w/ a normal one), and I have powered it with a 12 volt supply and have used signal generators I made from a 555 timer, and I am able to get the lights to function the way I want. I am not sure programming the Arduino. I have used them in the past, but I have yet to pick one up from radio shack. (The Radio Shack near me has the Arduino Uno!) I have my program set up with 8 different light zones. The program will go into one of the 8 modes based on the controls from the DSC and certain modes will either have red/blue lights (red/blue alliances), that can be switched by a toggle switch on the robot. The code is designed so that when I tell it to switch modes, it will finish the light sequence it has started. Before I go out and buy the Arduino I want to make sure that my code makes some sense so it would be helpful if somebody could check over my code. I've made this code without ever testing it so I'm sure that there are some mistakes. REDPIN/BLUEPIN/GREENPIN are the lights without the relay, and REDPIN2/BLUEPIN2/GREENPIN2 are lights with the relay.
Code:
//LED Control
//1-in1
//2-in2
//3-r1
//4-in3
//5-g1
//6-b1
//7-relay1
//8-relay2
//9-r2
//10-g2
//11-b2
//12-alliance color selector
//Set PWM pins
#define REDPIN 3
#define GREENPIN 5
#define BLUEPIN 6
#define REDPIN2 9
#define GREENPIN2 10
#define BLUEPIN2 11
//Set Digital Input Pins
#define in1 1
#define in2 2
#define in3 4
#define alliance 12
//Set Signal Light Relays
#define relay1 7
#define relay2 8
#define del 1
//Input Values
int val1 = 0;
int val2 = 0;
int val3 = 0;
int allval = 0;
//Alliance Color r/b vals
int rval = 0;
int bval = 0;
//Light mode from inputs
int lightMode = 0;
//Rainbow counters
int r;
int g;
int b;
int rr;
int gg;
int bb;
void setup() {
//define pin modes
pinMode(REDPIN, OUTPUT);
pinMode(GREENPIN, OUTPUT);
pinMode(BLUEPIN, OUTPUT);
pinMode(REDPIN2, OUTPUT);
pinMode(GREENPIN2, OUTPUT);
pinMode(BLUEPIN2, OUTPUT);
pinMode(in1, INPUT);
pinMode(in2, INPUT);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
randomSeed(analogRead(0));
//This code MAY be needed
//Serial.begin(9600);
}
void loop() {
//Read inputs and store them in val
val1 = digitalRead(in1);
val2 = digitalRead(in2);
val3 = digitalRead(in3);
//Get r/b alliance
allval = digitalRead(alliance);
//Determine which light mode is selected
if (val3 == 0 && val2 == 0)
{
if (val1 == 0)
{
lightMode = 0;
}
else
{
lightMode = 1;
}
}
if (val3 == 0 && val2 == 1)
{
if (val1 == 0)
{
lightMode = 2;
}
else
{
lightMode = 3;
}
}
if (val3 == 1 && val2 == 0)
{
if (val1 == 0)
{
lightMode = 4;
}
else
{
lightMode = 5;
}
}
if (val3 == 1 && val2 == 1)
{
if (val1 == 0)
{
lightMode = 6;
}
else
{
lightMode = 7;
}
}
//Set relays and PWMs depending on light mode
if (lightMode == 1)
{
//rainbow mode
//this sequence will make a rainbow thing
//Set both relays to on
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
//set PWMs
for (g = 0; g>255; g++){
analogWrite(REDPIN, 255);
analogWrite(GREENPIN, g);
analogWrite(BLUEPIN, 0);
analogWrite(REDPIN2, 0);
analogWrite(GREENPIN2, g);
analogWrite(BLUEPIN2, 255);
delay(del);
}
for (r = 255; r < 1; r = r - 1){
analogWrite(REDPIN, r);
analogWrite(GREENPIN, 255);
analogWrite(BLUEPIN, 0);
analogWrite(REDPIN2, 0);
analogWrite(GREENPIN2, r);
analogWrite(BLUEPIN2, 255-r);
delay(del);
}
for (b = 0; b>255; b++){
analogWrite(REDPIN, 0);
analogWrite(GREENPIN, 255-b); //may just be 255?
analogWrite(BLUEPIN, b);
analogWrite(REDPIN2, 255-b);
analogWrite(GREENPIN2, 255);
analogWrite(BLUEPIN2, 0);
delay(del);
}
for (g = 0; g>255; g++){
analogWrite(REDPIN, 0);
analogWrite(GREENPIN, g);
analogWrite(BLUEPIN, 255);
analogWrite(REDPIN2, 255);
analogWrite(GREENPIN2, g);
analogWrite(BLUEPIN2, 0);
delay(del);
}
}
if (lightMode == 0)
{
//Set both relays on
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
//Figure out alliance color values
if (allval == 0)
{
rval = 0;
bval = 255;
}
if (allval == 1)
{
rval = 255;
bval = 0;
}
analogWrite(REDPIN, rval);
analogWrite(GREENPIN, 0);
analogWrite(BLUEPIN, bval);
analogWrite(REDPIN2, rval);
analogWrite(GREENPIN2, 0);
analogWrite(BLUEPIN2, bval);
}
if (lightMode == 2)
{
//Alliance 2 (Flashing Singal Light alliance color)
//Figure out alliance color values
if (allval == 0)
{
rval = 0;
bval = 255;
}
if (allval == 1)
{
rval = 255;
bval = 0;
}
for(r = 0; r>1000; r++){
analogWrite(REDPIN, rval);
analogWrite(GREENPIN, 0);
analogWrite(BLUEPIN, bval);
analogWrite(REDPIN2, rval);
analogWrite(GREENPIN2, 0);
analogWrite(BLUEPIN2, bval);
if (r < 500)
{
digitalWrite(relay1, HIGH);
digitalWrite(relay2, LOW);
}
if (r > 500)
{
digitalWrite(relay1, LOW);
digitalWrite(relay2, HIGH);
}
}
}
if (lightMode == 3)
{
//Autonomous (All yellow)
analogWrite(REDPIN, 255);
analogWrite(GREENPIN, 255);
analogWrite(BLUEPIN, 0);
analogWrite(REDPIN2, 255);
analogWrite(GREENPIN2, 255);
analogWrite(BLUEPIN2, 0);
digitalWrite(relay1, HIGH);
digitalWrite(relay2, LOW);
}
if (lightMode == 4)
{
//Autonomous 2 (Yellow w/ flashing signal light)
for(r = 0; r>1000; r++){
analogWrite(REDPIN, 255);
analogWrite(GREENPIN, 255);
analogWrite(BLUEPIN, 0);
analogWrite(REDPIN2, 255);
analogWrite(GREENPIN2, 255);
analogWrite(BLUEPIN2, 0);
if (r < 500)
{
digitalWrite(relay1, HIGH);
digitalWrite(relay2, LOW);
}
if (r > 500)
{
digitalWrite(relay1, LOW);
digitalWrite(relay2, HIGH);
}
}
if (lightMode == 5)
{
//Red and Blue!
for(r = 0; r>1000; r++){
if (r < 500)
{
digitalWrite(relay1, HIGH);
digitalWrite(relay2, LOW);
analogWrite(REDPIN, 0);
analogWrite(GREENPIN, 0);
analogWrite(BLUEPIN, 255);
analogWrite(REDPIN2, 255);
analogWrite(GREENPIN2, 0);
analogWrite(BLUEPIN2, 0);
}
if (r > 500)
{
digitalWrite(relay1, LOW);
digitalWrite(relay2, HIGH);
analogWrite(REDPIN, 255);
analogWrite(GREENPIN, 0);
analogWrite(BLUEPIN, 0);
analogWrite(REDPIN2, 0);
analogWrite(GREENPIN2, 0);
analogWrite(BLUEPIN2, 255);
}
}
if (lightMode == 6)
{
analogWrite(REDPIN, 0);
analogWrite(GREENPIN, 255);
analogWrite(BLUEPIN, 0);
analogWrite(REDPIN2, 0);
analogWrite(GREENPIN2, 255);
analogWrite(BLUEPIN2, 0);
}
if (lightMode == 7)
{
//Alliance color with white signal light
if (allval == 0)
{
rval = 0;
bval = 255;
}
if (allval == 1)
{
rval = 255;
bval = 0;
}
analogWrite(REDPIN, rval);
analogWrite(GREENPIN, 0);
analogWrite(BLUEPIN, bval);
analogWrite(REDPIN2, 255);
analogWrite(GREENPIN2, 255);
analogWrite(BLUEPIN2, 255);
}
}}}
Sorry for the long post
Last edited by apples000 : 02-05-2012 at 19:43.
Reason: Forgot to attach file
|