Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //DIGITAL IO LAB CODE
- //goal is to use the red button to turn on red lights, and green button to turn on green lights
- //include neopixel library
- #include <Adafruit_NeoPixel.h>
- #ifdef __AVR__
- #include <avr/power.h>
- #endif
- //define neopixel strip pin
- #define PIN 6
- //define strip for number of neopixels
- Adafruit_NeoPixel strip = Adafruit_NeoPixel(5, PIN, NEO_GRB + NEO_KHZ800);
- //initialize which pins the buttons are located in
- //initialize the state to 0 so the LEDs are off
- int redbutton = 2;
- int greenbutton = 4;
- int redbuttonState = 0;
- int greenbuttonState = 0;
- void setup() {
- strip.begin();
- strip.show(); //initialize pixels off
- pinMode(redbutton, INPUT); //initialize the red button as an input
- pinMode(greenbutton, INPUT); //initialize the green button as an input
- }
- void loop() {
- //read which state the buttons are in (pressed or not) to determine output
- redbuttonState = digitalRead(redbutton);
- greenbuttonState = digitalRead(greenbutton);
- //turn on LEDs one at a time according to color
- //if red button is pushed and green button is not, turn on red lights
- if ((redbuttonState == HIGH) && (greenbuttonState == LOW)){
- for(uint16_t i=0; i<strip.numPixels(); i++) {
- strip.setPixelColor(i, 255, 0, 0); //set LED strip to red
- strip.show();
- delay(200);
- }
- //turn off the red lights
- for(uint16_t i=0; i<strip.numPixels(); i++) {
- strip.setPixelColor(i, 0, 0, 0); //turn all the LEDs off
- strip.show();
- delay(200);
- }
- }
- //if the green button is pushed and the red button is not, turn on green lights
- else if((greenbuttonState == HIGH) && (redbuttonState == LOW)){
- for(uint16_t i=0; i<strip.numPixels(); i++) {
- strip.setPixelColor(i, 0, 255, 0); //set LED strip to green
- strip.show();
- delay(200);
- }
- //turn off green lights
- for(uint16_t i=0; i<strip.numPixels(); i++) {
- strip.setPixelColor(i, 0, 0, 0); //turn all the LEDs off
- strip.show();
- delay(200);
- }
- }
- else {
- for(uint16_t i = 0; i<strip.numPixels(); i++){
- strip.setPixelColor(i, 0, 0, 0); // sets LEDs to off
- strip.show();
- delay(50);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement