Here's my code
const int led = 4;
void setup(){
pinMode(led, OUTPUT);
Serial.begin(9600);
}
void loop(){
while(Serial.available() == 0);
int k = Serial.read() - '0';
Serial.println(k);
if(k == 1){
digitalWrite(led, HIGH);
}
else if (k == 0){
digitalWrite(led, LOW);
}
}
For some reason whenever I put in a value it returns two, the value I gave it and then -38 in the next line. This is why I have no else block, because it is always triggered immediately. The code works right now, but it won't when it comes to actually taking specific values for calculations.
Does anyone know why this happens and what I can do to fix it? I'm using an elegoo uno r3 if that could be it.