So I've got a script to check to see the status of a software push button, and that turns on and off a color fading RGB led. The problem is that I can't get the red to power up, it goes from green --> blue and jumps back to green.
If someone could look over the code and see if I've over looked something, that would be appreciated.
/*
RGB LED Fade - Million Colors, push button on/off.
*/
//Pins Definition
#define greenPIN 9
#define bluePIN 10
#define redPIN 11
#define actionBtn 12
#define statusLED 13
/* ** Program Variables ** */
//Button Variables
int btnVal = 0;
int softVal = 0;
int bounceInterval = 50;
int bounceTime = 0;
int lastBtnVal = HIGH;
//RGB LED Million Color Fade Variables
int greenVal = 255; // Variables to store the values to send to the pins
int blueVal = 1; // Initial values are green full, blue and red off
int redVal = 1;
int i2 = 0; // Loop counter
int wait = 50; // 50ms (.05 second) delay; shorten for faster fades
//Pulsation Variables
long previousMilis = 0;
int interval = 0;
int i = 0;
int valLED = 0;
int stopNextRun = 0;
// Output
//int greenPin = 9; // Green LED, connected to digital pin 9
//int bluePin = 10; // Blue LED, connected to digital pin 10
//int redPin = 11; // Red LED, connected to digital pin 11
// Setup
void setup() {
pinMode(greenPIN, OUTPUT);
pinMode(bluePIN, OUTPUT);
pinMode(redPIN, OUTPUT);
pinMode(actionBtn, INPUT);
Serial.begin(9600);
// reportAVRState(3,1);
}
void loop() {
btnVal = digitalRead(actionBtn);
// if (btnVal) {
// readRemoteSoftVal();
// }
if (btnVal == LOW && lastBtnVal == HIGH && bounceTime != -1) {
lastBtnVal = LOW;
Serial.print("Going Low");
}
if (bounceTime == -1 && btnVal == HIGH) {
bounceTime = 0;
}
if (btnVal == LOW && lastBtnVal == LOW) {
bounceTime++;
if (bounceTime >= bounceInterval) {
bounceTime = -1;
btnVal = HIGH;
Serial.print("Action button debounced");
}
}
if (btnVal == HIGH && lastBtnVal == LOW) {
softVal = !softVal;
lastBtnVal = HIGH;
Serial.print("Going high with softVal to: ");
Serial.print(softVal);
}
// We want the LED to be off!
if (!softVal) {
digitalWrite(greenPIN, softVal);
digitalWrite(bluePIN, softVal);
digitalWrite(redPIN, softVal);
previousMilis = 0;
valLED = 0;
i = 0;
}
//Begin Color Fades
else {
i2 += 1; // Increment counter
if (i < 255) // First phase of fades
{
greenVal -= 1; // Green down
blueVal += 1; // Blue up
redVal = 1; // Red low
}
else if (i < 509) // Second phase of fades
{
greenVal = 1; // Green low
blueVal -= 1; // Blue down
redVal += 1; // Red up
}
else if (i < 763) // Third phase of fades
{
greenVal += 1; // Green up
blueVal = 1; // Blue low
redVal -= 1; // Red down
}
else // Re-set the counter, and start the fades again
{
i2 = 1;
}
analogWrite(greenPIN, greenVal); // Write current values to LED pins
analogWrite(bluePIN, blueVal);
analogWrite(redPIN, redVal);
delay(wait); // Pause for 'wait' milliseconds before resuming the loop
}
}
void readRemoteSoftVal ( void ) {
int incomingByte = 0;
//send data only when you recieve data:
if (Serial.available() > 0) {
if (incomingByte == 49) {
btnVal&= LOW;
}
}
btnVal&= HIGH;
}
//Reports the state of the controller via pin 13's led and serial
//void reportAVRState(int howManyTimes, int leaveON) {
// int i;
// pinMode(statusLED, OUTPUT);
// for (i=0; i< howManyTimes; i++) {
// digitalWrite(statusLED, HIGH);
// delay(200);
// digitalWrite(statusLED, LOW);
// delay(200);
// }
// if (leaveOn) {
// ditialWrite(statusLED, HIGH);
// }
// Serial.printIn("AVR Initialized");
//}
// {
// pinMode(greenPin, OUTPUT); // sets the pins as output
// pinMode(bluePin, OUTPUT);
// pinMode(redPin, OUTPUT);
// pinMode(inPin, INPUT);
// pinMode(outPin, OUTPUT);
// }