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!
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!

















Maker Faire 2013 Slide Show!
Fried Contest Launches 5/13, HQ Celebrates with Fried Day Friday
MEH! :D A Build Night at Montana Ethical Hackerspace!
Got contest ideas? Want to help HQ staff?
Large Instructables Robot head made out of driftwood, check it out!
Call for pre-made parts!
The Instructables Green Design Contest is starting on Earth Day!
My instructable made it into Popular Science!
Orbotix wants to see your hacks - you could win a Sphero!
Transformational experience for Instructables Artist-in-Residence


Visit Our Store »
Go Pro Today »




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);
}
}
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..!
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!!!!!!
Steve
At 100 PPS, you should be able to catch it OK surely ?
K now, can you help me with the code steve? I want to actually count the number of times the sensor detects a swining bat within the 10cm range and put a counter to count it and for every 3rd time, I want to activate the solenoid. The code which I have is:
#include
#include
int pingPin = 7; // Choose the input pin (for PING Sensor)
int val = 0; // variable for reading the sensor values
int output = 2; // Choose the pin for the Solenoid
int button = 8; // Choose the input pin (for a push button)
int check = 0; // A variable to read whether pushbutton is pressed or not
void setup() {
Serial.begin(9600);
pinMode(output, OUTPUT); // Declare Solenoid as output
pinMode(pingPin, INPUT); // Declare PING Sensor as input
pinMode(button, INPUT); // Declare Pushbutton as input
}
// Initialize duration and check for the pushbutton press and then perform sensor action
void loop(){
long duration;
check = digitalRead(button);
int count;
// count = count + 1;
for(count=0; count<100; count++)
{
int prime = 1;
//int prime1 = 3;
//int prime2 = 5;
//int prime3 = 7;
if(check == HIGH){
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH); // Read pulse length and store it in 'duration'
duration = constrain(duration, 1000, 20000); // Ignore values outside 1000 & 20000 //microseconds
int val = map(duration, 1000, 20000, 0, 255); // Map duration to the range 0 - 255 (consider data //type!)
//Check for the condition with the sensor values to activate the Solenoid
if(duration > 1000)
{
if(count == prime)
{
digitalWrite(output, HIGH);
}
else
{
digitalWrite(output, LOW);
}
}}
// This is for the pushbutton. If the button is not pressed, the machine will not start("LOW")
else
{
digitalWrite(output, LOW);
}
}}
Of course. You have to wait for the maximum permissible return time before you can go again.
If I'd been you, I'd've used capacitance sensing of the bat, not ultrasound, but its too late now
K so is it possible to modify the code for the way I want it to work? Can you help me with that?
That's
If (duration <2000)
{ ctr++};
If (ctr >=3)
{ ctr==0;
digitalwrite(output,high)
}
I've added your portion of the code to my main code. I still have problems with precise detection. I'm placing the Ping sensor 3cm above the ground on a breadboard. The bat i'm swinging infront of the sensor is something like a golf putter stick. Its a metal bat! It swings a little faster, is this a problem for the sensor to detect it? The sensor has to detect when bat crosses the sensor and send a signal to the solenoid, making it HIGH to activate!!! Could you help me with this? Thanks!
Steve,
I've one more question regarding to the Arduino UNO board. I need to know how to reset the board using a software. I've asked this on the questions section. If you know the answer, kindly answer me. Thanks in advance!
"if (duration > 1000)"
#include
#include
int pingPin = 7; // Choose the input pin (for PING Sensor)
int val = 0; // variable for reading the sensor values
int output = 2; // Choose the pin for the Solenoid
int button = 8; // Choose the input pin (for a push button)
int check = 0; // A variable to read whether pushbutton is pressed or not
void setup() {
Serial.begin(9600);
pinMode(output, OUTPUT); // Declare Solenoid as output
pinMode(pingPin, INPUT); // Declare PING Sensor as input
pinMode(button, INPUT); // Declare Pushbutton as input
}
// Initialize duration and check for the pushbutton press and then perform sensor action
void loop(){
long duration;
check = digitalRead(button);
int count;
// count = count + 1;
for(count=0; count<100; count++)
{
int prime = 1;
//int prime1 = 3;
//int prime2 = 5;
//int prime3 = 7;
if(check == HIGH){
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH); // Read pulse length and store it in 'duration'
duration = constrain(duration, 1000, 20000); // Ignore values outside 1000 & 20000 //microseconds
int val = map(duration, 1000, 20000, 0, 255); // Map duration to the range 0 - 255 (consider data //type!)
//Check for the condition with the sensor values to activate the Solenoid
if(duration > 1000)
{
if(count == prime)
{
digitalWrite(output, HIGH);
}
else
{
digitalWrite(output, LOW);
}
}}
// This is for the pushbutton. If the button is not pressed, the machine will not start("LOW")
else
{
digitalWrite(output, LOW);
}
}}