This is my third arduino project, and I am using the serial monitor for the first time. The project is meant to ask the user for the power and amount the time that the DC motor is to run at. Then the user presses a button and the motor runs with their defined inputs. I know that my set up isn't the issue, it's the code.
When it comes time for the user defined values, the program doesn't stop for input, it just displays the Serial.print statements over and over again until I unplug my Uno.
What am I doing wrong? I am fairly new at this, and do not have much of a background in programming.
// ******* GOAL *******
// run a motor at a power
// and time specified interactively
// over the serial connection,
// after a button is pressed
// ******* DEFINITIONS *******
#include <AFMotor.h>
// motor number 3
AF_DCMotor motor(3);
//UNCHANGING VALUES
// button pin
const int Button = 9;
//VARIABLES
// the current button reading
int buttonState;
// Last run’s reading
int lastButtonState = LOW;
// Using unsigned long to prevent overflow
// the last time toggled
unsigned long lastDebounceTime = 0;
// debouncing time minimum delay
unsigned long debounceDelay = 10;
int powerChar = Serial.read();
int timeChar = Serial.read();
//******* Start the program *******
// ******* SETUP *******
void setup() {
uint8_t i;
// establich communication
Serial.begin(9600);
// button is an input
pinMode(Button, INPUT);
} // end setup
// ******* THE DOING *******
void loop() {
// go to input function
askForUserInput();
// ******* DEBOUNCING *******
// read the state of button
int reading = digitalRead(Button);
// if Last button state is not equal to the reading
// make timer equal time of this run
if (reading != lastButtonState) {
// setting debounce timer using millis
// millis is built in timer. records run time in milisecondss
lastDebounceTime = millis();
} // end of debounce condonitions
// ******* UPDATING *******
// if the millisecond counter is longer than the last debouncing time,
// but greater than the debouncing delay, update the state changes
if ((millis() - lastDebounceTime) > debounceDelay) {
// let the button state update
if (reading != buttonState) {
// read the button state
buttonState = reading;
// ******* MAKE THE MOTOR GO *******
// Turn motor on with user specifications, if button pushed
if (buttonState == HIGH) {
// go to the motor control function
motorGo();
} // end if button pushed
} // end updating button state
} // end updating if statement
// save state for next reading
lastButtonState = reading;
// stop the motor
// motor.run(RELEASE);
// restart the loop
Serial.println("LETS GO AGAIN");
// pause for a second before going again
delay(1000);
} // end of iteration
// ******* USER DEFINED FUNCTION *******
// function for data interaction
void askForUserInput() {
// ******* ASK QUESTIONS *******
// statement for requested information
Serial.println("Please Enter the following information to run the motor.");
Serial.println();
// User defined variables
// define the power setting
Serial.println("What power do you want to motor to run?: \'");
Serial.println ("Power can range 1-255.");
Serial.write(powerChar);
Serial.println();
// define the time setting
Serial.println("How long would do you want to motor to run?: \'");
Serial.println("Time will be mesaured in miliseconds.");
Serial.write(timeChar);
Serial.println();
// instruct user to press the button
Serial.println( "Now please press the button.");
}
void motorGo() {
// testing reasonability of input
if (powerChar <= 255 && powerChar != 0) {
// run motor with user definition
motor.setSpeed(powerChar);
// user defined time
delay(timeChar);
} // end update the motors from user
// if too much power defined
if (powerChar > 255 || powerChar <= 0) {
// send correcting information
Serial.println( "THAT IS TOO HIGH OF A POWER!");
Serial.println( "POWER CAN ONLY RANGE 1-255!");
} // end correction
} // end motorGo