Attiny85 Ir control home lights

Hello
Its my first post hire because i cant find solution for my problem
So I make IR receiver with 4 relay who control my lights in home and i use remote from my TV Samsung and everything work just fine (switch on off lights) but problem is I have only four buttons free from my remote and i whant with same knob to make on\off and toggle one channel . How to make for example first channel when i press knob once to switch on , second turn off but if i press for 4-5 sec. to toggle?
Thanks

Hire is my code
const int irPin = 4;
const int Relay1 = 0;
const int Relay2 = 1;
const int Relay3 = 2;
const int Relay4 = 3;

boolean Relay1State = false;
boolean Relay2State = false;
boolean Relay3State = false;
boolean Relay4State = false;

void setup() {

//Serial.begin(115200);
pinMode(irPin, INPUT);
pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
pinMode(Relay3, OUTPUT);
pinMode(Relay4, OUTPUT);
}

void loop() {
int key = getIrKey();

if(key == 17845)
{
Relay1State = !Relay1State;
if(Relay1State == true)
digitalWrite(Relay1, HIGH);

else 
  digitalWrite(Relay1, LOW);

}

if(key == 18355)
{
Relay2State = !Relay2State;
if(Relay2State == true)
digitalWrite(Relay2, HIGH);
else
digitalWrite(Relay2, LOW);
}

if(key == 19120)
{
Relay3State = !Relay3State;
if(Relay3State == true)
digitalWrite(Relay3, HIGH);
else
digitalWrite(Relay3, LOW);
}

if(key == 18610)
{
Relay4State = !Relay4State;
if(Relay4State == true)
digitalWrite(Relay4, HIGH);
else
digitalWrite(Relay4, LOW);
}
}

int getIrKey(){
int len = pulseIn(irPin,LOW);
int key, temp;
key = 0;
//Serial.print("len=");
//Serial.println(len);
if(len > 3500) {
for(int i=1;i<=32;i++){
temp = pulseIn(irPin,HIGH);
if(temp > 1000)
key = key + (1<<(i-17));
}
}
if(key < 0 )
key = -key;

//if(key)
//Serial.println(key);

delay(250);

return key;
}

Use Tools/Autoformat in the IDE.
Search this forum how to post in code tags.
Also search in this forum 'state machines'

1 Like

Please read the forum guidelines, I cannot follow what you posted. The simplest way is to have 4 flags, when a button press is detected complement that flag and set a flag that represents change. Then later if the change flag has been set, clear it and then look at the flags, if it is high turn the appropriate` channel on, if low turn the channel off. The change flag keeps you from banging the output and will allow other things to happen as well.

Your code works. In my setup, Press "1" and Device1 turns on. Press "2" and Device2 turns on. Press "1" again and Device1 turns off... and up to the four output devices.

Remove "delay(250)". I think any problem with your code is the "delay(250)" makes button pressing unreliable... and you know Serial does not work on ATtiny85.

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