Home TI-Nspire Scripting HQ TI-Nspire STEM HQ Getting Started with TI LaunchPads TI LaunchPad Lesson 2

     
     

Lesson 2: Push Some Buttons

Energia Reference page

     
           
     

Lights AND Buttons!

Most LaunchPads come equipped with at least three buttons - these include a RESET button, and two programmable buttons at the bottom end of the board (the end opposite the USB port). These buttons tend to have two "states" - HIGH (unpressed) and LOW (pushed), and these can be readily linked to all sorts of interesting things.

Once again, we will begin with one of the Energia Example sketches - File > Examples > Digital > Button. As before, there are lots of green comment lines, which may or may not prove useful to you. Read through but don't worry if there are parts that you do not (yet) follow.

Once again, I will remove the comments and leave just the raw sketch for you to check, line by line.

int buttonPin = PUSH2; // the number of the pushbutton pin
int ledPin = GREEN_LED; // the number of the LED pin

int buttonState = 0;

void setup()
{

pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}

void loop()
{
buttonState = digitalRead(buttonPin);

if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}
}

Once again, feel free to copy the text from here and paste it into your 'new' sketch, or just use the example sketch provided by the software.

Look closely at each line of the sketch above. Two different types of variables are defined - constants and integer. Each element of the LaunchPad (LEDs, buttons, etc) live on a numbered pin and can be called from that number (if you know it) or by using the defined name (eg PUSH1 and PUSH2 name the two buttons).

In fact, for simple sketches like these, there is really no need to introduce too many variable names at all. It is much simpler to simply refer to each button and LED by its reserved name, as shown below.

In the setup, initialise the LED as OUTPUT and the button as INPUT.

The loop is important because it introduces a conditional statement - "if... else". Take note of the syntax: if (condition) { responses} else { alternatives }.

  
  

Challenges

Once again, go back and change some of the values to see what happens. Try adding the other LEDs that we know exist for our board, and link these two the other button (PUSH1).

Why not press PUSH1 to show red, then green, then blue, and PUSH2 to go the other way?

  
  

  
  
  
  

Now go back and try a few more examples from the examples menu! Much fun awaits!

  
  

Back to Top

  

Home TI-Nspire Scripting HQ TI-Nspire STEM HQ Getting Started with TI LaunchPads TI LaunchPad Lesson 2