Thanks for your help with the other sketch ''from the beginning'', I got it working now.
what I want to do with this one is add a second function to the same switch
so the UPPIN can change cases and if I hold it change brightness too, same with DNPIN.
In the sketch I added the
int brightness = 50; // how bright the LED is, stores the brigthness value
and the
unsigned long startTime = 0; // when did we begin pressing?
to use it with millis?
and the cases are set to brightness instead
analogWrite(REDPIN, brightness); // set to brightness
so this is the sketch
#define REDPIN 3
#define GREENPIN 5
#define BLUEPIN 6
#define UPPIN 4
#define DNPIN 2
int btnPressCount = 0;
int btnStateUp = LOW;
int lastBtnStateUp = LOW;
int btnStateDn = LOW;
int lastBtnStateDn = LOW;
int brightness = 50; // how bright the LED is, stores the brigthness value
unsigned long startTime = 0; // when did we begin pressing?
void setup()
{
pinMode(UPPIN, INPUT);
pinMode(DNPIN, INPUT);
Serial.begin(9600);
}
void loop()
{
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
Serial.print("count = ");
Serial.println(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 % 3; // Keep count in the range 0 to 6
switch(btnPressCount)
{
case 0:
analogWrite(REDPIN, brightness); // set to brightness
analogWrite(GREENPIN, 0); // off
analogWrite(BLUEPIN, 0);
break;
case 1:
analogWrite(REDPIN, 0);
analogWrite(GREENPIN, brightness);
analogWrite(BLUEPIN, 0 );
break;
case 2:
analogWrite(REDPIN, 0);
analogWrite(GREENPIN, 0);
analogWrite(BLUEPIN, brightness);
break;
}
}