Pages: [1]   Go Down
Author Topic: Ping timer trouble!!  (Read 364 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 2
Posts: 213
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everyone, I propose a question that is making me a little crazy ... I'm basically trying to create a kind of timer, operated by the Parallax PING ultrasonic sensor, this is my sketch

const int pingPin = 7;
int time = 0;
int dist = 0;
int led = 13;
int led2 = 4;
int led3 = 3;
void setup() {

 Serial.begin(9600);
}

 


void loop()
{
if (dist < 30) {
time = time + 1;
}
else
{ time = 0;
}
{
if (time > 300) {
digitalWrite(led, HIGH);
}
else
{
digitalWrite(led, LOW);
}
{
if (time > 500) {
digitalWrite(led2, HIGH);
}
else
{ digitalWrite(led2, LOW);
}
}
{
if (time > 1000)
{
digitalWrite(led3, HIGH);
}
else
{
digitalWrite(led3, LOW);
}
}
long duration, inches, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
cm = microsecondsToCentimeters(duration);
dist = cm;
Serial.print(cm);
Serial.print("cm");
Serial.println();
Serial.print(time);
Serial.print("tempo");
Serial.println();  
}
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}

The part of the ping I admit it shamelessly copied from basic sketch for ping for showing the distance on serial console ...  when it detects a distance less than a value (in this example 30), began to raise the variable "time", and with different values of "time",drive various LEDs. Now, I can not understand if the problem it's in the frequency of loop or something, on the outputs of  LEDs I don't have full voltage of 5 V, but a low voltage of 2 V (so the LED is fading) .. .. thinking that have damaged the outputs with some mistake, I made a simple sketch for fix exit to an output HIGH level, attached the led (of course with relative resistor), and with pleasure I noticed that my outputs are still intact,the output voltage it's 5 V and beautiful bright LED ... any suggestion?! Thanks to all!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49040
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The pins led, led2, and led3 are not declared OUTPUT pins in setup. Does the program work any better if you declare them to be output pins, using the pinMode function?
Logged

0
Offline Offline
Full Member
***
Karma: 2
Posts: 213
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Perfect!!!!!!!!!!!! It work perfect!!! Problem solved, add

pinMode(led, OUTPUT);
 pinMode(led2, OUTPUT);
 pinMode(led3, OUTPUT);
 pinMode(alarm, OUTPUT);

in void setup and now the signal comes out "full" and stable!!! Very thanks!!!
Logged

Pages: [1]   Go Up
Jump to: