Short and long buttonpress not working

Hello, i am trying to use one button to send two different IR signals to a lamp, but it seems that no matter what i do it will not work. I have tried to follow the examples posted by people on this site by first measuring how long the button is pressed, then performing a function based on how long it has been pressed. It seems simple but the fact that i spent all day trying to get this to work says otherwise :smiley:

#include <IRremote.h>
const int irSenderPin = 3;
const int RledPin = 10;
const int IRbutton1 = 8;
const int IRbutton2 = 7;
const int potenti = A3;
IRsend irsend;

void setup() {
	Serial.begin(9600); 
	pinMode(irSenderPin, OUTPUT);
	pinMode(IRbutton1, INPUT);
	pinMode(IRbutton2, INPUT);
	pinMode(RledPin, OUTPUT);
	pinMode(potenti, INPUT);
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

void loop() {
	irStyring();
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

void irStyring(){


	boolean buttIRstate1 = digitalRead(IRbutton1);
	boolean buttIRstate2 = digitalRead(IRbutton2);
	boolean  lastbuttIRstate1;
	boolean  lastbuttIRstate2;
	unsigned short potentVal = analogRead(potenti);
	unsigned short potentState = map(potentVal, 0, 1024, 0, 5);
	unsigned short static lastpotentState = 0;
	unsigned short static buttonDur = 0;
	unsigned short static button2Dur = 0;
	unsigned long static prevMillis, prevMillis2, prevMillis3;

	// Knapp for av-pÄ og hvit
	delay(10);

	if(buttIRstate1 == HIGH){
		buttonDur++;
		Serial.print("Buttondur = ");
		Serial.println(buttonDur);
		delay(10);
	}	
	if(buttonDur < 50 && buttIRstate1 != lastbuttIRstate1){
		irsend.sendNEC(0x807F38C7, 32); //Onoff
		Serial.println("Onoff");
		buttonDur = 0;
		delay(20);
	}

	if(buttonDur > 50 && buttIRstate1 != lastbuttIRstate1){
		Serial.println("White");
		irsend.sendNEC(0x807FB847, 32); // White
		buttonDur = 0;
		lastbuttIRstate1 = buttIRstate1;
		delay(20);
		
	lastbuttIRstate1 = buttIRstate1;
	delay(20);
}

What happens here is that, if i use a while loop for the timer it will just stay in the while loop forever even if the button is not pressed, and if i use an if loop like i do here, it will just go directly to the first if loop and send the signal two times which means it will turn turn the lamp on and off quickly

What is the point of looking for a lastbuttIRstate if you are just going to create a new one, every loop?

void irStyring(){

boolean buttIRstate1 = digitalRead(IRbutton1);
boolean buttIRstate2 = digitalRead(IRbutton2);
boolean lastbuttIRstate1;
boolean lastbuttIRstate2;

Make they either static or global.

Forgot to take one of these out, perhaps.

lastbuttIRstate1 = buttIRstate1;
delay(20);

lastbuttIRstate1 = buttIRstate1;
delay(20);

I had them as static but changed them to see if that would work for some reason and forgot to change them back, my apologies. In any case, with the static variables i can use the button for toggling the power, but since i use an if loop to measure the time the button is pressed, it will go directly to the if loop for sending the power signal so it's impossible to reach the second function... T

he serial output just repeats these lines:

Onoff
Buttondur = 1

Are you seeing the message on-off and white correctly. If so, that suggests your button code is working OK.

I think the problem is that loop() repeats quickly and each time the button is detected you set the count back to 0. So next iteration of loop() the test for <50 applies even if you don't press a button.

Maybe your test should be something like if (buttonDur > 30 && buttonDur < 50

...R

Thanks for the help, i got it working now :slight_smile: Here's the code if anyone is interested :

void irStyring(){


	boolean buttIRstate1 = digitalRead(IRbutton1);
	boolean buttIRstate2 = digitalRead(IRbutton2);
	boolean static lastbuttIRstate1;
	boolean static lastbuttIRstate2;
	unsigned short potentVal = analogRead(potenti);
	unsigned short potentState = map(potentVal, 0, 1024, 0, 5);
	unsigned short static lastpotentState = 0;
	unsigned short static buttonDur = 0;
	unsigned short static button2Dur = 0;
	unsigned long static prevMillis, prevMillis2, prevMillis3;

	// Knapp for av-pÄ og hvit
	delay(10);

	if(buttIRstate1 == HIGH){
		buttonDur++;
		Serial.print("Buttondur = ");
		Serial.println(buttonDur);
		delay(10);
	}	
	if(buttIRstate1 == LOW){
		if(buttonDur > 30 && buttonDur < 50){
			irsend.sendNEC(0x807F38C7, 32); //Onoff
			Serial.println("Onoff");
			buttonDur = 0;
			delay(20);
		}

		if(buttonDur > 50){
			Serial.println("White");
			irsend.sendNEC(0x807FB847, 32); // White
			buttonDur = 0;
			delay(20);
			
			lastbuttIRstate1 = buttIRstate1;
			delay(20);
		}
	}
}