where would the reverse code be written in this code?

Hi,

I am making an installation that gets activated via PIR sensor, which starts a 12v DC motor, rotating up for 3 seconds (pulling around 1kg weight), pauses for 5 seconds to make sure no motion is going on, and then reverses for 3 seconds

I have sourced a part of code from the library and tried placing the code for forward motion (upwards), but I am a bit lost as to where the reverse part goes in.

Can anyone help/guide me for this? It any tips, guidance, encouragement is much appreciated.

Thanks in advance.

* Switches a LED according to the state of the sensors output pin.
* Determines the beginning and end of continuous motion sequences.
*
* @author: Kristian Gohlke / krigoo (_) gmail (_) com / http://krx.at
* @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.
* 
*/

/////////////////////////////
//VARS
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 15;       

//the time when the sensor outputs a low impulse
long unsigned int lowIn;         

//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000; 

boolean lockLow = true;
boolean takeLowTime; 

int pirPin = 3;               //the digital pin connected to the PIR sensor's output
int ledPin = 13;              // digital pin for LED
int in1Pin = 10;           // rotates the motor forward
int in2Pin = 11;           // rotates the motor backward

/////////////////////////////
//SETUP

void setup(){
  Serial.begin(9600);
  pinMode(pirPin, INPUT);     //  declare sensor as input
  pinMode(ledPin, OUTPUT);    //  declare LED as output
  pinMode (in1Pin, OUTPUT);  // declares pin as output
  pinMode (in2Pin, OUTPUT);  // declares pin as 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(3000);
      }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");
    delay(50);
  }

////////////////////////////
//LOOP
void loop(){

     if(digitalRead(pirPin) == HIGH){
       digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state
       digitalWrite(in1Pin, HIGH);   // Motor turns forward
       delay(3000);                 // motor runs for 3 seconds
       digitalWrite(in2Pin, LOW);   
       
       if(lockLow){ 
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;           
         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
       // digitalWrite

       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
        }
       //if the sensor is low for more than the given pause,
       //we assume that no more motion is going to happen
       if(!lockLow && millis() - lowIn > pause){ 
           //makes sure this block of code is only executed again after
           //a new motion sequence has been detected
           lockLow = true;                       
           Serial.print("motion ended at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");
           delay(50);

The key is to read through your loop code and pretend you are the processor. imagine doing each of the things as you come to them. When it is time to reverse the motor, that’s where you put it. There’s nothing magic about where code goes. You put it all in there in the order you want it executed.

Lol, nope, don't think it's magic either, but the last few lines are a total toss over my head.

Anyhow, I figured out the reverse part, thanks, got another issue now.

  1. I need the motor to stop till the sensor says "motion ended". At the moment the motor keeps going backward and forward continuously in a loop even when the PIR is configured for continuous motion. How do I do that?

  2. I am getting the potentiometer code wrong somewhere, need to reduce the speed.

got another issue now.

Would that be issue 1 or issue 2?

Your code needs to be posted using something a bit larger than 0 point font.

/*
* //////////////////////////////////////////////////
* //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 / krigoo (_) gmail (_) com / http://krx.at
* @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.
* 
*/

/////////////////////////////
//VARS
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 15;       

//the time when the sensor outputs a low impulse
long unsigned int lowIn;         

//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000; 

boolean lockLow = true;
boolean takeLowTime; 

const int PotPin1  = A0; // Potentiometer1
const int analogOutPin1 = 10 && 9;   // the analog out for Pot 

int pirPin = 3;               //the digital pin connected to the PIR sensor's output
int ledPin = 13;              // digital pin for LED
int in1Pin = 10;           // rotates the motor forward
int in2Pin = 9;           // rotates the motor backward


/////////////////////////////
//SETUP

void setup(){
  Serial.begin(9600);
  pinMode(pirPin, INPUT);     //  declare sensor as input
  pinMode(ledPin, OUTPUT);    //  declare LED as output
  pinMode (in1Pin, OUTPUT);  // declares pin as output
  pinMode (in2Pin, OUTPUT);  // declares pin as output
  digitalWrite(pirPin, LOW);

 int speed = analogRead(PotPin1); // 4;


int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255);
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);

  // print the results to the serial monitor:
  Serial.print("sensor = " );
  Serial.print(sensorValue);
  Serial.print("\t output = ");
  Serial.println(outputValue);

  // wait 2 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(2);


 
  //give the sensor some time to calibrate
  Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++){
      Serial.print(".");
      delay(3000);
      }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");
    delay(500);
  }

