Build Your Own AI Assistant Part 1 - Creating the Assistant
116820 Views
Is the new Raspberry Pi AI Kit better than Google Coral?
114678 Views
Control Arduino with Python using Firmata / PyFirmata
87081 Views
How to Map with LiDAR - using a Raspberry Pi Zero 2W, RPLidar and Rviz
57314 Views
Creating a Supercomputer with a Raspberry Pi 5 Cluster and Docker Swarm!
53588 Views
Node-Red Automation, MQTT, NodeMCU & MicroPython
52067 Views
SMARS Lab upgrade with PyCharm
Chicken Nugget Piano
Pi Tray - Mini-rack - Part II
Weather Station Display
Pi 10 Inch Mini-rack
Installing and Using DeepSeek-R1:1.5 on a Raspberry Pi with Docker
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
14% Percent Complete
By Kevin McAleer, 9 Minutes
In this lesson you will learn how to move your robot forward by writing code and uploading it to your SMARS robot.
Before you begin, youâll need to make sure you have a couple of things before you start this lesson:
New Sketch
You can download the code below from GitHub, but its better to type it in yourself line by line, as you will get to understand what each line means.
Type the following lines:
// Lesson 01 Movement // www.smarsfan.com/play/lessons/lesson_01_movement // set Motor A to Arduino Pins int motor_A = 12; // official Arduino Motor Shield uses D12 int motor_B = 13; // official Arduino Motor Shield uses D13 // set the Motor Speed using the Arduino Pins int motor_A_speed = 10; // official Arduino Motor Shield uses D3 int motor_B_speed = 11; // official Arduino Motor Shield uses D11 // set the time between motor on and motor off int wait_in_milliseconds = 1000; // this code runs once at the start void setup() { // set the Arduino pin to OUTPUT mode pinMode(motor_A, OUTPUT); pinMode(motor_B, OUTPUT); } // move forward void forward() { // set the direction to forward digitalWrite(motor_A, LOW); 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 main program loop void loop(){ // move forward forward(); // wait 2 seconds delay(2000); }
The next section explains what each piece of code means.
// Lesson 01 Movement // www.smarsfan.com/play/lessons/lesson_01_movement
Comments are lines in code put there to help the programmer understand what the code next to it does; the computer ignores them.
Putting two // forward slashes will tell the computer to ignore the rest of the line.
//
// set Motor A to Arduino Pins int motor_A = 12; int motor_B = 13; // set the Motor Speed using the Arduino Pins int motor_A_speed = 10; int motor_B_speed = 11; // set the time between motor on and motor off int wait_in_milliseconds = 1000;
Variables are special words that hold a value, such as a number, some text or other value such as true or false.
true
false
int means integer, which is a whole number between -32,768 and 32,767.
int
The Arduino has 32 pins, 20 of which can be used for read from and writing to. We tell the motors to turn on and off by writing a 1 or 0, also known as HIGH or LOW values.
1
0
HIGH
LOW
int motor_A = 12; means tell the Arduino that there is a motor on pin 12, and we will refer to it as motor_A in our program.
int motor_A = 12;
int motor_A_speed = 10; means tell the Arduino that we will control the speed of Motor A using pin 10.
int motor_A_speed = 10;
int wait_in_milliseconds = 1000; means set the integer wait_in_milliseconds to the value 1000 - the delay command used later on uses a value in milliseconds, (or thousands of a second). This means we can very accurately turn things on and off. A value of 500 would represent half a second. 1000 represents 1 second (a thousand milliseconds).
int wait_in_milliseconds = 1000;
wait_in_milliseconds
// this code runs once at the start void setup() { // set the Arduino pin to OUTPUT mode pinMode(motor_A, OUTPUT); pinMode(motor_B, OUTPUT); }
Arduinoâs have two main areas of code:
void setup() âvoidâ seems like a strange word, often meaning empty. In this case it means that there is no value returned by the section setup - it is void of value. Sections of code (also call Functions can return values or return nothing when they complete running).
void setup()
setup
setup() is the name of the Function.
setup()
{ } - each function has a section of code that is contained within curly braces. This tells the Arduino to expect code between the two curly braces, and to refer to that section of code by its Function name.
{ }
pinMode(motor_A, OUTPUT); means set the mode of the pin that motor_A is using (pin 12) to OUTPUT. Digital pins can either be INPUT or OUTPUT, which means the Arduino is either reading from or writing to the PIN. Pins can be either Digital or Analog:
pinMode(motor_A, OUTPUT);
motor_A
OUTPUT
INPUT
Digital
Analog
// move forward void forward() { // set the direction to forward digitalWrite(motor_A, LOW); 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 next section of code is a Function named forward(). As you might expect from the name, this function moves the robot forward.
forward()
To move the robot forward we need to:
digitalWrite(motor_A, LOW); means set the pin for motor_A to low
digitalWrite(motor_A, LOW);
digitalWrite(motor_B, HIGH); means set the pin for motor_B to high, as its facing the opposite direction
digitalWrite(motor_B, HIGH);
analogWrite(motor_A_speed, 255); means set the speed of Motor_A to 255 which is the fastest speed.
analogWrite(motor_A_speed, 255);
analogWrite(motor_B_speed, 255); means set the speed of Motor_B to 255 which is the fastest speed.
analogWrite(motor_B_speed, 255);
delay(wait_in_milliseconds); means wait the amount of time stored in the variable wait_in_milliseconds (which we set earlier as 1000 milliseconds, or 1 second).
delay(wait_in_milliseconds);
We need to wait an amount of time othewise the Arduino will continue on to the next instruction which tells the motors to stop. Without the delay the motors wouldnât have any time to turn and the Robot would not move forward.
Changing the amount of time stored in wait_in_milliseconds will make the amount of distance travelled either longer or shorter.
analogWrite(motor_A_speed, 0); means stop Motor_A from turning by making its speed zero.
analogWrite(motor_A_speed, 0);
analogWrite(motor_B_speed, 0); means stop Motor_B from turning by making its speed zero.
analogWrite(motor_B_speed, 0);
// the main program loop void loop(){ // move forward forward(); // wait 2 seconds delay(2000); }
Every computer program has a main loop which is used to control all other parts of the program.
Arduinoâs always have a loop() function that is executed after the setup() function has completed running. Once the loop() function has completed executing all its instructions, it will return to the beginning and run them all again (which is why its called loop!).
loop()
void loop() { means this is the function called loop, and it doesnât return any values (which is why it starts with void).
void loop() {
loop
void
Again the curly braces { } contain all the code for this function.
forward(); means run the function called forward() that we created earlier on in the code. This function makes the robot move forward for 1 second.
forward();
delay(2000); means wait for 2 seconds.
delay(2000);
After waiting 2 seconds the loop completes, then start back at the very top and executes the code again, calling the forward(); function again.
Once you have typed in the code, click the Verify button (the Tick at the top of the screen). This will check that youâve not made any typing mistakes.
If it says âSuccess: Done verifyingâ then you have code that is ready to be uploaded.
If you get an error message, read the message and look at the area of code it is having a problem with and compare it to the code at the top of this page.
Select the Arduino connection using the dropdown box next to the upload arrow button. You will see a list of boards to choose from. SMARS robots use the Arduino/Generic UNO board type. Select the Port your SMARS is connected to (you should see this in the list).
Arduino/Generic
Click OK to continue.
You are now ready to upload this code to your SMARS robot. Press the upload button (the arrow next to the verify button), to upload the program to your robot.
Once the code have been uploaded, the robot will start to move forward for 1 second, stop and wait 2 seconds, then move forward again.
If youâve made it this far youâve successfully created your first SMARS program and have successfully moved the robot forward.
How would you change this program to make the robot go backwards?
Take a minute to appreciate what youâve done, and then move on to the next lesson: Lesson 2 - turning.
Next >