• Blogs
• Circuit 5A: Motor Basics

## Circuit 5A: Motor Basics

• Public
By Adam Kroeker August 14, 2022 - 12:18pm

I had no trouble with building the circuit. My program is like the example program, except that flipping the switch makes the motor change directions rather than turn it on and off. I have also added some keywords that can be entered into the serial monitor in place of numbers. For example, entering “very fast” sets the motor speed to 255. Here is the code:

`//PIN VARIABLES//the motor will be controlled by the motor A pins on the motor driverconst int AIN1 = 13; //control pin 1 on the motor driver for the right motorconst int AIN2 = 12; //control pin 2 on the motor driver for the right motorconst int PWMA = 11; //speed control pin on the motor driver for the right motor`
`int switchPin = 7; //switch to turn the robot on and off`
`//VARIABLESint motorSpeed = 0; //starting speed for the motor`
`//Various speeds that the motor could be set toint veryFast = 255;int fast = 191;int slow = 128;int verySlow = 64;int brake = 0;`
`void setup() { pinMode(switchPin, INPUT_PULLUP); //set this as a pullup to sense whether the switch is flipped`
`//set the motor control pins as outputs pinMode(AIN1, OUTPUT); pinMode(AIN2, OUTPUT); pinMode(PWMA, OUTPUT);`
`Serial.begin(9600); //begin serial communication with the computer`
`Serial.println("Enter motor speed: "); //Prompt to get input in the serial monitor.}`
`void loop() { if(Serial.available()>0){ //if the user has entered something in the serial monitor String enteredString = Serial.readString(); //String entered by user enteredString.trim(); //Remove whitespace from either end of the string`
`//Various keywords that could have been entered by the user if(enteredString.equalsIgnoreCase("Very Fast")){ motorSpeed = veryFast; } else if(enteredString.equalsIgnoreCase("Stop")){ motorSpeed = brake; } else if(enteredString.equalsIgnoreCase("Fast")){ motorSpeed = fast; } else if(enteredString.equalsIgnoreCase("Slow")){ motorSpeed = slow; } else if(enteredString.equalsIgnoreCase("Very Slow")){ motorSpeed = verySlow; } else { motorSpeed = enteredString.toInt(); //If no keyword, then convert to int } Serial.print("Motor Speed: "); //print the speed that the motor is set to run at Serial.println(motorSpeed); } if (digitalRead(7) == LOW) { //if the switch is on... spinMotor(motorSpeed); //set the motor to the motorSpeed } else { //if the switch is off... spinMotor(motorSpeed*(-1)); //change the direction of the motor }`
`}`
`/********************************************************************************/void spinMotor(int motorSpeed) //function for driving the right motor{ if (motorSpeed > 0) //if the motor should drive forward (positive speed) { digitalWrite(AIN1, HIGH); //set pin 1 to high digitalWrite(AIN2, LOW); //set pin 2 to low } else if (motorSpeed < 0) //if the motor should drive backward (negative speed) { digitalWrite(AIN1, LOW); //set pin 1 to low digitalWrite(AIN2, HIGH); //set pin 2 to high } else //if the motor should stop { digitalWrite(AIN1, LOW); //set pin 1 to low digitalWrite(AIN2, LOW); //set pin 2 to low } analogWrite(PWMA, abs(motorSpeed)); //now that the motor direction is set, drive it at the entered speed}`

My program is based on the example program. I have included variables to hold the values that each of the keywords represent. The setup() function is unchanged from the example program. My loop() function reads the input from the serial monitor as a string and uses if-statements to check whether the input matches any of the keywords. If it does, the motorSpeed variable is set to the value represented by the variable associated with that keyword. Otherwise, the string is converted into an int before being assigned to the motorSpeed variable. This allows the user to enter integers as well as keywords to set the motor speed. The spinMotor() function is called at the end of the loop() function, and it is passed either motorSpeed or motorSpeed*(-1) depending on the position of the switch. The spinMotor() function is the same as in the example program.

Here is a video of the program in action:

The one main issue I had while writing the program was that I had initially left out the line “enteredString.trim();” near the top of the loop() function. Omitting this line prevented my keywords from working because the serial monitor was automatically adding newline characters to the end of the input. This meant that all the comparisons in the if-statements were false, since none of the strings that were being compared to the input string had newline characters in them. There is an option in the serial monitor to stop sending newline characters, but instead of using that I decided to use the trim() function. According to the Arduino documentation, the trim() function removes all whitespace characters, including newline characters, from the beginning and the end of a string.