I wrote a code to switch on and off two LED on the arduino..and control the brightness of one of those two LEDs..
I am trying to save the current state of the LEDs, so that if the power is switched off and on the LEDs should resume to the last state they were in before the power off..
but sometimes they they are not resuming properly...
I am unable to make out where am I making the mistake.. is it in saving or in reading...??
here is the code below..
#include <IRremote.h>
#include <EEPROM.h>
int pin1 = 3;
int pin2 = 4;
int pin3 = 5;
int RECV_PIN = 11;
int fadered = 5;
int pin1State = LOW;
int pin2State = LOW;
int val;
int state;
int state1;
int btn1_Cnt = LOW;
int btn2_Cnt;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
irrecv.enableIRIn();
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
fadered = EEPROM.read(1);
/********** THIS IS TO READ WHAT WAS THE STATE OF PIN3 SAVED LAST TIME ***********/
btn2_Cnt = EEPROM.read(2);
if(btn2_Cnt % 2 == 0)
analogWrite(pin3, 0);
else
analogWrite(pin3, fadered);
/********** THIS IS TO READ WHAT WAS THE STATE OF PIN1 SAVED LAST TIME ***********/
btn2_Cnt = EEPROM.read(0);
if(btn2_Cnt % 2 == 0)
digitalWrite(pin1, LOW);
else
digitalWrite(pin1, HIGH);
}
void loop() {
if (irrecv.decode(&results)) {
/********** THIS PART IS FOR PIN1 ************/
if(results.value == 0xffb24d){
btn1_Cnt++;
if(btn1_Cnt % 2 == 0)
digitalWrite(pin1, LOW);
else
digitalWrite(pin1, HIGH);
}
/********** THIS PART IS FOR PIN3 ************/
if(results.value == 0xff38c7){
btn2_Cnt++;
if(btn2_Cnt % 2 == 0)
analogWrite(pin3, 0);
else
analogWrite(pin3, fadered);
}
if(results.value == 0xff7887 && fadered <=250){
fadered = fadered + 5;
analogWrite(pin3, fadered);
}
if(results.value == 0xff50af && fadered >=10){
fadered = fadered - 5;
analogWrite(pin3, fadered);
}
/********** THIS IS FOR SAVING THE BRIGHTNESS VALUE IN EEPROM ************/
val = fadered;
if(val != EEPROM.read (1)){
EEPROM.write(1, val);
}
/********** THIS IS TO SAVE THE LAST STATE OF THE PIN3 ************/
state = btn2_Cnt;
if(state != EEPROM.read (2)){
EEPROM.write(2, state);
}
/********** THIS IS TO SAVE THE LAST STATE OF THE PIN1 ************/
state1 = btn1_Cnt;
if(state1 != EEPROM.read (0)){
EEPROM.write(0, state1);
}
irrecv.resume();
}
}
I wrote the code in this way ...
But it is still reacting the same way....
#include <IRremote.h>
#include <EEPROM.h>
#include "EEPROMAnything.h"
int pin1 = 8;
int pin2 = 4;
int pin3 = 9;
int RECV_PIN = 11;
int fadered = 5;
int pin1State = LOW;
int pin2State = LOW;
int val;
int state;
int state1;
int btn1_Cnt = LOW;
int btn2_Cnt;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
irrecv.enableIRIn();
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
EEPROM_readAnything(1, fadered);
/********** THIS IS TO READ WHAT WAS THE STATE OF PIN3 SAVED LAST TIME ***********/
EEPROM_readAnything(2, btn2_Cnt);
if(btn2_Cnt % 2 == 0)
analogWrite(pin3, 0);
else
analogWrite(pin3, fadered);
/********** THIS IS TO READ WHAT WAS THE STATE OF PIN1 SAVED LAST TIME ***********/
EEPROM_readAnything(0, btn2_Cnt);
if(btn2_Cnt % 2 == 0){
digitalWrite(pin1, LOW);}
else{
digitalWrite(pin1, HIGH);
}
}
void loop() {
if (irrecv.decode(&results)) {
/********** THIS PART IS FOR PIN1 ************/
if(results.value == 0xffb24d){
btn1_Cnt++;
if(btn1_Cnt % 2 == 0)
digitalWrite(pin1, LOW);
else
digitalWrite(pin1, HIGH);
}
/********** THIS PART IS FOR PIN3 ************/
if(results.value == 0xff38c7){
btn2_Cnt++;
if(btn2_Cnt % 2 == 0)
analogWrite(pin3, 0);
else
analogWrite(pin3, fadered);
}
if(results.value == 0xff7887 && fadered <=250){
fadered = fadered + 5;
analogWrite(pin3, fadered);
}
if(results.value == 0xff50af && fadered >=10){
fadered = fadered - 5;
analogWrite(pin3, fadered);
}
/********** THIS IS FOR SAVING THE BRIGHTNESS VALUE IN EEPROM ************/
val = fadered;
if(val != EEPROM.read (1)){
EEPROM_writeAnything(1, val);
}
/********** THIS IS TO SAVE THE LAST STATE OF THE PIN3 ************/
state = btn2_Cnt;
if(state != EEPROM.read (2)){
EEPROM_writeAnything(2, state);
}
/********** THIS IS TO SAVE THE LAST STATE OF THE PIN1 ************/
state1 = btn1_Cnt;
if(state1 != EEPROM.read (0)){
EEPROM_writeAnything(0, state1);
}
irrecv.resume();
}
}
I tried to print the values what I am writing to the EEPROM everytime..
Every time I press the button for Pin1 LED the number 1 is ++, I mean on every press it adds +1...
same for the Pin3 button..
and when I press the buttons for the brightness, the numbers increase/ decrease by 5
Thats all what I get..
IT IS NOT THAT THE WRITE AND READ TO THE EEPROM IS NOT WORKING .. BUT SOMETIMES WHEN I PWR OFF AND ON THE ARDUINO THE LEDs DOES NOT GET BACK TO THE PREVIOUS STATE...THAT IS ONCE IN 4-5 TIMES OF OFF/ON OF ARDUINO..
PaulS:
Your addresses need to allow for storing ints. The write method actually returns the next available address.
Exactly what I was going to say.
To elaborate, an int takes two bytes to store, if you try to store an int in eeprom address 1, it takes both 1 and 2.
Now if you store one in 2, it takes 2 and 3.
At this point your first int's second half has been run over by the second int, and it comes out wildly different than it went in.
If you space them out (use say, addresses 0, 10, 20, 30, 40 etc.) it should work quite a bit better.
PaulS:
Your addresses need to allow for storing ints. The write method actually returns the next available address.
Exactly what I was going to say.
To elaborate, an int takes two bytes to store, if you try to store an int in eeprom address 1, it takes both 1 and 2.
Now if you store one in 2, it takes 2 and 3.
At this point your first int's second half has been run over by the second int, and it comes out wildly different than it went in.
If you space them out (use say, addresses 0, 10, 20, 30, 40 etc.) it should work quite a bit better.
Thanks Bobnova... now instead of saving in 0, 1, 2 I am saving to 0, 3, 6..It just solved the problem..