This is a Multi-switch using 3 push buttons and 3 LEDs. The Pins 7-12 are used. Pin 7 controls the yellow LED 8, Pin 9 controls the green LED 10, and Pin 11 controls all LEDs( 8,10,12). The wiring uses yellow, green, and blue to associate with the specific LEDs that get turned on while avoiding colors for power and ground (black and red). The yellow LED code uses a pushbutton that turns on only when the user holds it down. The Green LED code toggles the green LED based on the previous and current button state. This turns the LED off and on when the button goes down ( not up). Finally, the blue LED code uses the same toggle code as the green one but adds a delay animation to turn the yellow, green, and blue ones on and off.
//button stuff 1int ledPin = 8;
int buttonPin = 7;
//button stuff 2
int ledTwoPin = 10;
int buttonTwoPin = 9;
//button stuff 3
int ledThreePin = 12;
int buttonThreePin = 11;
//status being off and on bool for 2
//a lot comes from the official documentation https://docs.arduino.cc/built-in-examples/digital/StateChangeDetection/
bool status;
bool statusTwo;
// state later and new state
int previousState;
int currentState;
int previousStateTwo;
int currentStateTwo;
void setup() {
// put your setup code here, to run once:
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledTwoPin, OUTPUT);
pinMode(buttonTwoPin, INPUT_PULLUP);
pinMode(ledThreePin, OUTPUT);
pinMode(buttonThreePin, INPUT_PULLUP);
previousState = 1;
previousStateTwo = 1;
Serial.begin(9600);
}
void loop() {
YellowLED();
GreenLED();
//when the blue button is pressed do an animation of yellow green blue with delays through it
//digitalWrite(ledThreePin, !digitalRead(buttonThreePin));
BlueLED();
}
void BlueLED(){
currentStateTwo = digitalRead(buttonThreePin);
if(currentStateTwo != previousStateTwo){
if(currentStateTwo == 1)
{
statusTwo = !statusTwo;
}
Serial.println(statusTwo);
if(digitalRead(statusTwo)){
//turn on yellow
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
digitalWrite(ledTwoPin, HIGH);
delay(500);
digitalWrite(ledTwoPin, LOW);
digitalWrite(ledThreePin, HIGH);
delay(500);
digitalWrite(ledThreePin, LOW);
}
delay(50);
}
previousStateTwo = currentStateTwo;
}
void YellowLED(){
digitalWrite(ledPin, !digitalRead(buttonPin));
}
void GreenLED(){
currentState = digitalRead(buttonTwoPin);
if(currentState != previousState){
//button down = 0 1
//button up = 1 0
//if button down current state is 1 every button down invert the status
if(currentState == 1)
{
status = !status;
}
//else its button up so ignore
digitalWrite(ledTwoPin, status);
//read on doc site that there needs to be a delay to avoid "bouncing"
delay(50);
}
previousState = currentState;
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.