CrazyEngineers
  • Car Volume Based Traffic Light System using IR sensor (Arduino)

    Joerel Bacani

    Member

    Updated: Oct 26, 2024
    Views: 1.7K
    can someone give me advice about the code for this project

    this is my sample code

    int r1 = 1;// pin for the LED
    int g1 = 2;
    int r2 = 4;
    int g2 = 5;
    int r3 = 7;
    int g3 = 8;
    int r4 = 10;
    int g4 = 11;
    int y0 = 13;// all yellow LED are connected in parallel
    int timer = 7000;// set initial timer for the system after first boot
    int isObstaclePinA = 3;// pin for the IR sensor
    int isObstaclePinB = 6;
    int isObstaclePinC = 9;
    int isObstaclePinD = 12;
    int var = 0;

    void setup () {
    pinMode(r1, OUTPUT);
    pinMode(g1, OUTPUT);
    pinMode(r2, OUTPUT);
    pinMode(g2, OUTPUT);
    pinMode(r3, OUTPUT);
    pinMode(g3, OUTPUT);
    pinMode(r4, OUTPUT);
    pinMode(g4, OUTPUT);
    pinMode(y0, OUTPUT);
    pinMode(isObstaclePinA, INPUT);
    pinMode(isObstaclePinB, INPUT);
    pinMode(isObstaclePinC, INPUT);
    pinMode(isObstaclePinD, INPUT);

    }



    void loop () {

    switch (var) { // for enabling the switch command

    case 1:
    if (isObstaclePinA == HIGH);
    digitalWrite(isObstaclePinB, LOW);
    digitalWrite(isObstaclePinC, LOW);
    digitalWrite(isObstaclePinD, LOW);
    digitalWrite(g1, HIGH);// LED lights on
    digitalWrite(r1, LOW);
    digitalWrite(r2, HIGH);
    digitalWrite(r3, HIGH);
    digitalWrite(r4, HIGH);
    delay(timer); // time the lights will turn on

    digitalWrite(y0, HIGH);
    delay(3000); // time the yellow LED will light
    for (isObstaclePinA == HIGH; timer < 50000; timer + 5000); // for adding seconds in the timer every time the sensor responds
    break;

    case 2: // next case
    if (isObstaclePinB == HIGH);
    digitalWrite(isObstaclePinA, LOW);
    digitalWrite(isObstaclePinC, LOW);
    digitalWrite(isObstaclePinD, LOW);
    digitalWrite(g2, HIGH);
    digitalWrite(r2, LOW);
    digitalWrite(r1, HIGH);
    digitalWrite(r3, HIGH);
    digitalWrite(r4, HIGH);
    delay(timer);

    digitalWrite(y0, HIGH);
    delay(3000);
    for (isObstaclePinB == HIGH; timer < 50000; timer + 5000);
    break;

    case 3:
    if (isObstaclePinC == HIGH);
    digitalWrite(isObstaclePinB, LOW);
    digitalWrite(isObstaclePinA, LOW);
    digitalWrite(isObstaclePinD, LOW);
    digitalWrite(g3, HIGH);
    digitalWrite(r3, LOW);
    digitalWrite(r1, HIGH);
    digitalWrite(r2, HIGH);
    digitalWrite(r4, HIGH);
    delay(timer);

    digitalWrite(y0, HIGH);
    delay(3000);
    for (isObstaclePinC == HIGH; timer < 50000; timer + 5000);
    break;

    case 4:
    if (isObstaclePinD == HIGH);
    digitalWrite(isObstaclePinB, LOW);
    digitalWrite(isObstaclePinC, LOW);
    digitalWrite(isObstaclePinA, LOW);
    digitalWrite(g4, HIGH);
    digitalWrite(r4, LOW);
    digitalWrite(r1, HIGH);
    digitalWrite(r2, HIGH);
    digitalWrite(r3, HIGH);
    delay(timer);

    digitalWrite(y0, HIGH);
    delay(3000);
    for (isObstaclePinD == HIGH; timer < 50000; timer + 5000);
    break;
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Abhishek Havanur

    MemberDec 10, 2016

    Joerel Bacani
    can someone give me advice about the code for this project

    this is my sample code

    int r1 = 1;// pin for the LED
    int g1 = 2;
    int r2 = 4;
    int g2 = 5;
    int r3 = 7;
    int g3 = 8;
    int r4 = 10;
    int g4 = 11;
    int y0 = 13;// all yellow LED are connected in parallel
    int timer = 7000;// set initial timer for the system after first boot
    int isObstaclePinA = 3;// pin for the IR sensor
    int isObstaclePinB = 6;
    int isObstaclePinC = 9;
    int isObstaclePinD = 12;
    int var = 0;

    void setup () {
    pinMode(r1, OUTPUT);
    pinMode(g1, OUTPUT);
    pinMode(r2, OUTPUT);
    pinMode(g2, OUTPUT);
    pinMode(r3, OUTPUT);
    pinMode(g3, OUTPUT);
    pinMode(r4, OUTPUT);
    pinMode(g4, OUTPUT);
    pinMode(y0, OUTPUT);
    pinMode(isObstaclePinA, INPUT);
    pinMode(isObstaclePinB, INPUT);
    pinMode(isObstaclePinC, INPUT);
    pinMode(isObstaclePinD, INPUT);

    }



    void loop () {

    switch (var) { // for enabling the switch command

    case 1:
    if (isObstaclePinA == HIGH);
    digitalWrite(isObstaclePinB, LOW);
    digitalWrite(isObstaclePinC, LOW);
    digitalWrite(isObstaclePinD, LOW);
    digitalWrite(g1, HIGH);// LED lights on
    digitalWrite(r1, LOW);
    digitalWrite(r2, HIGH);
    digitalWrite(r3, HIGH);
    digitalWrite(r4, HIGH);
    delay(timer); // time the lights will turn on

    digitalWrite(y0, HIGH);
    delay(3000); // time the yellow LED will light
    for (isObstaclePinA == HIGH; timer < 50000; timer + 5000); // for adding seconds in the timer every time the sensor responds
    break;

    case 2: // next case
    if (isObstaclePinB == HIGH);
    digitalWrite(isObstaclePinA, LOW);
    digitalWrite(isObstaclePinC, LOW);
    digitalWrite(isObstaclePinD, LOW);
    digitalWrite(g2, HIGH);
    digitalWrite(r2, LOW);
    digitalWrite(r1, HIGH);
    digitalWrite(r3, HIGH);
    digitalWrite(r4, HIGH);
    delay(timer);

    digitalWrite(y0, HIGH);
    delay(3000);
    for (isObstaclePinB == HIGH; timer < 50000; timer + 5000);
    break;

    case 3:
    if (isObstaclePinC == HIGH);
    digitalWrite(isObstaclePinB, LOW);
    digitalWrite(isObstaclePinA, LOW);
    digitalWrite(isObstaclePinD, LOW);
    digitalWrite(g3, HIGH);
    digitalWrite(r3, LOW);
    digitalWrite(r1, HIGH);
    digitalWrite(r2, HIGH);
    digitalWrite(r4, HIGH);
    delay(timer);

    digitalWrite(y0, HIGH);
    delay(3000);
    for (isObstaclePinC == HIGH; timer < 50000; timer + 5000);
    break;

    case 4:
    if (isObstaclePinD == HIGH);
    digitalWrite(isObstaclePinB, LOW);
    digitalWrite(isObstaclePinC, LOW);
    digitalWrite(isObstaclePinA, LOW);
    digitalWrite(g4, HIGH);
    digitalWrite(r4, LOW);
    digitalWrite(r1, HIGH);
    digitalWrite(r2, HIGH);
    digitalWrite(r3, HIGH);
    delay(timer);

    digitalWrite(y0, HIGH);
    delay(3000);
    for (isObstaclePinD == HIGH; timer < 50000; timer + 5000);
    break;
    Are you sure? This action cannot be undone.
    Cancel
  • Abhishek Havanur

    MemberDec 10, 2016

    May i know your project problem Statement?
    Are you sure? This action cannot be undone.
    Cancel
  • Joerel Bacani

    MemberDec 13, 2016

    can you help me do the code for this?

    this is how the system will work

    the IR sensor will count the no. of vehicle then convert it to time that the green light will turn on.

    I'm having trouble dealing with it
    thank you
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register