Build Your Own AI Assistant Part 1 - Creating the Assistant
116316 Views
Is the new Raspberry Pi AI Kit better than Google Coral?
111570 Views
Control Arduino with Python using Firmata / PyFirmata
86865 Views
How to Map with LiDAR - using a Raspberry Pi Zero 2W, RPLidar and Rviz
56603 Views
Creating a Supercomputer with a Raspberry Pi 5 Cluster and Docker Swarm!
52712 Views
Node-Red Automation, MQTT, NodeMCU & MicroPython
51798 Views
Weather Station Display
Pi 10 Inch Mini-rack
Installing and Using DeepSeek-R1:1.5 on a Raspberry Pi with Docker
Gamepad & BurgerBot
Level Up your CAD Skills
Operation Pico
Mini-Rack 3D Design Tutorial
0h 20m
Using the Raspberry Pi Pico's Built-in Temperature Sensor
0h 24m
Getting Started with SQL
0h 32m
Introduction to the Linux Command Line on Raspberry Pi OS
0h 42m
How to install MicroPython
0h 8m
Wall Drawing Robot Tutorial
0h 22m
Learn Linux from the basics to advanced topics.
Learn how to use a Raspberry Pi Pico
Learn MicroPython the best language for MicroControllers
Learn Docker, the leading containerization platform. Docker is used to build, ship, and run applications in a consistent and reliable manner, making it a popular choice for DevOps and cloud-native development.
Learn how to build SMARS robots, starting with the 3D Printing the model, Designing SMARS and Programming SMARS
Learn how to build robots, starting with the basics, then move on to learning Python and MicroPython for microcontrollers, finally learn how to make things with Fusion 360.
Learn Python, the most popular programming language in the world. Python is used in many different areas, including Web Development, Data Science, Machine Learning, Robotics and more.
Learn how to create robots in 3D, using Fusion 360 and FreeCAD. The models can be printed out using a 3d printer and then assembled into a physical robot.
Learn how to create Databases in Python, with SQLite3 and Redis.
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 >