Go Down

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


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?


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


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);
  if (howfar > 20)
    digitalWrite(YELLOWLED , HIGH);
  digitalWrite(GREENLED , HIGH);   
Rob Tillaart

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

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131