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);
}
}
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.
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.
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.
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);
}
}