TRAFFIC LIGHT WITH BARRIER

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);
}

Step 1
Auto Format your code in the IDE to make it more readable

Step 2
Remove the superfluous pair of braces revealed by formatting the code

Step 3
Give the pins meaningful names to make following the code easier

Step 4
Make the comments match the code or remove them when they are not needed

Step 5
Read Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE because you need to use millis() for timing in your code

Step 0 :D Use code tags when posting code.

@jeffnavin, edit your post, type ``` [code] ``` before your code and ``` [/code] ``` after your code

Hi sterretje/bob;

Thanks for your response, could you please support in writing a code for me. the max knowledge i gained so far is to compile the freesource code available in internet and compile it. Hardware and understanding the code isnt challenging but writing one is really challenging. Please help me by sharing a code

Thanks jeff

Jeffnavin: could you please support in writing a code for me.

The keyword is indeed support. And UKHeliBob and sterretje did a good job at it. But we're not going to write it for you. This is a forum for help for people who want to learn how to write code. So main input has to be from you aka if you want [u]help[/u], start by doing what you're asked. However, we do have the section Gigs and Collaborations where you can ask to write code for you. But do expect to pay for it.

Jeffnavin: Thanks for your response, could you please support in writing a code for me. the max knowledge i gained so far is to compile the freesource code available in internet and compile it. Hardware and understanding the code isnt challenging but writing one is really challenging. Please help me by sharing a code

Glad to support you in writing your code.

To get a servo moving find the Sweep example in the IDE and understand it. Change the 180s to 120s and the delays so it moves as fast/slow as you want. Then just put the bits of code from there into the appropriate places in your traffic light code and you've done it.

Steve

i did it but it always shows error.

My crystal ball is out for service. ::) Can you please post your code and the complete error message? TIP: read How to use the forum to do it correct ;)

Jeffnavin: i did it but it always shows error.

Fine. So post what you did and full details of the error(s) it showed. Then we will have something to help you with.

Steve

Hi Steve,

Please find my code below, and correct my errors,

#include <Servo.h>
Servo myservo;
int pos = 0;
void setup()
{
myservo.attach(11); //servo pin
pinMode(10, OUTPUT); //red led
pinMode(9, OUTPUT); //yellow led
pinMode(8, OUTPUT); //green led
}

void loop() {

digitalWrite(8, HIGH);
delay(2500);
{
myservo.attach(11);
for (pos = 0; pos <= 120; pos += 1)
myservo.write(pos);
delay(1500);
}
digitalWrite(8, LOW);
delay(0);
}
digitalWrite(9, HIGH);
delay(2500);
{
for (pos = 120; pos >= 90; pos -= 1)
myservo.write(pos);
delay(1500)
}
digitalWrite(9, LOW);
delay(0);
digitalWrite(10, HIGH);
delay(2500);
{
for (pos = 90; pos >= 0; pos -= 1)
myservo.write(pos);
delay(1500)
}
digitalWrite(10, LOW);
}

and correct my errors

{No}. {You} {need} {to} {correct} {them}.

Start by learning when curly braces are needed, when they are optional (but highly recommended), and when they just look stupid.

What was so hard about:

septillion: TIP: read How to use the forum to do it correct ;)

and

sterretje: Step 0 :D Use code tags when posting code.

@jeffnavin, edit your post, type ``` [code] ``` before your code and ``` [/code] ``` after your code

:(

You’re getting there. Much of that code is indeed the sort of thing you need.

Normally I’d ask what works and what doesn’t. But I know that doesn’t work at all because it doesn’t even compile. And that’s because you ended loop() with a } far too early so you have lots of code that isn’t in loop() or any other function. Get that fixed so it at least compiles and post the next version.

Then when it runs you’ll find that most of your delay()s are either the wrong value or in the wrong places. E.g. after you switch the green LED on do you really want to wait 2.5 seconds before you even start moving the servo VERY VERY SLOWLY?

Steve

Hopefully i guess i got the code, compilation was success but still hardware test is pending, Need feedback;

#include <Servo.h>
Servo myservo;
int pos = 0;
int led = {1, 2, 3};
void setup() {
// initialize the digital pin as an output.
for (int i = 0; i < 3; i++)
pinMode(led*, OUTPUT);*

  • myservo.attach(9);*
    }
    void loop() {
  • int i = 0;*
    _ digitalWrite(led*, HIGH); //_
    _
    for (pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees*_
    * { // in steps of 1 degree*
    * myservo.write(pos); // tell servo to go to position in variable ‘pos’*
    * delay(15); // waits 15ms for the servo to reach the position*
    * }*
    * digitalWrite(led[i++], LOW); //*
    _ digitalWrite(led*, HIGH); //
    for (pos = 180; pos >= 1; pos -= 1) // goes from 180 degrees to 0 degrees*
    * {
    myservo.write(pos); // tell servo to go to position in variable ‘pos’
    delay(15); // waits 15ms for the servo to reach the position*
    * }
    digitalWrite(led[i++], LOW); //
    digitalWrite(led, HIGH); //
    delay(2000);
    digitalWrite(led, LOW); // turn the LED off by making the voltage LOW*

    }_

Why are you still refusing to use code tags? That’s going to end up resulting in people NOT helping you anymore. Please try to respect the forum etiquette. You’ve been asked to read more than once. Here is a picture showing you where to find this information. Please don’t post again until you read it and take it to heart. If you won’t even do that for us then why should we be writing code for you?

Look_Here.jpg