Feasibility?

I have just bought an Arduino, but since it's coming from China.. It's going to be about a month for shipping. :0 I was wondering if someone could tell me if the code I've written would actually work once it's set up in a circuit. It checks out within the Arduino coding program (ie. It gets compiled and has no errors show up). In the end I have a traffic light I'd like to set up as a parking aid in my garage. (Door opens; green light comes on. Enter the garage with the car; yellow light comes on. Get to the "parked" position in the garage; red light comes on.) Could anyone help me out? Even if you can't test it could I get some suggestions to improve the code? Also, what would be the most feasible method of lighting up the lights? LEDs powered by the Arduino? LEDs powered by an Arduino relay? Regular lights powered by a regular 110v relay? All the same? Thanks guys. 8)

PS I suppose I should get the code up for y'all to read... Here you go (I've annotated as best as I can)... (if you couldn't tell, I'm a total newbie..) :

/*Ultrasonic Parking aid 
 *Using an ultrasonic range sensor(duh)
 *trigPin & echoPin on the sensor
 *greenPin for the green light, 
 *yellowPin for the yellow light,
 *redPin for the red.
 */

int trigPin = 10;
int echoPin = 11; //I've just assigned random pins, I don't know if they would work.
int greenPin = 2;
int yellowPin = 3;
int redPin = 4;

void setup()
{
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop()
{
  int duration;
  int distance;
  digitalWrite(trigPin, HIGH);
  delay(250); //I'd like the sensor to basically refresh often
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) /29.1;
  int range = map(distance, 9000, 2000, 1, 3); //I want the green to come on at 9m,
  switch(distance){ 
    case 1:
    digitalWrite(greenPin, HIGH); 
    break;
    case 2:
    digitalWrite(greenPin, LOW);
    digitalWrite(yellowPin, HIGH); //yellow to come on half way,
    break;
    case 3:
    digitalWrite(yellowPin, LOW); //Red to come on at 2m away from the sensor.
    digitalWrite(redPin, HIGH); 
  }
}

You bought a clone more than likely, and not a real Arduino.

Have you had a look at any Ping Sensor examples?\

LEDs are easy to control - where will the Green LEDs be placed? Outside? With Yellow & Red inside? Or all inside, with Green to start and Yellow when you cross some sensor, or distance sensing kicks in?

CrossRoads: You bought a clone more than likely, and not a real Arduino.

Meh, It'll do the job.

CrossRoads: Have you had a look at any Ping Sensor examples?\

I have and this is where they've gotten me...

CrossRoads: LEDs are easy to control - where will the Green LEDs be placed? Outside? With Yellow & Red inside? Or all inside, with Green to start and Yellow when you cross some sensor, or distance sensing kicks in?

All the lights will be inside, whether they be LEDs or otherwise, in the same location. I was thinking Green comes on when the ultrasonic sensor can see past the garage door, yellow coming on when it can see the car(which would be closer than the door previously was) and red to stop.

int distance; //integer . . . distance = (duration/2) /29.1; //?

distance = (duration/2) /29.1 //equation I got from a couple ping sensor examples..

I was just pointing out 29.1 is a floating point number, use 29 an integer.

LarryD: I was just pointing out 29.1 is a floating point number, use 29 an integer.

Ah, I see..

Would there be any advantage to using:

float duration; float distance; . . . distance = (duration/2) /29.1 ?

Would it work?

Meh, It'll do the job.

You calculate range but then switch on distance. Should you not be switching on range which does not seem to be used?

The system seems fairly foolproof as you should know if things are going wrong e.g. If the sensor has been bumped and is pointing through the door at the sky you will reverse past the door but not see yellow. Likewise if the red light is broken it will not come on but you will see the yellow going out.

radman: You calculate range but then switch on distance. Should you not be switching on range which does not seem to be used?

You're right, my bad. It shouldn't be 'switch(distance)' rather 'switch(range)'. Thank you. :blush:

Just curious; What is the advantage of buying from China, particularly with the shipping delay of a a month?

radman: Just curious; What is the advantage of buying from China, particularly with the shipping delay of a a month?

My E-bay purchases from China arrive in about 10/11 days consistently. Main advantages are price and selection.

Lefty

Finally got my board today… experimented for hours… and this is the code that works the way I want it to… I think one of my relay boards isn’t working properly.

/*Ultrasonic Parking aid 
 *Using an ultrasonic range sensor(duh)
 *trigPin & echoPin on the sensor
 *greenPin for the green light, 
 *yellowPin for the yellow light,
 *redPin for the red.
 */
 
int trigPin = 10;
int echoPin = 11;
int greenPin = 2;
int yellowPin = 3;
int redPin = 4;

void setup()
{
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(redPin, OUTPUT);
  pinMode(yellowPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
}

void loop()
{
  float duration;
  float distance;
  digitalWrite(trigPin, HIGH);
  delay(200); //I'd like the sensor to basically refresh very often
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) /29.1;
  if (distance >= 100 , distance == 0)
  {
    digitalWrite(greenPin, HIGH);
    digitalWrite(yellowPin, LOW);
    digitalWrite(redPin, LOW);
  }
  else
    if (distance <= 50 && distance >= 26)
    {
      digitalWrite(greenPin, LOW);
      digitalWrite(yellowPin, HIGH);
      digitalWrite(redPin, LOW);
    }
    else
    if (distance <= 25 && distance >= 1)
    {
      digitalWrite(greenPin, HIGH);
      digitalWrite(yellowPin, HIGH);
      digitalWrite(redPin, HIGH);
    }
}

When the red was supposed to go on red yellow and green would all go on so I went back and reversed the outputs for when red was supposed to be on… as you can see it looks kind of backwards… but it works!

Rather than use HIGH and LOW directly, what I like to do is

#define ON LOW
#define OFF HIGH

at the top of the sketch. Now you can do

digitalWrite(redPin, OFF);
if (distance >= 100 , distance == 0)

That comma isn't doing whatever you think it's doing.

okay... Well how would I tell the Arduino "or" ? I know for "and" it's &&... how would you put "or"?

http://arduino.cc/en/Reference/Boolean

Joanruve:
how would you put “or”?

if (distance >= 100 or distance == 0)

Thank you,Tyler ]:)