108271 Views
83628 Views
56847 Views
48511 Views
47826 Views
47705 Views
Arduino Plug and Make Kit Review
Pi to Pico W Bluetooth Communication
Two-Way Bluetooth Communication Between Raspberry Pi Picos
Gamepad 2
Picotamachibi 2
Learning System updates
Introduction to the Linux Command Line on Raspberry Pi OS
How to install MicroPython
Wall Drawing Robot Tutorial
BrachioGraph Tutorial
Intermediate level MicroPython
Introduction to FreeCAD for Beginners
KevsRobots Learning Platform
28% Percent Complete
By Kevin McAleer, 5 Minutes
In this lesson you will learn how to move your robot backwards, turning left and right, and finally creating some code to control the SMARS over the serial port.
You can download the completed code from GitHub - www.github.com/kevinmcaleer/lesson_02_turning
Before you begin, you’ll need to make sure you have a couple of things before you start this lesson:
New Sketch
We are going to create 3 more functions, almost identical to the function we made in lesson 01, but with slightly different parameters for the direction of the motors.
// move backward void backward() { // set the direction to backward digitalWrite(motor_A, HIGH); digitalWrite(motor_B, LOW); // set to full speed analogWrite(motor_A_speed, 255); analogWrite(motor_B_speed, 255); // wait delay(wait_in_milliseconds); // stop analogWrite(motor_A_speed, 0); analogWrite(motor_B_speed, 0); }
// turn left void turnLeft() { // set the direction to backward digitalWrite(motor_A, LOW); digitalWrite(motor_B, LOW); // set to full speed analogWrite(motor_A_speed, 255); analogWrite(motor_B_speed, 255); // wait delay(wait_in_milliseconds); // stop analogWrite(motor_A_speed, 0); analogWrite(motor_B_speed, 0); }
// turn right void turnRight() { // set the direction to backward digitalWrite(motor_A, HIGH); digitalWrite(motor_B, HIGH); // set to full speed analogWrite(motor_A_speed, 255); analogWrite(motor_B_speed, 255); // wait delay(wait_in_milliseconds); // stop analogWrite(motor_A_speed, 0); analogWrite(motor_B_speed, 0); }
The code in each of these functions is virtually identical, apart from these line:
digitalWrite(motor_A, HIGH); digitalWrite(motor_B, HIGH;
These lines tell the motor-shield to set motor A and motor B to either rotate in different directions to move either backwards or forwards, or in the same direction to turn the SMARS either left or right. The reason for this is that the motors on the SMARS are facing opposite directions and therefore need to rotate in opposite directions to move the SMARS in a single direction. Rotating the motors in opposite directions has the affect of turning the body either left or right.
To make the SMARS controllable from the keyboard, we can use the Arduino Serial Monitor to issue commands to our SMARS. We will do this by creating a new piece of code that will look for the following 4 keys from the keyboard:
We will do this by reading in characters from the Serial interface, and then running either the forward(), backward(), turnLeft(), or turnRight() functions.
forward()
backward()
turnLeft()
turnRight()
We will change the code in the void loop() section to looks like this:
void loop()
while (Serial.available()) { char c = Serial.read(); switch(c) { // press the 'w' key to move SMARS Forward case 'w': Serial.println("Moving Forward"); forward(); break; // press the 's' key to move SMARS backward case 's': Serial.println("Moving Backward"); backward(); break; // press the 'a' key to move SMARS left case 'a': Serial.println("Turning Left"); turnLeft(); break; // press the 'd' key to move SMARS right case 'd': Serial.println("Turning Right"); turnRight(); break; } } // wait 2 seconds delay(2000);
We can now open the Serial monitor from the Tools menu, and being typing any of the four direction commands ‘w’,’s’,’a’,’d’ and return to move the SMARS in any of the four directions.
char c = Serial.read(); - this means create a new variable, of type Character (which can hold a single letter or number) and call it c and make that read in from the Serial interface. This is how we read in keys from the serial monitor.
char c = Serial.read();
c
switch(c) { - the switch function will look at the value within the variable c that we have passed it, and then depending on that it will execute different parts of code, depending on each case.
switch(c) {
case
There then follows four similar blocks of code:
// press the 'w' key to move SMARS Forward case 'w': Serial.println("Moving Forward"); forward(); break;
The first line is a comment (as denoted by the ‘//’ characters). The second line says, in the case of the value of c being w then execute the lines below. The third line says, write the string Moving Forward to the Serial monitor. The fouth line executes the forward() function. The last line break; means stop running through the case statements.
w
Moving Forward
Serial
break;
< Previous Next >