Button

Overview

This tutorial will teach you how to implement a push button that turns on an LED with two smorgasboards on different ports.

Components

Arduino + power and usb connectors
Museduino Shield
2 Museduino Smorgasboards
CAT5/RJ45 cables
LED
Button
Female/Female Jumpers

20150604_120952

20150608_145048

Setup

First, follow the setup for the LED Tutorial.

After you’ve completed the setup, attach your second Smorgasboard to the Museduino Shield on Port C.

20150608_142729

Next, add a button on Digital Pin 2. Our chart shows that Digital Pin 4 can be found on Signal Pin 4 via Port C.

2.0_pinchart

Below is the chart Satellite board pin configuration:

pinconfig

Our button has two wires. Connect one wire to the signal pin with resistance (pin 2) and ground (pin 4). The Museduino kit come with your choice of header pins or screw terminals.

20150608_162858

If using a satellite with screw terminals, place the ends of the wires accordingly and use a small screwdriver to shut the terminals. Otherwise, use jumper wires to connect to the header pins.

20150608_162924

Code

Please note that our button uses the INPUT_PULLUP pinmode. When using this method, inputs will report as LOW.

See here for details: http://www.arduino.cc/en/Reference/Constants


/*
  Museduino | Button Tutorial
  Button turns on LED when pushed.
*/
 
// Digital Pin 6 on Satellite Pin 5 via Port A
int s5A = 6;
// Digital Pin 4 on Satellite Pin 4 via Port C
int s4C = 4;

// variables
int buttonState = 0; // variable for reading the button status

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the led pin as an output.
  pinMode(s5A, OUTPUT); 
  // initialize the button pin as an output.
  pinMode(s4C, INPUT_PULLUP);  
    
}

// the loop routine runs over and over again forever:
void loop() {
  
  // read the state of the button value:
  buttonState = digitalRead(s4C);
  
  // check if the button is pushed
  // if it is, the buttonState is LOW:
  if (buttonState == LOW) {
    // turn LED on:
    digitalWrite(s5A, HIGH);
  }
  else {
      // turn LED off:
      digitalWrite(s5A, LOW);
  }
  
}

Github

Once the code has been successfully uploaded, test the button.

20150608_164821

Advertisements