Asking for multiple integer user input?

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?

Thanks.

int motorPin = 3;

void setup(){
 pinMode(motorPin, OUTPUT);
}

void loop(){
 for(int speed=0; speed<=255; speed+=1){
   analogWrite(motorPin, speed);
   delay(30);
 }
 
 
 for(int speed=255; speed>=0; speed-=1){
   analogWrite(motorPin, speed);
   delay(30);
 }
 

}

I'm guessing you want the user to be able to type 20 or 240 to set the values.

The code in this data input demo in the function readSeveralChars() should give you some ideas.

...R

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

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.

Yes it happens on all platforms even Linux. This is because it is a property of the circuit design not the host computer.

Grumpy_Mike:

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.

Yes it happens on all platforms even Linux. This is because it is a property of the circuit design not the host computer.

now I definitively know, thanks!

Thanks all. BullDog, your code works brilliantly, I just made a few modifications. I'm such a noob at programming :grin:

Yeah, so everything turns off when you close the Serial Monitor. I shall try to have the Arduino store the data in its EEPROM and post back.

leofelixzhu:
so everything turns off when you close the Serial Monitor.

There are ways to disable the auto-reset mechanism. Google should find them.

If you disable it permanently it makes it more difficult (but not impossible) to upload code.

...R