loading

Need help for improving PING Ultrasonic sensor code to make it precise

Hi guys,
              I havebuilt a circuit to detect the swinging of an object using ultrasonic sensor and send signals to the Arduino Duemilanove board which in turn activates the solenoid. The circuit is working fine but I'm using a PING Ultrasonic sensor and I'm having hard time controlling it with the code. Do you have any idea about how to control and make the readings precise for the Sensor? If you want, I can post my code here... Any help is appreciated. Thanks in advance!

sort by: active | newest | oldest
1-10 of 30Next »
What's the actual problem ?
kailash174 (author)  steveastrouk4 years ago
Also I want to put a counter. So the solenoid should be high only when the counter number is prime.. Can you help me code that as well? Thanks in advance!
Tricky. What kind of time intervals are there between bat transitions, and between the transitions. I'd reckon you go, say, to and fro in 1/2 second, then there's a second or two before the next attempt ?
kailash174 (author)  steveastrouk4 years ago
Hi Steve,
You're right! At first it was tricky for me, now its turned out to be a challenge! I think the time between bat intervals is about 1/2 a second... But the second transition keeps continuing! It doesn't stop.. So it means, there is 1/2 a second to one second gap for the next transition..!
You need to start a timer when you detect the bat the first time. You look at the count at the next bat transition If the count < bat time, you have a valid detection, other wise reset the timer.
kailash174 (author)  steveastrouk4 years ago
Steve,
Instead of a timer, I have tried this method. First detection, let go and there is a delay(300); and then again a "if" loop for testing the detection. This is working fine if the swing speed is constant. Even if there is a slight change in the swing speed, it goes wrong!!!!!!
kailash174 (author)  steveastrouk4 years ago
Good Idea! Can you help me with the program? I'm pretty bad with programming!!! Thanks!
Well, you COULD test for prime, but its probably easier to have a array with 30 or 40 primes in it.
kailash174 (author)  steveastrouk4 years ago
Sounds good.. Can you help me in editing my code for the 30 prime numbers. My code is posted below in one of the comments! Thanks steve.. You're of great help man. Appreciate it!
kailash174 (author)  steveastrouk4 years ago
Hi steve!
This is me, again. I'm almost done with my project. But I still have two major problems with the way my sensor is working! I need to swing the bat(oscillates) in front of the sensor.. The sensor has to detect only when the bat is swung for the second time. I mean, first it passes infront of the sensor, which is TO and then it comes back, FRO. It should stay idle for the first detection and detect the FRO! I tried various things with the code, its not working that way. Can you help me out with this.

Here is the code:

#include
#include
#include
int pingPin = 7;
int val = 0;
int output = 2;
int button = 8;
int check = 0;
int ctr = 0;
int p13 = 13;
void setup() {
Serial.begin(9600);
pinMode(output, OUTPUT);
pinMode(pingPin, INPUT);
pinMode(button, INPUT);
pinMode(p13, OUTPUT);
}
void loop(){
unsigned long duration;
check = digitalRead(button);


if(check == HIGH){
pinMode(pingPin, OUTPUT);

digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);


pinMode(pingPin, INPUT);

duration = pulseIn(pingPin, HIGH, 20000);
duration = constrain(duration, 1000, 20000);
int val = map(duration, 1000, 20000, 0, 255);
duration = (duration/2) / 29.1;



if(duration < 1700 && duration > 1000) {
{ ctr++;
}

if (ctr == 2)
{
ctr = 0;
delay(150);
digitalWrite(output, HIGH);
digitalWrite(p13, HIGH);
}}

else
{
digitalWrite(output, LOW);
digitalWrite(p13, LOW);
}}


else
{
digitalWrite(output, LOW);

digitalWrite(p13, LOW);
}
}
1-10 of 30Next »