const int redPin = 9; const int greenPin = 10; const int bluePin = 11; const int blackPin = 6; char redString[3]; char greenString[3]; char blueString[3]; char blackString[3]; byte Red, Green, Blue, Black; void setup() { pinMode (redPin, OUTPUT); pinMode (greenPin, OUTPUT); pinMode (bluePin, OUTPUT); pinMode (blackPin, OUTPUT); // Start serial connection at 9600 baud rate Serial.begin(9600); } void loop() { char ledString[12]; // create a string to hold the LED values when it's read memset(ledString,'\0', 12); //initialize that string to all null characters boolean ledStringValid = false; // declares and initializes a variable to track whether the string has all valid characters Serial.print("hi"); Serial.println(""); byte inByte = '\0'; // (changed 10 to 0)declare and initialize a byte to read in serial data while(inByte != '*') { inByte = Serial.read(); //read data and wait for an asterisk character } if (inByte == '*') { //if we got a correct start character ledStringValid = true; //declare and initialize a variable to track whether the string has all valid characters while(Serial.available() < 12) { ; //wait for enough data to be available (9 characters of led values), while doing nothing else } for (int i=0; i < 12; i++) { ledString[i] = Serial.read(); //reach each time string character into a character array if(ledString[i] < '0' || ledString[i] > '9') { ledStringValid = false; //if any character is a letter char and not a number char then the whole string is bad } } } if (ledStringValid == true) { char blackLed[4]; //create a string to hold the value for the red part of the string memset(blackString,'\0',4); //initialize that string to all NULL characters strncpy(blackString, ledString, 3); //copy the first three characters of the ledString into the redLed string Black = atoi(blackString); //convert ASCII red string to integer and store it in the Red integer variable Serial.print("Black: "); Serial.println(Black, DEC); char redLed[4]; //create a string to hold the value for the red part of the string memset(redString,'\0',4); //initialize that string to all NULL characters strncpy(redString, ledString+3, 3); //copy the first three characters of the ledString into the redLed string Red = atoi(redString); //convert ASCII red string to integer and store it in the Red integer variable Serial.print("Red: "); Serial.println(Red, DEC); char greenLed[4]; //create a string to hold the value for the red part of the string memset(greenString,'\0',4); //initialize that string to all NULL characters strncpy(greenString, ledString+6, 3); //copy the first three characters of the ledString into the redLed string Green = atoi(greenString); //convert ASCII red string to integer and store it in the Red integer variable Serial.print("Green: "); Serial.println(Green, DEC); char blueLed[4]; //create a string to hold the value for the red part of the string memset(blueString,'\0',4); //initialize that string to all NULL characters strncpy(blueString, ledString+9, 3); //copy the first three characters of the ledString into the redLed string Blue = atoi(blueString); //convert ASCII red string to integer and store it in the Red integer variable Serial.print("Blue: "); Serial.println(Blue, DEC); Serial.println(""); //print a blank line delay(50); } analogWrite(redPin, Red); analogWrite(greenPin, Green); analogWrite(bluePin, Blue); analogWrite(blackPin, Black); delay (50); }