Making sensor input longer

Hello guys, I hope I post this on the right page. And please excuse me for my bad English.

I have a infrared motion sensor and I want it to be on high a little longer. Is there like a way that my arduino can read the sensor as high for around 5 seconds but the sensor itself only activated for a short second?

Yes.

Welcome to the forum

When the sensor becomes HIGH save the value of millis() as the start time. In loop(), once the sensor has been activated, test whether 5 seconds has elapsed since the start time by subtracting the start time from the current time and take whatever action that you require if it has

Hello luukski

Post your sketch, well formated, with comments and in so called code tags "</>" and a none-Fritzing schematic to see how we can help.

I don't have an actual code.

I initiated pin 3 as Sensor
And used library servo.h

I want to make something like
If sensor is high
Then turn servo

But the sensor has to stay high for 5 seconds for the rest of the code.

The only problem is my item is already pushed away by the servo.

me neither

When you determine sensor has been activated,

  • set a bool flag variable to true when sensor fires
    -- Set another long integer to millis().
  • Do not test sensor if flag is true but
  • test the timestamp to determine if 5000 mS have elapsed and
    --then reset the flag.

Can you maybe make an example so I can kinda reverse engineer it? I have never worked with Bool, flags and millis.

Now would be a good time to start

A "help" forum as we are here, generally implies you have started your project, studied your requirements (and maybe school notes), do the appropriate research, attempt to implement the program logic into your sketch ... as a minimum. Like a homework assignment, you do not want to turn-in a blank response.

  • Show the code for lighting a LED from the IR sensor.
  • Show us the wiring you have implemented (schematic)

.... You should be able to do the following by simply finding your (specific) IR sensor model on the Internet. Most of the time, sellers that cater to experimenters also have sample code that is the basis for starting.

When you have a working circuit/program the info I previously provided can be added.

Procedure in action. Assume a break-beam sensor. I can search for: "Arduino" + "Infrared" + break-beam

https://duckduckgo.com/?q=%22Arduino%22+%2B+%22Infrared%22+%2B+breal-beam&ia=web

I know from "experience" that Adafruit & Sparkfun sell such sensors and provide support (they charge a higher price to cover the support.) So, just add "Adafruit" to the above search:

https://duckduckgo.com/?q=%22Arduino%22+%2B+%22Infrared%22+%2B+break-beam+%2B+%22Adafruit%22&ia=web

Our first return results is a goldmine:

https://learn.adafruit.com/ir-breakbeam-sensors?view=all

We not have a generic IR sensor and full test code!

/* 
  IR Breakbeam sensor demo!
*/

#define LEDPIN 13
  // Pin 13: Arduino has an LED connected on pin 13
  // Pin 11: Teensy 2.0 has the LED on pin 11
  // Pin  6: Teensy++ 2.0 has the LED on pin 6
  // Pin 13: Teensy 3.0 has the LED on pin 13

#define SENSORPIN 4

// variables will change:
int sensorState = 0, lastState=0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(LEDPIN, OUTPUT);      
  // initialize the sensor pin as an input:
  pinMode(SENSORPIN, INPUT);     
  digitalWrite(SENSORPIN, HIGH); // turn on the pullup
  
  Serial.begin(9600);
}

void loop(){
  // read the state of the pushbutton value:
  sensorState = digitalRead(SENSORPIN);

  // check if the sensor beam is broken
  // if it is, the sensorState is LOW:
  if (sensorState == LOW) {     
    // turn LED on:
    digitalWrite(LEDPIN, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(LEDPIN, LOW); 
  }
  
  if (sensorState && !lastState) {
    Serial.println("Unbroken");
  } 
  if (!sensorState && lastState) {
    Serial.println("Broken");
  }
  lastState = sensorState;
}

You should run the test code to make certain that your PC and solderless-project-board and pin-jumpers are all wired correctly.

At this point, you have your base code that can be modified.

You did not tell us which Arduino board you are using nor did you give us the model number or link to the IR sensor, so I really cannot provide exact code as different boards/sensors may require minor modification to the test sketch.

But, all of the elements that need to be added are easily found on the Arduino reference pages:
https://www.arduino.cc/reference/en/

bool

millis()

Give it a try.
If you need to come back to the forum, be certain to provided everything needed to assist:

  • Existing code (post using the "</>" code tags
  • make/model of Arduino and IR sensor (link helpful)
  • a drawing of how the wiring is hooked-up
  • what you have accomplished
  • what help you specifically need
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.