Okay, so the first one is the code I wrote to cycle two rgb led strips through a rainbow of colors. It could be totally amateur, but it works so Its fine by me. Anyways I want to add a motion sensor that will essentially mirror that of the second code that I got from a make project.
The code below controls the one led so that when you wave your hand over the sensor it turns on or off, as well as the ability to change brightness by raising or lowering your hand over the sensor. I have no problem redefining the pins and such to make it control the strips, but past that is where I'm in need of some help. What I'd like to do is to change the code to raise and lower the fade speed instead of brightness and to swap between each of the colors in the first code.
My train of thought so far is to set the strips to red then delay the length of the fadespeed then red again to make it blink.
//red
{analogWrite(GREENPIN, 0);
analogWrite(REDPIN, 255);
analogWrite(BLUEPIN, 0);
analogWrite(GREENPINX, 0);
analogWrite(REDPINX, 255);
analogWrite(BLUEPINX, 0);
delay(FADESPEED);}
//red
{analogWrite(GREENPIN, 0);
analogWrite(REDPIN, 255);
analogWrite(BLUEPIN, 0);
analogWrite(GREENPINX, 0);
analogWrite(REDPINX, 255);
analogWrite(BLUEPINX, 0);
delay(FADESPEED);}
Repeating that section of code for each of the different colors. So somehow I have to find out how to set the fadespeed delay based on the sensor as well as the switching of colors by the hand waving action. If this is possible I would seriously appreciate the help, but if I'm dreaming a little to big just let me know. Thanks.
#define REDPIN 5
#define GREENPIN 6
#define BLUEPIN 3
#define REDPINX 10
#define GREENPINX 11
#define BLUEPINX 9
#define FADESPEED 100
void setup() {
pinMode(REDPIN, OUTPUT);
pinMode(GREENPIN, OUTPUT);
pinMode(BLUEPIN, OUTPUT);
pinMode(REDPINX, OUTPUT);
pinMode(GREENPINX, OUTPUT);
pinMode(BLUEPINX, OUTPUT);
}
void loop() {
//red
{analogWrite(GREENPIN, 0);
analogWrite(REDPIN, 255);
analogWrite(BLUEPIN, 0);
analogWrite(GREENPINX, 0);
analogWrite(REDPINX, 255);
analogWrite(BLUEPINX, 0);
delay(FADESPEED);}
//orange
{analogWrite(GREENPIN, 10);
analogWrite(REDPIN, 255);
analogWrite(BLUEPIN, 0);
analogWrite(GREENPINX, 10);
analogWrite(REDPINX, 255);
analogWrite(BLUEPINX, 0);
delay(FADESPEED);}
//yellow
{analogWrite(GREENPIN, 100);
analogWrite(REDPIN, 255);
analogWrite(BLUEPIN, 0);
analogWrite(GREENPINX, 100);
analogWrite(REDPINX, 255);
analogWrite(BLUEPINX, 0);
delay(FADESPEED);}
//green
{analogWrite(GREENPIN, 255);
analogWrite(REDPIN, 0);
analogWrite(BLUEPIN, 0);
analogWrite(GREENPINX, 255);
analogWrite(REDPINX, 0);
analogWrite(BLUEPINX, 0);
delay(FADESPEED);}
//aqua
{analogWrite(GREENPIN, 255);
analogWrite(REDPIN, 0);
analogWrite(BLUEPIN, 255);
analogWrite(GREENPINX, 255);
analogWrite(REDPINX, 0);
analogWrite(BLUEPINX, 255);
delay(FADESPEED);}
//blue
{analogWrite(GREENPIN, 0);
analogWrite(REDPIN, 0);
analogWrite(BLUEPIN, 255);
analogWrite(GREENPINX, 0);
analogWrite(REDPINX, 0);
analogWrite(BLUEPINX, 255);
delay(FADESPEED);}
//purple
{analogWrite(GREENPIN, 0);
analogWrite(REDPIN, 100);
analogWrite(BLUEPIN, 255);
analogWrite(GREENPINX, 0);
analogWrite(REDPINX, 100);
analogWrite(BLUEPINX, 255);
delay(FADESPEED);}
//pink
{analogWrite(GREENPIN, 0);
analogWrite(REDPIN, 255);
analogWrite(BLUEPIN, 255);
analogWrite(GREENPINX, 0);
analogWrite(REDPINX, 255);
analogWrite(BLUEPINX, 255);
delay(FADESPEED);}
}
// Luminch One - Copyright 2012 by Francisco Castro <http://fran.cc>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#define LED_PIN 3
#define SENSOR_PIN A0
#define SENSE_THRESHOLD 150
#define TRACK_THRESHOLD 230
#define DEBOUNCE_CYCLES 30
#define START_TRACKING_CYCLES 180
#define END_TRACKING_CYCLES 210
#define HAND_MINIMUM_CHANGE 15
int adc_input = 0;
int pwm_output = 0;
int stored_bright = 0xFF;
int target_bright = 0;
int hand_tracked_bright = 0;
boolean lamp_lighted = false;
boolean hand_tracking = false;
unsigned char hand_cycles = 0;
unsigned char debounce_cycles = 0;
int sample_1 = 0;
int sample_2 = 0;
int sample_3 = 0;
int sample_4 = 0;
void setup() {
analogWrite(LED_PIN, 0);
}
void loop() {
sample_1 = analogRead(SENSOR_PIN); delay(1);
sample_2 = analogRead(SENSOR_PIN); delay(1);
sample_3 = analogRead(SENSOR_PIN); delay(1);
sample_4 = analogRead(SENSOR_PIN); delay(1);
adc_input = (sample_1 + sample_2 + sample_3 + sample_4) >> 2;
if(debounce_cycles)
debounce_cycles -= 1;
else {
if(hand_tracking) {
if(adc_input > SENSE_THRESHOLD) {
if(adc_input > TRACK_THRESHOLD + 0xFF)
hand_tracked_bright = 0;
else if(adc_input < TRACK_THRESHOLD)
hand_tracked_bright = 0xff;
else
hand_tracked_bright = 0xFF - (adc_input - TRACK_THRESHOLD);
if(abs(target_bright - hand_tracked_bright) > HAND_MINIMUM_CHANGE || !lamp_lighted) {
target_bright = (target_bright + (hand_tracked_bright > 8 ? hand_tracked_bright : 8)) >> 1;
lamp_lighted = true;
}
hand_cycles = 0;
}
else {
target_bright = pwm_output;
stored_bright = pwm_output;
hand_cycles += 1;
if(hand_cycles == END_TRACKING_CYCLES) {
hand_tracking = false;
hand_cycles = 0;
}
}
}
else {
if(adc_input > SENSE_THRESHOLD) {
hand_cycles += 1;
if(hand_cycles == START_TRACKING_CYCLES) {
hand_tracking = true;
hand_cycles = 0;
}
}
else {
if(hand_cycles) {
lamp_lighted = !lamp_lighted;
target_bright = lamp_lighted ? stored_bright : 0;
debounce_cycles = DEBOUNCE_CYCLES;
}
hand_cycles = 0;
}
}
}
if(pwm_output != target_bright) {
if(pwm_output > target_bright && pwm_output > 0) --pwm_output;
if(pwm_output < target_bright && pwm_output < 0xFF) ++pwm_output;
analogWrite(LED_PIN, pwm_output);
delay((0xFF - abs(target_bright - pwm_output)) >> 5);
}
}