Hey guys, I am new to the arduino and I love it. I'm programming it at the moment for controlling some rgb led lights. I have a program from VB I currently working on that sends the rgb values I enter. Is there a way to have arduino retrieve the last sent byte everytime it starts up so it doesn't lose the color I last set?
I was thinking of having in the arduino code itself to "save" the last byte that was sent and use that until a new command value comes in.
So how would i go about this. I just want to make sure im doing this right. Im still new at using the arduino and understanding the advance code. I found this sketch online and have been using it to talk to the arduino since.
Original
#include <EEPROM.h>
#define BLUE 11
#define GREEN 10
#define RED 9
int led_case;
byte incomingByte;
void set_leds(int value);
void setup(){
Serial.begin(9600);
led_case = 1;
}
void loop(){
Serial.flush();
byte input[9];
memset(input, '\0', 9);
byte startByte = '\0';
// Fetch incoming bytes until the char '!' was found
while(startByte != '!'){
startByte = Serial.read();
}
if(startByte == '!'){
// Fetch the next 9 characters
while(Serial.available() < 9){
;
}
for( int i = 0; i < 9; i++){
input[i] = Serial.read();
}
led_case = 1;
for(int j=0; j<3 ;j++){
int value = 0;
for(int i = 0; i<3; i++){
incomingByte = input[(3*j)+i];
if((incomingByte >= '0') && (incomingByte <='9')){
// Parse the characters into an integer value
value = value * 10 + (incomingByte - 48);
}
}
// Each time set_leds is called, the value of led_case is increased
// by one. Hence, we iterate over all colors.
set_leds(value);
}
}
Serial.flush();
}
void set_leds(int value){
if((value >= 0) && (value <= 255))
{
// -- Set the PWM values
switch(led_case){
case 1:
analogWrite(RED, value);
led_case++;
break;
case 2:
analogWrite(GREEN, value);
led_case++;
break;
case 3:
analogWrite(BLUE, value);
led_case++;
break;
}
}
}
Edited to the way i think i should have EEPROM set
#include <EEPROM.h>
#define BLUE 11
#define GREEN 10
#define RED 9
int led_case;
byte incomingByte;
void set_leds(int value);
void setup(){
Serial.begin(9600);
led_case = 1;
EEPROM.read(RED, value);
EEPROM.read(GREEN, value);
EEPROM.read(BLUE, value);
}
void loop(){
Serial.flush();
byte input[9];
memset(input, '\0', 9);
byte startByte = '\0';
// Fetch incoming bytes until the char '!' was found
while(startByte != '!'){
startByte = Serial.read();
}
if(startByte == '!'){
// Fetch the next 9 characters
while(Serial.available() < 9){
;
}
for( int i = 0; i < 9; i++){
input[i] = Serial.read();
}
led_case = 1;
for(int j=0; j<3 ;j++){
int value = 0;
for(int i = 0; i<3; i++){
incomingByte = input[(3*j)+i];
if((incomingByte >= '0') && (incomingByte <='9')){
// Parse the characters into an integer value
value = value * 10 + (incomingByte - 48);
}
}
// Each time set_leds is called, the value of led_case is increased
// by one. Hence, we iterate over all colors.
set_leds(value);
}
}
Serial.flush();
}
void set_leds(int value){
if((value >= 0) && (value <= 255))
{
// -- Set the PWM values
switch(led_case){
case 1:
analogWrite(RED, value);
EEPROM.write(RED, value);
led_case++;
break;
case 2:
analogWrite(GREEN, value);
EEPROM.write(GREEN, value);
led_case++;
break;
case 3:
analogWrite(BLUE, value);
EEPROM.write(BLUE, value);
led_case++;
break;
}
}
}
Well like I said I borrowed the code from an online source. It was posted on April 29, 2011. I'm using the latest IDE. Why would it matter if I used Serial.flush() or not? isnt the point of Serial.flush() to clean out the serial to start fresh and ready for the next code it may receive. It works for me the way I want it to so far and I'm just trying to learn where and how to use EEPROM correctly for this sketch.
The EEPROM can tolerate 100,000 writes. You may well wear it out in a second or so. Do what PaulS says and only write to it if the value changes. And maybe even then if a second or so has elapsed.
Ah ok! Thanks guys! I will mess around with it. I'm going to use the if command for if the value changes and then stays for more than 5-10mins it will save.
May I ask why you changed value = value * 10 + (incomingByte - '48'); to value = value * 10 + (incomingByte - '0'); ??? I guess this explains why the rgb values don't fully match?
I'm still trying to reverse engineer this code. I have a general idea what the sketch is doing but not 100% to where I could rewrite something like this on my own.
Aconolly:
May I ask why you changed value = value * 10 + (incomingByte - '48'); to value = value * 10 + (incomingByte - '0'); ??? I guess this explains why the rgb values don't fully match?
It's more readable, that's all. '0' and 48 are the same number.
Ah ok thank you for that!. What I'm planning to do is check if the serial port closes then save the last value that was set. Sounds good? Is this possible with serial.close()???
I coded my program to save the values last set but that only works if the arduino is still on. Once off the ardunio goes through my check list colors i set and it forgets the last color set.