diff --git a/platformio.ini b/platformio.ini index 3dcb42a..fd405ed 100644 --- a/platformio.ini +++ b/platformio.ini @@ -15,3 +15,4 @@ framework = arduino lib_deps = rfetick/MPU6050_light@^1.1.0 fastled/FastLED@^3.4.0 + mathertel/RotaryEncoder@^1.5.0 diff --git a/src/main.ino b/src/main.ino index c7216a0..8477e52 100644 --- a/src/main.ino +++ b/src/main.ino @@ -6,19 +6,22 @@ #include #include #include +#include -#define LED_PIN 7 +byte LED_PIN = 6; byte mode = 5; // 0 for acceleration, 1 for fire, 2 for waterfall, 3 pride, 4 glitter, 5 pulse, 6 off -const byte NUM_MODES = 8; +byte parameter = 128; +byte modeSelect; +const byte NUM_MODES = 9; const byte debug = 3; const byte STRIPS = 6; -const byte NUM_LEDS = 10; +const byte NUM_LEDS = 15; const byte FRAMES_PER_SECOND = 60; const float range = 0.5; //accelleration range in g -const byte BRIGHTNESS = 50; -const byte COOLING = 80; +byte BRIGHTNESS = 2; +const byte COOLING = 47; const byte SPARKING = 50; uint8_t gHue = 0; @@ -31,29 +34,45 @@ float h[NUM_BALLS] ; // An array of heights float vImpact0 = sqrt( -2 * GRAVITY * h0 ); // Impact velocity of the ball when it hits the ground if "dropped" from the top of the strip float vImpact[NUM_BALLS] ; // As time goes on the impact velocity will change, so make an array to store those values float tCycle[NUM_BALLS] ; // The time since the last time the ball struck the ground -int pos[NUM_BALLS] ; // The integer position of the dot on the strip (LED index) +byte pos[NUM_BALLS] ; // The integer position of the dot on the strip (LED index) long tLast[NUM_BALLS] ; // The clock time of the last ground strike float COR[NUM_BALLS] ; // Coefficient of Restitution (bounce damping) float accelerationHistory [STRIPS]; +int encoderPosition = 1; MPU6050 mpu(Wire); CRGB leds[NUM_LEDS * STRIPS]; +CRGB ledsR[NUM_LEDS * STRIPS]; CRGBPalette16 gPal; -CRGB flagcolors[6] = {CRGB::Red, CRGB::DarkOrange, CRGB::Yellow, CRGB::DarkGreen, CRGB::Blue, CRGB::DarkViolet}; +CRGB flagcolors[3][6] {{CRGB::Red, CRGB::DarkOrange, CRGB::Yellow, CRGB::DarkGreen, CRGB::Blue, CRGB::DarkViolet}, + {CRGB::DarkBlue, CRGB::DeepPink, CRGB::Gray, CRGB::Gray, CRGB::DeepPink, CRGB::DarkBlue}, + {CRGB::DeepPink, CRGB::Gray, CRGB::DarkViolet, CRGB::Black, CRGB::Blue, CRGB::Black}}; const byte modeSwitchPin = 2; +RotaryEncoder encoder(A2, A3); void setup() { Serial.begin(19200); //setup LEDs - FastLED.addLeds(leds, NUM_LEDS*STRIPS).setCorrection( TypicalLEDStrip ); + FastLED.addLeds(ledsR, NUM_LEDS * 0, NUM_LEDS).setCorrection( TypicalLEDStrip ); + FastLED.addLeds(ledsR, NUM_LEDS * 1, NUM_LEDS).setCorrection( TypicalLEDStrip ); + FastLED.addLeds(ledsR, NUM_LEDS * 2, NUM_LEDS).setCorrection( TypicalLEDStrip ); + FastLED.addLeds(ledsR, NUM_LEDS * 3, NUM_LEDS).setCorrection( TypicalLEDStrip ); + FastLED.addLeds(ledsR, NUM_LEDS * 4, NUM_LEDS).setCorrection( TypicalLEDStrip ); + FastLED.addLeds(ledsR, NUM_LEDS * 5, NUM_LEDS).setCorrection( TypicalLEDStrip ); FastLED.setBrightness( BRIGHTNESS ); pinMode(modeSwitchPin, INPUT_PULLUP); + pinMode(3, OUTPUT); + pinMode(4, OUTPUT); + pinMode(5, OUTPUT); attachInterrupt(digitalPinToInterrupt(modeSwitchPin), setMode, RISING); + PCICR |= (1 << PCIE1); + PCMSK1 |= (1 << PCINT10) | (1 << PCINT11); + for (int i = 0; i= NUM_MODES) { - mode = 0; + static unsigned long last_interrupt_time = 0; + unsigned long interrupt_time = millis(); + if (interrupt_time - last_interrupt_time > 200) { + if (modeSelect == 2) { + encoder.setPosition(mode); + modeSelect = 0; + digitalWrite(3, HIGH); + digitalWrite(4, LOW); + digitalWrite(5, LOW); + } + else if (modeSelect == 0) { + encoder.setPosition(parameter); + modeSelect = 1; + digitalWrite(3, LOW); + digitalWrite(4, HIGH); + digitalWrite(5, LOW); + } + else if (modeSelect == 1) { + encoder.setPosition(BRIGHTNESS); + modeSelect = 2; + digitalWrite(3, LOW); + digitalWrite(4, LOW); + digitalWrite(5, HIGH); + } + last_interrupt_time = interrupt_time; } - if (debug <= 3) { - Serial.print("Blinkmode:\t"); - Serial.println(mode); - } - } void loop() { + encoder.tick(); + + int newPos = encoder.getPosition(); + if (encoderPosition != newPos) { + encoderPosition = newPos; + if (debug <= 3) { + Serial.print("Position:\t"); + Serial.print(encoderPosition); + Serial.print("\t"); + Serial.print("modeSelect:\t"); + Serial.print(modeSelect); + Serial.print("\t"); + } + if (modeSelect == 0) { + mode = encoderPosition % NUM_MODES; + } + else if (modeSelect == 1){ + parameter = encoderPosition % 255; + } + else if (modeSelect == 2){ + BRIGHTNESS = encoderPosition % 32; + } + if (debug <= 3) { + Serial.print("Blinkmode:\t"); + Serial.print(mode); + Serial.print("\t"); + Serial.print("Parameter:\t"); + Serial.print(parameter); + Serial.print("\t"); + Serial.print("Brightness:\t"); + Serial.println(BRIGHTNESS); + } + } + + + if (mode == 0) { // === Read acceleromter data === // float accCombined = calculateOrientationData(); @@ -311,23 +378,32 @@ void loop() { drawFire(); } else if (mode == 3) { - drawPride(); + drawPride(parameter); } else if (mode == 4) { - drawGlitter(); + drawGlitter(parameter); } - else if (mode == 5) { - drawPulse(); - } - else if (mode == 6) { - drawKunsisLila(); + else if ((mode == 5) || (mode ==6)) { + drawPulse(parameter); } else if (mode == 7) { bounceBalls(); } - else if (mode == 8) { + else { drawOff(); } + FastLED.setBrightness( BRIGHTNESS * 8 ); + if (mode != 2){ + for (uint8_t i=0; i