Go Down

Topic: Can RangeFinder perform/trigger two operations? (Read 422 times) previous topic - next topic

blairjo

I currently have an ultrasonic range finder programmed to turn a servo within a distance <20. I also want this same sensor to operate my LEDs and was wondering if I can have that operation run in tandem?

The LEDs are on a separate arduino and are programmed to operate in succession as an individual comes closer and closer to the object. Therefore, I need the sensor to fire each specified pin for the LEDs as indicated, but still turn the motor. How can I combine these functions?

Thanks.

example code for LED using serial.print:

if (howfar < 50) { // start of in range actions
   if (howfar > 40) {  // "far"
     digitalWrite(9, HIGH);   // sets the red LED on
   }
   else if (howfar > 20) // "medium"
   {
     digitalWrite(10, HIGH);   // sets the yellow LED on
   }
   else // "near"
   {
     digitalWrite(11, HIGH);   // sets the green LED on
   }
 } // end of in-range actions

robtillaart


make a function out of it and call it wherever you like. You forgot to switch off the leds.

Code: [Select]

#define REDLED 9
#define YELLOWLED 10
#define GREENLED 11

void doLeds(int howfar)
{
  digitalWrite(REDLED , LOW);
  digitalWrite(YELLOWLED , LOW);
  digitalWrite(GREENLED , LOW);

  if (howfar >= 50) return;
  if (howfar > 40)
  {
    digitalWrite(REDLED , HIGH);
    return;
  }
  if (howfar > 20)
  {
    digitalWrite(YELLOWLED , HIGH);
    return;
  }
  digitalWrite(GREENLED , HIGH);   
  return;
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up