I added the brightness code to fade an led in this sketch but is executing both functions at the same time.
I would like to be able to hold the same button for more than 3,5 sec to change the brightness only.
btnStateUp = digitalRead(UPPIN);
startTime = millis();
if((btnStateUp == HIGH) && (lastBtnStateUP == LOW)) // Switch is pressed and was not
btnPressCountUpDn++; // Count this press
lastBtnStateUP = btnStateUp; // Use btnPressCount
if((btnStateUp == HIGH) && (millis() + startTime) > 3500)
brightness ++;
delay(10);
if (brightness > 255)
brightness = 255;
It would help if you posted all of your code and not just a fragment so we can see it in context.
but is executing both functions at the same time.
Both which functions?
(millis() + startTime)
Will always be > 3500 after the first 1.75 seconds of running this code.
#define REDPIN 3
#define GREENPIN 5
#define BLUEPIN 6
#define UPPIN 4
#define DNPIN 2
int btnPressCountUpDn = 0;
int btnStateUp = 0;
int lastBtnStateUp = 0;
int btnStateDn = 0;
int lastBtnStateDn = 0;
unsigned long startTime = 0;
int brightness = 30; // how bright the LED is, stores the brigthness value
void setup()
{
pinMode(UPPIN, INPUT);
pinMode(DNPIN, INPUT);
Serial.begin(9600);
}
void loop()
{
startTime = millis();
btnStateUp = digitalRead(UPPIN); // read the PIN and store it
if((btnStateUp == HIGH) && (lastBtnStateUp == LOW)) // true if operands are true
btnPressCountUpDn++; // Count this press,
lastBtnStateUp = btnStateUp; // Use btnPressCount
// This is the part I aded, but is fading at he same time is changing cases
if((btnStateUp == HIGH) && (millis() + startTime) > 3500)
brightness ++;
delay(10);
if (brightness > 255)
brightness = 255;
btnStateDn = digitalRead(DNPIN); // read the PIN and store it
if((btnStateDn == HIGH) && (lastBtnStateDn == LOW)) // check the current state of the button
btnPressCountUpDn--; // Count this press, if is true
lastBtnStateDn = btnStateDn; // reset btnStateDn
//this part mantains the counts equal to 0
// so it doesn't make the btnPressCount unsigned
if(btnPressCountUpDn < 0) // <-- Add me
btnPressCountUpDn = 0; // <-- And me
Serial.println(btnStateUp);
Serial.print(", ");
Serial.println(btnPressCountUpDn);
btnPressCountUpDn = btnPressCountUpDn % 7; // Keep count in the range 0 to 6
switch(btnPressCountUpDn)
{
case 0:
analogWrite(REDPIN,brightness); // starts with all on
analogWrite(GREENPIN, brightness);
analogWrite(BLUEPIN, brightness);
break;
case 1:
analogWrite(REDPIN, brightness); // on
analogWrite(GREENPIN, 0); // off
analogWrite(BLUEPIN, 0);
break;
case 2:
analogWrite(REDPIN, 0);
analogWrite(GREENPIN, brightness);
analogWrite(BLUEPIN, 0 );
break;
case 3:
analogWrite(REDPIN, 0);
analogWrite(GREENPIN, 0);
analogWrite(BLUEPIN, brightness);
break;
case 4:
analogWrite(REDPIN, brightness);
analogWrite(GREENPIN, brightness);
analogWrite(BLUEPIN, 0);
break;
case 5:
analogWrite(REDPIN, 0);
analogWrite(GREENPIN, brightness);
analogWrite(BLUEPIN,brightness);
break;
case 6:
analogWrite(REDPIN, brightness);
analogWrite(GREENPIN, 0);
analogWrite(BLUEPIN, brightness);
break;
}
}
Thanks but you still haven't corrected the fault I pointed out:-
(millis() + startTime)
Will always be > 3500 after the first 1.75 seconds of running this code.
I am sure this is not what you want.
You might want
(millis() - startTime)
in the if statement?
Even so this will then never exceed 3700 because at the start of each loop you have
startTime = millis();
so it is constantly reseting
so I need it at the end?
No you need it only when you first detect the button is being held down.
Ok, is working but the cases are not changing and the LEDs are flashing
void loop()
{
btnStateUp = digitalRead(UPPIN); // read the PIN and store it
if((btnStateUp == HIGH) && (lastBtnStateUp == LOW)) // true if operands are true
startTime = millis();
btnPressCountUpDn++; // Count this press,
lastBtnStateUp = btnStateUp; // Use btnPressCount
// This is the part I aded, but is fading at he same time is changing cases
if((btnStateUp == HIGH) && (millis() - startTime) > 3500)
brightness ++;
delay(10);
if (brightness > 255)
brightness = 255;
So what do you see in the console from the serial print?
btnStateUP and btnPressCountUPDN going from 0 to 7,
4
0
, 5
0
, 6
0
, 7
0
, 1
0
, 2
0
, 3
0
, 4
0
and when I press the button it changes to this,
5
1
, 6
1
, 7
1
, 1
1
, 2
1
, 3
1
, 4
Well its doing exactly what you wrote,
What did you expect to see?
Put a print statement in each case to check that it is getting into the right part of the switch statement.