Go Down

Topic: `Sharp distance sensor GP2y0d805z0f  (Read 402 times) previous topic - next topic

SMBeav

I an trying to find a code for this sensor as it senses motion to turn on LED for a delay of 5-6 seconds.
The sensor seems to have a slow response as I need it to be almost instantaneous to turn on the LED.
I will be using multiple sensors with there own LED. Can this be done?
I am using Arduino Mega 2560.
Here is what I tried...

int led = 42;                // the pin that the LED is atteched to
int sensor = 52;              // the pin that the sensor is atteched to
int state = HIGH;             // by default, no motion detected
int val = 1;                 // variable to store the sensor status (value)

void setup() {
  pinMode(led, OUTPUT);      // initalize LED as an output
  pinMode(sensor, INPUT);    // initialize sensor as an input
  Serial.begin(9600);        // initialize serial
}

void loop(){
  val = digitalRead(sensor);   // read sensor value
  if (val == HIGH) {           // check if the sensor is HIGH
    digitalWrite(led, HIGH);   // turn LED OFF
    delay(100);                // turn LED OFF
   
    if (state == LOW) {
      Serial.println("Motion detected!");
      state = LOW;       // update variable state to LOW
    }
  }
  else {
      digitalWrite(led, LOW); // turn LED On
      delay(7000);
 
     
      if (state == HIGH){
        Serial.println("Motion stopped!");
        state = HIGH;       // update variable state to HIGH
    }
  }

Thanks for any help on this!

pylon

First: To post code you must use code tags around it. In the editor it's the </> to produce them.

Quote
The sensor seems to have a slow response as I need it to be almost instantaneous to turn on the LED.
I will be using multiple sensors with there own LED. Can this be done?
I don't know the sensor but definitely a source of the slow response are the delay() calls in your code. Remove them, they are not needed.

Update the comments, some of them are simply wrong.

jackthom41

Sharp distance sensor GP2y0d805z0f is quite an awesome sensor. I have used it in many of my projects and it works really great.

I think the problem you are getting is because of 7 seconds delay. Obviously when your LED is ON then your code will stuck for 7 seconds in the delay and that will make the sensor slow. So, I would suggest you to find some other way of making LED glow for 7 seconds.
Technopreneur

SMBeav

I know I'm late , but thank you for your input.

Go Up