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

Dependencies:   mbed WS2812 WS2812_Example PixelArray

Files at this revision

API Documentation at this revision

Comitter:
czatlokowicz
Date:
Fri Aug 02 18:13:53 2019 +0000
Parent:
2:cb82a3dc4031
Commit message:
Button Neopixel Debounce

Changed in this revision

DebounceIn.lib Show annotated file Show diff for this revision Revisions of this file
WS2812_Example_WORKS.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r cb82a3dc4031 -r 25ad14f76641 DebounceIn.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DebounceIn.lib	Fri Aug 02 18:13:53 2019 +0000
@@ -0,0 +1,1 @@
+https://github.com/pilotak/DebounceIn/#a0d94322fdbcc039d2724c7c6cbc4334b10cace0
diff -r cb82a3dc4031 -r 25ad14f76641 WS2812_Example_WORKS.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WS2812_Example_WORKS.lib	Fri Aug 02 18:13:53 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/bridadan/code/WS2812_Example/#cb82a3dc4031
diff -r cb82a3dc4031 -r 25ad14f76641 main.cpp
--- a/main.cpp	Thu Feb 12 21:55:24 2015 +0000
+++ b/main.cpp	Fri Aug 02 18:13:53 2019 +0000
@@ -1,47 +1,101 @@
 #include "mbed.h"
 #include "WS2812.h"
 #include "PixelArray.h"
+#include "DebounceIn.h"
 
-#define WS2812_BUF 150
-#define NUM_COLORS 6
-#define NUM_LEDS_PER_COLOR 10
+#define WS2812_BUF 8  //number of LEDS in the array
+#define NUM_COLORS 5  //number of colors in the array
+
+//debouces buttons and attaches Pull Up resistors to each input
+DebounceIn button1(PB_4, PullUp);
+DebounceIn button2(PA_9, PullUp);
+DebounceIn button3(PB_6, PullUp);
+DebounceIn button4(PB_8, PullUp);
+PixelArray px(WS2812_BUF); //sets number of LEDs
+// See the program page for information on the timing numbers
+// Timing numbers for the NUCLEO-F411RE:  7, 15, 10, 15
+// Timing numbers for the NUCLEO-L476RG:  3, 12,  9, 12
+WS2812 ws(PB_3, WS2812_BUF,  3, 12, 9, 12);
+int state1 = 0;
+int state2 = 0;
+int state3 = 0;
+int state4 = 0;
+int state = 0;
 
-PixelArray px(WS2812_BUF);
-
-// See the program page for information on the timing numbers
-// The given numbers are for the K64F
-WS2812 ws(D9, WS2812_BUF, 0, 5, 5, 0);
-
+void button1fall() { //button1 is pushed
+    if(state2 == 0 & state3 == 0 & state4 == 0) { //if no other buttons are on
+        state1 = !state1; //change the state of button1
+    }
+}
+void button2fall() { //button2 is pushed
+    if(state1 == 0 & state3 == 0 & state4 == 0) { //if no other buttons are on
+        state2 = !state2; //change the state of button2
+    }
+}
+void button3fall() { //button3 is pushed
+    if(state2 == 0 & state1 == 0 & state4 == 0) { //if no other buttons are on
+        state3 = !state3; //change the state of button3
+    }
+}
+void button4fall() { //button4 is pushed
+    if(state2 == 0 & state3 == 0 & state1 == 0) { //if no other buttons are on
+        state4 = !state4; //change the state of button4
+    }
+}
 int main()
 {
-
+    button1.fall(callback(button1fall), 80000);
+    button2.fall(callback(button2fall), 80000);
+    button3.fall(callback(button3fall), 80000);
+    button4.fall(callback(button4fall), 80000);
+    
     ws.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling
     
     // set up the colours we want to draw with
-    int colorbuf[NUM_COLORS] = {0x2f0000,0x2f2f00,0x002f00,0x002f2f,0x00002f,0x2f002f};
-
-    // for each of the colours (j) write out 10 of them
-    // the pixels are written at the colour*10, plus the colour position
-    // all modulus 60 so it wraps around
-    for (int i = 0; i < WS2812_BUF; i++) {
-        px.Set(i, colorbuf[(i / NUM_LEDS_PER_COLOR) % NUM_COLORS]);
-    }
-
-    // now all the colours are computed, add a fade effect using intensity scaling
-    // compute and write the II value for each pixel
-    for (int j=0; j<WS2812_BUF; j++) {
-        // px.SetI(pixel position, II value)
-        px.SetI(j%WS2812_BUF, 0xf+(0xf*(j%NUM_LEDS_PER_COLOR)));
-    }
-
-
-    // Now the buffer is written, rotate it
-    // by writing it out with an increasing offset
-    while (1) {
-        for (int z=WS2812_BUF; z >= 0 ; z--) {
-            ws.write_offsets(px.getBuf(),z,z,z);
-            wait(0.075);
+    int colorbuf[NUM_COLORS] = {0x000000,0x00FFFF,0xFFFF00,0xFFFFFF,0xFF0000};
+    
+    while (1) 
+    {
+        // According to which button is pushed, a color will display until the same button is pushed again
+        if(button1 == 0 & state2 == 0 & state3 == 0 & state4 == 0) {
+            if(state1 == 1) {
+                state = 1;
+            }
+            else {
+                state = 0;
+            }
+        }
+        else if(button2 == 0 & state1 == 0 & state3 == 0 & state4 == 0) {
+            if(state2 == 1) {
+                state = 2;
+            }
+            else {
+                state = 0;
+            }
+        }
+        else if(button3 == 0 & state1 == 0 & state2 == 0 & state4 == 0) {
+            if(state3 == 1) {
+                state = 3;
+            }
+            else {
+                state = 0;
+            }
+        }
+        else if(button4 == 0 & state1 == 0 & state2 == 0 & state3 == 0) {
+            if(state4 == 1) {
+                state = 4;
+            }
+            else {
+                state = 0;
+            }
+        }
+        //write the color value for each pixel
+        px.SetAll(colorbuf[state]);  
+        //write the birghtness intensity value for each pixel
+        px.SetAllI(20);
+        //writes the colors to the neopixels
+        for (int i = WS2812_BUF; i >= 0; i--) {
+            ws.write(px.getBuf());
         }
     }
-
-}
+}
\ No newline at end of file
diff -r cb82a3dc4031 -r 25ad14f76641 mbed.bld
--- a/mbed.bld	Thu Feb 12 21:55:24 2015 +0000
+++ b/mbed.bld	Fri Aug 02 18:13:53 2019 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/e188a91d3eaa
\ No newline at end of file
+https://os.mbed.com/users/mbed_official/code/mbed/builds/3a7713b1edbc
\ No newline at end of file