Author Topic: Triggering a sketch to run when motion is sensed by a PIR sensor - programming??  (Read 513 times)

jaustin

  • Newbie
  • *
  • Posts: 9
I have a sketch that is fading an led up from zero to 255, holding there for 15 seconds, then fading back down to zero. Now I need to add code to the sketch that will trigger the fading sequence when motion is sensed by a PIR sensor with the signal attached to pin#2. The led is currently attached to pin#1. Testing is on a breadboard. I'm assuming I need an IF statement to test the state of pin#2, which is no problem, but when I add the ELSE statement at the end, after the fading sequence, I get an error message trying to compile..... it tells me there is no preceeding 'if'  any help?  :o

benalb

  • Newbie
  • *
  • Posts: 3
Post your code, so we had not to guess.  ;)

jaustin

  • Newbie
  • *
  • Posts: 9
 the actual 'fading' up or down in my sketch is working just like I want it to. It is the triggering by the PIR sensor and the timing that I am trying to control. I currently have a 30 second delay after the } else { and want it loop back to the beginning, not continue to the end of the sketch. Maybe that is not possible with this setup.
Using my test code below and connecting 3.3v to pin2, it goes straight to 'HOLD', which lights the led fully, then it fades out slowly, then loops directly back to 'HOLD'.
'START' & 'FADE IN' are completely ignored.
When I run it without connecting 3.3v to pin2, it goes directly to } else {, then goes to 'FADE OUT' and fades slowly, then loops directly back to } else {.
The functions that run are running exactly as I would expect. It is controlling the flow of the sketch itself that I am having trouble with.

Code: [Select]
/*
 updown_fade

 This example shows how to fade an LED in/out using the analogWrite() function.

 The circuit:
 * LED (+) attached to pin 1. (3.3v w/ resistor)
 * motion sensor 'signal' will be attached to pin 2 (for now I am just attaching and detatching a 3.3v lead to pin2)
 * digispark microcontroller on breadboard (prototyping)

 Created 7/15/2017
 By John A. Austin
 




 */
int sensorPin = 2;  //motion sensor out pin connected to digital pin 2
int ledPin = 1;    // LED connected to digital pin 1
int fadeValue = 0; //initial led brightness (variable)

void setup() {
// initialize the sensorPin as an input:
  pinMode(sensorPin, INPUT);
// initialize the ledPin as an output:
  pinMode(ledPin, OUTPUT);
}
void loop() {
//START
    for (int fadeValue = 0 ; fadeValue <= 255 ; fadeValue += 1); {
    if(digitalRead(sensorPin)==HIGH) {
      // sets the value (range from 0 to 255):
//FADE IN 
// fade in from off to max in increments of 1 point:   
    analogWrite(ledPin, fadeValue);
    // wait for .235 seconds then loop
    delay(235);
} else {
      analogWrite(ledPin, 50);
      delay(2000);
      analogWrite(ledPin, 0);
      delay(2000);
      analogWrite(ledPin, 50);
      delay(2000);
      analogWrite(ledPin, 0);
      delay(2000);
      analogWrite(ledPin, 50);
      delay(2000);
      analogWrite(ledPin, 0);
      delay(2000);  }
     
      //attachInterrupt(0, myInterruptFunction, CHANGE)
{     
//HOLD
//hold led at max brightness for 10 seconds
  analogWrite(ledPin, 255);
  delay(10000); }
//FADE OUT
// fade out from max to off in increments of 5 points:
 for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
     // wait for .25 seconds then loop
    delay(588);}
    }}

jaustin

  • Newbie
  • *
  • Posts: 9
I may need to add a pin interrupt so I had pasted the //attachInterrupt(0, myInterruptFunction, CHANGE) text into the sketch but commented it out to use later if needed.

exeng

  • Sr. Member
  • ****
  • Posts: 442
If I understand your current code correctly, your HOLD and FADE OUT code is executed inside the first "for" statement inside loop(). So of course it's going to run the HOLD FADE OUT code every time regardless of the state of the sensor pin.

If the sensor pin is HIGH, for each analog write to fade in, you'll execute the HOLD and FADE OUT code also. Same when the sensor pin is low. You'll see your blinky thing on the LED then the HOLD and FADE OUT code executed.

You need to restructure your code get the behavior you want. Also, just a suggestion... try to adopt some style with proper indents and placement of brackets to make your code easier to read.
For most new code I will typically write "puesdo" code to get the flow and logic correct before writing actual code.  There are probably many ways to code this but it sounds like you want to change the direction of the fade depending upon the state of the sensor pin and only hold it for 10 seconds when the max value is reached. Do you want it of fade up only when the sensor is HIGH or do you want the sensor pin to be a "trigger" so that when HIGH, the fade up completes 0 to 255?


« Last Edit: July 17, 2017, 05:27:55 pm by exeng »

PeterF

  • Hero Member
  • *****
  • Posts: 804
I think you did more than just add/comment that line. The code as you posted it compiles without any changes! :-P

Now, after choosing 'Tools -> Auto Format' in order to get some indenting to show what is happening... I see two issues. You have your test for whether to do the led fade INSIDE the loop that controls the fade. You also had a '{' and '}' doing absolutely nothing around the middle.

See if this works.

Code: [Select]
/*
  updown_fade

  This example shows how to fade an LED in/out using the analogWrite() function.

  The circuit:
   LED (+) attached to pin 1. (3.3v w/ resistor)
   motion sensor 'signal' will be attached to pin 2 (for now I am just attaching and detatching a 3.3v lead to pin2)
   digispark microcontroller on breadboard (prototyping)

  Created 7/15/2017
  By John A. Austin
*/
int sensorPin = 2;  //motion sensor out pin connected to digital pin 2
int ledPin = 1;    // LED connected to digital pin 1
int fadeValue = 0; //initial led brightness (variable)

void setup() {
  // initialize the sensorPin as an input:
  pinMode(sensorPin, INPUT);
  // initialize the ledPin as an output:
  pinMode(ledPin, OUTPUT);
}
void loop() {
  //START
  if (digitalRead(sensorPin) == HIGH) {
    // sets the value (range from 0 to 255):
    //FADE IN
    // fade in from off to max in increments of 1 point:
    for (int fadeValue = 0 ; fadeValue <= 255 ; fadeValue += 1);
    { analogWrite(ledPin, fadeValue);
      // wait for .235 seconds then loop
      delay(235);
    }

    //HOLD
    //hold led at max brightness for 10 seconds
    analogWrite(ledPin, 255);
    delay(10000);

    //FADE OUT
    // fade out from max to off in increments of 5 points:
    for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
      // sets the value (range from 0 to 255):
      analogWrite(ledPin, fadeValue);
      // wait for .25 seconds then loop
      delay(588);
    }
  }  else  {
    analogWrite(ledPin, 50);
    delay(2000);
    analogWrite(ledPin, 0);
    delay(2000);
    analogWrite(ledPin, 50);
    delay(2000);
    analogWrite(ledPin, 0);
    delay(2000);
    analogWrite(ledPin, 50);
    delay(2000);
    analogWrite(ledPin, 0);
    delay(2000);
  }
}

jaustin

  • Newbie
  • *
  • Posts: 9
Thanks guys for all the great input. I cleaned up the code and my sketch is working perfectly now. I appreciate the help. ;D

kennard42

  • Newbie
  • *
  • Posts: 3
Can you post the cleaned up code?