Hello all,
newb here, (go easy me!)
So... i've been trying to combine these two codes into one, and so far i've hit wall after wall.... I honestly don't know what i'm doing, and everytime i sit down with it, a few hours into it and my brain turns to mush.....
the setup-
i'm making an arcade cabinet, and thought to add some led lighting for flair. One RGB strip (split into two) for the back and console, and one white LED strip to light the marquee. My intent is to use a momentary switch to cycle through different modes:
-motion sensor mode (for after hours, while the machine is off)
-stable on mode (possibly with color cycle as well?)
-music reactive mode (for when i jukebox the thing while i work)
-and off mode (honestly, the least necessary, but if we're throwing logs on the fire....).
so far-
i got two codes from some youtube vids and have managed to tweak them to 'pretty close'.
the first is a motion sensor LED sketch. his code triggers three led strips, plus a fourth under his kitchen cabinets. i've taken the three and converted them to RGB pins of a single strip (split into two) and the last i kept as is, to function as a white led strip (for my marquee). He has a momentary button, that, when pressed, keeps the lights on, and when pressed again, turns the lights off (although if you're not out of the room in time, the sensor kicks in again.
the second, is a music reactive led sketch that (more or less) works for what i need it to do. I think i can tweek it a bit more, once i get how he devided the frequencies (but that's not important right now).
my thoughts-
i was thinking that a switch state could do it, but every time i try to combine them, it doesn't seem to work. I also tried adding the music reactive sketch into the motion sensor sketch, since that one had the bulk of what i needed. but then the music react doesn't loop... it just turns red and sits there. I figure if i "int" a 'hold button' to cycle through cases, i can "int" a 'press button' to cycle through colors within in a single case.
not sure if i'm making any sense, but i'm weeks into this mess, and am losing hope, and my mind. i spent a few hours with fritzing to give you an idea of my set up, and am uploading the code(s) that i have tried to combine. the one attempt that showed the most promise is now no longer even verrifying :-/ but i can post that too if needed.
... help?
(seriously, anything you can throw my way would be greatly apprieciated.)
cheers!
Component setup:
Motion sensor code (full code, as i didn't know what i could safely exclude):
//NeilFarr's Arduino project - make a number of kitchen LED strips fade in when an ultrasonic sensor or switch is triggered.
//contact neilsfarr@google.com
//define pins for the LEDs (using different ones for differnet strips to prevent MOSFET overloading and allow for more flexibility in the fade up sequence)
#define RLED 3 // red
#define GLED 5 // green
#define BLED 6 // blue
#define LIGHT_LED 9 // marquee
#define ONBOARD_LED 13 // so we can see what is supposed to be happening, just with the onboard LED.
#define SOUND_SENSOR A0 //microphone
#define SENSOR 4 // PIR module
#define BUTTON 7 // push to make switch
//overall brightness value
int stripbrightR = 0;
int stripbrightG = 0;
int stripbrightB = 0;
int lightbright = 0;
int ledmaxbrightness = 80; // this is the maximum PWM brightness for the LED strips (the under cabinet LED lights can be full 255 brightness, so we don't worry about those).
// note that I set this at 80 for a good lighting for my use - remember the lights don't follow a linear brightness so have a play to see what works
int fadeSpeed1 = 50;
int fadeSpeed2 = 10; //slightly faster fade out time
int offset = 15; //offset allows for one light to begin the glow after the other one
int totaltime = 255 + (offset * 4); // 255 is the maximum brightness value
bool stillactive = false;
bool switchactive = false;
bool switchpressed = false;
unsigned long previousMillis = 0; // will store last time LED was updated
unsigned long interval = 60000; // interval at which to wait in an ON state (milliseconds) - basically 1 minutes since last movement - change this temporarily for testing to 10000
unsigned long currentMillis = millis(); // defining the variable - it will get set to the current time when things need to start/stop
unsigned long switchpressedMillis = 0; //used to track debounce
unsigned long debouncetime = 250;
void setup() {
Serial.begin(9600);
//setup sensor and switch pins to input
pinMode (SENSOR, INPUT_PULLUP);
pinMode (BUTTON, INPUT);
//set up LED pins to output.
pinMode(RLED, OUTPUT);
pinMode(GLED, OUTPUT);
pinMode(BLED, OUTPUT);
pinMode(LIGHT_LED, OUTPUT);
//setup the onboard LED to show the same state
pinMode(ONBOARD_LED, OUTPUT);
//and set the lights to off
analogWrite(RLED, 0);
analogWrite(GLED, 0);
analogWrite(BLED, 0);
analogWrite(LIGHT_LED, 0);
analogWrite(ONBOARD_LED, 0);
}
void loop() {
//flash the light to show it in a ready state.
analogWrite(ONBOARD_LED, 255);
delay(250);
analogWrite(ONBOARD_LED, 0);
delay(250);
analogWrite(ONBOARD_LED, 255);
delay(250);
analogWrite(ONBOARD_LED, 0);
delay(250);
switchactive = false;
//wait for the sensor
do {
currentMillis = millis(); //twiddle thmbs, waiting for the sensor to trigger
} while (digitalRead(SENSOR) == LOW); // you could also wait for the button here but make sure to DEBOUNCE the input
//Once triggered, turn on
TurnOn();
previousMillis = millis();
stillactive = true;
Serial.println("Motion detected");
// and keep on for a few seconds unless triggered again
do {
currentMillis = millis();
if ((currentMillis - previousMillis >= interval) or (currentMillis < 5)) { // if the 'on' time has passeed OR the internal timer has reset to 0 (after c.50 days)
stillactive = false ;//QUIT the loop
}
if (switchactive == true) { //if sensor triggered
previousMillis = currentMillis; //reset the counter to allow constan movement to keep the lights on
Serial.println("switch mode enabled");
}
if (digitalRead(SENSOR) == HIGH) { //if sensor triggered
previousMillis = currentMillis; //reset the counter to allow constan movement to keep the lights on
Serial.println("sensor triggered");
}
if (digitalRead(BUTTON) == LOW) { // if switch pushed
//debounce the switch
switchpressedMillis = currentMillis;
switchpressed = true;
do {
if (digitalRead(BUTTON) == HIGH) {
switchpressed = false;
}
switchpressedMillis = millis();
} while (switchpressedMillis - currentMillis < debouncetime) ;
if (switchpressed == true) {
Serial.println("switch pressed");
if (switchactive == true) {
Serial.println("switch pressed to STOP the lights");
analogWrite(LIGHT_LED, 0);
delay(250); // remind you it has been set and allow you time to let go of the button
analogWrite(LIGHT_LED, 255);
delay(1000);
switchactive = false; // reset the variable
stillactive = false; // quit the loop
}
else {
switchactive = true; //set the variable to show the switch was activated
Serial.println("switch pressed to extend the lights");
analogWrite(LIGHT_LED, 0);
delay(250); // remind you it has been set and allow you time to let go of the button
analogWrite(LIGHT_LED, 255);
delay(250);
delay(500); // allow you time to let go of the button
}
}
}
} while (stillactive == true);
Serial.println("Time finished - turning off");
TurnOff();
}
void TurnOn() {
analogWrite(ONBOARD_LED, 255); // show the lights are supposed to be on
for (int i = 0; i < totaltime; i++) {
stripbrightR = constrain (i, 0, ledmaxbrightness); //maximum brightness set to a lower brightness - could be 255 but I prefer them a little dimmer - this also lengthens the life of the MOSFETs and LEDs
stripbrightB = constrain ((i - offset), 0, ledmaxbrightness); // start the next lights a little time after
lightbright = constrain ((i - (offset * 2)), 0, 180); // start the next lights a little time after - note that the max brightness is 255 for these as they are the undercounter ones
stripbrightG = constrain ((i - (offset * 3)), 0, ledmaxbrightness); // start the next lights a little time after
analogWrite(RLED, stripbrightR);
analogWrite(GLED, stripbrightG);
analogWrite(BLED, stripbrightB);
analogWrite(LIGHT_LED, lightbright);
delay(fadeSpeed1);
}
}
void TurnOff() {
analogWrite(ONBOARD_LED, 0); // show the lights are supposed to be off
for (int i = 255; i >= 0; --i) {
analogWrite(RLED, constrain (i, 0, ledmaxbrightness));
analogWrite(GLED, constrain (i, 0, ledmaxbrightness));
analogWrite(BLED, constrain (i, 0, ledmaxbrightness));
analogWrite(LIGHT_LED, i); // no constrain as these were set to 255 as they are the under counter lights.
delay(fadeSpeed2);
}
}
(hope i did that right)
music react code (minus defines):
float sensorValue = 0, filteredSignal = 0,
filteredSignalValues[] = {3.4, 3.1, 2.7, 2.4, 2.1, 1.7, 1.3, 0.9, 0.4};
void setup () {
Serial.begin (9600);
}
void loop () {
MainFunction();
}
void MainFunction() {
sensorValue = (float) analogRead(SOUND_SENSOR) * (5.0 / 1024.0);
FilterSignal(sensorValue);
Serial.print(sensorValue);
Serial.print(" ");
Serial.println(filteredSignal);
CompareSignalFiltered(filteredSignal);
}
void FilterSignal(float sensorSignal) {
filteredSignal = (0.945 * filteredSignal) + (0.0549 * sensorSignal);
}
void CompareSignalFiltered(float filteredSignal) {
if (filteredSignal > filteredSignalValues[0]) {
RGBColor(0, 0, 255);
Serial.println("Blue");
} else if (filteredSignal <= filteredSignalValues[0] && filteredSignal > filteredSignalValues[1]) {
Serial.println("Azure");
RGBColor(0, 255, 255);
} else if (filteredSignal <= filteredSignalValues[1] && filteredSignal > filteredSignalValues[2]) {
RGBColor(0, 127, 255);
Serial.println("Cyan");
} else if (filteredSignal <= filteredSignalValues[2] && filteredSignal > filteredSignalValues[3]) {
RGBColor(0, 255, 127);
Serial.println("Aqua marine");
} else if (filteredSignal <= filteredSignalValues[3] && filteredSignal > filteredSignalValues[4]) {
RGBColor(0, 255, 0);
Serial.println("Green");
} else if (filteredSignal <= filteredSignalValues[4] && filteredSignal > filteredSignalValues[5]) {
RGBColor(255, 255, 0);
Serial.println("Yellow");
} else if (filteredSignal <= filteredSignalValues[5] && filteredSignal > filteredSignalValues[6]) {
RGBColor(255, 0, 255);
Serial.println("Magenta");
} else if (filteredSignal <= filteredSignalValues[6] && filteredSignal > filteredSignalValues[7]) {
RGBColor(255, 0, 127);
Serial.println("Rose");
} else if (filteredSignal <= filteredSignalValues[7] && filteredSignal > filteredSignalValues[8]) {
RGBColor(255, 127, 0);
Serial.println("Orange");
} else if (filteredSignal <= filteredSignalValues[8]) {
RGBColor(255, 0, 0);
Serial.println("Red");
} else {
RGBColor(0, 0, 255);
Serial.println("Default: Blue");
}
}
void RGBColor(int Rcolor, int Gcolor, int Bcolor) {
analogWrite(BLED, Bcolor);
analogWrite(GLED, Gcolor);
analogWrite(RLED, Rcolor);
delay(delayLEDS);
}
p.s. if i've posted this incorrectly, please let me know... this is new to me too!