Go Down

Topic: Traffic Management System using arduino uno+ethernet shield and wamp server (Read 1 time) previous topic - next topic

RNSJR

Good day maam/sir, this project is about traffic lights in an intersection to be controlled through web. Now, i have already controlled a single led only as a start. Also, i have a code for a fixed time traffic light system in an intersection. And now the problem is i can't combine the two, controlling through web and the normal fixed time traffic light system in an intersection. i am a newbie in arduino w/ ethernet shield. i am having difficulties with this. Please, anyone who have ideas about this, help me with this.

by the way, here are the codes, these codes are from examples in the net and i combined them. this is a temporary code. its just that i want ideas "on how to's" so that i will recode it and make the final.

Code: [Select]
#include <SPI.h>
#include <Ethernet.h>

boolean incoming = 0;

//4 way Traffic Signal Controller
//By Nikhil Mascarenhas ( nikhil.mascarenhas@gmail.com )
//Feel free to use, distribute and modify this code.
//Code is hosted at: https://github.com/nm1291/4-way-traffic-Signal---Intersection-Controller-using-ArduinoUNO
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x83, 0x04 };
IPAddress ip(10,10,10,102);
//4 x 3 matrix rows represents  the traffic signals for the 4 roads
//columns represent red, yellow, green.
int sig[4][3] = {A0,A1,A2,10,9,8,7,6,5,4,3,2};
EthernetServer server(80);
//buzzer can be used if required
int buz = 1;


void setup()

  int i,j;
  for(i=0;i<4;i++)
    for(j=0;j<3;j++)
      pinMode(sig[i][j], OUTPUT);
  pinMode(buz,OUTPUT);
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.begin(9600);
 
}

//defines signal statuses when 'k' th  road is green
void stateGo(int k)
{

   //buzzer is off
   analogWrite(buz,0);
   //all lights are turned off for a fraction of a second.
   int i,j;
    for(i=0;i<4;i++)
      for(j=0;j<3;j++)
        digitalWrite(sig[i][j],LOW);
       
    //all roads except the 'k'th road is red.
    for(i=0;i<4;i++)
      if(i!=k)
        digitalWrite(sig[i][0],HIGH);
   
    //kth road is green.
    digitalWrite(sig[k][2],HIGH);
   
    //wait 24 seconds
    delay(24000);
   
    //blink green as a "hurry up" warning for next 6 seconds
    for(i=0;i<6;i++)
    {
      digitalWrite(sig[k][2],LOW);
      delay(500);
      digitalWrite(sig[k][2],HIGH);
      delay(500);
    }
}

//defines signal statuses when the 'k' th signal is "ready to go"
//'k-1'th signal is returning from green.
void statePause(int k)
{
  int i,j;
  int p = k-1;
  if(p==-1)
    p=3;
   
  //turn off all lights for a fraction of a second.
  for(i=0;i<4;i++)
      for(j=0;j<3;j++)
        digitalWrite(sig[i][j],LOW);
       
  //all red lights except p (i.e k-1) are red.
  for(i=0;i<4;i++)
    if(i!=p)
          digitalWrite(sig[i][0], HIGH);
 
  //also  p and k roads have yellow
  digitalWrite(sig[p][1],HIGH);
  digitalWrite(sig[k][1],HIGH);
 
  //for 5 seconds switch off and switch on buzzer.
  for(i=0;i<10;i++)
  {
    analogWrite(buz,255);
    delay(250);
    analogWrite(buz,0);
    delay(250);
  }
}
void loop()
{
  connection();
            stateGo(0);
            statePause(1);
            stateGo(1);
            statePause(2);
            stateGo(2);
            statePause(3);
            stateGo(3);
            statePause(0);
 
}
void connection()
{
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
       
        //reads URL string from $ to first blank space
        if(incoming && c == ' '){
          incoming = 0;
           
        }
        if(c == '$'){
          incoming = 1;
        }
       
        //Checks for the URL string $1 or $2
        if(incoming == 1){
          Serial.println(c);
         
          if(c == '1'){
            Serial.println("ON");
            digitalWrite(A0, LOW);
            digitalWrite(A1, LOW);
            digitalWrite(A2, HIGH);
          }
          if(c == '2'){
            Serial.println("OFF");
            digitalWrite(A0, HIGH);
            digitalWrite(A1, HIGH);
            digitalWrite(A2, LOW);
          }
       
        }

        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
}


