2

Author Topic: Trying to make a simple code to turn on an LED with the SRF05 and Arduino  (Read 2154 times)

0 Members and 1 Guest are viewing this topic.

Offline XyverTopic starter

  • Robot Overlord
  • ****
  • Posts: 125
  • Helpful? 0
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
}

Offline Razor Concepts

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
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....)

Offline amando96

  • Robot Overlord
  • ****
  • Posts: 187
  • Helpful? 1
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.
Rorcle, 60% complete
AATV, 5% complete

Offline madsci1016

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
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.

Offline Ro-Bot-X

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,431
  • Helpful? 25
  • Store: RoBotXDesigns.ca
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);
}
Check out the uBotino robot controller!

Offline XyverTopic starter

  • Robot Overlord
  • ****
  • Posts: 125
  • Helpful? 0
BEAUTIFUL! Thats exactly what I was looking for.  Thank you :D

 


Get Your Ad Here

data_list