2015-06-29 04:09 AM
I have this sensor HY-SRF05 with 5 pins. In Arduino using the function delayMicroseconds () to manage the operation of the sensor so:
Arduino code:
/*
Tested with HY-SRF05, HC-SR04
Assuming a room temp of 20 degrees centigrade
The circuit:
* VVC connection of the sensor attached to +5V
* GND connection of the sensor attached to ground
* TRIG connection of the sensor attached to digital pin 12
* ECHO connection of the sensor attached to digital pin 13
*/
const
int
TRIG_PIN = 12;
const
int
ECHO_PIN = 13;
void
setup() {
// initialize serial communication:
Serial.begin(9600);
pinMode(TRIG_PIN,OUTPUT);
pinMode(ECHO_PIN,INPUT);
}
void
loop()
{
long
duration, distanceCm, distanceIn;
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN,HIGH);
// convert the time into a distance
distanceCm = duration / 1 / 2 ;
distanceIn = duration / 74 / 2;
if
(distanceCm <= 0){
Serial.println(
''Out of range''
);
}
else
{
Serial.print(distanceIn);
Serial.print(
''in, ''
);
Serial.print(distanceCm);
Serial.print(
''cm''
);
Serial.println();
}
delay(1000);
}
Of SPC560P how can I do? The board does not handle microseconds.
Best regards
Gianluca.
Solved! Go to Solution.
2015-07-01 03:05 AM
Ok, the function osalThreadSleepMicroseconds it works.
Thanks. Best regards Gianluca2015-06-29 04:57 AM
Hello Gianluca ,
you have to use :osalThreadSleepMilliseconds(100);
Best regards
Erwan
2015-06-29 06:10 AM
But 100 milliseconds are 10,000 microseconds. I'd need 2 and 10 microseconds.
Best regards Gianluca2015-06-29 07:31 AM
Sorry ,
it is a bad copy-paste You can use the osal macro#define osalThreadSleepMicroseconds(usec) osalThreadSleep(OSAL_US2ST(usec))
Best regards
Erwan
2015-06-29 12:00 PM
Yes I know. Sorry for bad formatting in the last message, I do not know how it happened. Thanks.
Best Regards Gianluca2015-06-30 01:10 AM
2015-07-01 03:05 AM
Ok, the function osalThreadSleepMicroseconds it works.
Thanks. Best regards Gianluca2015-07-02 12:51 AM