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.
#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 == '
trafficlight.ino (4.02 KB)){
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();
}
}
[trafficlight.ino|attachment](upload://2i4sYjmu3Xa6H6tI38G0Nqeiuum.ino) (4.02 KB)