Hey so i am making a processing controlled led "lightshow". So i am sending the LED information to arduino via a serial port, but the LEDs don't do anything. At processing i am sending information as a 9digit number. It should work like this:
Processing seperates the music in 3frequencies, maps them from 0-255, puts all the numbers together. Then arduino should seperate that in to 3 integers. Exaple:
Processing output: 050255100 Arduino: R = 50 G=255 B=100.
so each int controlls one color of the LED. I also tryed with only one LED, but it doesn't seem to work. If i write the number through the serial monitor it works fine... I am new to arduino so help is appreciated.
Arduino code:
int Red = 11;
int Blue = 6;
int Green = 9;
int Rd = 0;
int Bl = 0;
int Gr = 0;
String readstring;
void setup() {
pinMode(Red, OUTPUT);
pinMode(Green, OUTPUT);
pinMode(Blue, OUTPUT);
Serial.begin(19200);
}
void loop() {
if(Serial.available()>0){
readstring = Serial.readString();
Rd = readstring.substring(0, 3).toInt();
Gr = readstring.substring(3,6).toInt();
Bl = readstring.substring(6,9).toInt();
analogWrite(Red, Rd);
analogWrite(Green, Gr);
analogWrite(Blue, Bl);
}
}
Using readString() is rarely a good idea. Using readStringUntil() is a marginally better idea. Instead of stuffing your head in the sand until the Serial class gives up hope that more data will arrive, as you are doing now, you should tell it that no more data is expected after the whatever that you pass to the function arrives. Then, when that whatever arrives, readStringUntil() will return, a whole sooner than readString() does.
Then, the useless 1 second delay won't be necessary.