Working on my first Arduino project and I need to make a sensor that detects motion passing through a doorway using an ultrasonic sensor and counts it. When it counts, it needs to blink an LED light a number of times equal to the count. I've figured out how to make the sensor count motion and how to wire it but I can't figure out how to make the code. Any help would be appreciated.
How do you get the sensor to count anything without any code?
Steve
slipstick:
How do you get the sensor to count anything without any code?Steve
I found someone else's code for it and got it to work on my setup
So you understand how the code you copied works? If so then great, let's take it apart and use it for what you want. If not then you've gone as far as you can. Your choices are to either learn these things or to just be limited to what you can find to copy.
This isn't a free code writing service. Show us what you've tried so far and we can help you get it right.
#define trigPin 13
#define echoPin 12
int counter = 0;
int currentState = 0;
int previousState = 0;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance <= 20){
currentState = 1;
}
else {
currentState = 0;
}
delay(100);
if(currentState != previousState){
if(currentState == 1){
counter = counter + 1;
Serial.println(counter);
}
}
}
Here's the code for the counter triggered by the motion sensor
dbenoit888:
Here's the code for the counter triggered by the motion sensor
And you're just going to ignore the question I asked you? Ok, sorry. I thought you wanted help with this. I guess I misunderstood.
Delta_G:
And you're just going to ignore the question I asked you? Ok, sorry. I thought you wanted help with this. I guess I misunderstood.
I apoligize, I would say I like ~50% understand the code for the counter. I'm still trying to work it out. I've been working with another code using the example codes that the kit I have came with. Like I said this is my first project, so what I've figured out playing around so far is how to make the light blink when the sensor detects an object close to it. But it just blinks endlessly as long as the object stays within the distance. I don't know what the next step is really to start to make the code my own so any guidance or suggestions would be helpful.
const int trigPin = 11; //connects to the echo pin on the distance sensor
const int echoPin = 12; //connects to the trigger pin on the distance sensor
float distance = 0; //stores the distance measured by the distance sensor
void setup()
{
Serial.begin (9600); //set up a serial connection with the computer
pinMode(trigPin, OUTPUT); //the trigger pin will output pulses of electricity
pinMode(echoPin, INPUT); //the echo pin will measure the duration of pulses coming back from the distance sensor
pinMode(10, OUTPUT); // Set pin 10 to output
}
void loop() {
distance = getDistance(); //variable to store the distance measured by the sensor
Serial.print(distance); //print the distance that was measured
Serial.println(" in"); //print units after the distance
if(distance <= 15){ //if the object is close
digitalWrite(10, HIGH); // Turn on the LED
delay(500); // Wait for .5 seconds
digitalWrite(10, LOW); // Turn off the LED
delay(500); // Wait for .5 seconds
} else if(10 < distance && distance < 30){ //if the object is a medium distance
digitalWrite(10, LOW); // Turn off the LED
}
delay(50); //delay 50ms between each reading
}
//------------------FUNCTIONS-------------------------------
//RETURNS THE DISTANCE MEASURED BY THE HC-SR04 DISTANCE SENSOR
float getDistance()
{
float echoTime; //variable to store the time it takes for a ping to bounce off an object
float calcualtedDistance; //variable to store the distance calculated from the echo time
//send out an ultrasonic pulse that's 10ms long
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
echoTime = pulseIn(echoPin, HIGH); //use the pulsein command to see how long it takes for the
//pulse to bounce back to the sensor
calcualtedDistance = echoTime / 148.0; //calculate the distance of the object that reflected the pulse (half the bounce time multiplied by the speed of sound)
return calcualtedDistance; //send back the distance that was calculated