|
| 1 | +/* |
| 2 | + */ |
| 3 | + |
| 4 | +#include <Adafruit_NeoPixel.h> |
| 5 | + |
| 6 | +#define PIN 8 |
| 7 | +#define NUMPIXELS 10 |
| 8 | +#define DELAYVAL 100 // Time (in milliseconds) to pause between pixels |
| 9 | + |
| 10 | +Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); |
| 11 | + |
| 12 | +char receivedChar; |
| 13 | +boolean newData = false; |
| 14 | +uint32_t available = pixels.Color(0, 50, 0); |
| 15 | +uint32_t busy = pixels.Color(50, 0, 0); |
| 16 | + |
| 17 | +// the setup function runs once when you press reset or power the board |
| 18 | +void setup() { |
| 19 | + // initialize digital pin LED_BUILTIN as an output. |
| 20 | + Serial.begin(9600); |
| 21 | + Serial.println("<Arduino is ready>"); |
| 22 | + pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED) |
| 23 | +} |
| 24 | + |
| 25 | +void loop() { |
| 26 | + recvOneChar(); |
| 27 | + showNewData(); |
| 28 | +} |
| 29 | + |
| 30 | +void recvOneChar() { |
| 31 | + if (Serial.available() > 0) { |
| 32 | + receivedChar = Serial.read(); |
| 33 | + newData = true; |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +void showNewData() { |
| 38 | + if (newData == true) { |
| 39 | + pixels.clear(); // Set all pixel colors to 'off' |
| 40 | + Serial.print("This just in ... "); |
| 41 | + Serial.println(receivedChar); |
| 42 | + |
| 43 | + bool shouldUpdate = false; |
| 44 | + uint32_t newColor; |
| 45 | + |
| 46 | + if (receivedChar == 'n') { |
| 47 | + digitalWrite(LED_BUILTIN, HIGH); |
| 48 | + newColor = busy; |
| 49 | + shouldUpdate = true; |
| 50 | + } else if (receivedChar == 'f') { |
| 51 | + digitalWrite(LED_BUILTIN, LOW); |
| 52 | + newColor = available; |
| 53 | + shouldUpdate = true; |
| 54 | + } |
| 55 | + newData = false; |
| 56 | + if (shouldUpdate) { |
| 57 | + for (int i = 0; i < NUMPIXELS; i++) { // For each pixel... |
| 58 | + |
| 59 | + // pixels.Color() takes RGB values, from 0,0,0 up to 255,255,255 |
| 60 | + // Here we're using a moderately bright green color: |
| 61 | + pixels.setPixelColor(i, newColor); |
| 62 | + |
| 63 | + pixels.show(); // Send the updated pixel colors to the hardware. |
| 64 | + |
| 65 | + delay(DELAYVAL); // Pause before next pass through loop |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments