void loop not looping

So I have a motor controlled by a serial input. Once I input my speed into the serial monitor, the motor runs for an instant. and then turns off and doesnt run again, until I input another value. I feel like I have made a mistake in the code somewhere, but what I need it to do is run at the inputted speed, and then change the speed when a new input is entered. Thanks in advance.

// connect motor controller pins to Arduino digital pins
// motor one
int enA = 11;
int in1 = 9;
int in2 = 8;
// motor two
int enB = 10;
int in3 = 7;
int in4 = 6;
int motorspeedA = 0;
void setup()
{
 Serial.begin(9600);
 // set all the motor control pins to outputs
 pinMode(enA, OUTPUT);
 pinMode(enB, OUTPUT);
 pinMode(in1, OUTPUT);
 pinMode(in2, OUTPUT);
 pinMode(in3, OUTPUT);
 pinMode(in4, OUTPUT);


 Serial.println("Arduino is ready");
}

void loop()
{

digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
 if (Serial.available()){
   motorspeedA = Serial.parseInt();
 }
  if (motorspeedA >= 0 && motorspeedA <= 255){   
       analogWrite(enA, motorspeedA);
   }
}

Please use code tags when posting (see “How to use this forum”).

You might find Serial Input Basics helpful, as well as the idea that delay(2000) causes the Arduino to be completely unresponsive for 2 seconds.

I took the delay out, and it still did the same thing. I keep trying to read through the Serial Input Basics forum., but I’m really new to programming and a lot of it doesn’t seem to apply. I don’t really need to parse through data or anything I’m just trying to turn the serial monitor into a digital potentiometer.

What is the serial monitor line ending?

Then I would put in some Serial.print() statements to see what’s going on:

void loop()
{
 
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
  if (Serial.available()){
    motorspeedA = Serial.parseInt();
    Serial.print("new val:");
    Serial.println(motorspeedA);
  }
   if (motorspeedA >= 0 && motorspeedA <= 255){   
        analogWrite(enA, motorspeedA);
        Serial.print("writing to motor");
        Serial.println(motorspeedA);
    }
  delay(1000); //just to avoid spamming the serial monitor - remove once problem is understood. 
}

Then open up serial monitor and see where things are going wrong - look for things like Serial.println(“Arduino is ready”); being printed after you send a value (indicating that the board is resetting due to power issue), or to confirm that the sketch thinks it’s getting a new value different from what you sent.

My guess would be either power problems resetting the board, or line endings confusing it.

@AWOL, the line ending is newline

@DrAzzy, I tried your code and this is what I got.

It looks like the motorspeed integer somehow resets to 0 right after you input, so it runs once and resets. I’m not sure why it’s resetting.



If i'm not mistaken, your problem is right on the top. int motorspeedA = 0;

if you remove the = 0. it would stay set to whatever you set it as thru your serial.

right now, at the end of each loop it gets resets to 0 because that's what it says, and awaits your input

wildcat99s:
If i’m not mistaken, your problem is right on the top. int motorspeedA = 0;

if you remove the = 0. it would stay set to whatever you set it as thru your serial.

right now, at the end of each loop it gets resets to 0 because that’s what it says, and awaits your input

No, that’s at global scope. It only gets set to zero at the beginning of the program.

The problem is the line ending. He sends the speed and it gets set and then the next serial read reads that new line and tries to set the speed to that. The code either needs to ignore the new line or the settings on the serial monitor changed to no line ending.

If i'm not mistaken, your problem is right on the top.

You are mistaken. The declaration is required and the default is zero.

Only change the motor speed when it needs to be changed.

int motorspeedA = 0;

void setup() {
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  analogWrite(motor pin, motorspeedA);
}

void loop()
{

 if (Serial.available()){
   int newMotorspeed = Serial.parseInt();
    if (newMotorspeed >= 0 && newMotorspeed <= 255 && newMotorspeed != motorspeedA){  
       motorspeedA = newMotorSpeed;
       analogWrite(enA, motorspeedA);
   }
 }
}

Your other problem is - how do you know when the used has stopped typing input? Are they setting the motor to 10, or to 100? Presumably Serial.parseInt() takes care of this. But maybe it doesn’t.

Your other issue is that you will want to look at using classes if you have more than 1 motor. But tat’s a whole new topic.

yeah. i guess i was wrong.

Did you try:

if (Serial.available() > 0){

?

JCA34F:
Did you try:

if (Serial.available() > 0){

?

It's right there in his code:

if (Serial.available()){

I seem to recall that construct causing trouble before: Look at the "official" example.
https://www.arduino.cc/reference/en/language/functions/communication/serial/available/

JCA34F:
I seem to recall that construct causing trouble before:

Where?

JCA34F:
Look at the "official" example.

Look at the second example on that page.

It's just basic C++. There's nothing esoteric about it. available returns a number or a zero and that works in if statements like that all over the world.

Please show an example of where it causes an issue?

Where?

Can't recall now, wonder if the OP tried it?

JCA34F:
Can't recall now, wonder if the OP tried it?

It doesn't make any difference whatsoever. The reason you can't remember is because it isn't real.

The OP's problem is the line endings. It's been pointed out and explained exactly why it is happening.

So I figured it out, sorta.
I think the problem was that whenever there was no input from the serial monitor it assumed the value to be 0, which is why I kept seeing it write a motor speed of 0 to the motor. So I just changed it to only look for values from 1 - 255, so that when it sees a 0 it does nothing. I did have to make an extra off switch, in this case I just type in 999 and it turns off. Thanks for the help though guys. using the serial monitor to debug the code was extremely helpful, and I’ll definitely need to make sure to use that again.

// connect motor controller pins to Arduino digital pins
// motor one
int enA = 11;
int in1 = 9;
int in2 = 8;
// motor two
int enB = 10;
int in3 = 7;
int in4 = 6;
int motorspeed;

void setup()
{
  Serial.begin(9600);
  // set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);


  Serial.println("Arduino is ready");
  Serial.println("Enter a number between 1 and 255, 999 to turn off");
}

void loop()
{
  if (Serial.available()){
    motorspeed = Serial.parseInt();
    Serial.println(motorspeed);

  }
   if (motorspeed > 0 && motorspeed <= 255){  
        digitalWrite(in1, LOW);
        digitalWrite(in2, HIGH);
        digitalWrite(in3, HIGH);
        digitalWrite(in4, LOW);
        analogWrite(enA, motorspeed);
        analogWrite(enB, motorspeed);
        //Serial.print("Motor Speed: ");
        //Serial.println(motorspeed);
    }
   if(motorspeed == 999){
      analogWrite(enA, 0);
      analogWrite(enB, 0);
      digitalWrite(in1, LOW);
      digitalWrite(in2, LOW);
      digitalWrite(in3, LOW);
      digitalWrite(in4, LOW);
   }
}

Nevermind, @DeltaG, you were right, I just needed to make the serial monitor no line ending.