My program is pretty simple: I am using PWM to control a DC motor hooked up to the Uno. What I am wanting to do next is ask for user input to set a maximum and minimum speed for the motor. This way the motor will start from the min. speed and gradually increase up to the max. speed. Then it will slowly decrease back down to the min. And this will cycle forever. And is it possible to have the board run the program even after I close the Serial Monitor? I have the control program figured out already, just can’t get the Serial Monitor to work like it should. Any tips?
And is it possible to have the board run the program even after I close the Serial Monitor?
To do that you have to disable the auto reset circuit. Then you have to master the skill of uploading code by pressing the manual reset button at exactly the right time.
What I am wanting to do next is ask for user input to set a maximum and minimum speed for the motor. This way the motor will start from the min. speed and gradually increase up to the max. speed. Then it will slowly decrease back down to the min. And this will cycle forever. And is it possible to have the board run the program even after I close the Serial Monitor?
My mac resets the arduino if i disconnect the Serial Monitor, but I am unsure what happens if you use a PC… I don’t believe it is a problem. If it is you could store your values in your arduino’s EEPROM, its persistent memory.
here is an untested sample of what you may want to try. If you need help storing the values in EEPROM, there is help here for that too.
int hiSpeed;
int lowSpeed;
boolean doneHi, doneLo, printedHi, printedLo;
int state;
int motorPin = 3;
//
void setup()
{
pinMode(motorPin, OUTPUT);
Serial.begin(9600);
Serial.println("Control system ready!");
}
//
void loop()
{
if (state==0)
{
if (printedHi==false)
{
Serial.println("Enter high limit:");
printedHi = true;
}
if (Serial.available())
{
hiSpeed = Serial.parseInt();
if (hiSpeed > 255)
{
hiSpeed = 255;
}
doneHi = true;
}
if (doneHi)
{
Serial.println(hiSpeed);
printedHi = false;
doneHi = false;
state++;
}
}
if (state == 1)
{
if (printedLo == false)
{
Serial.println("Enter Low Limit:");
printedLo = true;
}
if (Serial.available())
{
lowSpeed = Serial.parseInt();
if (lowSpeed < 0)
{
lowSpeed = 0;
}
doneLo = true;
}
if (doneLo)
{
if (hiSpeed < lowSpeed)
{
Serial.println("Input Error... bad values!");
Serial.println("Using default Values!!!");
hiSpeed = 255;
lowSpeed = 0;
}
Serial.println(lowSpeed);
printedLo = false;
doneLo = false;
state++;
}
}
if (state==2)
{
for(int i = lowSpeed; i <= hiSpeed; i++)
{
analogWrite(motorPin, i);
delay(30);
}
for(int i = hiSpeed; i >= lowSpeed; i--)
{
analogWrite(motorPin, i);
delay(30);
}
}
}