Real Robots
Make some cool stuff

Robby the Robot v2

Coding 2: How to Code


Alright, now we've done the set up and got the basics out of the way, let's do some coding!

Today we're going to learn the basics of coding that we need to teach Robby how to move.


Lets start with our code from last time.


    void setup() {    
        Serial.begin(115200);  
    }
    
    void loop() {
        Serial.println("HELLO!");                                         
        delay(1000);            
    }
                

First we want to learn how to use Variables. These are words that we can assign numbers, text and a few other things for later use.

There's three parts we need to know to make a variable, this is what the whole thing looks like.

int counter = 0;

First we write the type of variable we want, an "int" is a simple number with no decimal places. It can be negative as well.

int

Then we give our variable a name, it can be any name we like with just a few rules that we won't worry about now.

counter

Finally we assign our variable a value. Now writing "counter" in our code is the same as writing 0.

= 0;

Note the ';' at the end of the line, every line ending has to have one of those in Arduino.


Now let's add this to the very top of our code, assigning a variable can be done any time, so we're going to do it right at the top.

We're also going to replace "HELLO!" with counter. Make sure not to include the "" quotation marks any more.


    int counter = 0;                    // Define and assign our variable

    void setup() {    
        Serial.begin(115200);  
    }
    
    void loop() {
        Serial.println(counter);        // Say the variable through serial                                 
        delay(1000);            
    }
                

Now press the UPLOAD button and check the serial monitor to see what it says.

So here we can see where we wrote counter in our Serial.println() it sent the 0 value to us.

Let's add a new line, just under the Serial.println(counter); line.

counter = counter + 1;

This is similar to when we assigned the value of 0 to our variable. However in this case we are assign it's own value + 1.

This means every time our code loops we'll be adding one to our variable.

UPLOAD and check!

We can write all kinds of math in here. Lets try something else.

counter = 75 * 2;

UPLOAD and check!

You can see it did the math for us, but it doesn't keep getting higher because we are multiplying two numbers, rather than using our previous number again like we did before.

Let's see what happens if you multiply counter by 2 each time.

counter = counter * 2;

UPLOAD and check!

Of course it's always 0 because we started at 0! 0 x 2 = 0.

Let's go all the way back to the start and change our original counter to = 1 instead of 0.


    int counter = 1;                    // Change this to 1

    void setup() {    
        Serial.begin(115200);  
    }
    
    void loop() {
        Serial.println(counter);        
        counter = counter * 2;                          
        delay(1000);            
    }
                

Now it should double every time.

UPLOAD and check!

Oh No! So it worked, but eventually became a negative number, then got stuck at zero.

That's because we hit the maximum our int can be so the value wrapped around to negative.

If we need to count higher than that we can use a long instead, but I think that's high enough for us now.

You may find the motors don't work or only barely work, that's because the battery isn't in yet and the motors are only getting a tiny 3.3v.

If nothing at all is happening check to make sure you wires are in the right spot.

If you don't have the battery yet, ask Mr Jake. We haven't put them in yet because we are being careful of short circuits.


CHALLENGE

Try creating the counter as something other than an int and see what happens.

uint8_t counter = 0; // creates counter as a small (8-bit) int that only goes up to 255

long counter = 0; // creates counter as a huge (32-bit) int that goes up to 2,147,483,647

float counter = 0.1; // creates a counter that can do decimals. eg 0.1, 0.2 etc.

string counter = "hello"; // a string stores characters, like letters and numbers. You can't add or subtract it!

Conclusion

We're getting better now, we can control what's going on inside our microcontroller and have it do our math homework.

Next we're going to get it sending messages to the outside world in ways other than Serial.

PREV NEXT