Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. //DIGITAL IO LAB CODE
  2. //goal is to use the red button to turn on red lights, and green button to turn on green lights
  3. //include neopixel library
  4. #include <Adafruit_NeoPixel.h>
  5. #ifdef __AVR__
  6. #include <avr/power.h>
  7. #endif
  8.  
  9. //define neopixel strip pin
  10. #define PIN 6
  11.  
  12. //define strip for number of neopixels
  13. Adafruit_NeoPixel strip = Adafruit_NeoPixel(5, PIN, NEO_GRB + NEO_KHZ800);
  14. //initialize which pins the buttons are located in
  15. //initialize the state to 0 so the LEDs are off
  16. int redbutton = 2;
  17. int greenbutton = 4;
  18. int redbuttonState = 0;
  19. int greenbuttonState = 0;
  20.  
  21. void setup() {
  22. strip.begin();
  23. strip.show(); //initialize pixels off
  24. pinMode(redbutton, INPUT); //initialize the red button as an input
  25. pinMode(greenbutton, INPUT); //initialize the green button as an input
  26. }
  27.  
  28. void loop() {
  29. //read which state the buttons are in (pressed or not) to determine output
  30. redbuttonState = digitalRead(redbutton);
  31. greenbuttonState = digitalRead(greenbutton);
  32.  
  33. //turn on LEDs one at a time according to color
  34. //if red button is pushed and green button is not, turn on red lights
  35. if ((redbuttonState == HIGH) && (greenbuttonState == LOW)){
  36. for(uint16_t i=0; i<strip.numPixels(); i++) {
  37. strip.setPixelColor(i, 255, 0, 0); //set LED strip to red
  38. strip.show();
  39. delay(200);
  40. }
  41.  
  42. //turn off the red lights
  43. for(uint16_t i=0; i<strip.numPixels(); i++) {
  44. strip.setPixelColor(i, 0, 0, 0); //turn all the LEDs off
  45. strip.show();
  46. delay(200);
  47. }
  48. }
  49. //if the green button is pushed and the red button is not, turn on green lights
  50. else if((greenbuttonState == HIGH) && (redbuttonState == LOW)){
  51. for(uint16_t i=0; i<strip.numPixels(); i++) {
  52. strip.setPixelColor(i, 0, 255, 0); //set LED strip to green
  53. strip.show();
  54. delay(200);
  55.  
  56. }
  57. //turn off green lights
  58. for(uint16_t i=0; i<strip.numPixels(); i++) {
  59. strip.setPixelColor(i, 0, 0, 0); //turn all the LEDs off
  60. strip.show();
  61. delay(200);
  62. }
  63. }
  64. else {
  65. for(uint16_t i = 0; i<strip.numPixels(); i++){
  66. strip.setPixelColor(i, 0, 0, 0); // sets LEDs to off
  67. strip.show();
  68. delay(50);
  69. }
  70. }
  71.  
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement