Not looping in void loop in other computer?

char Num;

void setup() {
  Serial.begin(9600);
}

void loop() {
if(Serial.available()){
    Num=Serial.read();
    Serial.println(Num);
  } 
   if(Num=='1'){ 
int sensorValueS = analogRead(A1);
float voltage = sensorValueS * (5.0/1024.0*2);
Serial.println(voltage);
delay(30);
 }
   if(Num=='0'){ 
int sensorValueB = analogRead(A5);
float voltage = sensorValueB* (5.0/1024.0);
Serial.println(voltage);
delay(30);
 }
}

In A computers, it works fine. When I type "1", the Serial Monitor will keep printing very "Delay(3)" But when I use another computer. The If action will only do once.

Which IDE version do you use? What operating system does your other computer have that it doesn't work on? And which Arduino device do you use? We need several info to be able to help them!

I suspect the "another computer" is set to send line ending characters, like newline or return or both. After processing the '1' or '0' the next character is read (newline or return) and the latest character read is no longer '1' or '0'.

Try with not using Serial.available(); or Serial.println(Num);

char Num;

void setup() {
  Serial.begin(9600);
}

void loop() {
  // if (Serial.available()) {
    Num = Serial.read();
  //   Serial.println(Num);
  // }
  if (Num == '1') {
    int sensorValueS = analogRead(A1);
    float voltage = sensorValueS * (5.0 / 1024.0 * 2);
    Serial.println(voltage);
    delay(30);
  }
  if (Num == '0') {
    int sensorValueB = analogRead(A5);
    float voltage = sensorValueB * (5.0 / 1024.0);
    Serial.println(voltage);
    delay(30);
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.