Digital Electronics Lab #7

Push-On Push-Off Button Explanation:

The push-on push-off button concept has three major parts: checkButton(); flipButtonState();, and updateLed();.

The checkButton(); function is very similar to the code we’ve used in the past for MIDI notes. First, we set lastButton1State equal to button1State, and button1State equal to the status of the buttonPin (pushed or un-pushed). From there, we establish that if button1State is HIGH, meaning that the button is currently pressed (button1State=HIGH), but wasn’t pressed a moment ago, so lastButton1State is LOW, we flipButtonState();. A delay of 5 milliseconds is included as a slight buffer to avoid any electronics noise. Using the second half of the conditional “else if,” we establish that the code will just continue if the button was previously on a moment ago, but is not being pushed at the moment (lastButtonState=HIGH, buttonState=LOW);. So, in this case it will start over, and if the button isn’t pressed again, it will set button1State to LOW, and therefore not flipButtonState according to the conditional. It will only flipButtonState if we push the button.

To better understand the purpose of the checkButton function though, it is important to understand what flipButtonState is doing and what updateLed is doing. flipButtonState perpetually flips switchedOn between true and false. switchedOn is set equal to false from our variables, so according to the conditional, it will be set to true. However, there is another part of the conditional “if(switchedOn == true) { switchedOn = false;,” which will set switchedOn to false again, and the cycle would theoretically continue forever if placed alone in the loop. Here, when we push the button, triggering the flipButtonState function, we make switchedOn true according to the conditionals. When we push the button again, however, we set switchedOn to false again. This influences what happens in our updateLed(); function, allowing the LED to be turned on and off, respectively.

When switchedOn is true, the function updateLed(); will turn the LED on, whereas if it is false, it will turn the LED off. When the button is pressed, because of the nature of the checkButton(); and flipButtonState(); function, switchedOn will become true and we will turn the LED on. On the contrary, when we hit the button again, switchedOn is changed back to false, and subsequently shuts off the LED.

Video:

Code:

#include <Adafruit_NeoPixel.h>

Adafruit_NeoPixel neopixel = Adafruit_NeoPixel(2, 32, NEO_RGB);

int redValue = 0;
int redPot = 0;
int greenValue = 0;
int greenPot = 0;
int blueValue = 0;
int bluePot = 0;
int buttonPin = 31;

bool lastButton1State = LOW;
bool button1State = LOW;

bool switchedOn = false;


void setup() {
  Serial.begin(9600);

  neopixel.begin();
  neopixel.clear();
  neopixel.show();
  pinMode(buttonPin, INPUT);
}

void loop() {
  checkButton();
  updateLed();

}

void colorChange() {
  
  redPot = analogRead(A14);
  redValue = map(redPot,0,1023,0,225);

  greenPot = analogRead(A15);
  greenValue = map(greenPot,0,1023,0,225);

  bluePot = analogRead(A16);
  blueValue = map(bluePot,0,1023,0,225);

  neopixel.setPixelColor(0, redValue, greenValue, blueValue);
  neopixel.setPixelColor(1, redValue, greenValue, blueValue);
  
  
  neopixel.show();
}

void colorOff() {

  neopixel.setPixelColor(0, 0, 0, 0);
  neopixel.setPixelColor(1, 0, 0, 0);
  
  neopixel.show();
}

void checkButton() {
  lastButton1State = button1State;
  button1State = digitalRead(buttonPin);
  if (lastButton1State == LOW && button1State == HIGH) {
    flipButtonState();
    delay(5);
  } else if (lastButton1State == HIGH && button1State == LOW) {
    delay(5);
  }
}

void flipButtonState() {
  if (switchedOn == true) {
    switchedOn = false;
  } else if (switchedOn == false) {
    switchedOn = true;
  }
}

void updateLed() {
  if (switchedOn == true) {
    colorChange();
  } else {
    colorOff();
  }
}


Leave a comment

Design a site like this with WordPress.com
Get started