Serial monitor integer input

hello
i've been trying to run a motor which takes speed as input from the serial monitor, however it immediately reads a default value of 0 straight after
an image of the serial monitor after inputting 25
image

#include <AFMotor.h>

int speed = 0; //for the serial data coming in
AF_DCMotor frontLeft(1);
AF_DCMotor rearLeft(2);
AF_DCMotor rearRight(3);
AF_DCMotor frontRight(4);

void setup()
{
  Serial.begin(9600);
  frontLeft.run(RELEASE);
  rearLeft.run(RELEASE);
  rearRight.run(RELEASE);
  frontRight.run(RELEASE);
}
void loop()
{
  if (Serial.available() > 0);
    speed = Serial.parseInt();
    Serial.println(speed);
  frontLeft.setSpeed(speed);
  frontLeft.run(FORWARD);
  rearLeft.setSpeed(speed);
  rearLeft.run(FORWARD);
  rearRight.setSpeed(speed);
  rearRight.run(FORWARD);
  frontRight.setSpeed(speed);
  frontRight.run(FORWARD);
}

Lose the semicolon ( ; )

The semicolon ends the if statement. All code after the if will execute unconditionally.

Put all the code that you want to execute conditionally inside of curly brackets ({}) even if it is just one line. Makes the code easier to follow.

And here, in C++, indentation only serves to make code more readable.

Unlike Python, for example, it makes no difference whether any number of lines following a control statement, such as an if statement are indented.

thanks to everyone, that fixed it

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