78Views5Replies

Author Options:

Turn multiple LEDs on when motion is detected? Answered

Hi,

I'm working on a code where the situation is "if motion sensor reads movement, turn one led on'. This has to happen three times. When the motion sensor read 3 times a movement, the third and las led will turn on, which activates the servo motor to move. Right now I'm stuck at some points in the code:

- How do I write that by the first movement, only the first one will turn on, and keeps it on, until the second movement has bean read, which turns on the second one?
- How do I count, that when the third led is on, the servo motor will rotate?
- When the servo motor has rotated enough, how do I stop the motor? (I have to write motor.write(90), but where?

This is how my code is right now:

int sensor = 2;
int beweging = LOW;
int waarde = 0;
int analogInPin = A0;
int sensorValue = 0;
int aantalLED = 0;

#include <Servo.h>

Servo motor;

void setup() {
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  Serial.begin(9600);
  motor.attach(9);
}

void loop() {
  waarde = digitalRead(sensor);
  if (beweging = LOW) {
    Serial.println("Motion detected!");
    beweging = HIGH;
    digitalWrite(2, HIGH);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
  }

  else if (beweging = HIGH) {
      Serial.println("Motion stopped!");
      beweging = LOW;
    }

  else if (beweging = LOW) {
    Serial.println("Motion detected!");
    beweging = HIGH;
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, LOW);
  }

  else if (beweging = HIGH) {
    Serial.println("Motion stopped!");
    beweging = LOW;
  }

  else if (beweging = LOW) {
    Serial.println("Motion detected!");
    beweging = HIGH;
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
  }

  if (aantalLED > 3) {
    motor.write(0);
  }

  }

}

Discussions

None
iceng

3 years ago

You want to say

IF motion is detected add 1 to integer AAKI

I picked part of your name for this variable,

( like AAKI = AAKI +1 )

When AAKI is > 2 you turn on motor and set AAKI to zero

How hard is it to code that ?

None
Nighter3D

3 years ago

Oh boy...may i be honest and say that your code is kind of a wreck? it doesn't make any sense. Well... lets see if i can ive you a good example. The following code i'm going to past is a example of a simple counter:

aantalLED = aantalLED + 1;
if (aantalLED == 1) {
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
} else if (aantalLED == 2) {
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
} else if (aantalLED == 3) {
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
motor.write(0);
} else {
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
motor.write(90);
aantalLED = 0;
}
delay(1000);

What this will do is every second it will increase the aantalLED variable by 1. Then it goes through a set of IFs where at each value it will activate the right led and at the third it will start the servo. if the variable isn't 1,2 or 3 it will stop the servo, turn of the leds and resets the counter to 0 to start all over again, finally it waits for 1 second before starting over. Hopefully this example code will give you an idea at a simple counter and perhaps help you implement it into your own code.

Let me know if you need help with anything else.

None
xMisaaakiNighter3D

Answer 3 years ago

Thanks for your reply!
Right now, you wrote a simple counter, but what I want is, that for example, when the motion sensor detects motion, the second led will turn on, but the first led stays on, so you can see 'oh, this is the second time the sensor has detected my motion'. Do I only have to change the digitalWrite to HIGH where you wrote down LOW?

And, for the counter you're using if else statement, but for the motion sensor I need to use an if else statement too, how do I combine those? Because I want to say 'if motion is detected, turn one led on, if the motion is detected again, turn second one on while first one stays on etc.'

None
Nighter3DxMisaaaki

Answer 3 years ago

Pretty much. Digitalwrite HIGH will turn on a led, LOW will turn it off. use in whatever way you need to know.

Hmm. Well that wouldn't be that hard. If you take the code where it detects motion and put the (aantalLED = aantalLED + 1;) part in it. then it will count up at every detection.... Which is what you want. Make the Detection function count up everytime it detects something.

Here is a quick draft of how that would look in your code:

waarde = digitalRead(sensor);
if (waarde == LOW) {
Serial.println("Motion detected!");
if (beweging == LOW) {
aantalLED = aantalLED + 1;
beweging = HIGH;
}
} else if (waarde == HIGH) {
beweging = LOW;
}

This code Will check for motion, when it does that (which happens when the sensor reads LOW) it will then do a simple check to see if was already detecting last time (in the form of wether beweging is HIGH already), if not it will then trigger a count and then set beweging as HIGH. after that so long as the sensor is detecting the same motion it wont do anything (because beweging is already HIGH).The moment the sensor no longer sees anything (waarde becomes 1) it will then set beweging back to LOW

This is a simple way of having a counter count up whenever a button (or in this case a sensor) is triggered.

None
Downunder35m

3 years ago

You count the events the sensor was trigger.
Assign each count to a LED.
Reset counter when full and activate the motor.

None
xMisaaakiDownunder35m

Answer 3 years ago

Thank you for your reply! Can you maybe explain this with code? I find it hard to understand if I don't have an example of how it works. Sorry, I'm still a early beginner..