First post here! I'm a little wet behind the ears when it comes to Processing, so go easy on me. Let me give you a little background on what I'm doing:
I have an Arduino that is controlling 4 LEDs on separate analog output channels. On the PC side, I have a Processing sketch that I have cobbled together from code I have found on the net (had no idea about Processing when I started, but I'm learning as I go) that has 4 sliders that adjusts the brightness of each LED channel on the Arduino (broadcast over serial). It also allows me to save the settings to EEPROM on the Arduino. So far, so good. The problem I'm having is that when I start the Processing sketch, it defaults the channel values to zero, bringing the LEDs to zero output, regardless of the original state. The Arduino recalls the EEPROM values for each channel on power up, and I would like the Processing sketch to be able to read those values as it starts, and set the sliders in the program according to said values.
The character 'Q' is sent by the Processing sketch to tell the Arduino to send the relevant values over serial (the Arduino is typically only receiving during normal operation). The Arduino spits the right info out, as seen on the serial terminal, but the Processing sketch doesn't seem to be parsing the data correctly. I have used the same basic construct to read the serial data on the Arduino and Processing sketch, but it's still not happy. The class that deals with the sliders deals with the data properly, as I have tried feeding it fixed numbers. It's just the serial side of things that's giving me grief.
On to the code. There are some commented out sections of code, as well as some stuff in there that is unnecessary that was used just for testing.
First up, the Arduino code:
#include <Wire.h>
#include <EEPROM.h>
#define DS1307_ADDRESS 0x68
byte rVal, gVal, bVal, wVal;
int EEred=0;
int EEgreen=1;
int EEblue=2;
int EEwhite=3;
byte rPin=6;
byte gPin=9;
byte bPin=10;
int wPin=11;
int second, minute, hour, weekDay, monthDay, month, year;
int onMin=00;
int onHour=10;
int offMin=00;
int offHour=21;
int moonLights=15;
void setup()
{
// declare the serial comm at 9600 baud rate
Serial.begin(9600);
rVal=EEPROM.read(EEred);
gVal=EEPROM.read(EEgreen);
bVal=EEPROM.read(EEblue);
wVal=EEPROM.read(EEwhite);
/*
Serial.println("EEPROM");
Serial.println(rVal);
Serial.println(gVal);
Serial.println(bVal);
Serial.println(wVal);
*/
// hour = 15;
Wire.begin();
}
void loop()
{
readTime();
onOff();
if(Serial.available()!=0)
{
// call the returned value from GetFromSerial() function
switch(GetFromSerial())
{
case 'R':
rVal=GetFromSerial();
break;
case 'G':
gVal=GetFromSerial();
break;
case 'B':
bVal=GetFromSerial();
break;
case 'W':
wVal=GetFromSerial();
break;
case 'S':
EEPROM.write(EEred, rVal);
EEPROM.write(EEgreen, gVal);
EEPROM.write(EEblue, bVal);
EEPROM.write(EEwhite, wVal);
break;
case 'Q':
Serial.flush();
/* Serial.write('R');
Serial.write(rVal);
Serial.write('G');
Serial.write(gVal);
Serial.write('B');
Serial.write(bVal);
Serial.write('W');
Serial.write(wVal);
*/
delay(100);
Serial.print('R');
Serial.print(rVal, DEC);
Serial.print('G');
Serial.print(gVal, DEC);
Serial.print('B');
Serial.print(bVal, DEC);
Serial.print('W');
Serial.println(wVal, DEC);
break;
}
}
/*
delay(2000);
if(hour>22)
{
hour=0;
}
else
{
hour++;
}
Serial.println(hour);
*/
}
// read the serial port
int GetFromSerial()
{
while (Serial.available()<=0) {
}
return Serial.read();
}
byte bcdToDec(byte val)
{
// Convert binary coded decimal to normal decimal numbers
return ( (val/16*10) + (val) );
}
void readTime()
{
// Reset the register pointer
Wire.beginTransmission(DS1307_ADDRESS);
byte zero = 0x00;
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
second = bcdToDec(Wire.read());
minute = bcdToDec(Wire.read());
hour = bcdToDec(Wire.read() & 0b111111); //24 hour time