11 Replies Latest reply on May 20, 2019 8:04 PM by shabaz

    Arduino LED Dimmer , TV Remote Controll,  Saving  EEPROM

    arduino007

      Hallo

       

      Actually I want to make a LED Dimmer with Arduino with Remote Controll. I tried with online help , It was good. But I need some changing in it. Please help me for it.

       

      I want to save last state of LED brightness level after turn it off. I want to save it in EEPROM . If I restart Arduino, It use last Brightness level information from EEPROM and turn LED on at last brightness level. Arduino use its last setting Automatically  and I dont need to Turn it on from Remote Controller. I just change brightness from it. After that I can decrease LED brightness from Remote without Turn it on /Off.

       

      Pleas help me for it.

       

       

       

      Code for Dimmer LED Strip with TV Remote Controller

      https://www.youtube.com/user/greatscottlab

       

       

       

      #include <IRremote.h>

      int bright;

      int before;

      int out=9; //connect your LED to pin 9

      int steps=5; //dimmer steps, vary those to increase/decrease the steps between full brightness and turned off

      int RECV_PIN = 11; //data out of IR receiver connects to pin 11

       

      IRrecv irrecv(RECV_PIN);

       

      decode_results results;

       

      void setup(){

      irrecv.enableIRIn(); // start the receiver

        before=0; //LED is turned off

        bright=255; //brightness value is at maximum (255)

      pinMode(out,OUTPUT);

      }

       

      void loop() {

        if (irrecv.decode(&results)) {

       

        if (results.value==0x20DF8D72){ //Code to turn the LED ON/OFF

      if(before==0){ // if the LED was turned off, then we turn it on

      digitalWrite(out,HIGH);

            before=1; //LED is now turned on

          }

          else{

      digitalWrite(out,LOW); //if the LED was turned on, then we turn it off

            before=0;

      bright=255;

          }}

        if (results.value==0x20DFF10E && before==1){ //Code to decrease the brightness

      if(bright-255/steps<0){

      analogWrite(out,bright);

          }

          else{

      bright=bright-255/steps;

      analogWrite(out,bright);

        }}

        if (results.value==0x20DF718E && before==1){ //Code to increase the brightness

      if(bright+255/steps>255){

      analogWrite(out,bright);

          }

          else{   

      bright=bright+255/steps;

      analogWrite(out,bright);

        }}

       

      irrecv.resume();

      }}

        • Re: Arduino LED Dimmer , TV Remote Controll,  Saving  EEPROM
          dougw

          The code to control LED brightness should always get its value by reading it from EEPROM.

          The code to receive remote control data should only write the received data to EEPROM.

          There are lots of examples of reading and writing EEPROM including on the arduino site:

          https://www.arduino.cc/en/Reference/EEPROM

           

          to write to EEPROM:

          EEPROM.put(eeAddress, f);

           

          to read from EEPROM:

          EEPROM.get(eeAddress, customVar);

          2 of 2 people found this helpful
            • Re: Arduino LED Dimmer , TV Remote Controll,  Saving  EEPROM
              arduino007

              Thanks for Reply ;

              I read all possible information online. But I am not expert , therefore I did not find good solution. I tried that code, that ist not perfect, but its work. I need better code for that. please help me for it.

              In start LED brightness is good as saved in EEPROM. But I can not controll properly with remote at start. I press remote button one time, that Its work. Secondly the button ( to increase brightnes) increase light at first press at 255.

               

              my Code

               

              #include <EEPROM.h>

               

              #include <IRremote.h>

               

              int bright;

              int before;

              int out=9; //connect your LED to pin 9

              int steps=10; //dimmer steps, vary those to increase/decrease the steps between full brightness and turned off

              int RECV_PIN = 11; //data out of IR receiver connects to pin 11

               

               

              IRrecv irrecv(RECV_PIN);

               

              decode_results results;

               

              void setup(){

                irrecv.enableIRIn(); // start the receiver

                before=5; //LED is turned off

                bright=255; //brightness value is at maximum (255)

               

               

               

               

                if( EEPROM.read(0)>0){

                  analogWrite(out,EEPROM.read(0));

                }

               

                pinMode(out,OUTPUT);

               

                Serial.begin(9600);

              }

               

              void loop() {

               

                if (irrecv.decode(&results)) {

               

               

                if (results.value==0x2FDB847 && before>0 ){ //Code to decrease the brightness

                      if(bright-255/steps<0){

                    analogWrite(out,bright);

                  }

                  else{

                  bright=bright-255/steps;

                  analogWrite(out,bright);

                  delay(50);

                   EEPROM.write(0, bright);

                  Serial.println(bright);

                }}

               

                if (results.value==0x2FD9867 ){ //Code to increase the brightness

                     if(bright+255/steps>255){

                    analogWrite(out,bright);

                  }

                  else{   

                  bright=bright+255/steps;

                  analogWrite(out,bright);

                  delay(50);

                   EEPROM.write(0, bright);

                  Serial.println(bright);

                }}

               

               

                irrecv.resume();

              }}

                • Re: Arduino LED Dimmer , TV Remote Controll,  Saving  EEPROM
                  dougw

                  If I am reading this correctly, in the main loop, right after the decode line, you should write the results.value to EEPROM.

                  That is the only time you need to write to EEPROM.

                  The "if" statements to test whether to increase or decrease brightness should not look at the results.value - they should test the value that was stored in EEPROM.

                  On start up, the EEPROM value should be used to set brightness before the main loop.

              • Re: Arduino LED Dimmer , TV Remote Controll,  Saving  EEPROM
                lui_gough

                One should be careful in using the EEPROM in this manner as it has a finite lifetime of about 100,000 programming cycles. Using just one address will wear out the memory at that address - to improve lifetime, many systems recommend spreading the writes across the EEPROM (e.g. by using a circular log style approach) or by limiting it so that your code only writes to EEPROM very infrequently.

                 

                With your code, it seems you have 26 "steps", thus going from zero to full brightness will already consume 26 cycles each time. As a result, turning the light up from zero to full and back to zero just 1923 times will consume the cycle life of that particular EEPROM location. Assuming you use the light 5 times a day, this would only last about a year before you might run into reliability issues.

                 

                Instead, maybe you can define a timed process looking at the running time and save the brightness to EEPROM only after the light has reached a set brightness and stayed there for a minimum of a minute.. Or you could perhaps just store in the EEPROM a default power-up brightness and a flag that is either on or off, which will tell the unit whether to be on or off at the next power-up but loading a default brightness instead. This way, a write cycle is only consumed every switch-on or switch-off.

                 

                If you could make the program use the memory in a circular way (e.g. sequence number + flag written to a single byte, then you could theoretically extend the lifetime by writing new data to the next location and wrapping around once you reach the end. On boot-up, you would read all the EEPROM locations looking for the byte with the highest sequence number or a sudden decrement to the sequence number excepting wrap-around to reload the data, noting that the sequence number must have more bits than the number of locations to be unambiguous as to which one is the latest data. Otherwise you could use a pointer, although this will result in one location being programmed more frequently than the others and being a potential point of failure.

                 

                - Gough

                2 of 2 people found this helpful
                  • Re: Arduino LED Dimmer , TV Remote Controll,  Saving  EEPROM
                    arduino007

                    Thank you very much for very detailed reply. can you please write a example code for me. I am not expert programmer.  Thanks

                      • Re: Arduino LED Dimmer , TV Remote Controll,  Saving  EEPROM
                        arduino007

                        after many tries , I written that Code. but still not perfect. If I restart Arduino more then one times. I loose last brightness state of LED,  Please check it

                         

                         

                        #include <EEPROM.h>

                         

                        #include <IRremote.h>

                         

                        int bright;

                        int before;

                        int out=9; //connect your LED to pin 9

                        int steps=10; //dimmer steps, vary those to increase/decrease the steps between full brightness and turned off

                        int RECV_PIN = 11; //data out of IR receiver connects to pin 11

                        int addr = 2;  // EEPROM address to save last state

                        int addr2 = EEPROM.read(0);  //save next EEPROM number alway in EEPROM addr (0)to save last state of LED Brightnes

                        int addr3;

                        IRrecv irrecv(RECV_PIN);

                         

                        decode_results results;

                         

                         

                        void setup(){

                         

                         

                         

                         

                         

                        if (addr2<256 && addr2>0){ 

                          EEPROM.write(0,addr2+1);  // add every time next EEPROM number to save last last LED Brigtness State to save EEPROM life

                        }

                         

                         

                          irrecv.enableIRIn(); // start the receiver

                         

                         

                         

                             if( EEPROM.read(addr2-1)>=0 && EEPROM.read(addr2-1)<=255){            //Check last EEPROM save Data for LED Brightnes

                            analogWrite(out,EEPROM.read(addr2-1));  //Save now in Next EEPROM address, That add every time in ERPROM Address(0)

                            }

                         

                         

                         

                          addr3 = addr2;

                          before=5; //LED is turned off

                          bright=255; //brightness value is at maximum (255)

                         

                          pinMode(out,OUTPUT);

                         

                          Serial.begin(9600);

                         

                        }

                         

                         

                         

                         

                        void loop() {

                         

                         

                          if (irrecv.decode(&results)) {

                         

                         

                          if (results.value==0x2FDB847 && before>0 ){ //Code to decrease the brightness

                                if(bright-255/steps<0){

                              analogWrite(out,bright);

                            }

                            else{

                            bright=bright-255/steps;

                            analogWrite(out,bright);

                            delay(50);

                             EEPROM.write(addr3, bright);

                           

                          }}

                         

                          if (results.value==0x2FD9867 ){ //Code to increase the brightness

                               if(bright+255/steps>255){

                              analogWrite(out,bright);

                            }

                            else{   

                            bright=bright+255/steps;

                            analogWrite(out,bright);

                            delay(50);

                             EEPROM.write(addr3, bright);

                           

                          }}

                         

                         

                          irrecv.resume();

                        }}

                          • Re: Arduino LED Dimmer , TV Remote Controll,  Saving  EEPROM
                            dougw

                            It seems you need to increase or decrease the steps variable when you want to decrease or increase the brightness, but I don't see the code to do this.

                            1 of 1 people found this helpful
                            • Re: Arduino LED Dimmer , TV Remote Controll,  Saving  EEPROM
                              lui_gough

                              I would say that the problem is with the way you're attempting to spread EEPROM writes. Remember that on each boot, you are resetting the addr variable to 2 and the addr3 variable is undefined.

                               

                              In your setup, you are reading the first byte from the EEPROM, but then, you appear to be trying to store a zero in the next EEPROM address which makes no sense to me. Then after you enable the receiver, you're checking if the data from the address *minus one* is within a given range, which again, seems to be superfluous since EEPROM returns byte type (i.e. unsigned 8-bit) rather than int type, so it cannot be <0 or >255 either, hence the check always passes.

                               

                              That's all a mess. Perhaps you should start with just one address first, before trying to implement a circular log storage so to get your ideas firm as to when you are going to store and what you are going to store. There may be no need to "waste" the whole byte with the step value either if you're a little clever with how you define your steps - 16 steps for example could fit into 4-bits, leaving 4-bits to implement a sequence number, allowing you to spread it out between 16-byte positions without needing to implement separate data structures. Otherwise perhaps use "pairs" of EEPROM byte values - first byte = sequence number, second byte = step value. In this case, with 8-bits of sequence number, you could unambiguously determine which is the latest value with <=255 values recorded (occupying 510 bytes of the EEPROM) but now every update costs two-bytes of write.

                               

                              - Gough

                              1 of 1 people found this helpful
                                • Re: Arduino LED Dimmer , TV Remote Controll,  Saving  EEPROM
                                  arduino007

                                  Thank a lot for detailed information. I have no good experience in programing. I just try to find a solution. I try change my code again after your information.

                                  Actually I use a mini LED Dimmer at my home. Its very small and work great. Its save also last brigtness setting and with only two buttons i can controll brightness and i can turn it on/Off.I attached Photo of Dimmer.

                                   

                                  I want actually same controller with Ardino. Because of no experience in Programing its for me very difficult to solve to problems in Code. Can you please send me Code for me. that I past in my Program.

                                   

                                  Thanks again for Help

                          • Re: Arduino LED Dimmer , TV Remote Controll,  Saving  EEPROM
                            shabaz

                            Hi,

                             

                            Not sure why you're making this more complicated than it need be.

                            If you're happy with the behavior of the existing code (i.e. the dimming and on/off works fine) then why not delete (or comment out, with two slashes like this //

                            the line that says

                            bright=255;
                            

                            (in the code area that is concerned with switching off the LED) and replace the line that says digitalWrite(out,HIGH); with:

                            analogWrite(out,bright);
                            

                             

                            Then, when the remote is used to turn on the LED, it will light at the previous level. No need for EEPROM, unless you're going to be unplugging/plugging the device often, which makes no sense if you're intending to use the remote control. You only have 5 levels of dimness anyway, so on the rarer occasion that you unplug the device (or switch off the mains), then it's quick to set the level again.

                             

                            Still, if you wish to place the level in EEPROM, then at the point the LED is switched off (at that bright=255; line in the loop function, not the setup function) stick the current brightness level into EEPROM, using:

                            EEPROM.write(0, bright);
                            

                            That line will stick the brightness level into address 0 of EEPROM.

                             

                            Then, at the point where the lamp is switched on (which is the line that said digitalWrite(out,HIGH);   ) you can replace that line with these two lines that read from EEPROM and set the level, using:

                            bright=EEPROM.read(0);
                            analogWrite(out,bright);
                            

                             

                            And that's it.. you'd need to test it, because I'm mentally doing it, trying to understand this badly formatted code. But I believe those code changes are likely to cover the usual situations, where the brightness level gets recovered after the light is switched off, and then the power is turned off. It won't cover the scenario that the light is on, then the user adjusts the brightness, and then pulls the power. But that's a corner case that would require you to re-architect the code if you wish to do it properly - not worth it until you're more experienced in programming (maybe take a week to learn Arduino, it isn't difficult).

                            And please use the syntax highlighting feature, it is described here:

                            How to Ask Questions using the Create Discussion tool

                            And also please make your code look neat, it allows people to help you. Some tips to get you started are described here: https://www.cs.swarthmore.edu/~newhall/unixhelp/c_codestyle.html

                            1 of 1 people found this helpful