Push Button Experiments

After completing the Push Button project, test yourself by trying these experiments.

1 The push button connections that you just made with the pull-up resistor is used very often and is used not only for push buttons. To simplify the connections, there is an internal pull-up resistor built-in to each of the digital pins on the Arduino so that the external resistor is not needed. This is depicted by the dash lines used for the resistor symbol shown on the right. Built-in pull-up resistor
1a When using the internal pull-up resistor, you can remove the external resistor and the wire that connects the resistor to 5V as shown on the right. Circuit using the built-in pull-up resistor
1b To tell the Arduino that you want to use the internal pull-up resistor, change the
pinMode command INPUT mode to INPUT_PULLUP.

Run your modified program.
You should get the same result as before.
Code for built-in pull-up resistor
2 Modify the program so that the led turns on when you press the button, and turns off when you release the button.
3 Instead of using the push button to control the internal LED connected to pin 13, make the push button to control an external LED that is connected to pin 3.

Do not change the push button connections. Just add the connections for an external LED like the picture on the right.

Modify the code to control the external LED instead of the internal LED. Hint: change pin 13 to pin 3 in the code.
Circuit with push button, buzzer, and LED
4 Add a buzzer to what you have from experiment 3. Make it so that when you press the push button the external LED will turn on and the buzzer will sound a note, and when you release the button both of them will turn off.











The if command checks the buttonState variable to see if it is HIGH or LOW. The double-equal sign == is used to test for equality. If buttonState is equal to HIGH then the first digitalWrite command is executed to turn on the LED and then the tone command is executed to sound the buzzer,

otherwise the second digitalWrite command in the else part of the if command is executed to turn off the LED followed by the noTone command to turn off the buzzer.

Notice the use of the open and close braces to bracket the body of the true and false parts of the if command.
const int pushButton = 2;
const int buzzer = 8;
const int led = 3;

void setup() {
  pinMode(led, OUTPUT);
  pinMode(pushButton, INPUT_PULLUP);  // use internal pull-up resistor
  pinMode(buzzer, OUTPUT);
}

void loop() {
  int buttonState = digitalRead(pushButton);
  if (buttonState == LOW) {
    digitalWrite(led, HIGH);
    tone(buzzer, 440);
  } else {
    digitalWrite(led, LOW);
    noTone(buzzer);
  }
}
5 Create an electronic dice using the push button and the 7-segment LED display. Roll the die by pressing the button. The 7-segment LED display will show a random number between 1 and 6.

You will need to use the random command to generate a random number between 1 and 6. This random number is then passed to the displayDigit() function that you wrote in the 7-segment LED display project.

A sample code segment is shown on the right. Make sure you add the displayDigit function code.

What happens when you reduce the delay to a much smaller number such as 10?

To solve this problem, you will have to do something similar to experiment 5 above.
const int pushButton = 2;

void setup() {
  pinMode(pushButton, INPUT_PULLUP);  // use internal pull-up resistor
  randomSeed(analogRead(0)); 	// to initialize the random number generator
}

void loop() {
  int buttonState = digitalRead(pushButton);
  if (buttonState == LOW) {
    int number = random(1, 7); // generate a random number between 1 and 6
    displayDigit(number);  // display number
    delay(2000);
  }
}
6 (Difficult) Write a program so that when you press the button once it will turn on a LED. The LED stays on even after you release the button. When you press the button a second time, the LED will turn off. It will remain off until you press the button again. This is sometimes referred to as the push-on-push-off circuit.
const int pushButton = 2;
int led = 13;
int ledOn = false;    // to toggle the LED on/off state
int flipflop = false; // to cycle between two button presses

void setup() {
  pinMode(pushButton, INPUT_PULLUP);  // use internal pull-up resistor
  pinMode(led, OUTPUT);
}

void loop() {
  int buttonState = digitalRead(pushButton);
  delay(10); // need this to debounce switch
  if (buttonState == LOW) {
    if (flipflop == false) {
      flipflop = true;    // first press of button
      ledOn = !ledOn;     // toggle the LED on/off state
      digitalWrite(led, ledOn);
    }
  } else {
    if (flipflop == true) {
      flipflop = false;   // second press of button
    }
  }
}
7 (Difficult) Write a program to count how many times a push button is pressed.


Because the computer executes the commands so fast, even the short time that you press the button down, the computer will have executed the loop many many times.
const int pushButton = 2;
int count = 0;

void setup() {
  Serial.begin(9600);
  pinMode(pushButton, INPUT_PULLUP);  // use internal pull-up resistor
}

void loop() {
  // add your code here

}
Answer
8 (Difficult) Write a program to distinquish between a short button press and a long button press. A long press is one where you press for more than 700 millisecond. Answer
9 (Difficult) Write a program to play the Star Wars theme song (from Project 2) when a button is pressed, and stops playing the song as soon as the button is released.

You might start off with the code on the right, but it is not correct because when you release the button, the song does not stop playing immediately. The reason is while playing the song there are many long blocking delay commands. So that during all this time, the code does not check the status of the button, and not be able to stop the song as soon as the button is released.
const int pushButton = 2;
const int buzzer = 8;

void StarWars() {
  int NOTE_C4 = 262;
  int NOTE_D4 = 294;
  int NOTE_E4 = 330;
  int NOTE_F4 = 349;
  int NOTE_G4 = 392;
  int NOTE_A4 = 440;
  int NOTE_B4 = 494;
  int NOTE_C5 = 523;

  // code to play the first 16 notes of the Star Wars Theme song
  tone(buzzer, NOTE_C4);
  delay(1000);
  tone(buzzer, NOTE_G4);
  delay(1000);
  tone(buzzer, NOTE_F4);
  delay(167);
  tone(buzzer, NOTE_E4);
  delay(167);
  tone(buzzer, NOTE_D4);
  delay(167);
  tone(buzzer, NOTE_C5);
  delay(1000);
  tone(buzzer, NOTE_G4);
  delay(500);

  tone(buzzer, NOTE_F4);
  delay(167);
  tone(buzzer, NOTE_E4);
  delay(167);
  tone(buzzer, NOTE_D4);
  delay(167);
  tone(buzzer, NOTE_C5);
  delay(1000);
  tone(buzzer, NOTE_G4);
  delay(500);
  
  tone(buzzer, NOTE_F4);
  delay(167);
  tone(buzzer, NOTE_E4);
  delay(167);
  tone(buzzer, NOTE_F4);
  delay(167);
  tone(buzzer, NOTE_D4);
  delay(2000);
  noTone(8);
}

void setup() {
  pinMode(pushButton, INPUT_PULLUP);  // use internal pull-up resistor
  pinMode(buzzer, OUTPUT);
}

void loop() {
  if (digitalRead(pushButton) == LOW) {
    StarWars();
  } else {
    noTone(buzzer);
  }
  // the problem here is that when the song is playing and
  // you release the button the song will not stop immediately
  // because of all the long delays in the StarWars function.
}
Answer