Arrch

If you're wondering why your code is italicized, it's because you didn't post your code using the code tags. Edit your post to fix that.

PaulS

If you can post all your code, there is no reason to attach it, too. If you modify your post, delete all that code (your code doesn't REALLY look like that, does it?), use the # icon, and repost your code, we might actually be inclined to look at it.

If you say something about what the combined code does that you don't want, or doesn't do that you do want, we might actually even be able to help you.

I don't expect anyone to log on to a web site and change traffic lights as I'm driving to work. I don't see why you are trying to enable that in your system. Please explain.

RNSJR

Sirs PaulS and Arrch, thanks for your replies. I have edited the post.

Actually, the project is on web based traffic management system. The purpose of this is to control or balance or reduce congestion. Also, this project enables Activities on the road such as parades or other stuffs like road reconstruction to be prioritized and also so that people will know which intersections are congested and so that they will find convenient ways for them to go through. Because of these cited problems, the project has been realized. and so i have to make a prototype of the system. im still a student and the project is a requirement to pass so please help me sirs. :D

the code i have uploaded is a combination of controlling a simple led through a web server and a normal traffic light logic.

so in the connection() function.. instead of single led be controlled, i have replaced it with a traffic light which composes(red,orange,green)

so if i enter the url http://ip address/$1... traffic light connected to A0,A1,A2 will have values 0,0,1(go sign) and if http://ip address/$2, 1,0,0(stop sign).., but all i get is a normal logic of traffic lights in an intersection.. i cant control one way traffic light.

how can i do this sirs? or anyone? i am very open to suggestions and ideas. needed badly. hehe

Arrch

but all i get is a normal logic of traffic lights in an intersection.. i cant control one way traffic light.

Because right after you read the http data and make changes to the lights with this function:
Code: [Select]
connection();

You undo those changes by running "normal logic" right afterwords with this part:
Code: [Select]
            stateGo(0);
            statePause(1);
            stateGo(1);
            statePause(2);
            stateGo(2);
            statePause(3);
            stateGo(3);
            statePause(0);


What you need to do is define your requirements better. What happens after the change is made over the web request? Does it never return to "normal logic"? Do you send it a command to return it? Is there a timeout? Once you get those questions answered, you can work on the logic to come up with exactly how you want this thing controlled.

RNSJR

i have already a plan on how my traffic light will be functioning, but the problem is i cant code it. i dont know how. sorry sir but im still a novice.

maybe i just need to see examples... somewhat like while the one way traffic light is running in green, i can stop it and make it in red light... just that example code maybe i can set that as a reference... :(

i hope you'd help me with codes sir,

zoomkat

One place to start is learning to use a google advanced search of the forum for previous similar post. Traffic light projects seem to be fairly frequent.

https://www.google.com/search?hl=en&as_q=traffic+light&as_epq=&as_oq=&as_eq=&as_nlo=&as_nhi=&lr=&cr=&as_qdr=all&as_sitesearch=http%3A%2F%2Farduino.cc%2Fforum%2F&as_occt=any&safe=images&tbs=&as_filetype=&as_rights=
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

RNSJR

Need help...

Code: [Select]
#include <SPI.h>
#include <Ethernet.h>

boolean incoming = 0;

//4 way Traffic Signal Controller
//By Nikhil Mascarenhas ( nikhil.mascarenhas@gmail.com )
//Feel free to use, distribute and modify this code.
//Demo Video: http://www.youtube.com/watch?v=srhC-1_fXeQ
//Code is hosted at: https://github.com/nm1291/4-way-traffic-Signal---Intersection-Controller-using-ArduinoUNO
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x83, 0x04 };
IPAddress ip(169,254,138,93);
//4 x 3 matrix rows represents  the traffic signals for the 4 roads
//columns represent red, yellow, green.
int sig[4][3] = {A0,A1,A2,A3,9,8,7,6,5,4,3,2};
EthernetServer server(80);
//buzzer can be used if required
int buz = 1;
unsigned long time;
unsigned long looptime;
int count_down=24;

void setup()

  int i,j;
  for(i=0;i<4;i++)
    for(j=0;j<3;j++)
      pinMode(sig[i][j], OUTPUT);
  pinMode(buz,OUTPUT);
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.begin(9600);
  time=millis();
  looptime=time;
}

