Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Photobooth NanoDuo LED controller ---- 31 January 2018
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- This sketch uses the current Adafruit Neopixel library.
- The NanoDuo is two Arduino Nanos set up to act as a USB adressable-LED controller.
- Using the same Nano to do both jobs seems to result in interrupt conflicts,
- so the task is divided between two Nano boards here.
- In the NanoDuo setup, one Nano board receives the USB messages
- and sets eight output pins in a way that represents the current LED settings.
- The second Nano monitors eight input pins, and changes the LED values accordingly.
- The pins used to communicate between the boards are pins D2 through D9, using external 220k pulldown resistors.
- This sketch is for the Nano that drives the LEDs based on the state of 8 input pins.
- The sketch for the other Nano is named "Photobooth NanoDuo USB".
- This code was tested with a string of 8 and 64 WS2812 LEDs.
- ----------------------------
- PATTERNS NEEDED:
- The 1st letter I assume starts and stops the LEDS. When I run the command in the software for "stop LED", it just sends one message: #0=101 [e]
- The 2nd letter represents the speed. s=slow, m=medium, q=fast
- The number [8] represents a color choice: 1=white, 2=red, 3=green, 4=blue, 5=purple, 6=yellow, 7=cyan, 8=orange
- The final letter represents the pattern. z=spinning wheel, x=right fill, c=left fill, v=right ball, b=left ball
- */
- #include <Adafruit_NeoPixel.h>
- #ifdef __AVR__
- #include <avr/power.h>
- #endif
- #define PIN 12 // This is the pin that the LED chain is connected to.
- #define NUMPIXELS 64
- Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
- int pinStatus_checkInterval = 250; // This is the interval that the Nano checks its input pins for a state change.
- unsigned long checkInterval_beginTime = 0;
- unsigned long checkInterval_currentTime = 0;
- int LED_speed = 0; // This is the on/off control and also the speed: 0 = off, 1 = slow, 2 = medium and 3 = fast
- int LED_colorSetting = 0; // This is used to check for state changes.
- // 1=white, 2=red, 3=green, 4=blue, 5=purple, 6=yellow, 7=cyan, 8=orange
- int LED_color_red = 0; // These are the three values used by the LEDs.
- int LED_color_green = 0; // Do not change these values, they will just get ignored.
- int LED_color_blue = 0; // The values used are assigned in the function update_LED_mode().
- int LED_pattern = 0; // the pattern to use: z=spinning wheel, x=right fill, c=left fill, v=right ball, b=left ball
- // the numerical version is 0=spinning wheel, 1=right fill, 2=left fill, 3=right ball, 4=left ball
- int LED_master_dim_setting = 10; // This is an optional dimmer setting that is applied to all LEDs.
- // It is a factor of 100%, that is-- 100 = 100% of original coded brightness. 50 would mean (50% brightness).
- int led_pattern_control_1 = 0; // This is a variable used to generate the different patterns.
- // The different patterns use it different ways.
- int LED_animate_speed = 0; // This is the speed that the LED display changes at. Do not change this value!
- // One of the three values below gets copied into here.
- int LED_animate_speed_slow = 500; // milliseconds
- int LED_animate_speed_medium = 300;// milliseconds
- int LED_animate_speed_fast = 150;// milliseconds
- unsigned long LED_change_beginTime = 0; // These are timer values for animating the LEDs.
- unsigned long LED_change_currentTime = 0;
- int pinValue_A = 0; // These are three values to make pin reading more convenient.
- int pinValue_B = 0;
- int pinValue_C = 0;
- // function prototypes
- void check_pin_status(); // This compares the previous pin status to the current one,
- // to find out if the LED display status must be updated
- void update_LED_mode(); // This updates the mode that the display is set to use, if necessary
- void update_LED_animation(); // This updates the LEDs if an animated display is running.
- void turn_all_LEDs_off(); // This is used to turn the LEDs off.
- int apply_dimmer_value(int colorValue); // This is used to apply the master dimmer value to color values before they are sent to the LEDs.
- void led_pattern_0(); // These are the fuctions that control the five different display patterns.
- void led_pattern_1();
- void led_pattern_2();
- void led_pattern_3();
- void led_pattern_4();
- void setup() {
- // pins 2 through 9 are the machine state pins, using external 220k pulldown resistors.
- pinMode(2, INPUT);
- pinMode(3, INPUT);
- pinMode(4, INPUT);
- pinMode(5, INPUT);
- pinMode(6, INPUT);
- pinMode(7, INPUT);
- pinMode(8, INPUT);
- pinMode(9, INPUT);
- pixels.begin(); // This initializes the NeoPixel library.
- turn_all_LEDs_off();
- }
- void loop() {
- // This section checks the input pin states at the time interval of pinStatus_checkInterval.
- checkInterval_currentTime = millis();
- if (checkInterval_currentTime > checkInterval_beginTime) {
- if (checkInterval_currentTime > (checkInterval_beginTime + pinStatus_checkInterval)) {
- check_pin_status();
- checkInterval_beginTime = millis(); // reset the previous change time to now.
- }
- }
- else {
- checkInterval_beginTime = millis(); // millis() rollover condition
- }
- // Below animates the LED display, if any pattern is running.
- if (LED_speed > 0) { // zero = [off]
- LED_change_currentTime = millis();
- if (LED_change_currentTime > LED_change_beginTime) {
- if (LED_change_currentTime > (LED_change_beginTime + LED_animate_speed)) {
- update_LED_animation();
- LED_change_beginTime = millis(); // reset the previous change time to now.
- }
- }
- else {
- LED_change_beginTime = millis(); // millis() rollover condition
- }
- }
- } // end of main loop
- void check_pin_status() {
- // This compares the previous pin status to the current one,
- // to find out if the LED display status must be updated
- bool LED_update_required = false;
- int tempSpeedValue = 0;
- pinValue_A = digitalRead(2);
- pinValue_B = digitalRead(3);
- //int LED_speed = 0; // 0 = off, 1 = slow, 2 = medium and 3 = fast
- if (pinValue_A == LOW && pinValue_B == LOW) {
- tempSpeedValue = 0;
- }
- if (pinValue_A == LOW && pinValue_B == HIGH) {
- tempSpeedValue = 1;
- }
- if (pinValue_A == HIGH && pinValue_B == LOW) {
- tempSpeedValue = 2;
- }
- if (pinValue_A == HIGH && pinValue_B == HIGH) {
- tempSpeedValue = 3;
- }
- //LED_speed = tempValue;
- if (LED_speed != tempSpeedValue) {
- LED_update_required = true;
- }
- LED_speed = tempSpeedValue;
- //int LEDs_colorSetting = 0; // This is used to check for state changes.
- // 1=white, 2=red, 3=green, 4=blue, 5=purple, 6=yellow, 7=cyan, 8=orange
- int tempColorValue = 0;
- pinValue_A = digitalRead(4);
- pinValue_B = digitalRead(5);
- pinValue_C = digitalRead(6);
- if (pinValue_A == LOW && pinValue_B == LOW && pinValue_C == LOW) {
- tempColorValue = 1;
- }
- if (pinValue_A == LOW && pinValue_B == LOW && pinValue_C == HIGH) {
- tempColorValue = 2;
- }
- if (pinValue_A == LOW && pinValue_B == HIGH && pinValue_C == LOW) {
- tempColorValue = 3;
- }
- if (pinValue_A == LOW && pinValue_B == HIGH && pinValue_C == HIGH) {
- tempColorValue = 4;
- }
- if (pinValue_A == HIGH && pinValue_B == LOW && pinValue_C == LOW) {
- tempColorValue = 5;
- }
- if (pinValue_A == HIGH && pinValue_B == LOW && pinValue_C == HIGH) {
- tempColorValue = 6;
- }
- if (pinValue_A == HIGH && pinValue_B == HIGH && pinValue_C == LOW) {
- tempColorValue = 7;
- }
- if (pinValue_A == HIGH && pinValue_B == HIGH && pinValue_C == HIGH) {
- tempColorValue = 8;
- }
- if (LED_colorSetting != tempColorValue) {
- LED_update_required = true;
- }
- LED_colorSetting = tempColorValue;
- // int LED_pattern = 0; // the pattern to use: z=spinning wheel, x=right fill, c=left fill, v=right ball, b=left ball
- // the numerical version is 0=spinning wheel, 1=right fill, 2=left fill, 3=right ball, 4=left ball
- int tempPatternValue = 0;
- pinValue_A = digitalRead(7);
- pinValue_B = digitalRead(8);
- pinValue_C = digitalRead(9);
- if (pinValue_A == LOW && pinValue_B == LOW && pinValue_C == LOW) {
- tempPatternValue = 0;
- }
- if (pinValue_A == LOW && pinValue_B == LOW && pinValue_C == HIGH) {
- tempPatternValue = 1;
- }
- if (pinValue_A == LOW && pinValue_B == HIGH && pinValue_C == LOW) {
- tempPatternValue = 2;
- }
- if (pinValue_A == LOW && pinValue_B == HIGH && pinValue_C == HIGH) {
- tempPatternValue = 3;
- }
- if (pinValue_A == HIGH && pinValue_B == LOW && pinValue_C == LOW) {
- tempPatternValue = 4;
- }
- if (LED_pattern != tempPatternValue) {
- LED_update_required = true;
- }
- LED_pattern = tempPatternValue;
- if (LED_update_required) {
- update_LED_mode();
- }
- }
- int apply_dimmer_value(int colorValue) {
- // This function applies a dimmer value (%) to an int color value and returns an int.
- if (colorValue > 100) {
- colorValue = 100;
- }
- if (colorValue > 0) {
- if (LED_master_dim_setting > 0) {
- int adjustedColorValue = (int) ((colorValue * LED_master_dim_setting) / 100);
- return adjustedColorValue;
- }
- else {
- return 0;
- }
- }
- else {
- return 0;
- }
- }
- void update_LED_animation() {
- // This updates the LEDs if an animated display is running.
- // Note: I just wrote five different LED patterns here. I didn't try to duplicate the exact photobooth patterns.
- if (LED_speed > 0) {
- switch (LED_pattern) {
- case 0:
- led_pattern_0();
- break;
- case 1:
- led_pattern_1();
- break;
- case 2:
- led_pattern_2();
- break;
- case 3:
- led_pattern_3();
- break;
- case 4:
- led_pattern_4();
- break;
- }
- }
- else {
- turn_all_LEDs_off();
- }
- }
- void led_pattern_0() {
- // This pattern blinks every other light off and on, in two sets.
- if (led_pattern_control_1 == 0) {
- for (int x = 0; x < NUMPIXELS; x = x + 2) {
- pixels.setPixelColor(x, pixels.Color(LED_color_red, LED_color_green, LED_color_blue));
- }
- for (int y = 1; y < NUMPIXELS; y = y + 2) {
- pixels.setPixelColor(y, pixels.Color(0, 0, 0));
- }
- led_pattern_control_1 = 1;
- pixels.show();
- }
- else {
- for (int x = 0; x < NUMPIXELS; x = x + 2) {
- pixels.setPixelColor(x, pixels.Color(0, 0, 0));
- }
- for (int y = 1; y < NUMPIXELS; y = y + 2) {
- pixels.setPixelColor(y, pixels.Color(LED_color_red, LED_color_green, LED_color_blue));
- }
- led_pattern_control_1 = 0;
- pixels.show();
- }
- }
- void led_pattern_1() {
- // This pattern chases one LED in one direction.
- for (int x = 0; x < NUMPIXELS; x++) {
- pixels.setPixelColor(x, pixels.Color(0, 0, 0));
- if (x == led_pattern_control_1) {
- pixels.setPixelColor(x, pixels.Color(LED_color_red, LED_color_green, LED_color_blue));
- }
- }
- led_pattern_control_1++;
- if (led_pattern_control_1 == NUMPIXELS) {
- led_pattern_control_1 = 0;
- }
- pixels.show();
- }
- void led_pattern_2() {
- // This pattern chases one LED in the opposite direction of pattern #1.
- for (int x = 0; x < NUMPIXELS; x++) {
- pixels.setPixelColor(x, pixels.Color(0, 0, 0));
- if (x == led_pattern_control_1) {
- pixels.setPixelColor(x, pixels.Color(LED_color_red, LED_color_green, LED_color_blue));
- }
- }
- led_pattern_control_1--; // <--------- to reverse the direction, only 1 line is changed compared to pattern #1
- if (led_pattern_control_1 == -1) {
- led_pattern_control_1 = (NUMPIXELS - 1);
- }
- pixels.show();
- }
- void led_pattern_3() {
- // This pattern fills the LEDs in one direction.
- for (int x = 0; x < NUMPIXELS; x++) {
- if (x <= led_pattern_control_1) {
- pixels.setPixelColor(x, pixels.Color(LED_color_red, LED_color_green, LED_color_blue));
- }
- else {
- pixels.setPixelColor(x, pixels.Color(0, 0, 0));
- }
- }
- led_pattern_control_1++;
- if (led_pattern_control_1 == NUMPIXELS) {
- led_pattern_control_1 = 0;
- }
- pixels.show();
- }
- void led_pattern_4() {
- // This pattern fills the LEDs in the opposite direction of pattern #3.
- for (int x = 0; x < NUMPIXELS; x++) {
- if (x >= led_pattern_control_1) {
- pixels.setPixelColor(x, pixels.Color(LED_color_red, LED_color_green, LED_color_blue));
- }
- else {
- pixels.setPixelColor(x, pixels.Color(0, 0, 0));
- }
- }
- led_pattern_control_1--;
- if (led_pattern_control_1 == -1) {
- led_pattern_control_1 = (NUMPIXELS - 1);
- }
- pixels.show();
- }
- void turn_all_LEDs_off() {
- // This just turns all the LEDs off.
- for (int x = 0; x < NUMPIXELS; x++) {
- pixels.setPixelColor(x, pixels.Color(0, 0, 0));
- }
- pixels.show();
- }
- void update_LED_mode() {
- // This updates the mode that the display is set to use, if necessary
- // This function allows you to set up a starting point for an animation,
- // when the display transitions from one animation to a different one.
- // The section below updates the animation speed.
- if (LED_speed == 1) {
- LED_animate_speed = LED_animate_speed_slow;
- }
- if (LED_speed == 2) {
- LED_animate_speed = LED_animate_speed_medium;
- }
- if (LED_speed == 3) {
- LED_animate_speed = LED_animate_speed_fast;
- }
- if (LED_speed == 0) {
- turn_all_LEDs_off();
- }
- // The section below updates the color setting.
- switch (LED_colorSetting) {
- case 1:
- // color = white
- LED_color_red = apply_dimmer_value(255);
- LED_color_green = apply_dimmer_value(255);
- LED_color_blue = apply_dimmer_value(255);
- break;
- case 2:
- // color = red
- LED_color_red = apply_dimmer_value(255);
- LED_color_green = apply_dimmer_value(0);
- LED_color_blue = apply_dimmer_value(0);
- break;
- case 3:
- // color = green
- LED_color_red = apply_dimmer_value(0);
- LED_color_green = apply_dimmer_value(255);
- LED_color_blue = apply_dimmer_value(0);
- break;
- case 4:
- // color = blue
- LED_color_red = apply_dimmer_value(0);
- LED_color_green = apply_dimmer_value(0);
- LED_color_blue = apply_dimmer_value(255);
- break;
- case 5:
- // color = purple
- LED_color_red = apply_dimmer_value(128);
- LED_color_green = apply_dimmer_value(0);
- LED_color_blue = apply_dimmer_value(128);
- break;
- case 6:
- // color = yellow
- LED_color_red = apply_dimmer_value(255);
- LED_color_green = apply_dimmer_value(255);
- LED_color_blue = apply_dimmer_value(0);
- break;
- case 7:
- // color = cyan
- LED_color_red = apply_dimmer_value(0);
- LED_color_green = apply_dimmer_value(255);
- LED_color_blue = apply_dimmer_value(255);
- break;
- case 8:
- // color = orange
- LED_color_red = apply_dimmer_value(255);
- LED_color_green = apply_dimmer_value(64);
- LED_color_blue = apply_dimmer_value(0);
- break;
- }
- // The section below initializes starting positions for the different patterns:
- switch (LED_pattern) {
- case 0:
- // This pattern blinks every other light off and on, in two sets.
- // It doesn't really have a direction that it moves.
- // The led_pattern_control_1 is only used to switch back and forth between the two sets of lights to turn on.
- turn_all_LEDs_off();
- led_pattern_control_1 = 0;
- break;
- case 1:
- // This pattern chases one LED in one direction.
- turn_all_LEDs_off();
- led_pattern_control_1 = 0;
- break;
- case 2:
- // This pattern chases one LED in the opposite direction of pattern #1.
- turn_all_LEDs_off();
- led_pattern_control_1 = (NUMPIXELS - 1);
- break;
- case 3:
- // This pattern fills the LEDs in one direction.
- turn_all_LEDs_off();
- led_pattern_control_1 = 0;
- break;
- case 4:
- // This pattern fills the LEDs in the opposite direction of pattern #3.
- turn_all_LEDs_off();
- led_pattern_control_1 = (NUMPIXELS - 1);
- break;
- }
- // The pattern itself is updated in check_pin_status().
- update_LED_animation();
- LED_change_beginTime = millis();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement