Saving values to eeprom

Im messing around having fun with RGB leds, but my problem is whenever I cut power, then turn on power to the leds I lose the color. How can I save the RGB values to eeprom so when I attach power again the leds automatically show the last color before being shut off? Is this possible?

It is possible, but be aware that EEPROM has limited erase//write lifetime, so you don't want to be writing too often, so it is inadvisable to do this if you're doing some kind of automatic colour sweep/fade.
If you're setting the colour by user controls, maybe only save the colours if the user hasn't changed them for, say, ten seconds.

Is this possible?

Yes.

How can I save the RGB values to eeprom

Use EEPROM.write() and EEPROM.read().

Be aware that EEPROM has a finite number of writes (on the order of 100,000).

Thanks for the replies.. Im controlling the leds thru my android app, so can I create a button on my app, that when pressed calls a function on the arduino for the values to be saved on eeprom. Is this the best way to do it?

Is this the best way to do it?

I think so.

Thanks alot.

How would I know if I saved the values correctly?

  case 'S':
   EEPROM.write(redpin, redvalue);
   EEPROM.write(greenpin, greenvalue);
   EEPROM.write(bluepin, bluevalue);
   EEPROM.write(redpin2, redvalue2);
   EEPROM.write(greenpin2, greenvalue2);
   EEPROM.write(bluepin2, bluevalue2);
   break;

AllenI:
How would I know if I saved the values correctly?

  case 'S':

EEPROM.write(redpin, redvalue);
   EEPROM.write(greenpin, greenvalue);
   EEPROM.write(bluepin, bluevalue);
   EEPROM.write(redpin2, redvalue2);
   EEPROM.write(greenpin2, greenvalue2);
   EEPROM.write(bluepin2, bluevalue2);
   break;

Read them back right after you write them and compare the read values to the ones you intended to write.

Im a bit confused. Say the color I want to save to EEPROM is blue for light 1 and green for light 2, the values would be 0, 0, 255 for light 1 and 0, 255, 0 for light 2. So I press the button and it triggers the corresponding case. Now those values are supposedly saved to eeprom. I want to be able to power off then power on and have those colors automatically light with those two colors I saved. Right now when I reset Im getting a strobing effect? Is the arduino continuously writing or something? Im lost
inside the void loop:

  case 'S':
   EEPROM.write(1, redvalue);
   EEPROM.write(2, greenvalue);
   EEPROM.write(3, bluevalue);
   EEPROM.write(4, redvalue2);
   EEPROM.write(5, greenvalue2);
   EEPROM.write(6, bluevalue2);
   break;

  } // close switch case
  
  analogWrite(redpin, 1);
  analogWrite(greenpin, 2);
  analogWrite(bluepin, 3);
  analogWrite(redpin2, 4);
  analogWrite(greenpin2, 5);
  analogWrite(bluepin2, 6);
} //close void loop

Post the whole of your program. All we can see at the moment is a snippet of code that seems to write to the EEPROM. Have you got any code to read the values back from the EEPROM as was suggested ?

void loop() {
  //App Inventor sends three bytes (RGB, 0 to 255).
  if ( BTSerial.available() > 3){
    
    if ( BTSerial.available() == 4){
      //read each of the 3 bytes for brightness into the variables
      redvalue=BTSerial.read(); 
      greenvalue=BTSerial.read();
      bluevalue=BTSerial.read();
      //flush the buffer
      BTSerial.flush();
    } 
    else {
      
      if (byte(BTSerial.read() == 'A')){
        
      redvalue2=BTSerial.read();
      greenvalue2=BTSerial.read();
      bluevalue2=BTSerial.read();
    }
        
      BTSerial.flush();
    }
  } 
  //write the current values to the pwm pins.
  analogWrite(redpin, redvalue);
  analogWrite(greenpin, greenvalue);
  analogWrite(bluepin, bluevalue);
  analogWrite(redpin2, redvalue2);
  analogWrite(greenpin2, greenvalue2);
  analogWrite(bluepin2, bluevalue2);
  

  
  switch (BTSerial.peek()){
  
  case 'S':
   EEPROM.write(1, redvalue);
   EEPROM.write(2, greenvalue);
   EEPROM.write(3, bluevalue);
   EEPROM.write(4, redvalue2);
   EEPROM.write(5, greenvalue2);
   EEPROM.write(6, bluevalue2);
   break;

  }
  

  analogWrite(redpin, 1);
  analogWrite(greenpin, 2);
  analogWrite(bluepin, 3);
  analogWrite(redpin2, 4);
  analogWrite(greenpin2, 5);
  analogWrite(bluepin2, 6);
}

If you use Tools + Auto Format, you will see a real problem with that code, once it is properly indented.

void loop()
{
  //App Inventor sends three bytes (RGB, 0 to 255).
  if ( BTSerial.available() > 3)
  {
    if ( BTSerial.available() == 4)
    {
      //read each of the 3 bytes for brightness into the variables
      redvalue=BTSerial.read(); 
      greenvalue=BTSerial.read();
      bluevalue=BTSerial.read();
      //flush the buffer
      BTSerial.flush();
    } 
    else
    {
      if (byte(BTSerial.read() == 'A'))
      {
        redvalue2=BTSerial.read();
        greenvalue2=BTSerial.read();
        bluevalue2=BTSerial.read();
      }

      BTSerial.flush();
    }
  }
  
  //write the current values to the pwm pins.
  analogWrite(redpin, redvalue);
  analogWrite(greenpin, greenvalue);
  analogWrite(bluepin, bluevalue);
  analogWrite(redpin2, redvalue2);
  analogWrite(greenpin2, greenvalue2);
  analogWrite(bluepin2, bluevalue2);

  switch (BTSerial.peek())
  {
    case 'S':
      EEPROM.write(1, redvalue);
      EEPROM.write(2, greenvalue);
      EEPROM.write(3, bluevalue);
      EEPROM.write(4, redvalue2);
      EEPROM.write(5, greenvalue2);
      EEPROM.write(6, bluevalue2);
      break;
  }

  analogWrite(redpin, 1);
  analogWrite(greenpin, 2);
  analogWrite(bluepin, 3);
  analogWrite(redpin2, 4);
  analogWrite(greenpin2, 5);
  analogWrite(bluepin2, 6);
}

On every pass through loop, you read all available serial data. There may be enough to actually set the pins some defined values. Then, regardless of whether or not there was, you right very small values to the pins EVERY pass through loop.

There is another problem with the code, in that serial data arrives very slowly compared to how fast the Arduino executed. The outer if block will only ever be true when the 4th character arrives. The inner if statement will always be true. While fetching the 4 characters, a 5th one might arrive, but you will throw it away. Why, I can't imagine.

In the case where there are 4 bytes of data (more than 3) that triggers the if statement, you read three values, and flush the 4th one.

Every pass through loop, you peek at the serial data to see if the first character is an 'S'. If so, you save some data in EEPROM. That 'S' might sit in the serial buffer for half a million executions of loop(). Do you really want to write to EEPROM that often?

You REALLY need to make the sender send packets, with defined start and end markers. Every packet, whether it contains three values, 6 values, or one value needs to be inside those start and end markers. Then, you read a whole packet, and decide what to do based on the size of the packet and the contents of the packet.

UKHeliBob:
Post the whole of your program.

This means the entire content of the sketch file, not just interesting parts you have picked out from it.

Every pass through loop, you peek at the serial data to see if the first character is an 'S'. If so, you save some data in EEPROM. That 'S' might sit in the serial buffer for half a million executions of loop(). Do you really want to write to EEPROM that often?

You REALLY need to make the sender send packets, with defined start and end markers. Every packet, whether it contains three values, 6 values, or one value needs to be inside those start and end markers. Then, you read a whole packet, and decide what to do based on the size of the packet and the contents of the packet.

No I dont, realistically, the user will probably choose a color(s) and only save the color settings once, thats it. Are you saying I should send the 'S' and after its read and values are saved get rid of that 'S' in the buffer so I dont continuously write to the EEPROM? I'll work on the packets.

Are you saying I should send the 'S' and after its read and values are saved get rid of that 'S' in the buffer so I dont continuously write to the EEPROM?

Yes, I am.

Ok I understand. My next question would be, how would I go about writing the the saved values when powering on the arduino after its been off? Or is how I have it correct besides the changes to be made in the buffer?

Speaking of buffer, here's something I was working on throughout most the nite before finally getting the bugs out earlier today. I added another case to my program to simply turn on/off the red led in my RGB. I was able to turn it on on first attempt with no problem, however it would only turn off on the 4th button press. (I have the button setup as a toggle switch)
ex. Press button- red light turns on
Press button 2nd time to turn off- nothing
Press button 3rd time- light is already on so no change obviously
Press button 4th time- finally it turns off

I tried every combination of Serial.read(), Serial.flush(), Serial.peek() and any other combo that mite work within the case and nothing. Finally instead of having of the app send only 1byte(3) to turn the light off, I changed it to send 3bytes(9,3,9) and FINALLY it worked properly lol. I believe it was an issue with the buffer, but I cannot understand what exactly. Here's the code..

  case 'R':
   digitalWrite(redpin2, HIGH);
   break;
  case 3:
   digitalWrite(redpin2, LOW);
   break;

Sending the 'R' - no problems there. Sending only 1byte(3) gave me the headache I just explained. Sending (9,3,9) and it works perfect, which Im glad, but Im not sure why though. If you have any idea, I'd really like to know, this thing was driving me crazy lol

My next question would be, how would I go about writing the the saved values when powering on the arduino after its been off?

Writing what values? You seem to not understand that EEPROM is to save data so that you can READ it after a power down/reset.