I built a basic sketch using 3 pins - 2 out, 1 in. Both outputs are on PWM pins and I'm using a Pro Mini. The configuration is:
D7 = pushbutton input (PB)
D9 = LED 1
D10 = LED 2
Sketch outline:
PB 1 - Turn on LED 1
PB 2 - Turn on LED 2
PB 3 - Fade LED 2 in and out
PB 4 - Fade LED 1 and 2 in opposite directions
PB 5 - Turn off all LEDs and reset the button count to restart through the sequence
My issue is - I'm polling the pushbutton and so to accept the PB4 (and 5), I have to make sure it is pushed and held through a complete 'fade' cycle. I wanted to modify the sketch to put the fading into the loop to pick up the button press quicker. I added 2 variables... varFade as an increment counter (1-255 back and forth) and varDir to determine whether I am going up or down.
As you can see from the code - I've put in some 'Serial.println' debugging in and I can see I enter the loop, the varFade value enters as 0 which I think it should do from the global variable setup. It successfully calls my function EyeFade and passes the value of varFade. It successfully increments varFade by 5... but then varFade gets reset back to the initial value.
What am I missing here?
Here is my code... I know there is a better way to insert code - I apologize for not knowing that method.
Arden
int BGLEDPin = 9; //Background LED driver circuit pin 9
int EyeLEDPin = 10; //Eye LED assigned to pin 8
int BtnPin = 7; //Button assigned to pin 7
int BtnState = 13; //Onboard LED
int varBtnPress; //Will be used to capture button state
int varBtnCnt = 0; //Button press counter (initialized to 0)
int EyeDelay; //A variable to establish the speed of eye fade
int varBtnChng = 0; //variable to store the switch change state to 'pressed'
int varDir = 1;
int varFade = 100;
void setup() {
// Set up serial port
Serial.begin(9600); // for troubleshooting
// Set up pins
pinMode(BGLEDPin,OUTPUT); //Assigned pin for background set to output mode
digitalWrite(BGLEDPin,LOW); //Make sure it is off
pinMode(EyeLEDPin, OUTPUT); //Assigned pin for eye set to output
digitalWrite(EyeLEDPin,LOW);//Make sure it is off
pinMode(BtnPin,INPUT_PULLUP); //Assigned pin for button set to input
pinMode(BtnState,OUTPUT); //Onboard LED
}
void loop() {
// put your main code here, to run repeatedly:
//Read the button state
varBtnPress = BtnPress(); // call the button pressed function; return is 0 if pressed
if (varBtnPress == 0) // this means the button was pressed
{
varBtnChng = 1; // set the 'change' variable to 1 (button state changed)
}
if (varBtnPress == 1 && varBtnChng == 1) // if the button is not pressed but we had a button changed state once
{
varBtnChng = 0; // reset the change capture variable to 0
varBtnCnt = ++varBtnCnt; // increment the button count
}
switch (varBtnCnt)
{
case 1: // First button push
BGOn (); // Call the routine to turn on the background LEDs
break; // Exit this 'switch' routine
case 2: // Second button push
EyeOn (); // Call the routine to turn the eye LED on
break; // Exit this 'switch' routine
// THIS IS THE LOOP I AM HAVING TROUBLE WITH
case 3: // Third button push
if (varDir == 1)
{
Serial.println("top of case 3");
Serial.println(varFade);
EyeFade (varFade);
varFade = varFade + 5; //varFade increments here, but it always goes back to the default value
Serial.println(varFade);
if (varFade == 255)
{
varDir = 0;
}
else
{
int EyeFade (varFade);
varFade = varFade - 5;
if (varFade == 0)
{
varDir = 1;
}
}
}
// END OF THE LOOP I AM HAVING TROUBLE WITH
break; // Exit this 'switch' routine
case 4: // Fourth button push
EyeBGFade ();
break;
case 5: // Fifth and final button push
digitalWrite(BGLEDPin,LOW); // turn off the eye LED
digitalWrite(EyeLEDPin,LOW); // turn off the background LEDs
varBtnCnt = 0; // reset the button count
}
}
// Functions all defined here
/* Function Name: BtnPress
* Return variable: varBtnPress
* Return value: 0 = button pressed
* This routine does simple switch read and debounce by using a 10 mS delay
*/
int BtnPress() {
int varResult,varBtnTest1,varBtnTest2 ; // local variables
varBtnTest1 = digitalRead(BtnPin); // read the switch once
delay (10); // delay 10 mS to debounce
varBtnTest2 = digitalRead(BtnPin); // read the switch again
if (varBtnTest1 == varBtnTest2) // make sure we got 2 consistant readings!
{
if (varBtnTest1 == 0) // if the button is pressed...
{
return varBtnTest1; // return the button value (0)
}
else // else...
{
return 1; // return a 1 (button not pressed)
}
}
}
/* Function Name: BGOn
* Return variable: none
* Return value: none
* This routine turns on the background LEDs
*/
int BGOn() {
digitalWrite(BGLEDPin, HIGH);
// Serial.println("BGOn function"); // for troubleshooting
}
/* Function Name: EyeOn
* Return variable: none
* Return value: none
* This routine turns on the eye LED
*/
int EyeOn() {
digitalWrite(EyeLEDPin, HIGH);
// Serial.println("EyeOn function"); // for troubleshooting
}
void EyeFade(int fadeValue) {
Serial.print("fadeValue=");
Serial.println(fadeValue);
analogWrite(EyeLEDPin,fadeValue);
}
/* Function Name: EyeBGFade
* Return variable: none
* Return value: none
* This routine causes the eye to bright and dim
*/
void EyeBGFade() {
// fade in from min to max in increments of 5 points:
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
// sets the value (range from 0 to 255):
analogWrite(EyeLEDPin, fadeValue);
analogWrite(BGLEDPin, 255-fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
// fade out from max to min in increments of 5 points:
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
// sets the value (range from 0 to 255):
analogWrite(EyeLEDPin, fadeValue);
analogWrite(BGLEDPin, 255-fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}