salve ragazzi,
sto smanettando un pò con arduino per testare un sensore ultrasuoni hy-srf05.
ho provato a collegarlo con la modalita a 3 fili (collegando il pin mode a gnd) ma non funziona. mi da sempre 0
ho provato allora con il funzionamento classico a 4 fili (lasciano scollegato il pin mode e collegando ad arduino sia il trigger che l'echo)
Funziona perfattamente.
Allora per imparare volevo implementare un led che lampeggia con fequenze differenti a seconda della distanza, ho implementato il codice ma non mi funziona.
Mi spieghereste per cortesia dove sbaglio?
dall'errore sembra che manchi una parentesi ma non capisco..
const int trigPin = 8; //Change to pin you use
const int echoPin = 9; //Here too
void setup() {
// initialize serial communication:
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(10, OUTPUT);
digitalWrite(trigPin, LOW);
}
void loop()
{
// establish variables for duration of the ping,
// and the distance result in centimeters:
long duration, dcm;
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
// convert the time into a distance
dcm = microsecondsToCentimeters(duration);
Serial.print(dcm);
Serial.print("cm");
Serial.println();
if ( dcm>10 && dcm<20)
{
Serial.print("distante ");
digitalWrite(10,HIGH);
delay(1000);
digitalWrite(10,LOW);
delay(1000);
}
else if (dcm>20)
{
Serial.print("distantissimo ");
digitalWrite(10,HIGH);
delay(2000);
digitalWrite(10,LOW);
delay(2000);
}
}
delay(500);
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
ecco l'errore che mi compare
hy_srf05_2:55: error: expected constructor, destructor, or type conversion before '(' token
cosi è meglio secondo voi?
const int trigPin = 8; //Change to pin you use
const int echoPin = 9; //Here too
void setup() {
// initialize serial communication:
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(10,OUTPUT);
digitalWrite(trigPin, LOW);
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
void loop()
{
// establish variables for duration of the ping,
// and the distance result in centimeters:
long duration, dcm;
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
// convert the time into a distance
dcm = microsecondsToCentimeters(duration);
Serial.print(dcm);
Serial.print("cm");
Serial.println();
if ( dcm>10 && dcm<20)
{
Serial.print("distante ");
digitalWrite(10,HIGH);
delay(1000);
digitalWrite(10,LOW);
delay(1000);
}
else if (dcm>20)
{
Serial.print("distantissimo ");
digitalWrite(10,HIGH);
delay(2000);
digitalWrite(10,LOW);
delay(2000);
}
delay(500);
}