Hey Guys,
I have pieced together this code. it is supposed to be a 2 state lamp.
1 state allows manual control using 3 pots to control an RGB LED.
The other state runs through a 'Rainbow Effect'. the modes should be changeable by a switch I have connected.
I have kinda got it working, but not.. lol
At boot it enters 'rainbow' mode. if i hold my finger on the switch (i have to do this for a certain period of time, i presume until its finished the color cycle and looks at the button) i get the little green tx light on the board and the monitor reads 'pressed'
however, no change to its behavior. i.e it doesn't enter 'manual' mode
Here's my code.. anything jumping out at you? I must admit I am pretty much a noob here.
Thanks in advance.
//===============================================================
// Global Variables & Constants
//===============================================================
//Init the Pins used for PWM
const int redPin = 9; // LED1 ANODE
const int greenPin = 10; // LED2 ANODE
const int bluePin = 11; // LED3 ANODE
// Init the Pins used for 10K pots
const int redPotPin = 0;
const int greenPotPin = 1;
const int bluePotPin = 2;
// Init our Vars
int currentColorValueRed;
int currentColorValueGreen;
int currentColorValueBlue;
//Init the Switch
const int modePin = 13; // Active HIGH, held low by 4.7K
int mode = 0; // Selector State (Initial state = ALL OFF)
int val = 0; // Pin 7 HIGH/LOW Status
int butState = 0; // Last Button State
int modeState = 0; // Last Mode State
boolean debug = 1; // 1 = Print Serial Enabled / 0 = disabled
//===============================================================
// SETUP
//===============================================================
void setup () {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(modePin, INPUT);
setColourRgb(0,0,0);
Serial.begin(9600);
}
void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
void loop(){
if (digitalRead(modePin)==HIGH) {
// toggles mode on push
mode != mode;
Serial.println("Pressed");
}
if (mode==1) {
// Here we hve chosen our first loop AKA 'Manual Control'
// Read the voltage on each analog pin then scale down to 0-255 and inverting the value for common anode
currentColorValueRed = (255 - map( analogRead(redPotPin), 0, 1024, 0, 255 ) );
currentColorValueBlue = (255 - map( analogRead(bluePotPin), 0, 1024, 0, 255 ) );
currentColorValueGreen = (255 - map( analogRead(greenPotPin), 0, 1024, 0, 255 ) );
// Write the color to each pin using PWM and the value gathered above
analogWrite(redPin, currentColorValueRed);
analogWrite(bluePin, currentColorValueBlue);
analogWrite(greenPin, currentColorValueGreen);
}
else {
// Here we have chosen out 'Rainbow Cycle'
unsigned int rgbColour[3];
// Start off with red.
rgbColour[0] = 255;
rgbColour[1] = 0;
rgbColour[2] = 0;
// Choose the colours to increment and decrement.
for (int decColour = 0; decColour < 3; decColour += 1) {
int incColour = decColour == 2 ? 0 : decColour + 1;
// cross-fade the two colours.
for(int i = 0; i < 255; i += 1) {
rgbColour[decColour] -= 1;
rgbColour[incColour] += 1;
setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
delay(5);
}
}
}
}
]