0

I'm relatively new to Arduino, and here are what I was trying to do.

I want to control a relay circuit using IR(InfarRet) remote. Here is the code what I'm using :

#include <IRremote.h>
int RECV_PIN = 6;
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  irrecv.enableIRIn();
  pinMode (5 ,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:  
  if (irrecv.decode(&results)){
    Serial.println(results.value,DEC);
    irrecv.resume();
  switch (results.value){
    case 3150073167:
      digitalWrite(5,HIGH);
      break;
    case 68850955:
      digitalWrite (5,LOW);
      break;
    }
  }
}

circuit is working properly,

but after power restart it is not working properly, hear is a snap :

Error : enter image description here

how to fix this error ?

J. Chomel
  • 8,193
  • 15
  • 41
  • 69
Ylet
  • 1
  • That's probably not an error. Be happy that relay goes into a safe state on power off. – datafiddler Jul 26 '16 at 22:08
  • Please respect people that will read your question and format properly. – J. Chomel Jul 27 '16 at 09:11
  • thanks you sir for read and commend , with your help finally solved it, but it only works for one relay (only one LED), .. so please be kind enough to look my code and help me to make it for two and more (LED) ...Hear is my code : http://textuploader.com/5ezr9 – Ylet Jul 28 '16 at 11:41

2 Answers2

1

RAM is volatile memory, and after power loss it is lost too (IO Ports are reset to INPUT mode without pull-ups).

You can use EEPROM to store last state and restore it in setup() function.

For AVR based arduinos something like this can be used:

#include <EEPROM.h>

#include <IRremote.h>

const int    RELAY_PIN = 5;
const int     RECV_PIN = 6;
const int      address = 0;
byte             state = 0;
decode_results results;

IRrecv          irrecv(RECV_PIN);

void setup() {
    Serial.begin(9600);
    irrecv.enableIRIn();

    state = EEPROM.read(address);

    pinMode (RELAY_PIN, OUTPUT);
    digitalWrite(RELAY_PIN, state);
}

void loop() {

    if (irrecv.decode(&results)) {
        Serial.println(results.value,DEC);
        irrecv.resume();
        switch (results.value){
            case 3150073167LU:
                if (state == LOW) {
                    state = HIGH;
                    EEPROM.write(address, state);
                }
                break;

            case 68850955LU:
                if (state == HIGH) {
                    state = LOW;
                    EEPROM.write(address, state);
                }
                break;

            default:
                break;
        }
        digitalWrite(RELAY_PIN, state);
    }
}
KIIV
  • 3,534
  • 2
  • 18
  • 23
  • thanks you sir for read and commend , with your help finally solved it, but it only works for one relay (only one LED), .. so please be kind enough to look my code and help me to make it for two and more (LED) ...Hear is my code : http://textuploader.com/5ezr9 – Ylet Jul 28 '16 at 11:41
1

Usually, a controller is meant to run forever ("24/7"). In the rare case of a restart, the whole system should be set to a defined initial safe state. (That should usually be the same as during power off)

How come your relay remains ON while the arduino is OFF?

If you really want to store a previous state, EEPROM is a good place. (Fully agree with KIIV)

datafiddler
  • 1,755
  • 3
  • 17
  • 30
  • KIIV already did more spoon feeding than I'd do. Good luck with his suggestion – datafiddler Jul 26 '16 at 22:05
  • thanks you sir for read and commend , with your help finally solved it, but it only works for one relay (only one LED), .. so please be kind enough to look my code and help me to make it for two and more (LED) ...Hear is my code : http://textuploader.com/5ezr9 – Ylet Jul 28 '16 at 11:42