I've written python code that will transmit a series of integers through the serial interface. For the moment, all integers will be single digit, until i decide they ought be larger. The code i've written will transmit numbers in sets of 4, but delimiters can vary as per my choosing. A couple of possible transmittance options are listed below:
-1 -1 -1 -1
1,1,1,1
3,5,-4,6
....9,,,,-9,,,,,,8,,,,,6
Basically, i need to separate integers, identify if they are positive or negative (and make them such), and assign them to variables. I've attempted to write code that does this for one variable, but cannot solve the problem for negative integers or multiple values. My code is below. I suspect that once i've solved it for one integer it can be replicated for multiple variables, so the trick is to identify and isolate individual integers. Ignore LED2-LED4, they are placeholders for the other integers i intend to send.
I defer to your expertise, since my experience is limited. Any ideas?
/* Use a variable called byteRead to temporarily store
the data coming from the computer */
int byteRead;
int LED1;
int LED2;
int LED3;
int LED4;
int num1 = 2;
int num2 = 3;
int num3 = 4;
int num4 = 5;
void setup() {
// Turn the Serial Protocol ON
Serial.begin(9600);
}
void loop() {
/* check if data has been sent from the computer: */
while (Serial.available() > 0) {
/* read the most recent byte */
byteRead = Serial.read(); // grabs first integer
while (byteRead > 57 || byteRead != 45 && byteRead < 48){
byteRead =Serial.read();
}
switch (byteRead) {
case 45:
byteRead = Serial.read();
Serial.println(byteRead);
LED1 = -1 * (byteRead - 48);
break;
default:
LED1 = byteRead - 48;
}
Serial.println(LED1);
}
if (LED1 > 0){
digitalWrite(num1, HIGH);
}
else{
digitalWrite(num1, LOW);
}
}