////////////////////////////
//LOOP
void loop(){

     if(digitalRead(pirPin) == HIGH){
       digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state
       analogWrite (analogOutPin1, 30);
       digitalWrite(in1Pin, HIGH);   // Motor turns forward
       delay(1500);                 // motor runs for 2 seconds
       digitalWrite(in1Pin, LOW);   //  stops the motor
       digitalWrite(in2Pin, HIGH);  // runs in reverse
              delay(1300);
     
       digitalWrite (in2Pin,LOW);    //  stops motor
       
       if(lockLow){ 
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;           
         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
       // digitalWrite

       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
        }
       //if the sensor is low for more than the given pause,
       //we assume that no more motion is going to happen
       if(!lockLow && millis() - lowIn > pause){ 
           //makes sure this block of code is only executed again after
           //a new motion sequence has been detected
           lockLow = true;                       
           Serial.print("motion ended at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");
           delay(50);
           }
       }
  }

the site wont let me post for a few mins
still working on this

That code won't even compile.

PaulS: That code won't even compile.

A little more clarity please. what's wrong with it?

aksbonsra: A little more clarity please. what's wrong with it?

void setup(){
  Serial.begin(9600);
  pinMode(pirPin, INPUT);     //  declare sensor as input
  pinMode(ledPin, OUTPUT);    //  declare LED as output
  pinMode (in1Pin, OUTPUT);  // declares pin as output
  pinMode (in2Pin, OUTPUT);  // declares pin as output
  digitalWrite(pirPin, LOW);

 int speed = analogRead(PotPin1); // 4;


int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}

void loop() {

Where does your first setup() function end?

How many setup() functions can you have?

Are you allowed to define functions inside of functions?

Did you even bother to try to compile that code before posting it?

Where does your first setup() function end?

How many setup() functions can you have?

Are you allowed to define functions inside of functions?

Did you even bother to try to compile that code before posting it?

This is my very first coding, EVER. I am learning and figuring as it goes.

I first made changes to the PIR code, which worked.
Then added the motor codes, still worked, needs fine tunning
but when added the potentiometer codes, it’s giving errors, fixed some, figuring out others.

I had posted the the first two codes before, you can see the changes, nevertheless, posting them again,

I know I am making mistakes, I don’t know what they are

//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 15;       

//the time when the sensor outputs a low impulse
long unsigned int lowIn;         

//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000; 

boolean lockLow = true;
boolean takeLowTime; 

const int PotPin1  = A0; // Potentiometer1
const int analogOutPin1 = 10 && 9;   // the analog out for Pot 

int pirPin = 3;               //the digital pin connected to the PIR sensor's output
int ledPin = 13;              // digital pin for LED
int in1Pin = 10;           // rotates the motor forward
int in2Pin = 9;           // rotates the motor backward

/////////////////////////////
//SETUP

void setup(){
  Serial.begin(9600);
  pinMode(pirPin, INPUT);     //  declare sensor as input
  pinMode(ledPin, OUTPUT);    //  declare LED as output
  pinMode (in1Pin, OUTPUT);  // declares pin as output
  pinMode (in2Pin, OUTPUT);  // declares pin as output
  digitalWrite(pirPin, LOW);

 int speed = analogRead(PotPin1) / 4;
 
  //give the sensor some time to calibrate
  Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++){
      Serial.print(".");
      delay(3000);
      }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");
    delay(500);
  }

////////////////////////////
//LOOP
void loop(){

     if(digitalRead(pirPin) == HIGH){
       digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state
       digitalWrite(in1Pin, HIGH);   // Motor turns forward
       delay(1000);                 // motor runs for 2 seconds
       digitalWrite(in1Pin, LOW);   //  stops the motor
       digitalWrite(in2Pin, HIGH);  // runs in reverse
       delay(1000);
     
       digitalWrite (in2Pin,LOW);    //  stops motor
       
       if(lockLow){ 
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;           
         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
       // digitalWrite

       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
        }
       //if the sensor is low for more than the given pause,
       //we assume that no more motion is going to happen
       if(!lockLow && millis() - lowIn > pause){ 
           //makes sure this block of code is only executed again after
           //a new motion sequence has been detected
           lockLow = true;                       
           Serial.print("motion ended at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");
           delay(50);
           }
       }
  }

this one is newest one, has trouble in the last part

//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 15;       

//the time when the sensor outputs a low impulse
long unsigned int lowIn;         

//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000; 

boolean lockLow = true;
boolean takeLowTime; 

