Real Robots
Make some cool stuff

Robby the Robot v2

Coding 5: The Remote


Now that we know how to get inputs, we just need to get all the inputs at once.

Our controller has 7 inputs, including 2 potentiometers and buttons.

The pin connections are as follows.

pinX 14
pinY 26
pinButton1 27
pinButton2 32
pinButton3 33
pinButton4 25
pinButton5 23

So for each pin we need add it as a variable int pinX = 14; first.

Then for each pin we need to set it's pinMode(pin, mode);.

Then we'll print the result with a Serial.print(analogRead(pinX));.

We're going to put a tab between each printed result with Serial.print("\t");.

When we've printed each result, THEN we go to the next line with an empty Serial.println();.

    
        int pinX = 14;                                  // X (left/right) thumbstick on pin 14
        // repeat for each pin

        void setup() {    
            Serial.begin(115200);
            pinMode(pinX, INPUT_PULLUP);
            // repeat for each pin
        }
        
        void loop() {
            Serial.println(analogRead(pinX)) ;          // check pin 14/send to Serial
            Serial.print("\t");                         // print a gap between this and the next

            // Repeat for each pin (digitalRead for buttons, analog for thumbstick)

            Serial.println();
            delay(10);                                  // wait 0.01 sec
        }
                                

Now press the UPLOAD button, connect the Serial Monitor and see what happens when you push the button.

Try out each input to make sure it's working properly.

If you've finished ahead of everyone else, close the Serial Monitor and try Tools->Serial Plotter.

Sending the Signal

We won't go into actually connecting and sending data over wifi, we'll us a library for that (code Mr Jake already did separately).

But we will write some code to detect when a button is pushed and do something only then.

First, we don't want to delete our current code, but we also don't want it right now.

We're going to add it to a function called PrintInputs() so we can use it for testing in future.

  
            
        void loop() {
            PrintInputs();
            delay(10);                                  // wait 0.01 sec
        }

        void PrintInputs(){
            Serial.println(analogRead(pinX)) ;          // check pin 14/send to Serial
            Serial.print("\t");                         // print a gap between this and the next

            // Repeat for each pin (digitalRead for buttons, analog for thumbstick)

            Serial.println();
        }
        

Now press the UPLOAD button, nothing should be different, whenever we write PrintInputs(); it will run all the code in our new PrintInputs() function.

For now, comment out the call to PrintInputs() with a "//" before it, and we'll make a new function called DetectButton().

  

        void loop() {
            DetectButton()
            //PrintInputs();
            delay(10);                                  // wait 0.01 sec
        }

        void DetectButton(){
            
        }
        

We're going to learn a new kind of code called a "conditional", that is code that only runs under certain conditions.

They're laid out similar to a function, with a block of code following, if the condition is true, the the code in the block is run.

  
        void DetectButton(){
            if (digitalRead(pinButton1) == 0){
                Serial.println("Button 1 Pushed!");
            }
        }
        

We used a new symbol, "==" this checks if the things on either side are the same. Usually the digitalRead() returns "1", if the buttons is pushed it returns "0", so if digitalRead is the same (==) as "0", then we print the line of code.

CHALLENGE

Add conditional IF statements for the rest of the buttons, feel free to get a bit silly.

HINT:

  
        void DetectButton(){
            if (digitalRead(pinButton1) == 0){
                Serial.println("Button 1 Pushed!");
            }
            if (digitalRead(pinButton2) == 0){
                Serial.println("Hey, don't push button 2 again!");
            }
        }
                

PREV NEXT