Arduino Led and sensor

Hi,

I have Arduino board Mega 2056. I am using below code which trigger LED to blink when motion is detected but I have 3 issues:

  1. Here is my code, with one motion, LED blink 3 times instead of one:
int motion_1 = 2;
int light_1 = 13;
void setup(){
  pinMode (motion_1,INPUT);
  pinMode (light_1, OUTPUT);
} 
 
 void loop (){
 //digitalWrite (light_1,LOW);
  delay(1000); //this delay is to let the sensor settle down before taking a reading
  int sensor_1 = digitalRead(motion_1);
  if (sensor_1 == HIGH){
    digitalWrite(light_1,HIGH);
    delay(500);
    digitalWrite(light_1,LOW);
  //  delay(500);
    
  }
 }

Please help me how to make it blink only 1 time instead of 3 times.

  1. Is it possible to use more than 1 sensors with Arduino Mega 2056? If yes then can guide or tip?

  2. Is it possible to change degree of motion sensor using code? If yes then any guideline?

Thanks

  1. I think your sensor is still high next time it goes through loop() and so it passes the “if” test. You can’t just check it’s high: you need to store a state and see if it changed since last time.

So if it’s high, but was low before, flash the light and set last state to high. then next time through, even if it’s still high, since it was already high the time before it won’t flash.

  1. Yes, why not just put another sensor on a different pin and read that.

  2. I don’t understand that question, sorry.

HTH?

Jim

memon421:
Please help me how to make it blink only 1 time instead of 3 times.

  1. Is it possible to use more than 1 sensors with Arduino Mega 2056? If yes then can guide or tip?

  2. Is it possible to change degree of motion sensor using code? If yes then any guideline?

I don't know what motion sensor you are using, but it'sclear that once it indicates motion (HIGH), it must keep showing motion for some length of time after that, and since you keep checking it, and turning the LED on, it will keep on turning on until the motion sensor input goes low.

You might want to wait fot the sensor input to go low again...

    while (sensor_1 == HIGH) {
    }

Of course, you can't do anything else while that happens.

Are you using a PIR? If so, and maybe even if you're not, the below code might help. I use the serial monitor to indicate "new" motion detected. But I don't display that there is "continuing" motion if there was motion last time through. It would only display motion if the motion had stopped, and the sensor had settled: then there would be "new" motion.

/*
 * PIR sensor tester
 */
 
int ledPin = 13;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
 
void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input
 
  Serial.begin(9600);
}//setup
 
void loop(){
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    //disturbed
    digitalWrite(ledPin, HIGH);  // turn LED ON
    if (pirState == LOW) {
      // we have just turned on
      Serial.println("Motion detected!");
      // We only want to print on the output change, not state
      pirState = HIGH;
    }//pirstate low
  }//val high
  else {  // val is low... settled
    digitalWrite(ledPin, LOW); // turn LED OFF
    if (pirState == HIGH){
      // we have just turned off
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      pirState = LOW;
    }//pirstare high in the else
  }//else
}

Thank you all for great help. I really appreciate it.

yes, I am using PIR sensor. There is feature of Detecting Range: Approx. 7m / 23 feet and Detecting Angle: Less than 100 degrees. Is it possible to play with range and angle using code?

If yes then how I can? Please guide me or put some sample code?

If No then what else I can do using code other than detecting motion. I am willing to play with features of sensor using code.

Please guide me.

Thanks

I don't think you can adjust those characteristics in code- almost certain they are hardware related.

If you haven't seen it before, Adafruit has a very good tutorial, which is where my code above came from.

thanks for link,

Oh, I was assuming that I can control Sensor by code :slight_smile: Is it possible to control ultrasonic sensor like HC-SR04 Ultrasonic Sensor? I am willing to play with sensor using code? Or suggest me if I can use any sensor to control it by code?

Is there any way to enable or disable sensor using code?

Please advise.

Thanks

I don’t know what you mean when you say you want to “control” an HC-SR04. But its measurement is kicked-off by you sending it a pulse, so if that’s what you mean by control, then yes you can.

To disable a PIR I guess you could power it from the 5V of an i/o line rather than the Arduino 5V, and so disable it by setting that pin low and turning the PIR off. Check the PIR datasheet for current consumption.