//defines signal statuses when 'k' th  road is green
void stateGo(int k)
{

   //buzzer is off
   analogWrite(buz,0);
   //all lights are turned off for a fraction of a second.
   int i,j;
    for(i=0;i<4;i++)
      for(j=0;j<3;j++)
        digitalWrite(sig[i][j],LOW);
       
   do{
   
    //all roads except the 'k'th road is red.
    for(i=0;i<4;i++)
      if(i!=k)
      {
        digitalWrite(sig[i][0],HIGH);
   
    //kth road is green.
        digitalWrite(sig[k][2],HIGH);
      }
     
     delay(1000);
    }while((millis()/1000) < count_down+(time/1000));
   
   

    //blink green as a "hurry up" warning for next 6 seconds
    for(i=0;i<6;i++)
    {
      digitalWrite(sig[k][2],LOW);
      delay(500);
      digitalWrite(sig[k][2],HIGH);
      delay(500);
    }
    time=millis();
}

//defines signal statuses when the 'k' th signal is "ready to go"
//'k-1'th signal is returning from green.
void statePause(int k)
{
  int i,j;
  int p = k-1;
  if(p==-1)
    p=3;
   
  //turn off all lights for a fraction of a second.
  for(i=0;i<4;i++)
      for(j=0;j<3;j++)
        digitalWrite(sig[i][j],LOW);
       
  //all red lights except p (i.e k-1) are red.
  for(i=0;i<4;i++)
    if(i!=p)
          digitalWrite(sig[i][0], HIGH);
 
  //also  p and k roads have yellow
  digitalWrite(sig[p][1],HIGH);
  digitalWrite(sig[k][1],HIGH);
 
  //for 5 seconds switch off and switch on buzzer.
  for(i=0;i<10;i++)
  {
    analogWrite(buz,255);
    delay(250);
    analogWrite(buz,0);
    delay(250);
  }
  time=millis();
}
void loop()
{
   
  connection();
 
}
void connection()
{
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
       
        //reads URL string from $ to first blank space
        if(incoming && c == ' '){
          incoming = 0;
           
        }
        if(c == '$'){
          incoming = 1;
        }
       
        //Checks for the URL string $1 or $2
        if(incoming == 1){
          Serial.println(c);
         
          if(c == '1'){
            Serial.println("ON");
            while(c == '1'){
            reset();
            }
           
          }
          if(c == '2'){
            Serial.println("OFF");
            redAll();
          }
          if(c == '3'){
            Serial.println("OFF");
           
          }
          if(c == '4'){
            int i,j;
    for(i=0;i<4;i++)
      for(j=0;j<3;j++)
        digitalWrite(sig[i][j],LOW);
          }
       
        }

        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
}
void reset()
{
            stateGo(0);
            statePause(1);
            stateGo(1);
            statePause(2);
            stateGo(2);
            statePause(3);
            stateGo(3);
            statePause(0);
}
void redAll()
{
  int i,j;
  for(i=0;i<4;i++)
      for(j=0;j<3;j++)
        digitalWrite(sig[i][j],LOW);
  for(i=0;i<4;i++)
          digitalWrite(sig[i][0], HIGH);
}


