Need Help to complete this code

Hi Coders,

I am totally new to arduino and to this forum, I am from class 8 looking for a project for my expo. I had this idea of automatic barrier integrated to traffic lights.

This is the idea poped on my head and i am looking for experts help to complete my code.

  1. automatic traffic light system , where the R Y G LEDs goes on and off on the timing fixed

  2. Now i wanted to upgrade with barrier system

  3. when green is high i want the servo to rotate from say 0' to 120' - R and Y low

  4. when yellow is high the servo has to move slowly from 120' to 0' - R and G low

  5. once it reaches to 0' the RED LED has to be high, rest all LED should be low

Please help me at the earliest.I have written the simple code traffic light but need support to complete as per my idea.

void setup() {

pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);

}

void loop()
{
{
digitalWrite(8, HIGH); // Turns LED on pin 13 on
delay(2500); // LED on pin 13 remains on for 5 seconds
digitalWrite(8, LOW); // Turns LED on pin 13 off
delay(0);}
digitalWrite(9, HIGH); // Turns LED on pin 12 on
delay(2500); // LED on pin 12 remains on for 5 seconds
digitalWrite(9, LOW); // Turns LED on pin 12 off
delay(0);
digitalWrite(10, HIGH); // Turns LED on pin 11 on
delay(2500); // LED on pin 11 remains on for 5 seconds
digitalWrite(10, LOW); // Turns LED on pin 11 off
delay(0);
}

Well, there's the right way to do this and the easy way to do it. Let's talk about the easy way.

I will assume that you can work out for yourself how to add a servo to your sketch and set the position on it. Have a look at any of the servo examples in the 'file->examples" menu in the IDE.

The only tricky thing is making the servo move slowly while the light is yellow. To do this, you use a loop and the millis() function.

this:

  digitalWrite(9, HIGH); // Turns LED on pin 12 on
  delay(2500); // LED on pin 12 remains on for 5 seconds
  digitalWrite(9, LOW); //  Turns LED on pin 12 off

becomes:

  digitalWrite(9, HIGH); // Turns LED on pin 12 on

  uint32_t startTime = millis();
  while(millis() - startTime < 2500) {
    double ms = millis() - startTime();
    int servo_angle = 120 - (120.0 * ms / 2500.0);
    barrierServo.write(servo_angle);
    delay(100); // short delay so as not to spam the servo with updates
  }

  barrierServo.write(0); // unconditionally write 0 to the servo just in case something weird happens
  digitalWrite(9, LOW); //  Turns LED on pin 12 off

The code does have a tiny flaw - millis() might update between the execution of the loop condition and the calculation of ms, meaning that ms can wind up with a value of -1 in some very rare instances, whgich might make the servi do something weird. Several ways to fix this. One is:

  digitalWrite(9, HIGH); // Turns LED on pin 12 on

  uint32_t startTime = millis();
  for(;;) {
    double ms = millis() - startTime();
    if(ms>=2500) break;

    int servo_angle = 120 - (120.0 * ms / 2500.0);
    barrierServo.write(servo_angle);
    delay(100); // short delay so as not to spam the servo with updates
  }

  barrierServo.write(0); // unconditionally write 0 to the servo just in case something weird happens
  digitalWrite(9, LOW); //  Turns LED on pin 12 off

You are really great Paul, by the way to be honest. I wish i learn writing code at the earliest as of now these are greek and latin to me, i am just 3 days exposed to what arduino is and what it can do. I have learnt to execute programs available internet and execute the same. Could you please share me the complete code so that it will help me a lot. guess its not too challenging. Please

thanks
jeff

Jeffnavin:
Could you please share me the complete code so that it will help me a lot. guess its not too challenging. Please

If you want a complete sketch written for you, take it to the "Gigs and Collaborations" subforum.

PaulMurrayCbr:
If you want a complete sketch written for you, take it to the "Gigs and Collaborations" subforum.

It's there now :wink: Thought that it actually was there all the time.

sterretje:
It's there now :wink: Thought that it actually was there all the time.

Oh, my mistake.

In that case: I usually charge $50 for a simple sketch like this. I accept payPal. I prefer to do any communication on the public thread, because this board is a teaching/learning resource.