Ok, I joined the two of them without thinking much it may look rough but it is compiling.
It looked like it tried to change state but it stayed always on, so I rename the cases because I had two of them with the same description case 0 and case 1, and now at least is turning on and off, but with the same two switches :
btnOnoffState = digitalRead(ONOFFPIN);
btnStateUp = digitalRead(UPPIN);
which they share the same :btnPressCount++; // Count this press
So here is everything :
#define REDPIN 3
#define GREENPIN 5
#define BLUEPIN 6
#define ONOFFPIN 7
#define UPPIN 4
#define DNPIN 2
int btnPressCount = 0;
int btnOnoffState = LOW;
int lastBtnOnoffState = LOW;
int btnStateUp = LOW;
int lastBtnStateUP = LOW;
int btnStateDn = LOW;
int lastBtnStateDn = LOW;
void setup()
{
pinMode(ONOFFPIN, INPUT);
pinMode(UPPIN, INPUT);
pinMode(DNPIN, INPUT);
Serial.begin(9600);
}
void loop()
{
btnOnoffState = digitalRead(ONOFFPIN);
if((btnOnoffState == HIGH) && (lastBtnOnoffState == LOW)) // Switch is pressed and was not
{
btnPressCount++; // Count this press
Serial.print("count = ");
Serial.println(btnPressCount);
}
btnPressCount = btnPressCount % 2; // Keep count in the range 0 to 4
lastBtnOnoffState = btnOnoffState;
switch(btnPressCount)
{
case 0:
analogWrite(REDPIN, 0);
analogWrite(GREENPIN, 0);
analogWrite(BLUEPIN, 0);
break;
case 1:
analogWrite(REDPIN, 255);
analogWrite(GREENPIN, 255);
analogWrite(BLUEPIN, 255);
break;
}
btnStateUp = digitalRead(UPPIN);
if((btnStateUp == HIGH) && (lastBtnStateUP == LOW)) // Switch is pressed and was not
btnPressCount++; // Count this press
lastBtnStateUP = btnStateUp; // Use btnPressCount
btnStateDn = digitalRead(DNPIN);
if((btnStateDn == HIGH) && (lastBtnStateDn == LOW)) // Switch is pressed and was not
btnPressCount--; // Count this press
lastBtnStateDn = btnStateDn; // Use btnPressCount
//this part mantains the counts equal to 0
// so it doesn't make the btnPressCount unsigned
if(btnPressCount < 0) // <-- Add me
btnPressCount = 0; // <-- And me
btnPressCount = btnPressCount % 7; // Keep count in the range 0 to 6
Serial.print("count = ");
Serial.println(btnPressCount);
switch(btnPressCount)
{
case 2:
analogWrite(REDPIN, 255); // turns the LED on
analogWrite(GREENPIN, 255);
analogWrite(BLUEPIN, 255);
break;
case 3:
analogWrite(REDPIN, 255); // on
analogWrite(GREENPIN, 0); // off
analogWrite(BLUEPIN, 0);
break;
case 4:
analogWrite(REDPIN, 0);
analogWrite(GREENPIN, 255);
analogWrite(BLUEPIN, 0 );
break;
case 5:
analogWrite(REDPIN, 0);
analogWrite(GREENPIN, 0);
analogWrite(BLUEPIN, 255);
break;
case 6:
analogWrite(REDPIN, 255);
analogWrite(GREENPIN, 255);
analogWrite(BLUEPIN, 0);
break;
case 7:
analogWrite(REDPIN, 0);
analogWrite(GREENPIN, 255);
analogWrite(BLUEPIN, 255);
break;
case 8:
analogWrite(REDPIN, 255);
analogWrite(GREENPIN, 0);
analogWrite(BLUEPIN, 255);
break;
}
}