Quantcast
Channel: Intel Communities: Message List
Viewing all articles
Browse latest Browse all 22051

HC-SR04 Sketch

$
0
0

HI All,

I'm attempting to find a way to hook up my HC-SR04 to Galileo and since the NewPing and Ultrasonic libraries both don't work because of either the pulseIn function not being implemented or dependencies on the AVR. I decided to try a different route and wrote up this sketch using digital read and write functions to get the reading from the HC-SR04.  The sketch works fine on the Arduino Uno, but when I transfer the sketch to the Galileo board it appears to hang if the value from the HC-SR04 changes...  I'm stuck on figuring this out any insights would be awesome.

 

#define ECHOPIN 3
#define TRIGGERPIN 4
#define US_ROUNDTRIP_IN 146   
#define US_ROUNDTRIP_CM 57    
#define PingConvert(echoTime, conversionFactor) ((echoTime + conversionFactor / 2) / conversionFactor)
unsigned long pinStart;
unsigned long pinEnd;
void setup(){  pinMode(TRIGGERPIN, OUTPUT);  pinMode(ECHOPIN, INPUT);  pinMode(13, OUTPUT); 
}
void loop(){  digitalWrite(TRIGGERPIN,LOW);  delayMicroseconds(2);  digitalWrite(TRIGGERPIN,HIGH);  delayMicroseconds(10);  digitalWrite(TRIGGERPIN,LOW);  while(digitalRead(ECHOPIN)==LOW){}; //waiting for the pin to trigger  pinStart = micros();  while(digitalRead(ECHOPIN)==HIGH){};//waiting for the pin to go low  pinEnd = micros();  unsigned int echoTime = pinEnd - pinStart;  if(PingConvert(echoTime,US_ROUNDTRIP_IN)<6){    digitalWrite(13, HIGH);    delay(2000);  }  digitalWrite(13, LOW);  delay(1000);
}

Viewing all articles
Browse latest Browse all 22051

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>