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

Dependencies:   mbed WS2812 WS2812_Example PixelArray

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "WS2812.h"
00003 #include "PixelArray.h"
00004 #include "DebounceIn.h"
00005 
00006 #define WS2812_BUF 8  //number of LEDS in the array
00007 #define NUM_COLORS 5  //number of colors in the array
00008 
00009 //debouces buttons and attaches Pull Up resistors to each input
00010 DebounceIn button1(PB_4, PullUp);
00011 DebounceIn button2(PA_9, PullUp);
00012 DebounceIn button3(PB_6, PullUp);
00013 DebounceIn button4(PB_8, PullUp);
00014 PixelArray px(WS2812_BUF); //sets number of LEDs
00015 // See the program page for information on the timing numbers
00016 // Timing numbers for the NUCLEO-F411RE:  7, 15, 10, 15
00017 // Timing numbers for the NUCLEO-L476RG:  3, 12,  9, 12
00018 WS2812 ws(PB_3, WS2812_BUF,  3, 12, 9, 12);
00019 int state1 = 0;
00020 int state2 = 0;
00021 int state3 = 0;
00022 int state4 = 0;
00023 int state = 0;
00024 
00025 void button1fall() { //button1 is pushed
00026     if(state2 == 0 & state3 == 0 & state4 == 0) { //if no other buttons are on
00027         state1 = !state1; //change the state of button1
00028     }
00029 }
00030 void button2fall() { //button2 is pushed
00031     if(state1 == 0 & state3 == 0 & state4 == 0) { //if no other buttons are on
00032         state2 = !state2; //change the state of button2
00033     }
00034 }
00035 void button3fall() { //button3 is pushed
00036     if(state2 == 0 & state1 == 0 & state4 == 0) { //if no other buttons are on
00037         state3 = !state3; //change the state of button3
00038     }
00039 }
00040 void button4fall() { //button4 is pushed
00041     if(state2 == 0 & state3 == 0 & state1 == 0) { //if no other buttons are on
00042         state4 = !state4; //change the state of button4
00043     }
00044 }
00045 int main()
00046 {
00047     button1.fall(callback(button1fall), 80000);
00048     button2.fall(callback(button2fall), 80000);
00049     button3.fall(callback(button3fall), 80000);
00050     button4.fall(callback(button4fall), 80000);
00051     
00052     ws.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling
00053     
00054     // set up the colours we want to draw with
00055     int colorbuf[NUM_COLORS] = {0x000000,0x00FFFF,0xFFFF00,0xFFFFFF,0xFF0000};
00056     
00057     while (1) 
00058     {
00059         // According to which button is pushed, a color will display until the same button is pushed again
00060         if(button1 == 0 & state2 == 0 & state3 == 0 & state4 == 0) {
00061             if(state1 == 1) {
00062                 state = 1;
00063             }
00064             else {
00065                 state = 0;
00066             }
00067         }
00068         else if(button2 == 0 & state1 == 0 & state3 == 0 & state4 == 0) {
00069             if(state2 == 1) {
00070                 state = 2;
00071             }
00072             else {
00073                 state = 0;
00074             }
00075         }
00076         else if(button3 == 0 & state1 == 0 & state2 == 0 & state4 == 0) {
00077             if(state3 == 1) {
00078                 state = 3;
00079             }
00080             else {
00081                 state = 0;
00082             }
00083         }
00084         else if(button4 == 0 & state1 == 0 & state2 == 0 & state3 == 0) {
00085             if(state4 == 1) {
00086                 state = 4;
00087             }
00088             else {
00089                 state = 0;
00090             }
00091         }
00092         //write the color value for each pixel
00093         px.SetAll(colorbuf[state]);  
00094         //write the birghtness intensity value for each pixel
00095         px.SetAllI(20);
00096         //writes the colors to the neopixels
00097         for (int i = WS2812_BUF; i >= 0; i--) {
00098             ws.write(px.getBuf());
00099         }
00100     }
00101 }