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 ![]()
#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