CxD Archive
Code: Robotics
Code: Robotics
  • Code Introduction
  • TUTORIALS
    • A. Meet Your Robot
      • A-1 Circuit Board
      • A-2 Motors and Wheels
      • A-3 Other Components
    • B. Hello World Test
      • B-1 Arduino Code Editor
      • B-2 New App Template
      • B-3 Global Variable
      • B-4 Setup Function
      • B-5 Loop Function
      • B-6 Upload App to Robot
      • B-7 Add Sound
      • B-8 Use Button
    • C. Driving and Turning
      • C-1 Driving
      • C-2 Turns (Pivoting)
      • C-3 Test Wheel Encoders
      • C-4 Drive for Specific Distance
      • C-5 Pivot By Specific Angle
      • C-6 Drive Straight Continuously
    • D. Detect Objects in Path
      • D-1 Test Mechanical Bumpers
      • D-2 Detect Collisions
      • D-3 Test Ultrasonic Sensor
      • D-4 Avoid Collisions
    • E. Detect Lines on Surface
      • E-1 Test IR Line Sensors
      • E-2 Follow Line
      • E-3 Avoid Line
      • E-4 Count Lines Crossed
      • E-5 Follow and Count Lines
    • F. Detect Other Conditions
      • F-1 Detect Surface Drop-Off
      • F-2 Test Accelerometer
      • F-3 Detect If Upside-Down
      • F-4 Detect If Bumped
  • REFERENCES
    • Arduino Language
    • Arduino Code Editor
      • Create New App
      • Save and Rename App
      • Include RedBot Library
      • Verify App Code
      • Upload App to Robot
      • Download Copy of App
      • Use Serial Monitor to View Data
      • Robot Demo App Template
    • Physical Inputs
      • Push Button (D12)
      • Mechanical Bumpers
      • IR Line Sensors
      • Wheel Encoders
      • Accelerometer
      • Ultrasonic Sensor *
    • Physical Outputs
      • LED Light (D13)
      • Speaker (Buzzer)
      • Motors
    • Robot Behaviors
      • Producing Alerts
      • Driving
      • Turning
      • Detecting Objects
      • Detecting Lines
      • Detecting Other Conditions
    • Navigation Modes
      • Distance Navigation
      • Line Counting Navigation
      • Line Following + Counting Navigation
      • Autonomous Navigation
  • LINKS
    • Robotics Project Guidebook
    • Arduino Create Web Editor
    • Arduino Language Reference
    • RedBot Experiment Guide
  • ELEGOO Experiments
    • Getting the Arduino IDE
    • Starting with the `techCar` Template
    • Template Code for techCar.ino
    • Template Experiments
    • Reinstall the Original Remote App Functionality, If Needed
Powered by GitBook
On this page
  • Add Global Variable for Button
  • Set Pin Mode for Button
  • Check If Button Pressed
  • Upload Modified App to Robot
Export as PDF
  1. TUTORIALS
  2. B. Hello World Test

B-8 Use Button

As your last step of this tutorial, you'll modify the "Hello World" app so your robot's LED and speaker will "blink" and "beep" whenever the D12 button on the robot's circuit board is pressed.

Add Global Variable for Button

You'll need to create a global variable to store the pin number of the RedBot's built-in button, which is connected to pin D12 (via internal circuitry)

Add this code statement before the setup() function:

int button = 12;

Your code should now have three separate code statements before the setup() function to declare a global variable for the LED, a global variable for the speaker, and a global variable for the button.

Set Pin Mode for Button

The button is an example of an input because your app will use it to receive data (i.e., to detect when someone presses the button). So you'll need to set the pin mode for the button to be an input.

Add this code statement within the setup() function:

pinMode(button, INPUT_PULLUP);

INPUT_PULLUP indicates the button pin will be used for input and will use a pull-up resistor (which is something that buttons and switches typically use, but other inputs do not).

Your code should now have three separate code statements within the setup() function to set the pin mode for the LED, set the pin mode for the speaker, and set the pin mode for the button.

Check If Button Pressed

Your app can receive signals from inputs using the digitalRead() or analogRead() methods, depending on whether the values being received will be digital or analog.

DIGITAL VS. ANALOG: Digital inputs and outputs use binary values (such as: HIGH vs. LOW, etc.). Analog inputs and outputs use a range of values (such as: 0-255, etc.)

The button can be read as a digital input that is either "pressed" or "not pressed."

  if (digitalRead(button) == LOW) {
    // add code to perform when button is pressed
    
  }

The digitalRead() method requires one parameter insides its parentheses:

  1. The I/O pin number, which can be the actual pin number (such as: 12, etc.) or a variable that stores a pin number. In this case, the variable named button is listed (which has a value equal to 12).

The digitalRead() method will check the button and return a value of either HIGH or LOW:

  • HIGH indicates the button is NOT currently pressed.

  • LOW indicates the button is currently pressed.

An if statement checks whether a specific condition (listed insides its parentheses) is true or false.

In this case, the condition being checked is whether the value returned from digitalRead(button) is equal to LOW:

  • If this condition is true, the app will perform whatever code statements are listed within the curly braces of the if statement.

  • If this condition is false, the app will not perform the code statements within the curly braces of the if statement.

Here's what we want to happen: if the button is pressed, the LED will blink and the speaker will beep at the same time.

To do that, you're going to move the existing code statements in the loop() that make the LED blink and speaker beep, so those code statements are now listed within the curly braces of the if statement.

  1. Select the code statements to be moved, cut them (choose "Cut" from Edit menu – or press Control-X on keyboard), and then paste them in their new location (choose "Paste" from Edit menu – or press Control-V on keyboard).

  2. You may want to use the tab key to indent the code statements that you moved (so it is more visually clear that they are contained within the curly braces of the ifstatement).

  3. Modify the second delay() method, so the delay is 200 milliseconds. This will act as a 0.2 second pause between each check of the button (to allow sufficient time for a person to release the button, so the code doesn't read a single button press as if it were multiple presses).

Just for reference, here's what your loop() function should now look like:

void loop() {
  // put your main code here, to run repeatedly:
  if (digitalRead(button) == LOW) {
    // add code to perform when button is pressed
    digitalWrite(LED, HIGH);
    tone(speaker, 2000);
    delay(200);
    digitalWrite(LED, LOW);
    noTone(speaker);
    delay(200);
  }
}

Upload Modified App to Robot

Upload the modified app to your robot, confirm that it works correctly.

The robot's LED and speaker should briefly blink and beep (in sync) only when you press the D12 button on the RedBot circuit board.

In the next tutorial, you'll program apps to make your robot drive around and make turns.

PreviousB-7 Add SoundNextC. Driving and Turning

Last updated 6 years ago

Your app will need to use an statement and the digitalRead() method to check if the button is being pressed. Add this code within the loop() function (before the first digitalWrite() statement that turns on the LED):

If your teacher requires you to submit a file containing your completed "Hello World" app code, .

if
follow these instruction to download a copy of your app