Society of Robots - Robot Forum

Software => Software => Topic started by: Xyver on May 29, 2010, 05:02:30 PM

Title: Trying to make a simple code to turn on an LED with the SRF05 and Arduino
Post by: Xyver on May 29, 2010, 05:02:30 PM
I've been working on this for at least 5 hours now, trying to modify codes but nothing is working.  Im trying to get the SRF05 to read distance, and when that distance is 20cm, turn on an LED.  If not, leave the LED off.  I know it's simple, but Im really lost.    The closest I've gotten was to get the LED to flash off for a fraction of a second everytime the SRF05 sent a signal.

Im using the single pin mode, I know i have soldered it right, the basic code is working I just cant modify it.  Heres the basic:
Code: [Select]
/*
Arduino example for SRF05
Using a single pin for both trigger and echo.
*/

int duration;                          // Stores duratiuon of pulse in
int distance;                          // Stores distance
int srfPin = 2;                        // Pin for SRF05

void setup(){
Serial.begin(9600);
}

void loop(){
  pinMode(srfPin, OUTPUT);
  digitalWrite(srfPin, LOW);           // Make sure pin is low before sending a short high to trigger ranging
  delayMicroseconds(2);
  digitalWrite(srfPin, HIGH);          // Send a short 10 microsecond high burst on pin to start ranging
  delayMicroseconds(10);
  digitalWrite(srfPin, LOW);           // Send pin low again before waiting for pulse back in
  pinMode(srfPin, INPUT);
  duration = pulseIn(srfPin, HIGH);    // Reads echo pulse in from SRF05 in micro seconds
  distance = duration/58;              // Dividing this by 58 gives us a distance in cm
  Serial.println(distance);
  delay(50);                           // Wait before looping to do it again
}
Title: Re: Trying to make a simple code to turn on an LED with the SRF05 and Arduino
Post by: Razor Concepts on May 29, 2010, 05:32:00 PM
Your code seems 100% correct.

Is the trigger pin (center one) on pin 2 of arduino?
Is the mode pin connected to ground?
Are you using the proper set of pins on the sonar (they designed it pretty horribly with the programming pins looking exactly the same like the control ones... and unlabeled....)
Title: Re: Trying to make a simple code to turn on an LED with the SRF05 and Arduino
Post by: amando96 on May 29, 2010, 06:17:43 PM
never used those sensors, try putting the pin mode inside the setup(), never seen it in the loop, not sure if it makes a difference, but try it anyway.
Title: Re: Trying to make a simple code to turn on an LED with the SRF05 and Arduino
Post by: madsci1016 on May 29, 2010, 08:23:47 PM
never used those sensors, try putting the pin mode inside the setup(), never seen it in the loop, not sure if it makes a difference, but try it anyway.

he has to change the pinmode to initiate then read the sensor, so moving it to the setup will just mess it up further.
Title: Re: Trying to make a simple code to turn on an LED with the SRF05 and Arduino
Post by: Ro-Bot-X on May 29, 2010, 09:06:48 PM
Code: [Select]
/*
Arduino example for SRF05
Using a single pin for both trigger and echo.
*/

#define LedPin 13                        // pin for LED                         
#define srfPin    2                        // Pin for SRF05

int duration = 0;                          // Stores duration of pulse in
int distance = 0;                          // Stores distance

void setup(){
  pinMode(LedPin, OUTPUT);
  digitalWrite(LedPin, LOW);     //Led off
  Serial.begin(9600);
}

void loop(){
  Read_SRF05();
  if(distance<20){                    // distance less than 20 cm
    digitalWrite(LedPin, HIGH);   //Led on
  } else {
    digitalWrite(LedPin, LOW);   //Led off
  }
  delay(100);                           // Wait 0.1 second before looping to do it again


void Read_SRF05(){
  pinMode(srfPin, OUTPUT);
  digitalWrite(srfPin, LOW);           // Make sure pin is low before sending a short high to trigger ranging
  delayMicroseconds(2);
  digitalWrite(srfPin, HIGH);          // Send a short 10 microsecond high burst on pin to start ranging
  delayMicroseconds(10);
  digitalWrite(srfPin, LOW);           // Send pin low again before waiting for pulse back in
  pinMode(srfPin, INPUT);
  duration = pulseIn(srfPin, HIGH);    // Reads echo pulse in from SRF05 in micro seconds
  distance = duration/58;              // Dividing this by 58 gives us a distance in cm
  Serial.println(distance);
}
Title: Re: Trying to make a simple code to turn on an LED with the SRF05 and Arduino
Post by: Xyver on May 30, 2010, 09:27:53 PM
BEAUTIFUL! Thats exactly what I was looking for.  Thank you :D