Pages: [1]   Go Down
Author Topic: Can RangeFinder perform/trigger two operations?  (Read 402 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 217
Posts: 13739
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Code:
#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;
}
Logged

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: