HI guys,
I am working in Crossfade LEDs via PWM ( RGB LEDS)
stage one making crossfade worked,
now I am adding a PIR sensor ( motion sensor from parallax)
but I can not make the crossfading start when motion is detected
Hope that you guys can helpme out.
here is the code.
is there a code structure issue or syntax.
thank you in advance.
/*
* Code for cross-fading 3 LEDs, red, green and blue, or one tri-color LED, using PWM
* The program cross-fades slowly from red to green, green to blue, and blue to red
* The debugging code assumes Arduino 0004, as it uses the new Serial.begin()-style functions
* originally "dimmingLEDs" by Clay Shirky <clay.shirky@nyu.edu>
*/
// Output
int redPin = 9; // Red LED, connected to digital pin 9
int greenPin = 10; // Green LED, connected to digital pin 10
int bluePin = 11; // Blue LED, connected to digital pin 11
// Program variables
int redVal = 255; // Variables to store the values to send to the pins
int greenVal = 1; // Initial values are Red full, Green and Blue off
int blueVal = 1;
int i = 0; // Loop counter
int wait = 15; // 50ms (.05 second) delay; shorten for faster fades
int DEBUG = 0; // DEBUG counter; if set to 1, will write values back via serial
void setup()
{
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
// Main program
void loop()
{
i += 1; // Increment counter
if (i < 255) // First phase of fades
{
redVal -= 1; // Red down
greenVal += 1; // Green up
blueVal = 1; // Blue low
}
else if (i < 509) // Second phase of fades
{
redVal = 1; // Red low
greenVal -= 1; // Green down
blueVal += 1; // Blue up
}
else if (i < 763) // Third phase of fades
{
redVal += 1; // Red up
greenVal = 1; // Green lo2
blueVal -= 1; // Blue down
}
else // Re-set the counter, and start the fades again
{
i = 1;
}
// we do "255-redVal" instead of just "redVal" because the
// LEDs are hooked up to +5V instead of Gnd
analogWrite(redPin, 255 - redVal); // Write current values to LED pins
analogWrite(greenPin, 255 - greenVal);
analogWrite(bluePin, 255 - blueVal);
delay(wait); // Pause for 'wait' milliseconds before resuming the loop
}
thanks you AWOL
I am looking for help in adding the code.
the example code from arduino site is to control a single LED via digital pin.
how ever making the pwm on is making my head spin.
thank you in advance
Just a helpful pointer, on the Arduino forums "bumping" messages generally isn't considered polite, particularly when it's only a few hours since it was posted.
Many people will only check the forums once every day or so and then catch up on all the messages they've missed. Bumping messages tends to just bug the people who have already seen your message and been unable to help.
Hope you get a solution to your problem soon, thanks for being part of the Arduino community.
Ok.
I been reasearchng almost all day in how to make it this work,
I mix 2 codes ( credits to their creators)
the pir sensor gets motion detected but the LEDs wont stop when motion is not detected.
here is the mashup. any help is verywelcome
/*
* //////////////////////////////////////////////////
* //making sense of the Parallax PIR sensor's output
* //////////////////////////////////////////////////
*
* Switches a LED according to the state of the sensors output pin.
* Determines the beginning and end of continuous motion sequences.
*
* @author: Kristian Gohlke / krigo(_)web.de / http://filformat.net
* @date: 3. September 2006
*
* kr1 (cleft) 2006
* released under a creative commons "Attribution-NonCommercial-ShareAlike 2.0" license
* http://creativecommons.org/licenses/by-nc-sa/2.0/de/
*
*
* The Parallax PIR Sensor is an easy to use digital infrared motion sensor module.
* (http://www.parallax.com/detail.asp?product_id=555-28027)
*
* The sensor´s output pin goes to HIGH if motion is present.
* However, even if motion is present it goes to LOW from time to time,
* which might give the impression no motion is present.
* This program deals with this issue by ignoring LOW-phases shorter than a given time,
* assuming continuous motion is present during these phases.
*
*/
///// LED fading
float r,g,b,wr,wg,wb ;
// Select which PWM-capable pins are to be used.
int redPin = 11;
int greenPin = 10;
int bluePin = 9;
// A function set an float RGB Value active-low or common-anode ( plus) output.
//
void fsetRGB(float r, float g, float b)
{
analogWrite(redPin, int(r*255.0));
analogWrite(greenPin, int(g*255.0));
analogWrite(bluePin, int(b*255.0));
}
// Set up our outputs, and give full high values so the
// LEDs start off. Then fade in the blue LED.
//
/////////////////////////////
//VARS
int calibrationTime = 10; //the time we give the sensor to calibrate itself (10-60 secs according to the datasheet)
long unsigned int lowIn; //the time when the sensor outputs a low impulse
long unsigned int pause = 5000; //the amount of milliseconds the sensor has to be low before we assume all motion has stopped
boolean lockLow = true;
boolean takeLowTime;
int pirPin = 3; //the digital pin connected to the PIR sensor's output
int ledPin = 13;
/////////////////////////////
//SETUP
void setup(){
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(pirPin, LOW);
//give the sensor some time to calibrate
Serial.print("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(50);
}
////////////////////////////
//LOOP
void loop(){
if(digitalRead(pirPin) == HIGH){
wr = wr + random(10)/3000.0;
wg = wg + random(12)/3000.0;
wb = wb + random(8)/3000.0;
r=(sin(wr)+1.0)/2.0;
g=(sin(wg)+2.0)/2.0;
b=(sin(wb)+1.0)/2.0;
fsetRGB(r,g,b);
//delay(5);
digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
if(lockLow){
lockLow = false; //make sure we wait for a transition to LOW before any further output is made
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
}
if(digitalRead(pirPin) == LOW){
digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state
if(takeLowTime){
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
analogWrite(redPin,LOW);
analogWrite(greenPin,LOW);
analogWrite(bluePin,LOW);
}
if(!lockLow && millis() - lowIn > pause){ //if the sensor is low for more than the given pause, we assume that no more motion is going to happen
lockLow = true; //makes sure this block of code is only executed again after a new motion sequence has been detected
Serial.print("motion ended at "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
delay(50);
}
}
}
I think that I got it working ;D
I will deeply tested .
so far is working as I want.
I am adding the code , perhaps some one alse is looking for the same solution , here is ( several days or research )
( just clean the serial.print if you want)
// Kansas City Robotics Society - Sensor of the Month - January 2008
// This example is using the PIR Sensor to detect Motion.
// More information on this sensor can be found at: www.Parallax.com/RS
//Author: Vince Thompson - www.SomeoneKnows.com/VThompson
float r,g,b,wr,wg,wb ;
// Select which PWM-capable pins are to be used.
int redPin = 11;
int greenPin = 10;
int bluePin = 9;
// A function set an float RGB Value active-low or common-anode ( plus) output.
//
void fsetRGB(float r, float g, float b)
{
analogWrite(redPin, int(r*255.0));
analogWrite(greenPin, int(g*255.0));
analogWrite(bluePin, int(b*255.0));
}
// Global variables.
boolean isMotionDetected = false;
int pin_PIR = 3; //Connect PIR sensor to pin 3
int pin_LED = 13; //Use LED on pin 13
int waitTime = 5000; //Time to wait is 5 seconds
unsigned long PIR_ResetTime = 0;
unsigned long timeNow;
unsigned long TimeMotionStarted;
void setup() //This part of the program is used to set up features used in the program
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(pin_PIR, INPUT);
digitalWrite(pin_PIR, HIGH); //See PinMode documentation - turn on pullup resistors
pinMode(pin_LED, OUTPUT);
Serial.begin(9600);
//Perform sensor calibration by waiting
Serial.print("PIR Sensor Calibrating");
for(int i=0; i<10; i++) //Repeat 30 times, wait 1 second each time.
{
Serial.print(".");
delay(1000); //One second wait time (1000 milliseconds = 1 second)
}
Serial.println("PIR Sensor Ready");
}
void loop() //Main Arduino Loop
{
timeNow = millis(); //Get Arduino's current time.
if(digitalRead(pin_PIR) == HIGH) //Is the PIR Sensor detecting motion?
{
//YES, motion is detected.
isMotionDetected = true;
wr = wr + random(10)/3000.0;
wg = wg + random(12)/3000.0;
wb = wb + random(8)/3000.0;
r=(sin(wr)+1.0)/2.0;
g=(sin(wg)+2.0)/2.0;
b=(sin(wb)+1.0)/2.0;
fsetRGB(r,g,b);
delay(5);
if(timeNow > PIR_ResetTime)
{
//It is time to recognize a new Motion Event.
TimeMotionStarted = timeNow;
Serial.println("");
Serial.print("Motion Detected at ");
Serial.print(timeNow/1000);
Serial.println(" Seconds Since Startup...");
digitalWrite(pin_LED, HIGH); //Turn on LED
PIR_ResetTime = timeNow + waitTime; //Time to wait before resetting PIR sensor
}
else
{
//Bump time up to reset for another interval. This is
//like hitting the snooze button on an alarm clock
PIR_ResetTime = timeNow + waitTime;
}
}
else
{
//NO motion is not detected.
if(timeNow > PIR_ResetTime) //Check to see if it has been long enough to say the motion has stopped?
{
if(isMotionDetected)
{
analogWrite(redPin,LOW);
analogWrite(bluePin,LOW);
analogWrite(greenPin,LOW);
Serial.print("...Movement Period Lasted ");
Serial.print((timeNow - TimeMotionStarted)/1000);
Serial.println(" Seconds.");
digitalWrite(pin_LED, LOW); //Turn off LED
isMotionDetected = false; //Stops repeating this message while waiting for a new event.
}
}
}
}
I'm not trying to be a smart-arse here so I really mean it when I say, "Don't you feel a lot better when you figure out how to do stuff with just a little guidance?"
I know a lot of people assume someone must have done this already so it must be easy for those more knowledgeable to share what they know. No answers to a post is not usually because people are holding out on you...
The truth seems to be that newcomers are not always aware that programming is about stringing together a lot of little things that "work" to make what you want.
If someone asks a simple question like "how do I sense motion using the Arduino and the parallax sensor?", you are more likely to have someone share a working snippet... or you can even "Google" and get the snippet you need on your own.
I agree; way better to coax and lead the questioner to an answer (note, not the answer) by making them think about the whole problem, than to spoon-feed them a solution.
What's to learn there?
(please note, just because it says "newbie" over there <---
that only means I'm new to the Arduino)
I gree.
I like to make things and learn, and later make another thing re-learn.
how ever, I really think that being a clear guidence is always a good habbit for share of ideas. if you care about the open source you will care about new members, other wise ....
a section for newbies is a great tool, video tutorial on a single place makes learning faster. like kineme.net ( quartz composer )
I love arduino , It leads the way to major development in computing.