x = map(analogRead(pin), 1023, 0, 0, 1023);
This is an expensive way to accomplish:
x = 1023 - analogRead(pin);
if(serialvalue == 0){
digitalWrite(12, LOW);
}
else if(serialvalue == 1){
digitalWrite(13, HIGH);
}
else if(serialvalue == 2){
digitalWrite(12, LOW);
}
else if(serialvalue == 3){
digitalWrite(13, HIGH);
}
So, you are sending 0, 1, 2, 3, or 'r'. Really?
Or, are you sending '0', '1', '2', '3', or 'r'?
void sendValue (int x){ // function to send the pin value followed by a "space".
Serial.print(x);
Serial.write(byte(32));
}
Serial.print(" "); would be a whole lot clearer.
You know that this is converting the integer to a string, right? Are you expecting a string in Max?