Getting User Input in setup()

I want to start a new arduino project for dc motor speed control using PWM. :grinning:
where arduino will take inputs from user in the setup() and start processing the data in loop().

I have the following things:

  • Arduino UNO
  • 1602 arduino lcd keypad shield
  • DC motor"

Questions on the "lcd keypad shield" will be as follows:

  • Input the frequency?
  • Input the duty cycle?
  • How much time motor should run?

I had google up, but cant find how to get user input in setup part. :roll_eyes: :roll_eyes:

Please help me with this..... :frowning:

There's nothing special about setup() except it only gets called once. You can put any code there.

Usually though, the setup is just used to set the pinMode() and Serial.begin() and then the loop() function starts controlling whatever it is you need to control before it asks the user anything. Usually you want the thing in a safe state with the motors de-energised when you leave the setup().

Show us the code that you have already.

More typical is looking for user input in the main loop as well as running the main code.

Put the user input code in a function and you can call the function from setup() and from loop() if necessary.

There is a user input function in planning and implementing a program.

...R

Thank you guys for your quick response. :wink: :wink:

I was thinking of a "DO WHILE" to get user input till i finish all the questions but its getting to much complicated on the scratch-pad .... :confused:

now it seems function is the best way... ---> thanks "Robin2"^

any other help would be appreciated... :slight_smile: :slight_smile:

User input code gets surprisingly complex surprisingly quickly. It's usually more than 50% of my Arduino programs.

Here's a very simple example, which requires three while loops in setup(), just to process "press any key."

void setup() {
  unsigned long WaitStart;
  Serial.begin(9600);

  //For Micro, Leonardo and Due(nativeUSB) we need to wait for the serial monitor to connect.
  //This wait will be skipped on other Arduinos, as Serial always returns true.
  //wait up to 50 seconds
  WaitStart = millis();
  while(!Serial) {
    if(millis() - WaitStart > 50000) break;
  }
  
  Serial.println("Press any key to continue");

  //wait for user input - give them 20 seconds then continue anyway
  WaitStart = millis(); 
  while(!Serial.available()) {
    if(millis() - WaitStart > 20000) break;
  }

  //OK, we've received at least one character. That counts for "any key".
  //But the serial monitor is probably configured to send both CR and LF
  //If we were to look for the LF now, we would not find it because
  //it will take about 10ms for that char to arrive at 9600 baud.
  //so we wait a bit more, reading from the serial input to discard each
  //arriving char.

  while(Serial.available()) {
    Serial.read();
    delay(20);
  }

  //OK, now we've finally processed the "press any key"
  //let's start the actual work...
}
void loop() {
  Serial.println("Working");
}

You could use the buttons on the LCD shield to control the variables.
Up Down for frequency
Left Right for duty cycle (you control the increment in code)

eg.
Within the LOOP function:
If motorRunning variable is FALSE then
If Up button detected - then frequency = frequency +10
If Down button detected - then frequency = frequency - 10

You could use the constrain function to ensure that the frequency variable never exceeds the limits.

You could also display the frequency and duty cycle values on the lcd screen.

If "select" button detected then call some sort of runMotor() function that uses the frequency and duty cycle variables. Change the motorRunning variable to TRUE.

When motor stops running - change the motorRunning variable to FALSE.

This method does not take into account the amount of time to run the motor, but once you get this to work,
you should be able to modify the sketch to make it more complicated.

Start simple - and go from there.

This link has some code which may prove to be useful in your project ??
http://www.dfrobot.com/wiki/index.php?title=Arduino_LCD_KeyPad_Shield_(SKU:_DFR0009)