Browse Source

Added example here and updates to latest changes

Fred Damstra (Macbook 2015) 5 months ago
parent
commit
99f62d8d7e

+ 30 - 0
BackgroundRingClass.cpp

@@ -52,6 +52,36 @@ void BackgroundRingClass::tick_idle() {
 }
 
 
+///////////////////////// Random
+int BackgroundRingClass::random_event(int copy) {
+  int draw = random(0, 480); // this lets us choose events with weighted probability
+  if(copy >= 0) { // we wish to copy the other one's action
+    draw = copy;
+  }
+
+  // Christmas Colors
+  if(draw < 50)       this->init_pulse(DARKRED, 80, 2000);
+  else if(draw < 100) this->init_pulse(DARKGREEN, 80, 2000);
+  else if(draw < 150) this->init_spin(DARKRED, DARKGREEN);
+  else if(draw < 200) this->init_spin(DARKGREEN, DARKRED);
+  else if(draw < 250) this->init_flip(DARKRED, DARKGREEN);
+  else if(draw < 300) this->init_flip(DARKGREEN, DARKRED);
+  else if(draw < 350) this->init_slowroll(DARKRED, DARKGREEN);
+  else if(draw < 400) this->init_slowroll(DARKGREEN, DARKRED);
+
+  // random colors
+  else if(draw < 410) this->init_pulse(random(0, 0xFFFFFF), 80, 2000);
+  else if(draw < 420) this->init_pulse(random(0, 0xFFFFFF), 80, 2000);
+  else if(draw < 430) this->init_spin(random(0, 0xFFFFFF), random(0, 0xFFFFFF));
+  else if(draw < 440) this->init_spin(random(0, 0xFFFFFF), random(0, 0xFFFFFF));
+  else if(draw < 450) this->init_flip(random(0, 0xFFFFFF), random(0, 0xFFFFFF));
+  else if(draw < 460) this->init_flip(random(0, 0xFFFFFF), random(0, 0xFFFFFF));
+  else if(draw < 470) this->init_slowroll(random(0, 0xFFFFFF), random(0, 0xFFFFFF));
+  else if(draw < 480) this->init_slowroll(random(0, 0xFFFFFF), random(0, 0xFFFFFF));
+
+  return draw;
+}
+
 
 ///////////////////////// Solid
 void BackgroundRingClass::init_solid(unsigned long color, uint8_t brightness) {

+ 3 - 0
BackgroundRingClass.h

@@ -30,6 +30,9 @@ class BackgroundRingClass : public Adafruit_NeoPixel {
     void init_idle();
     void tick_idle();
 
+    // Random Event
+    int random_event(int copy=-1);
+
     // Solid
     void init_solid(unsigned long int color, uint8_t brightness);
     void tick_solid();

+ 147 - 0
examples/background_rings_demo/background_rings_demo.ino

@@ -0,0 +1,147 @@
+
+#include <SoftwareSerial.h>
+#include <WS2812_Definitions.h>
+#include <eRCaGuy_ButtonReader.h>
+#include <BackgroundRingClass.h>
+
+const int LED_COUNT = 24;
+const int MAXBRIGHTNESS = 80;
+
+const int leftFSRSimPin = A7;
+const int rightFSRSimPin = A0;
+
+/* Start Button */
+const int leftReadyButtonPin = 13;
+const int rightReadyButtonPin = 8;
+
+/* WS2812 Christmas Tree Setup:
+    Two strips of 6 LEDs connected to 5V and Ground, plus the signal pin
+*/
+const int ledPinRight = 12;
+const int ledPinLeft  = 11;
+
+const int randomPin = A5;
+
+BackgroundRingClass rightRing = BackgroundRingClass(LED_COUNT, ledPinRight);
+BackgroundRingClass leftRing  = BackgroundRingClass(LED_COUNT, ledPinLeft);
+
+eRCaGuy_ButtonReader leftReadyButton = eRCaGuy_ButtonReader(leftReadyButtonPin, 25);
+eRCaGuy_ButtonReader rightReadyButton = eRCaGuy_ButtonReader(rightReadyButtonPin, 25);
+eRCaGuy_ButtonReader leftFSRButton = eRCaGuy_ButtonReader(leftFSRSimPin, 25);
+eRCaGuy_ButtonReader rightFSRButton = eRCaGuy_ButtonReader(rightFSRSimPin, 25);
+
+
+void setup() {
+  // put your setup code here, to run once:
+  Serial.begin(115200); // Serial used for logging
+
+  randomSeed(analogRead(randomPin));
+
+  /* WS2812 LED Setup */
+  //rightRing.begin();
+  //leftRing.begin();
+
+  pinMode(leftReadyButtonPin, INPUT_PULLUP);
+  pinMode(rightReadyButtonPin, INPUT_PULLUP);
+  pinMode(leftFSRSimPin, INPUT_PULLUP);
+  pinMode(rightFSRSimPin, INPUT_PULLUP);
+
+  Serial.println("Testing Solids");
+  rightRing.init_solid(DARKRED, MAXBRIGHTNESS);
+  leftRing.init_solid(DARKGREEN, MAXBRIGHTNESS);
+  delay(1000);
+  leftRing.init_idle();
+  rightRing.init_idle();
+  Serial.println("Begin.");
+}
+
+
+
+void loop() {
+  static unsigned long last_random_check = millis();
+
+  rightRing.tick();
+  leftRing.tick();
+  
+  if(button_pressed(rightFSRButton)) {
+    Serial.println("Right button pressed. Switching.");
+    //rightRing.init_pulse(random(0, 0xFFFFFF), 255, 2000);
+    //rightRing.init_pulse(DARKRED, MAXBRIGHTNESS, 2000);
+    //rightRing.init_spin(DARKRED, DARKGREEN);
+    //rightRing.init_flip(DARKRED, DARKGREEN);
+    //rightRing.init_slowroll(DARKRED, DARKGREEN);
+    rightRing.random_event();
+  }
+
+  if(button_pressed(leftFSRButton)) {
+    Serial.println("Right button pressed. Switching.");
+    //leftRing.init_pulse(random(0, 0xFFFFFF), 255, 2000);
+    //leftRing.init_pulse(DARKGREEN, MAXBRIGHTNESS, 2000);
+    //leftRing.init_spin(DARKGREEN, DARKRED);
+    //leftRing.init_flip(DARKGREEN, DARKRED);
+    //leftRing.init_slowroll(DARKGREEN, DARKRED);
+    leftRing.random_event();
+  }
+
+  if(leftRing.action == Idle && rightRing.action == Idle) {
+    if(millis() - last_random_check > 1000) { // only check every 10 seconds
+      if(random(0, 10) == 0) {
+        int draw = leftRing.random_event();
+        rightRing.random_event(draw); // synchronized
+      }
+      last_random_check = millis();
+    }
+  } else {
+    last_random_check = millis(); // don't count time in an action
+  }
+
+  //test_solids();
+  //test_pulses();
+  //test_random_pulses();
+}
+
+bool button_pressed(eRCaGuy_ButtonReader& button)
+{
+  int8_t buttonaction;
+  boolean buttonstate;
+
+  button.readButton(&buttonaction, &buttonstate);
+  return buttonaction == 1;
+}
+
+
+void test_pulses() {
+  if(rightRing.action == Idle) {
+    Serial.println("Changing right");
+    rightRing.init_pulse(DARKGREEN, MAXBRIGHTNESS, 2000);
+  }
+  if(leftRing.action == Idle) {
+    Serial.println("Changing left");
+    leftRing.init_pulse(DARKGREEN, MAXBRIGHTNESS, 2000);
+  }
+}
+
+void test_random_pulses() {
+  if(rightRing.action == Idle) {
+    Serial.println("Changing right");
+    rightRing.init_pulse(random(0, 0xFFFFFF), MAXBRIGHTNESS, 2000);
+  }
+  if(leftRing.action == Idle) {
+    Serial.println("Changing left");
+    leftRing.init_pulse(random(0, 0xFFFFFF), MAXBRIGHTNESS, 2000);
+  }
+}
+
+void test_random_solids() {
+  if(rightRing.action == Idle) {
+    Serial.println("Changing right");
+    rightRing.init_solid(random(0, 0xFFFFFF), MAXBRIGHTNESS);
+  }
+  if(leftRing.action == Idle) {
+    Serial.println("Changing left");
+    leftRing.init_solid(random(0, 0xFFFFFF), MAXBRIGHTNESS);
+  }
+  delay(500);
+  leftRing.init_idle();
+  rightRing.init_idle();
+}