button press length

I’ve been trying to fix this code for the second day, but I can’t :frowning: . That’s why I turned to you. I have a button which, if I hold it down for a long time (over 5 s), the setting is switched on. And then with short presses (under 5 s) I would switch modes. And if I held the button again for 5 seconds, the setting would end. Turning the setting on and off goes but switching there is some problem.

#define butt 3
long lastPress;
long TimePress;
boolean stateButt= LOW;
boolean SettBegin = LOW;
int pozition;
void setup() {
pinMode(butt, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(butt), Sett, CHANGE);
Serial.begin(9600);
}

void loop() {
Sett();
}
void Sett() {
if (millis() > lastPress + 50 && digitalRead(butt) != stateButt) {
stateButt = !stateButt;
lastPress = millis();
if (stateButt == LOW) {
TimePress = millis();
}

if (stateButt == HIGH) {
if (millis() - TimePress > 5000) {
SettBegin = !SettBegin;
Serial.println(" on/off");
}
if (SettBegin == HIGH) {
if (millis() - TimePress < 5000 && digitalRead(butt) != stateButt) {
stateButt = !stateButt;
pozition++;
if (pozition > 6) {
pozition = 0;
}
Serial.println(pozition);
}
}
}
}
}

Topic moved here as its original location was not appropriate

Please follow the advice on posting code given in posting code

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

Hello Sir.

Why don't You use library for example :

Greets
Marek Wojciechowski

The code in this link illustrates how to do different button clicks

Don't use millis() like this

if (millis() > lastPress + 50

you have it the correct way later on

...R

/*here is my corrected code
*/
#define butt 3
long lastPress;
long TimePress;
boolean stateButt = LOW;
boolean SettBegin = LOW;
boolean newPressed = LOW;
int pozition;
void setup() {
pinMode(butt, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(butt), Sett, CHANGE);
Serial.begin(9600);
}

void loop() {
Sett();
}
void Sett() {
if (millis() > lastPress + 50 && digitalRead(butt) != stateButt) {
newPressed = HIGH;
stateButt = !stateButt;
lastPress = millis();
if (stateButt == LOW) {
TimePress = millis();
}

if (stateButt == HIGH) {
if (millis() > TimePress + 5000 ) {
SettBegin = !SettBegin;
Serial.print("(ON = 1 / OFF = 0) => ");
Serial.println(SettBegin);
}
if (SettBegin == HIGH) {

if (newPressed == HIGH) {
pozition++;

if (pozition > 6) {
pozition = 0;

}
Serial.println(pozition);
}

}
}
newPressed = LOW;
}
}

grizzly42:
/*here is my corrected code
*/

Good to hear you have it working.

For the future please use the code button </> when posting code.

See How to get the best out of the Forum

…R

/*here is my corrected code

Posted without code tags again

Did you read the advice I linked to in reply #1 ?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.