Digital Electrics Lab 7

Push On and Off Button:
There are two variables, lastButton1State and button1State. In the checkButton mode, it first sets the two variables equal to each other. Then it reads what state the buttonpin is, whether it is high or low and sets that equals to button1State. Then in the if statement, it tells that if the lastButton1State is low and button1State is high, thus if the two are not equal together, run flipButtonState. In flipButtonState mode, it checks if the initially stated false switchedOn variable is true. If it changed to true, it changes it back to false, and if its false, it changes it to true. If the switchedOn variable is true it will turn the led on, if not it will turn it off.

Below is my code:

#include <Adafruit_NeoPixel.h>

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

int tempo = 1000;
int potRed = 0;
int potGreen = 0;
int potBlue = 0;
int mappedPotRed = 0;
int mappedPotGreen = 0;
int mappedPotBlue = 0;

int buttonPin = 31;
bool lastButton1State = LOW;
bool button1State = LOW;

bool switchedOn = false;


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

void loop() {
  potRed = analogRead(A14);
  potGreen = analogRead(A15);
  potBlue = analogRead(A16);
  mappedPotRed = map(potRed, 0, 1023, 0, 255);
  mappedPotGreen = map(potGreen, 0, 1023, 0, 255);
  mappedPotBlue = map(potBlue, 0, 1023, 0, 255);

  //  neopixel.setPixelColor(0, mappedPotRed, mappedPotGreen, mappedPotBlue);
  //  neopixel.show();
  //  delay(tempo);

  checkButton();
  updateNeo();
}

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 updateNeo() {
  if (switchedOn == true) {
    neopixel.setPixelColor(0, mappedPotRed, mappedPotGreen, mappedPotBlue);
    neopixel.setPixelColor(1, mappedPotRed, mappedPotGreen, mappedPotBlue);
    neopixel.show();
  } else {
    neopixel.setPixelColor(0, 0, 0, 0);
    neopixel.setPixelColor(1, 0, 0, 0);
    neopixel.show();
  }
}

Leave a comment

Design a site like this with WordPress.com
Get started