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!