Go Down

Topic: need a quick syntax check (pwm led + pushbutton) (Read 682 times) previous topic - next topic

coldfusion01

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.

Code: [Select]
/*
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);
//  }

CosineKitty

The thing I notice is that most of the program logic is based on the value of the variable "i"... but you never change the value of the variable "i".  It is always zero.

coldfusion01

well 'i2' is used for the color changing loop.

'i' is used for a status led, which I'm not exactly using at the moment.

Sorry I'm just a tad bit new to all of this, I pieced this code together after some time from a couple tutorials and a little thought. The push button is doing fine, turning on and off the RGB led, and starting it again from where it left off. But the problem comes  when i get done powering the bluePIN of the led to full power, just before the redPIN comes in. (The red pin does nothing at all, the loop just jumps back to the greenPIN. I'm just wondering if I'm somehow canceling out that part of the loop).

coldfusion01

Quote
The thing I notice is that most of the program logic is based on the value of the variable "i"... but you never change the value of the variable "i".  It is always zero.


wow. haha, thank you!

I just rechecked my code... I left the variables in the color changing LED's loop as 'i' instead of 'i2' . Code works perfectly now!  :-X

CosineKitty

You're welcome... and the kitty says... Meow!  :)

Go Up