now, when i type this http://169.254.138.93/$1.. traffic lights start because of this
Code: [Select]
if(c == '1'){
            Serial.println("ON");
            while(c == '1'){
            reset();
            }

anyways i put while loop so that reset function operates.

but i cant interrupt it.. i entered http://169.254.138.93/$2 to make it all red lights but its not working
Code: [Select]
if(c == '2'){
            Serial.println("OFF");
            redAll();
          }


any suggestions pls... are there other ways?.. reply asap.. ty sirs

wildbill

You can't interrupt it because that while loop will call the reset function forever once c is '1' and you'll thus never read anything else from the ethernet shield.

A way round this is to use a global state variable that you set to tell you which action to perform. Set it where you're currently checking for c being '1', '2' etc. Then in loop, use the status variable to decide which of your light setting routines to call. Then every time round loop, connection will go to see if there's been a new http request. If there has, the state variable may have changed and you will start calling a different light function. Otherwise, you'll just call the same one you did the last time round loop.

RNSJR

Quote
A way round this is to use a global state variable that you set to tell you which action to perform.

isn't c a global variable?
Quote
Then every time round loop, connection will go to see if there's been a new http request. If there has, the state variable may have changed and you will start calling a different light function. Otherwise, you'll just call the same one you did the last time round loop.

sir, in if(c=='1') even if there is no while loop there, the reset function has to be finished in execution so that a new http request will be entertained. i just think so. i tried it sir.
maybe you have the idea sir but, i cant quite get it, maybe an example code would be great sir.. :)

wildbill

c isn't a global variable - it's declared in the connection function. Globals are declared outside functions, e.g. your looptime variable.

What I'm suggesting is that you declare a global integer variable, let's call it state. Then, in your loop routine, you can do this:
Code: [Select]

void loop()
{
connection();
if(state==1)
  reset();
else if (state==2)
  redall();
}


In the connection routine, where you check the parameter you got in the http request, rather than calling your routines to set lights, just assign the approriate value to the state variable. Then when connection completes, you'll be back to loop which will choose the appropriate light function repeatedly. Subsequent http requests will now get read and can change state so that loop will pick a different function to run the lights.

Note that a switch statement would probably be more appropriate and the 1 and 2 above should really be declared as const ints or an enum but that should get you started.

 

RNSJR

i have tried it sir,

i made
Code: [Select]
char connection()
its the same execution as from my previous code.
it still cannot interfere or interrupt the function reset() while it is executing.

RNSJR

Good day sirs! now i can interrupt my traffic lights. And so i have to proceed to the next step.. which is checking the status of the traffic lights.

somewhat like reading the pin state and post it on php web. i have this code but its not working..
Code: [Select]

void fetching()
{
   int i,j,m=0;
  EthernetClient client=server.available();
  if (client) {
    Serial.println("new client");
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
       
        client.println("GET /php_site/ledStatus.php?");
       
        for(i=0;i<4;i++)
        {
            for(j=0;j<3;j++)
            {
                client.print("L");
                Serial.print("L");
                client.print(m);
                Serial.print(m);
                client.print("=");
                Serial.print("=");
                client.print(digitalRead(led[i][j]));
                Serial.print(digitalRead(led[i][j]));
                Serial.println();
                m++;
            }
        }
         client.println(" HTTP/1.1");
         Serial.println(" HTTP/1.1");
         client.println("Host: www.jfkreuter.com");
         Serial.println("Host: www.jfkreuter.com");
         client.println("User-Agent: Arduino");
         Serial.println("User-Agent: Arduino");
         client.println("Accept: text/html");
         Serial.println("Accept: text/html");
         client.println("Connection: close");
         Serial.println("Connection: close");
         client.println();
         Serial.println();
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}


i hope someone can help me with this problem. :)


PeterH

I only provide help via the forum - please do not contact me for private consultancy.

PaulS

Quote
i have this code but its not working..

It works, as in it does something. What it does is a mystery, as is what you want it to do, and how those two differ. If you provide more details, we'll provide more help.

Go Up