When you press a button, it lights up the neopixel until the same button is pressed again

Dependencies:   mbed WS2812 WS2812_Example PixelArray

Committer:
czatlokowicz
Date:
Fri Aug 02 18:13:53 2019 +0000
Revision:
3:25ad14f76641
Parent:
2:cb82a3dc4031
Button Neopixel Debounce

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bridadan 0:12cb6f0c2788 1 #include "mbed.h"
bridadan 0:12cb6f0c2788 2 #include "WS2812.h"
bridadan 0:12cb6f0c2788 3 #include "PixelArray.h"
czatlokowicz 3:25ad14f76641 4 #include "DebounceIn.h"
bridadan 0:12cb6f0c2788 5
czatlokowicz 3:25ad14f76641 6 #define WS2812_BUF 8 //number of LEDS in the array
czatlokowicz 3:25ad14f76641 7 #define NUM_COLORS 5 //number of colors in the array
czatlokowicz 3:25ad14f76641 8
czatlokowicz 3:25ad14f76641 9 //debouces buttons and attaches Pull Up resistors to each input
czatlokowicz 3:25ad14f76641 10 DebounceIn button1(PB_4, PullUp);
czatlokowicz 3:25ad14f76641 11 DebounceIn button2(PA_9, PullUp);
czatlokowicz 3:25ad14f76641 12 DebounceIn button3(PB_6, PullUp);
czatlokowicz 3:25ad14f76641 13 DebounceIn button4(PB_8, PullUp);
czatlokowicz 3:25ad14f76641 14 PixelArray px(WS2812_BUF); //sets number of LEDs
czatlokowicz 3:25ad14f76641 15 // See the program page for information on the timing numbers
czatlokowicz 3:25ad14f76641 16 // Timing numbers for the NUCLEO-F411RE: 7, 15, 10, 15
czatlokowicz 3:25ad14f76641 17 // Timing numbers for the NUCLEO-L476RG: 3, 12, 9, 12
czatlokowicz 3:25ad14f76641 18 WS2812 ws(PB_3, WS2812_BUF, 3, 12, 9, 12);
czatlokowicz 3:25ad14f76641 19 int state1 = 0;
czatlokowicz 3:25ad14f76641 20 int state2 = 0;
czatlokowicz 3:25ad14f76641 21 int state3 = 0;
czatlokowicz 3:25ad14f76641 22 int state4 = 0;
czatlokowicz 3:25ad14f76641 23 int state = 0;
bridadan 0:12cb6f0c2788 24
czatlokowicz 3:25ad14f76641 25 void button1fall() { //button1 is pushed
czatlokowicz 3:25ad14f76641 26 if(state2 == 0 & state3 == 0 & state4 == 0) { //if no other buttons are on
czatlokowicz 3:25ad14f76641 27 state1 = !state1; //change the state of button1
czatlokowicz 3:25ad14f76641 28 }
czatlokowicz 3:25ad14f76641 29 }
czatlokowicz 3:25ad14f76641 30 void button2fall() { //button2 is pushed
czatlokowicz 3:25ad14f76641 31 if(state1 == 0 & state3 == 0 & state4 == 0) { //if no other buttons are on
czatlokowicz 3:25ad14f76641 32 state2 = !state2; //change the state of button2
czatlokowicz 3:25ad14f76641 33 }
czatlokowicz 3:25ad14f76641 34 }
czatlokowicz 3:25ad14f76641 35 void button3fall() { //button3 is pushed
czatlokowicz 3:25ad14f76641 36 if(state2 == 0 & state1 == 0 & state4 == 0) { //if no other buttons are on
czatlokowicz 3:25ad14f76641 37 state3 = !state3; //change the state of button3
czatlokowicz 3:25ad14f76641 38 }
czatlokowicz 3:25ad14f76641 39 }
czatlokowicz 3:25ad14f76641 40 void button4fall() { //button4 is pushed
czatlokowicz 3:25ad14f76641 41 if(state2 == 0 & state3 == 0 & state1 == 0) { //if no other buttons are on
czatlokowicz 3:25ad14f76641 42 state4 = !state4; //change the state of button4
czatlokowicz 3:25ad14f76641 43 }
czatlokowicz 3:25ad14f76641 44 }
bridadan 0:12cb6f0c2788 45 int main()
bridadan 0:12cb6f0c2788 46 {
czatlokowicz 3:25ad14f76641 47 button1.fall(callback(button1fall), 80000);
czatlokowicz 3:25ad14f76641 48 button2.fall(callback(button2fall), 80000);
czatlokowicz 3:25ad14f76641 49 button3.fall(callback(button3fall), 80000);
czatlokowicz 3:25ad14f76641 50 button4.fall(callback(button4fall), 80000);
czatlokowicz 3:25ad14f76641 51
bridadan 1:e04a0ecefa29 52 ws.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling
bridadan 0:12cb6f0c2788 53
bridadan 0:12cb6f0c2788 54 // set up the colours we want to draw with
czatlokowicz 3:25ad14f76641 55 int colorbuf[NUM_COLORS] = {0x000000,0x00FFFF,0xFFFF00,0xFFFFFF,0xFF0000};
czatlokowicz 3:25ad14f76641 56
czatlokowicz 3:25ad14f76641 57 while (1)
czatlokowicz 3:25ad14f76641 58 {
czatlokowicz 3:25ad14f76641 59 // According to which button is pushed, a color will display until the same button is pushed again
czatlokowicz 3:25ad14f76641 60 if(button1 == 0 & state2 == 0 & state3 == 0 & state4 == 0) {
czatlokowicz 3:25ad14f76641 61 if(state1 == 1) {
czatlokowicz 3:25ad14f76641 62 state = 1;
czatlokowicz 3:25ad14f76641 63 }
czatlokowicz 3:25ad14f76641 64 else {
czatlokowicz 3:25ad14f76641 65 state = 0;
czatlokowicz 3:25ad14f76641 66 }
czatlokowicz 3:25ad14f76641 67 }
czatlokowicz 3:25ad14f76641 68 else if(button2 == 0 & state1 == 0 & state3 == 0 & state4 == 0) {
czatlokowicz 3:25ad14f76641 69 if(state2 == 1) {
czatlokowicz 3:25ad14f76641 70 state = 2;
czatlokowicz 3:25ad14f76641 71 }
czatlokowicz 3:25ad14f76641 72 else {
czatlokowicz 3:25ad14f76641 73 state = 0;
czatlokowicz 3:25ad14f76641 74 }
czatlokowicz 3:25ad14f76641 75 }
czatlokowicz 3:25ad14f76641 76 else if(button3 == 0 & state1 == 0 & state2 == 0 & state4 == 0) {
czatlokowicz 3:25ad14f76641 77 if(state3 == 1) {
czatlokowicz 3:25ad14f76641 78 state = 3;
czatlokowicz 3:25ad14f76641 79 }
czatlokowicz 3:25ad14f76641 80 else {
czatlokowicz 3:25ad14f76641 81 state = 0;
czatlokowicz 3:25ad14f76641 82 }
czatlokowicz 3:25ad14f76641 83 }
czatlokowicz 3:25ad14f76641 84 else if(button4 == 0 & state1 == 0 & state2 == 0 & state3 == 0) {
czatlokowicz 3:25ad14f76641 85 if(state4 == 1) {
czatlokowicz 3:25ad14f76641 86 state = 4;
czatlokowicz 3:25ad14f76641 87 }
czatlokowicz 3:25ad14f76641 88 else {
czatlokowicz 3:25ad14f76641 89 state = 0;
czatlokowicz 3:25ad14f76641 90 }
czatlokowicz 3:25ad14f76641 91 }
czatlokowicz 3:25ad14f76641 92 //write the color value for each pixel
czatlokowicz 3:25ad14f76641 93 px.SetAll(colorbuf[state]);
czatlokowicz 3:25ad14f76641 94 //write the birghtness intensity value for each pixel
czatlokowicz 3:25ad14f76641 95 px.SetAllI(20);
czatlokowicz 3:25ad14f76641 96 //writes the colors to the neopixels
czatlokowicz 3:25ad14f76641 97 for (int i = WS2812_BUF; i >= 0; i--) {
czatlokowicz 3:25ad14f76641 98 ws.write(px.getBuf());
bridadan 0:12cb6f0c2788 99 }
bridadan 0:12cb6f0c2788 100 }
czatlokowicz 3:25ad14f76641 101 }