Loop is not following intended execution and is changing variables

Board Used: Arduino Mega 2560 REV3
PC OS: Windows 10
IDE Version: 1.8.13
Connected via USB

Trying to have the loop() poll serial communications so it knows how to respond when value of 2 or 3 is sent via the Serial Monitor.

#define RelayTop 22
#define RelayBottom 25

//color sensor
#define S0 4
#define S1 5
#define S2 6
#define S3 7
#define sensorOut 8

//motor
#define motor1 53
#define motor2 51
#define motorspeed 49

//G Variables
int frequency = 0;
int color = 0;
int blue = 0;
int incomingByte = 1;


void setup() {
  //Begin COM
  Serial.begin(9600);
  //Relay Pins
  pinMode(RelayTop, OUTPUT);
  pinMode(RelayBottom, OUTPUT);
  //Color Sensor Pins
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(sensorOut, INPUT);
  //Motor Pins
  pinMode(motor1, OUTPUT);
  pinMode(motor2, OUTPUT);
  pinMode(motorspeed, OUTPUT);

  //Setting frequency-scaling to 20%
  digitalWrite(S0, HIGH);
  digitalWrite(S1, LOW);

  //Close Relays
  digitalWrite(RelayTop, LOW);
  digitalWrite(RelayBottom, LOW);

  //Motor Initial
  digitalWrite(motor1, LOW);
  digitalWrite(motor2, LOW);
  digitalWrite(motorspeed, LOW);

  delay(150);
}

// Relay Controls to Dispense ball
void Unload() {
  Serial.print("\n UB 2");
  digitalWrite(RelayBottom, HIGH);
  delay(100);
  digitalWrite(RelayBottom, LOW);
  Serial.print("\n UB 3");
}
// Relay Controls to Load Ball
void Load() {
  Serial.print("\n LB 2");
  digitalWrite(RelayTop, HIGH);
  delay(100);
  digitalWrite(RelayTop, LOW);
  Serial.print("\n LB 3");
}
// Color Sensing
void ColorSensing() {
  Serial.print("\n SC 2");
  // Setting Blue filtered photodiodes to be read
  digitalWrite(S2, LOW);
  digitalWrite(S3, HIGH);
  blue = pulseIn(sensorOut, LOW);
  delay(10);
  Serial.print("\n");
  Serial.print(blue);
  if (blue < 50) {
    color = 0;
    Serial.print("\n White");
  }
  else {
    color = 1;
    Serial.print("\n Painted");
  }
  Serial.print("\n SC 3");
}

//Motor Turning
void TurnMotor() {
  Serial.print("\n TM 2");
  if (color == 1) {
    digitalWrite(motor1, HIGH);
    digitalWrite(motor2, LOW);
    digitalWrite(motorspeed, HIGH);
    delay(30);
    digitalWrite(motor1, LOW);
    digitalWrite(motor2, LOW);
    digitalWrite(motorspeed, LOW);
    delay(25);
  }
  if (color == 0) {
    digitalWrite(motor1, LOW);
    digitalWrite(motor2, HIGH);
    digitalWrite(motorspeed, HIGH);
    delay(30);
    digitalWrite(motor1, LOW);
    digitalWrite(motor2, LOW);
    digitalWrite(motorspeed, LOW);
    delay(25);
  }
  Serial.print("\n TM 3");
}


void loop() {
  Serial.print("\n Idle");
  if (Serial.available() > 0) {
    incomingByte = Serial.read();
    Serial.print("\n Idle 2 ");
    Serial.print("\n ");
    Serial.print(incomingByte);
    if (incomingByte == 2) {
      Serial.print("\n LB1");
      Load();
      incomingByte = 1;
    }
    while (incomingByte == 3) {
      Serial.print("\n LB1");
      Load();
      delay(100);
      Serial.print("\n CS1");
      ColorSensing();
      delay(100);
      Serial.print("\n TM1");
      TurnMotor();
      delay(100);
      Serial.print("\n UB1");
      Unload();
      delay(100);
      Serial.print("\n ");
      Serial.print(incomingByte);

    }
  }
  delay(1000);
  Serial.print("\n Idle 3 ");
  Serial.print("\n ");
  Serial.print(incomingByte);
  delay(1000);
}

The loop enters the first if() statement when prompted and returns the “Idle 2” marker, after that things get weird. When sending a value using the serial monitor, it is being altered. For example, 2=50, 3=51, 4=52. Sending a value of -48 returned 45, then 52, then 56. After the values listed above, it is settles at a value of 10. Here is what the Serial Monitor shows by inputting a value of 2 and then 3.

 Idle
 Idle 3 
 incomingByte = 1
 Idle
 Idle 3 
 incomingByte = 1
 Idle
 Idle 2 
 incomingByte = 50
 Idle 3 
 incomingByte = 50
 Idle
 Idle 2 
 incomingByte = 10
 Idle 3 
 incomingByte = 10
 Idle
 Idle 2 
 incomingByte = 51
 Idle 3 
 incomingByte = 51
 Idle
 Idle 2 
 incomingByte = 10
 Idle 3 
 incomingByte = 10

Any assistance would be greatly appreciated., let me know if I missed any important information.

When I changed the conditions for the if() and while() loops to match the altered values they functioned as intended. While it works in this state I would still be interested in figuring out what is causing this interaction.

Hi,
Welcome to the forum.

This tutorial will help,
If you look at the ASCII code for 0 it is 48 decimal, 1 is 49 decimal,

Tom… :slight_smile:

What is sending the data that your read with Serial.read?

Is it the intention that you will get stuck in this while-loop?

    while (incomingByte == 3) {
      Serial.print("\n LB1");
      Load();
      delay(100);
      Serial.print("\n CS1");
      ColorSensing();
      delay(100);
      Serial.print("\n TM1");
      TurnMotor();
      delay(100);
      Serial.print("\n UB1");
      Unload();
      delay(100);
      Serial.print("\n ");
      Serial.print(incomingByte);

    }

If you want to read decimal numbers from your input you could use Serial.parseInt(); instead of Serial.read(); The 'parseInt()' function will read characters until it finds characters that represent a number. If no number is received before the timeout (default: 1 second) it returns zero.

If you want to read float numbers (number with a decimal point), use .parseFloat().

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