Go Down

Topic: Problem with ultrasonic sensor (Read 4 times) previous topic - next topic

coos


wkuace

I found a "datasheet" for your sensor its more of a guide then a datasheet, it even comes with some source code for the arduino. It looks like you have the right idea, hopefully your sensors are compatible. I took your code tweaked it and added a delay at the end. This will slow it down while your watching the serial terminal. It may also fix the freezing problem but no guarantee.  It could be that you were polling it to fast, to many times and it overloaded the arduino or the sonar. I hope this helps

Code: [Select]

int outputPin = ?; // set you output pin
int inputPin = ?; // set your input pin
long distance;
long cm;
void setup(){
Serial.begin(); // set your baud rate
pinMode(outputPin, OUTPUT);
pinMode(inputPin,INPUT);
}
void loop()

{
  digitalWrite(outputPin, LOW);
  delayMicroseconds(2);
  digitalWrite(outputPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(outputPin, LOW);   
  distance = pulseIn(inputPin, HIGH); //it now initalizes before setup to speed up the process
  cm= distance/58;   //I don't like reusing the same variable name, it probably works fine though,
                             //but i don't really trust it, besides you have plenty of memory for it.
  Serial.println(cm);   
  delay(500); // this will slow down your readings to 2 per second it should help when watching the Serial terminal
// if this helps then start to shrink it down to 100 then 10 then 1, maybe even go to microsecond delays or remove completely if it stops freezing
}


Go Up