This is the first code ive wrote all by myself and it seems to be working but not very good. The led seems to only turn on when i hit a button on the remote like 50 percent of the time. The rest of the time i can see that my ir sensors build in light is blinking when i hit a button but nothing happens. I wasnt sure if i have to declare the receive pin as an input or not. I also wasnt sure if i had to declare my variables and include my libraries in the setup() statement or before it. Im using a attiny 85 processor and not sure what pins can be used for what tasks. I know they all are capable digital read/write, and only some are capable of analog read and write. If anyone has any ideas to make this code better and run more smoothly please let me know.
#include <IRremote.h>
#include <IRremoteInt.h>
int RECV_PIN = 3;
int RELAY_PIN = 2;
int LED_PIN = 1;
int BLINK_PIN = 0;
bool on = 0;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
pinMode(RELAY_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(BLINK_PIN, OUTPUT);
pinMode(RECV_PIN, INPUT);
irrecv.enableIRIn();
digitalWrite(RELAY_PIN, LOW);
digitalWrite(LED_PIN, LOW);
digitalWrite(BLINK_PIN, LOW);
}
void loop() {
digitalWrite(BLINK_PIN, HIGH);
delay(250);
digitalWrite(BLINK_PIN, LOW);
delay(250);
if (irrecv.decode(&results)) {
on = !on;
digitalWrite(RELAY_PIN, on ? HIGH : LOW);
digitalWrite(LED_PIN, on ? HIGH : LOW);
irrecv.resume();
}
}