From 2fe1ce813fa1c2ea556dbb43d5b8af1b04e6efd4 Mon Sep 17 00:00:00 2001 From: Akhil Raj Baranwal Date: Sat, 26 May 2018 11:14:51 +0530 Subject: [PATCH] added a few optimizations --- README.md | 5 +- code/LED.h | 229 +++++++++++++++++++++++++++++++++----------------- code/code.ino | 174 +++++++++++++++++++++++++++++--------- 3 files changed, 293 insertions(+), 115 deletions(-) diff --git a/README.md b/README.md index 22db950..1365b2d 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ An extremely aesthetic add-on to your workspace. ## Hardware -* 3 x NPN MOSFETs dedicated for each colour +* ATMega328 * HC-05 Bluetooth Module -* ATMega328 \ No newline at end of file +* 3 x NPN MOSFETs dedicated for each colour +* LM324 (Second Order Active Filters (LowPass, BandPass, HighPass)) diff --git a/code/LED.h b/code/LED.h index 9e3b395..444f066 100644 --- a/code/LED.h +++ b/code/LED.h @@ -1,31 +1,14 @@ -enum positions {TREBLE, MID, BASS, USER, LIGHT, PULSE, FLASH, TOTAL}; +#include "definitions.h" class LED { private: - uint8_t _pin, _brightness[8] = {0,0,0,0,0,0,0,0}, activeChannels = 0; + uint8_t _pin, _brightness[8] = {0,0,0,0,0,0,0,0}, activeChannels = 0, _maxBrightness = 255, _minBrightness = 0; uint16_t _incrementStepTime = 0, _decrementStepTime = 0; uint16_t _onTime = 0, _offTime = 0; unsigned long _prevUpdate = 0; bool _state; - byte control = 0b00000000; - - uint8_t calculateBrightness() - { - float totalBrightness = 0; - // weighted values of all the individual brightness values make the total - // a future update may have provisions for absolute value, but it isn't as aesthetic - - // weight = 255/activeChannels; - - for (int i = 0; i < 7; i++) - { - if(control & (1<>2; + if(value > _brightness[LIGHT]) + { + digitalWrite(_pin, LOW); + return; + } + } + // flash if(control & (1< _onTime) + // here, state is used to check whether the LED is activated or not + if((_state) && (millis() - _prevUpdate) > _onTime/100) { - _brightness[FLASH] = 0; + _brightness[FLASH] = _minBrightness; _state = false; _prevUpdate = millis(); } - if((!_state) && (millis() - _prevUpdate) > _offTime) + if((!_state) && (millis() - _prevUpdate) > _offTime/5) { - _brightness[FLASH] = 255; + _brightness[FLASH] = _maxBrightness; _state = true; _prevUpdate = millis(); } @@ -226,11 +265,11 @@ class LED //pulse if(control & (1< _incrementStepTime) { - if(_brightness[PULSE] == 255) + if(_brightness[PULSE] == _maxBrightness) _state = true; else _brightness[PULSE] += 1; @@ -238,7 +277,7 @@ class LED } if((_state) && (millis() - _prevUpdate) > _decrementStepTime) { - if(_brightness[PULSE] == 0) + if(_brightness[PULSE] == _minBrightness) _state = false; else _brightness[PULSE] -= 1; @@ -247,7 +286,47 @@ class LED } - _brightness[TOTAL] = calculateBrightness(); - analogWrite(_pin, _brightness[TOTAL]); + // bass routing + if(control & (1<>2; + } + + // mid routing + if(control & (1<>2; + } + + // treble routing + if(control & (1<>2; + } + + float totalBrightness = 0; + // weighted values of all the individual brightness values make the total. + // a future update may have provisions for absolute value or custom weights. + // equal weighting is used for now: + // weight = 255/activeChannels; + + for (int i = 1; i < 7; i++) + { + if(control & (1< +#include #include "LED.h" -#define IND 13 +String red = "", green = "", blue = ""; +SoftwareSerial blt(8,7); -//uint8_t red = 0, green = 0, blue = 0; -//SoftwareSerial blt(8,7); -/* - -1 byte sent for each colour - -pos 7 6 5 4 3 2 1 0 -route 0 0 0 0 0 0 0 0 - F P L U B M T - -F : Flash -L : Watch External Light Levels -P : Pulse (Breather Mode) -U : User -B : Bass -M : Mid -T : Treble - -*/ - - -LED Red(5); -LED Green(3); -LED Blue(6); +LED Red(6); +LED Green(9); +LED Blue(10); void setup() { - //blt.begin(9600); + blt.begin(9600); Serial.begin(115200); pinMode(IND, OUTPUT); - digitalWrite(IND, HIGH); - Red.setTime(500,1000); - Green.setTime(1000,1500); - Blue.setTime(1500,2000); - Red.pulse(true); - Green.pulse(true); - Blue.pulse(true); - Red.flash(true); + + Red.setTime(5000,5000); + Green.setTime(5000,5000); + Blue.setTime(5000,5000); + + // Red.pulse(true); + // Red.watchExternalLight(true,127); + // Red.routeBass(true); + Red.routeUser(true); + // Red.setUserBrightness(255); + // Serial.println(Red.getActiveChannels()); + + // Green.pulse(true); + // Green.watchExternalLight(true,200); + // Green.routeMid(true); + Green.routeUser(true); + // Green.setUserBrightness(255); + // Serial.println(Green.getActiveChannels()); + + // Blue.pulse(true); + // Blue.watchExternalLight(true,127); + // Blue.routeTreble(true); + Blue.routeUser(true); + // Blue.setUserBrightness(255); + // Serial.println(Blue.getActiveChannels()); + } String rxBuff; @@ -52,17 +50,117 @@ void loop() if((1<<6) & redControl || (1<<5) & redControl) red.update(); */ - Serial.println(Red.getBrightness()); Red.update(); Green.update(); Blue.update(); - /*int i; - String rx = ""; + int i; + String rx = "", red = "", green = "", blue = ""; while(blt.available()) { char c = blt.read(); + /*Serial.print(c); + Serial.print("\t"); + Serial.print(char(c)); + Serial.print("\t"); + Serial.println(uint8_t(c));*/ rx += c; + delay(2); + } + if(rx != "") + { + Serial.println(rx); + uint8_t n = rx.length(); + for(uint8_t i = 0; i < n; i++) + { + if(rx.charAt(i) == 'R') + { + for(uint8_t j = i+1; j < n; j++) + { + if(isDigit(rx.charAt(j))) + red += rx.charAt(j); + else + break; + } + } + else if(rx.charAt(i) == 'G') + { + for(uint8_t j = i+1; j < n; j++) + { + if(isDigit(rx.charAt(j))) + green += rx.charAt(j); + else + break; + } + } + else if(rx.charAt(i) == 'B') + { + for(uint8_t j = i+1; j < n; j++) + { + if(isDigit(rx.charAt(j))) + blue += rx.charAt(j); + else + break; + } + } + } + n = rx.indexOf('~'); + if(rx.charAt(n+1) == 'F') + Red.flash(true); + else if(rx.charAt(n+1) == 'P') + Red.pulse(true); + else + { + Red.flash(false); + Red.pulse(false); + } + if(rx.charAt(n+2) == 'F') + Green.flash(true); + else if(rx.charAt(n+2) == 'P') + Green.pulse(true); + else + { + Green.flash(false); + Green.pulse(false); + } + if(rx.charAt(n+3) == 'F') + Blue.flash(true); + else if(rx.charAt(n+3) == 'P') + Blue.pulse(true); + else + { + Blue.flash(false); + Blue.pulse(false); + } + Serial.print(red); + Serial.print("\t"); + Serial.print(green); + Serial.print("\t"); + Serial.println(blue); + if(!red.toInt()) + Red.routeUser(false); + else + { + Red.routeUser(true); + Red.setUserBrightness(red.toInt()); + } + if(!green.toInt()) + Green.routeUser(false); + else + { + Green.routeUser(true); + Green.setUserBrightness(green.toInt()); + } + if(!blue.toInt()) + Blue.routeUser(false); + else + { + Blue.routeUser(true); + Blue.setUserBrightness(blue.toInt()); + } + blt.write("#1~"); } + + /* if(rx.length() == 1) rxBuff = rx; if(rx != "")