const int PotPin1  = A0; // Potentiometer1
const int analogOutPin1 = 10 && 9;   // the analog out for Pot 

int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)

int pirPin = 3;               //the digital pin connected to the PIR sensor's output
int ledPin = 13;              // digital pin for LED
int in1Pin = 10;           // rotates the motor forward
int in2Pin = 9;           // rotates the motor backward

/////////////////////////////
//SETUP

void setup(){
  Serial.begin(9600);
  pinMode(pirPin, INPUT);     //  declare sensor as input
  pinMode(ledPin, OUTPUT);    //  declare LED as output
  pinMode (in1Pin, OUTPUT);  // declares pin as output
  pinMode (in2Pin, OUTPUT);  // declares pin as output
  digitalWrite(pirPin, LOW);

  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}

// LOOP
void loop(){

     if(digitalRead(pirPin) == HIGH){
       digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state

         // read the analog in value:
  sensorValue = analogRead(PotPin1);
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255);
  // change the analog out value:
  analogWrite(analogOutPin1, outputValue);

  // print the results to the serial monitor:
  Serial.print("sensor = " );
  Serial.print(sensorValue);
  Serial.print("\t output = ");
  Serial.println(outputValue);

  // wait 2 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(2);


 
  //give the sensor some time to calibrate
  Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++){
      Serial.print(".");
      delay(3000);
      }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");
    delay(500);
  }

       
       analogWrite (analogOutPin1, 30);
       digitalWrite(in1Pin, HIGH);   // Motor turns forward
       delay(1500);                 // motor runs for 2 seconds
       digitalWrite(in1Pin, LOW);   //  stops the motor
       digitalWrite(in2Pin, HIGH);  // runs in reverse
       delay(1300);
     
       digitalWrite (in2Pin,LOW);    //  stops motor
       
       if(lockLow){ 
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;           
         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
       // digitalWrite

       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
        }
       //if the sensor is low for more than the given pause,
       //we assume that no more motion is going to happen
       if(!lockLow && millis() - lowIn > pause){ 
           //makes sure this block of code is only executed again after
           //a new motion sequence has been detected
           lockLow = true;                       
           Serial.print("motion ended at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");
           delay(50);
           }
       }
  }

this one is newest one, has trouble in the last part

I'm certainly glad that one of us knows what the trouble is.

We don't play word games here - if you want help, help us to help you. We can't see what you can see, we don't know what your hopes or expectations are.

if (digitalRead(pirPin) == LOW)The code after this is not in a function. Where do you think the loop() function ends ?

Auto Format the code in the IDE and the problem will show up more clearly.

If this is your first code ever, put it aside and start simpler. Need to learn how to walk before you run.

J-M-L: If this is your first code ever, put it aside and start simpler. Need to learn how to walk before you run.

yep, I am doing that now, thanks.

I am going back to the earlier code, discarding the potentiometer. (I have to finish what I started.)

Now the only thing I am looking for is a way to make a small set of code run only ONCE within the code in loop. It starts and ends at @ (I don't know another way of marking here for reference). Is there a command that can do this? I tried write and break, but I get errors.

if(digitalRead(pirPin) == HIGH){
       digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state
    @   digitalWrite(in1Pin, HIGH);   // Motor turns forward
       delay(1000);                 // motor runs for 2 seconds
       digitalWrite(in1Pin, LOW);   //  stops the motor
       digitalWrite(in2Pin, HIGH);  // runs in reverse
       delay(1000);
       digitalWrite (in2Pin,LOW);    //  stops motor @


if(lockLow){ 
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;           
         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
        }
        
       //if the sensor is low for more than the given pause,
       //we assume that no more motion is going to happen
       if(!lockLow && millis() - lowIn > pause){ 
           //makes sure this block of code is only executed again after
           //a new motion sequence has been detected
           lockLow = true;                       
           Serial.print("motion ended at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");
           delay(50);
           }
       }
  }

[/code]

If you only want it to happen once put it in setup. Or make a flag to mark whether or not it has been done.

boolean didThatThing = false;

void loop(){
   if(didThatThing == false){
       // do that thing
       didThatThing = true;
   }
}

I did that, but got the same result like other codes I tried. The motor starts running in one direction without stopping and the PIR sensor remains on even though there's no motion any more.

Anyways, I made do with the earlier code, it did an interesting thing when I added another motor to the mix. Only one motor moved at one time, continuously till PIR stopped.

Unfortunately, as I was trying to show an action of horse galloping, this didn't look right. Ah well, I am sure there's many more lessons to go. Thank you for your time and energy everyone :)