Computer Club Meeting Notes: 11-03-2015

Today we will be working on the Button Project.  This Project led us to the “Guess What Number I’m Thinking” project.  Keep in mind that in the Guess What Number I’m Thinking project the Arduino will select a number from 1 to 4 and using the 4 buttons you will attempt to guess what number the Arduino has thought of.

The following is a diagram for how to wire the Arduino and components for this project:

Project 5 - Push buttons Diagram

The code for the project is as follows:

/*
SparkFun Inventor's Kit
Example sketch 05
http://www.sparkfun.com/
This sketch was written by SparkFun Electronics,
with lots of help from the Arduino community.
This code is completely free for any use.
Visit http://learn.sparkfun.com/products/2 for SIK information.
Visit http://www.arduino.cc to learn about the Arduino.
Version 2.0 6/2012 MDG
*/

// First we'll set up constants for the pin numbers.
// This will make it easier to follow the code below.

const int button1Pin = 2; // pushbutton 1 pin
const int button2Pin = 3; // pushbutton 2 pin
const int ledPin = 13; // LED pin

void setup(){
      // Set up the pushbutton pins to be an input:
     pinMode(button1Pin, INPUT);
     pinMode(button2Pin, INPUT);
     // Set up the LED pin to be an output:
     pinMode(ledPin, OUTPUT); 
}


void loop(){
 int button1State, button2State; 

 // Since a pushbutton has only two states (pushed or not pushed),
 // we've run them into digital inputs. To read an input, we'll
 // use the digitalRead() function. This function takes one
 // parameter, the pin number, and returns either HIGH (5V)
 // or LOW (GND).

 // Here we'll read the current pushbutton states into
 // two variables:

 button1State = digitalRead(button1Pin);
 button2State = digitalRead(button2Pin);

 // Remember that if the button is being pressed, it will be
 // connected to GND. If the button is not being pressed,
 // the pullup resistor will connect it to 5 Volts.

 // So the state will be LOW when it is being pressed,
 // and HIGH when it is not being pressed.
 
 // Now we'll use those states to control the LED.
 // Here's what we want to do:
 
 // "If either button is being pressed, light up the LED"
 // "But, if BOTH buttons are being pressed, DON'T light up the LED"

 if (((button1State == LOW) || (button2State == LOW))&&
 !((button1State == LOW) && (button2State == LOW))){

       digitalWrite(ledPin, HIGH); // turn the LED on
 }
 else
 {
       digitalWrite(ledPin, LOW); // turn the LED off
 }
 
}

mrcampbell / November 3, 2015 / Computer Club