Button Activity Number 4

This activity will guide you through the process of creating a new program from what you have done in the other activities you have done. From the traffic light you learned how to control several LEDs. In the button program you learned to read a button state, and you used an IF statement. In the edge detection activity you learned how to count those button pushes. For this activity you will make the button count through a sequence of LEDs.

THE CIRCUIT

STEP 1

Start a new Arduino program:

void setup() {

// put your setup code here, to run once:

}

void loop() {

// put your main code here, to run repeatedly:

}

STEP 2

Remember how you set up for three LEDs in the traffic light activity, for this program for this program we need to set-up for four LEDs on pin 2 through pin 5. If you can't remember, open your traffic light program, you can even copy and past the code you need into your new program and then make what ever changes there.

void setup() {

pinMode(2, OUTPUT); // light pin

pinMode(3, OUTPUT); // light pin

pinMode(4, OUTPUT); // light pin

pinMode(5, OUTPUT); // light pin

}

void loop() {

// put your main code here, to run repeatedly:

}

Look in the button program and see if there is any set-up required for the input?

The input button also needs to be setup, add this to your codee

void setup() {

pinMode(2, OUTPUT); // light pin

pinMode(3, OUTPUT); // light pin

pinMode(4, OUTPUT); // light pin

pinMode(5, OUTPUT); // light pin

pinMode(13, INPUT);

}

void loop() {

// put your main code here, to run repeatedly:

}

STEP 3

Variables are words or names used to represent a value in a calculation, the same way in math you would use a let statement at the beginning of a problem in a computer program you must declare your variables at the start of the program. The state change detection program uses several variables to keep track of the button state and count of button pushes. Look at the state change detection program and see what variable you need and add the appropriate lines to the start of your program.

// this constant won't change:

const int buttonPin = 13; // the pin that the pushbutton is attached to

const int ledPin1 = 2; // the pin that the LED is attached to

const int ledPin2 = 3; // the pin that the LED is attached to

const int ledPin3 = 4; // the pin that the LED is attached to

const int ledPin4 = 5; // the pin that the LED is attached to

// Variables will change:

int buttonPushCounter = 0; // counter for the number of button presses

int buttonState = 0; // current state of the button

int lastButtonState = 0; // previous state of the button

void setup() {

pinMode( ledPin1, OUTPUT); // light pin 1

pinMode(ledPin2, OUTPUT); // light pin 2

pinMode(ledPin3, OUTPUT); // light pin 3

pinMode( ledPin4, OUTPUT); // light pin 4

pinMode( buttonPin, INPUT);

}

void loop() {

// put your main code here, to run repeatedly:

}

STEP 4

// this constant won't change:

const int buttonPin = 13; // the pin that the pushbutton is attached to

const int ledPin1 = 2; // the pin that the LED is attached to

const int ledPin2 = 3; // the pin that the LED is attached to

const int ledPin3 = 4; // the pin that the LED is attached to

const int ledPin4 = 5; // the pin that the LED is attached to

// Variables will change:

int buttonPushCounter = 0; // counter for the number of button presses

int buttonState = 0; // current state of the button

int lastButtonState = 0; // previous state of the button

void setup() {

pinMode( ledPin1, OUTPUT); // light pin 1

pinMode(ledPin2, OUTPUT); // light pin 2

pinMode(ledPin3, OUTPUT); // light pin 3

pinMode( ledPin4, OUTPUT); // light pin 4

pinMode( buttonPin, INPUT);

}

void loop() {

// put your main code here, to run repeatedly:

}