Home ← TI-Nspire Scripting HQ ←STEM HQ ← Getting Started with TI LaunchPads ← TI LaunchPad Lesson 2
Lesson 2: Push Some Buttons
Lights AND Buttons!
Lesson 2: Push Some Buttons
Lesson 7 - Real world data at your fingertips: Light, Ultrasonic Motion and more...
Lesson 8 - Build your own BLE ultrasonic motion detector for under $USD30
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 }.
Hacking the Hub™
The only change required for this activity to work with the Hub is to remove the clear plastic cover by removing the four screws, in order to access the three buttons!
Remember: You can always reflash your Innovator Hub to return it to its original state, using the tools that TI has made available (just follow the Resources tab, and then "Keep your Innovator up to date"). So it is possible to explore coding the Hub in new ways!
TI Innovator™ Hub (with MSP432 LaunchPad)
Challengesint buttonState1 = 0; int buttonState2 = 0;
void setup() {
pinMode(RED_LED, OUTPUT); pinMode(GREEN_LED, OUTPUT);
pinMode(PUSH1, INPUT_PULLUP); pinMode(PUSH2, INPUT_PULLUP); }
void loop() { buttonState1 = digitalRead(PUSH1); buttonState2 = digitalRead(PUSH2); if (buttonState1 == LOW) {
digitalWrite(RED_LED, HIGH); delay(1000); digitalWrite(RED_LED, LOW); digitalWrite(GREEN_LED, HIGH); delay(1000); digitalWrite(GREEN_LED, LOW); delay(1000);} if (buttonState2 == LOW) {digitalWrite(GREEN_LED, HIGH); delay(1000); digitalWrite(GREEN_LED, LOW); digitalWrite(RED_LED, HIGH); delay(1000); digitalWrite(RED_LED, LOW); delay(1000);} }int buttonState1 = 0; int buttonState2 = 0;
void setup() {
pinMode(RED_LED, OUTPUT); pinMode(BLUE_LED, OUTPUT); pinMode(GREEN_LED, OUTPUT);
pinMode(PUSH1, INPUT_PULLUP); pinMode(PUSH2, INPUT_PULLUP); }
void loop() { buttonState1 = digitalRead(PUSH1); buttonState2 = digitalRead(PUSH2); if (buttonState1 == LOW) {
digitalWrite(RED_LED, HIGH); delay(1000); digitalWrite(RED_LED, LOW); digitalWrite(GREEN_LED, HIGH); delay(1000); digitalWrite(GREEN_LED, LOW); digitalWrite(BLUE_LED, HIGH); delay(1000); digitalWrite(BLUE_LED, LOW); delay(1000);} if (buttonState2 == LOW) {digitalWrite(BLUE_LED, HIGH); delay(1000); digitalWrite(BLUE_LED, LOW); digitalWrite(GREEN_LED, HIGH); delay(1000); digitalWrite(GREEN_LED, LOW); digitalWrite(RED_LED, HIGH); delay(1000); digitalWrite(RED_LED, LOW); delay(1000);} }
Now go back and try a few more examples from the examples menu! Much fun awaits!
Home ← TI-Nspire Scripting HQ ←STEM HQ ← Getting Started with TI LaunchPads ← TI LaunchPad Lesson 2