Step 1
To get familiar with an Arduino Sketch and what a Sketch that controls PopPet looks like, let's download and open our first Sketch.
step1.zip | |
File Size: | 1 kb |
File Type: | zip |
You'll need to extract this zip file then open the Arduino IDE. To open this Sketch follow the pictures below.
As you can see, we have a void setup() and void loop(). If you go to the 'motor_config.h' tab you will see lots of other things. The things in the motor_config are used to make our Sketch easier to read. Instead of writing the pin numbers all over our Sketch, which would make the Sketch quite hard to read, we give the pins names. for example:
Pin Naming
If we go back to the main tab, we will see that in the void setup() we have the statement 'configureMotors'. In the motor_config, this section is used to tell the Arduino that the pins used for the motors are all outputs:
This lets the Arduino know that we will be sending signals out of these pins in order to control the motors speed and direction.
If you scroll down further in the motor_config you will see that in order for PopPet to move certain directions, we will make the 'dirPin's' turn HIGH and LOW (On and Off) in a special combination.
If you scroll down further in the motor_config you will see that in order for PopPet to move certain directions, we will make the 'dirPin's' turn HIGH and LOW (On and Off) in a special combination.
Code Editor
Now let’s look at what this Sketch does. Go back to the main Sketch tab now.
The void loop() contains everything that PopPet will do whilst it is on.
The void loop() contains everything that PopPet will do whilst it is on.
Code Editor
Code Editor
If the left motor was going forwards instead of backwards, then change the 0 to a 1 for 'motLInverse'.
If the right motor was going backwards instead of forwards, then change the 0 to a 1 for 'motRInverse'.
Tick off 'Get PopPet to turn left' on the 'PopPet Goal Checklist'.
If the right motor was going backwards instead of forwards, then change the 0 to a 1 for 'motRInverse'.
Tick off 'Get PopPet to turn left' on the 'PopPet Goal Checklist'.
Here we can see that PopPet will turn Left, wait half a second, stop and wait another half a second. The delay is measured in milliseconds and there is 1000 milliseconds to a second, so if we wanted PopPet to turn left for a second, we would change this number to 1000. Because we have the delay right after the turnLeft() command, PopPet will continue on turning left for the duration of the delay, then stop. Once PopPet has done everything in the loop(), it will go back to the start and do it all over again.
Now let's see if our Sketch works. Get the USB cable and plug it into your computer. In the Arduino IDE make sure that you have selected the correct Arduino and COM port.
Go to 'Tools > Board' and select the 'Arduino Duemilanove'.
To change the COM port, go to 'Tools > Port' and select the COM port that PopPet is attached to.
Now that we have the right Board and COM port selected we can upload our Sketch to PopPet. We do this by clicking the UPLOAD button.
When the Sketch has been uploaded, remove the USB cable from PopPet and pull switch out to turn PopPet on.
Hopefully you will see the left motor go backwards and the right motor go forwards, making PopPet turn left. PopPet should do this for half a second, stop and wait half a second, then turn left again. If this is what happened, then congratulations! You have successfully uploaded your first Sketch!
If PopPet didn't turn left, but instead moved in some other direction, this means the motors weren't wired up properly. Never mind though, we can fix this in the Sketch. Go back to the motor_config and look for this:
Now let's see if our Sketch works. Get the USB cable and plug it into your computer. In the Arduino IDE make sure that you have selected the correct Arduino and COM port.
Go to 'Tools > Board' and select the 'Arduino Duemilanove'.
To change the COM port, go to 'Tools > Port' and select the COM port that PopPet is attached to.
Now that we have the right Board and COM port selected we can upload our Sketch to PopPet. We do this by clicking the UPLOAD button.
When the Sketch has been uploaded, remove the USB cable from PopPet and pull switch out to turn PopPet on.
Hopefully you will see the left motor go backwards and the right motor go forwards, making PopPet turn left. PopPet should do this for half a second, stop and wait half a second, then turn left again. If this is what happened, then congratulations! You have successfully uploaded your first Sketch!
If PopPet didn't turn left, but instead moved in some other direction, this means the motors weren't wired up properly. Never mind though, we can fix this in the Sketch. Go back to the motor_config and look for this: