I need help on my project

hello people. i have a problem here to my project. my project is like that you see in the picture.

the bridge is doing by himself lift and close when the boat is attend to cross over.

the bridge is controlled by infrared sensors. when the car pass by add one car, when he pass by the

other decreases that number. on the side of boats. they have too infrared sensors to do all the same

thing. but when the boat pass by the sensor, the traffic light goes to red and check if is something on the

bridge to make a may to boat pass by. i have 2 motors that help to lift up the bridge.

i have two relays to make the bridge open or close. but i don’t know how to put in the program

sorry about my english, not so good i believe xD

my program so far is this:

int WestRed=1;
int WestYellow=2;
int WestGreen=3;
int EastRed=4;
int EastYellow=5;
int EastGreen=6;
int NorthRed=7;
int NorthGreen=8;
int SouthRed=9;
int SouthGreen=10;
int WestInfra1=11;
int WestInfra2=12;
int EastInfra1=13;
int EastInfra2=14;
int NorthInfra=15;
int SouthInfra=16;
int WestMotor=17;
int EastMotor=18;
int way1;
int way2;
int way3;

void setup() { 
 pinMode(WestRed,OUTPUT);
 pinMode(WestYellow,OUTPUT);
 pinMode(WestGreen,OUTPUT);
 pinMode(EastRed,OUTPUT);
 pinMode(EastYellow,OUTPUT);
 pinMode(EastGreen,OUTPUT);
 pinMode(NorthRed,OUTPUT);
 pinMode(NorthGreen,OUTPUT);
 pinMode(SouthRed,OUTPUT);
 pinMode(SouthGreen,OUTPUT);
 pinMode(WestInfra1,INPUT);
 pinMode(WestInfra2,INPUT);
 pinMode(EastInfra1,INPUT);
 pinMode(EastInfra2,INPUT);
 pinMode(NorthInfra,INPUT);
 pinMode(SouthInfra,INPUT);
 pinMode(WestMotor,OUTPUT);
 pinMode(EastMotor,OUTPUT);
}




void GreenYellow(int g, int y){ 
 digitalWrite(g, LOW);
 digitalWrite(y, HIGH);
}



void YellowRed(int y, int r){ 
 digitalWrite(y, LOW);
 digitalWrite(r, HIGH);

}



void RedGreen(int r, int g){ 
 digitalWrite(r, LOW);
 digitalWrite(g, HIGH);
}



void wait(int i){
 delay(i * 1000);
}



void bridge(){
if( WestGreen==HIGH && WestInfra1<300){
   WestInfra1++;  
 }else if(WestGreen==HIGH && WestInfra2<300){
   WestInfra2--;
   way1=WestInfra1-WestInfra2;
 }
  if(EastGreen==HIGH && EastInfra1<300){
   EastInfra1++;  
 }else if(EastGreen==HIGH && EastInfra2<300){
   EastInfra2--;
   way2=EastInfra1-EastInfra2;
}
}
 


void Ship(){
if(NorthInfra<300){
 GreenYellow(WestGreen,WestYellow);
 GreenYellow(EastGreen,EastYellow);
 wait(2);
 YellowRed(WestYellow,WestRed);
 YellowRed(EastYellow,EastRed);
 wait(4);
 }
if(way1==0 && way2==0){
 digitalWrite(WestMotor,HIGH);
 digitalWrite(EastMotor,HIGH);
 wait(5);
 NorthInfra++;
 SouthInfra--;
 way3=NorthInfra-SouthInfra;
}else if(SouthInfra<300){
 GreenYellow(WestGreen,WestYellow);
 GreenYellow(EastGreen,EastYellow);
 wait(2);
 YellowRed(WestYellow,WestRed);
 YellowRed(EastYellow,EastRed);
 wait(4);
if(way1==0 && way2==0){
 digitalWrite(WestMotor,HIGH);
 digitalWrite(EastMotor,HIGH);
 wait(5);
 SouthInfra++;
 NorthInfra--;
 way3=SouthInfra-NorthInfra;                
}
}
}


void Return(){
 if(way3==0){
   digitalWrite(WestMotor,LOW);
   digitalWrite(EastMotor,LOW);
   RedGreen(WestRed,WestGreen);
   RedGreen(EastRed,EastGreen);
 }
}


void loop() {

bridge();
Ship();
Return();
}

bridge.jpg

Please put your code in its own window as seen in other posts. This can be done by placing     [code]  and [/code]  around the code or use the </> icon. This makes it easier for others to read.

Before doing so please also use the Auto format command under the Tools menu. This lays out the code so it is easier to follow.

How to use this forum

Weedpharma

now it is better now to follow?

Please document your code.

I assume that e.g. a line like int WestInfra1 = 11; defines a pin for an infrared sensor. In which case the below does not make sense

  if ( WestGreen == HIGH && WestInfra1 < 300) {
    WestInfra1++;
  }

I suspect that this is supposed to count cars going from west to east so keep a counter (e.g. countWE). I’m not sure if your sensor will be HIGH or LOW when it does not detect light; let’s assume LOW.

In the below, WestInfra1 detects cars that enter the bridge from the west and WestInfra2 detect cars leaving the bridge going to the east.

if(WestGreen == HIGH && digitalRead(WestInfra1) == LOW)
{
  // wait for car to clear sensor
  while(digitalRead(WestInfra1) != HIGH);
  countWE++;
}

if(WestGreen == HIGH && digitalRead(WestInfra2) == LOW)
{
  // wait for car to clear sensor
  while(digitalRead(WestInfra2) != HIGH);
  countWE--;
}

Note:
The traffic light for traffic entering the bridge from the west might be red (so WestGreen will not be HIGH, I assume). In which case countWE will never be decremented; same if a car skips the red traffic light :wink: So the color of a traffic light should not influence the counting.

Note:
This is blocking; if a car breaks down in front of a sensor, the rest of your code will not execute.

You have several differently named functions that do exactly the same thing:

void GreenYellow(int g, int y){
 digitalWrite(g, LOW);
 digitalWrite(y, HIGH);
}

void YellowRed(int y, int r){
 digitalWrite(y, LOW);
 digitalWrite(r, HIGH);

}

void RedGreen(int r, int g){
 digitalWrite(r, LOW);
 digitalWrite(g, HIGH);
}

They are all equivalent to:

void writeLights(int toLow, int toHigh)
{
 digitalWrite(toLow, LOW);
 digitalWrite(toHigh, HIGH);
}

Which seems like an oddball kind of function. Perhaps it reveals a design weakness elsewhere.

when you said that i was trying to understand but i didn't understanding very well Can you explain to me in the other way? i'm like a noob to make this things

``void writeLights(int toLow, int toHigh) { digitalWrite(toLow, LOW); digitalWrite(toHigh, HIGH); }

sterretje:
Please document your code.

I assume that e.g. a line like int WestInfra1 = 11; defines a pin for an infrared sensor. In which case the below does not make sense

  if ( WestGreen == HIGH && WestInfra1 < 300) {

WestInfra1++;
 }




I suspect that this is supposed to count cars going from west to east so keep a counter (e.g. countWE). I'm not sure if your sensor will be HIGH or LOW when it does not detect light; let's assume LOW.

In the below, WestInfra1 detects cars that enter the bridge from the west and WestInfra2 detect cars leaving the bridge going to the east.



if(WestGreen == HIGH && digitalRead(WestInfra1) == LOW)
{
 // wait for car to clear sensor
 while(digitalRead(WestInfra1) != HIGH);
 countWE++;
}

if(WestGreen == HIGH && digitalRead(WestInfra2) == LOW)
{
 // wait for car to clear sensor
 while(digitalRead(WestInfra2) != HIGH);
 countWE–;
}



Note:
The traffic light for traffic entering the bridge from the west might be red (so WestGreen will not be HIGH, I assume). In which case countWE will never be decremented; same if a car skips the red traffic light ;) So the color of a traffic light should not influence the counting.

Note:
This is blocking; if a car breaks down in front of a sensor, the rest of your code will not execute.

the point is you are right, if a car breaks down, the bridge never lift up. that is a way to protection to when the boat waits for the bridge lift up, if a car breaks down, he has to wait until the car is gone if you understand

CptSnakePT: the point is you are right, if a car breaks down, the bridge never lift up. that is a way to protection to when the boat waits for the bridge lift up, if a car breaks down, he has to wait until the car is gone if you understand

The problem is that it will also block counting for the traffic from east to west; so cars can enter or leave and you will never count it.

sterretje: The problem is that it will also block counting for the traffic from east to west; so cars can enter or leave and you will never count it.

so, how can i resolve that problem?

CptSnakePT: when you said that i was trying to understand but i didn't understanding very well Can you explain to me in the other way? i'm like a noob to make this things

void writeLights(int toLow, int toHigh) { digitalWrite(toLow, LOW); digitalWrite(toHigh, HIGH); }

What other way can I explain it? All those functions do the same thing. You could say for example:

GreenYellow(WestYellow,WestRed);

and it would work fine (but any way you use this will turn your readers minds to mush).

It's like having four different bottle openers, one for each different kind of beer. :)

aarg: What other way can I explain it? All those functions do the same thing. You could say for example:

GreenYellow(WestYellow,WestRed);

and it would work fine (but any way you use this will turn your readers minds to mush).

It's like having four different bottle openers, one for each different kind of beer. :)

i understand but if you know the traffic light has 3 colours. but i don't need the yellow at the bridge?

CptSnakePT: so, how can i resolve that problem?

Make it non-blocking ;)

The below code counts the traffic coming from the west. It uses a static variable to remember if the sensor is blocked or not; because it's local to the function, it is only accessible inside the function. The variable is part of a state-change detection.

/*
  count cars entering bridge from the west
  assumes that the infrared sensor goes LOW when a car blocks the beam
  increments the global countWE variable
*/
void countWestIn()
{
  // indicator that car blocked sensor (or not); start with not blocked
  static bool blocked = false;

  // if sensor was not blocked and is blocked now
  if (blocked == false && digitalRead(WestIn) == LOW)
  {
    // remember that sensor is blocked
    blocked = true;
  }
  // if sensor was blocked an is no longer blocked
  if (blocked == true && digitalRead(WestIn) == HIGH)
  {
    // increment west-east counter
    countWE++;
    // remember that sensor is not blocked
    blocked = false;
  }
}

You can use the below as a framework. It implements counting for west-east and east-west traffic; you can add other counting. Note that I renamed the pins to something a bit more sensible (in my opinion) and also changed the declaration to use 'const' to indicate to the compiler that these variables can not be changed.

/***********************************************
  west-east traffic pins and variables
***********************************************/
// sensor to count traffic coming in from the west
const int WestIn = 11;
// sensor to count traffic going out to the east
const int EastOut = 12;
// counter to keep track of west-east traffic
int countWE = 0;

/***********************************************
  east-west traffic pins and variables
***********************************************/
// sensor to count traffic coming in from the west
const int EastIn = 13;
// sensor to count traffic going out to the east
const int WestOut = 14;
// counter to keep track of east-west traffic
int countEW = 0;


void setup()
{
  ...
  ...
}


/***********************************************
west-east traffic counter functions
***********************************************/
/*
  count cars entering bridge from the west
  assumes that the infrared sensor goes LOW when a car blocks the beam
  increments the global countWE variable
*/
void countWestIn()
{
  // indicator that car blocked sensor (or not); start with not blocked
  static bool blocked = false;

  // if sensor was not blocked and is blocked now
  if (blocked == false && digitalRead(WestIn) == LOW)
  {
    // remember that sensor is blocked
    blocked = true;
  }
  // if sensor was blocked an is no longer blocked
  if (blocked == true && digitalRead(WestIn) == HIGH)
  {
    // increment west-east counter
    countWE++;
    // remember that sensor is not blocked
    blocked = false;
  }
}

/*
  count cars leaving bridge to the east
  assumes that the infrared sensor goes LOW when a car blocks the beam
  decrements the global countWE variable
*/
void countEastOut()
{
  // indicator that car blocked sensor (or not); start with not blocked
  static bool blocked = false;

  // if sensor was not blocked and is blocked now
  if (blocked == false && digitalRead(EastOut) == LOW)
  {
    // remember that sensor is blocked
    blocked = true;
  }
  // if sensor was blocked an is no longer blocked
  if (blocked == true && digitalRead(EastOut) == HIGH)
  {
    // decrement west-east counter
    countWE--;
    // remember that sensor is not blocked
    blocked = false;
  }
}

/***********************************************
east-west traffic counter functions
***********************************************/

/*
  count cars entering bridge from the east
  assumes that the infrared sensor goes LOW when a car blocks the beam
  increments the global countEW variable
*/
void countEastIn()
{
  // indicator that car blocked sensor (or not); start with not blocked
  static bool blocked = false;

  // if sensor was not blocked and is blocked now
  if (blocked == false && digitalRead(EastIn) == LOW)
  {
    // remember that sensor is blocked
    blocked = true;
  }
  // if sensor was blocked an is no longer blocked
  if (blocked == true && digitalRead(EastIn) == HIGH)
  {
    // increment east-west counter
    countEW++;
    // remember that sensor is not blocked
    blocked = false;
  }
}

/*
  count cars leaving bridge to the west
  assumes that the infrared sensor goes LOW when a car blocks the beam
  decrements the global countEW variable
*/
void countWestOut()
{
  // indicator that car blocked sensor (or not); start with not blocked
  static bool blocked = false;

  // if sensor was not blocked and is blocked now
  if (blocked == false && digitalRead(WestOut) == LOW)
  {
    // remember that sensor is blocked
    blocked = true;
  }
  // if sensor was blocked an is no longer blocked
  if (blocked == true && digitalRead(WestOut) == HIGH)
  {
    // decrement east-west counter
    countEW--;
    // remember that sensor is not blocked
    blocked = false;
  }
}

void loop()
{
  countWestIn();
  countEastOut();
  countEastIn();
  countWestOut();
  ...
  ...

}

It compiles but is not tested. I hope this gets you on track.

sterretje: Make it non-blocking ;)

The below code counts the traffic coming from the west. It uses a static variable to remember if the sensor is blocked or not; because it's local to the function, it is only accessible inside the function. The variable is part of a state-change detection.

/*
  count cars entering bridge from the west
  assumes that the infrared sensor goes LOW when a car blocks the beam
  increments the global countWE variable
*/
void countWestIn()
{
  // indicator that car blocked sensor (or not); start with not blocked
  static bool blocked = false;

  // if sensor was not blocked and is blocked now   if (blocked == false && digitalRead(WestIn) == LOW)   {     // remember that sensor is blocked     blocked = true;   }   // if sensor was blocked an is no longer blocked   if (blocked == true && digitalRead(WestIn) == HIGH)   {     // increment west-east counter     countWE++;     // remember that sensor is not blocked     blocked = false;   } }




You can use the below as a framework. It implements counting for west-east and east-west traffic; you can add other counting. Note that I renamed the pins to something a bit more sensible (in my opinion) and also changed the declaration to use 'const' to indicate to the compiler that these variables can not be changed.



/***********************************************   west-east traffic pins and variables ***********************************************/ // sensor to count traffic coming in from the west const int WestIn = 11; // sensor to count traffic going out to the east const int EastOut = 12; // counter to keep track of west-east traffic int countWE = 0;

/***********************************************   east-west traffic pins and variables ***********************************************/ // sensor to count traffic coming in from the west const int EastIn = 13; // sensor to count traffic going out to the east const int WestOut = 14; // counter to keep track of east-west traffic int countEW = 0;

void setup() {   ...   ... }

/*********************************************** west-east traffic counter functions **********************************************/ /   count cars entering bridge from the west   assumes that the infrared sensor goes LOW when a car blocks the beam   increments the global countWE variable */ void countWestIn() {   // indicator that car blocked sensor (or not); start with not blocked   static bool blocked = false;

  // if sensor was not blocked and is blocked now   if (blocked == false && digitalRead(WestIn) == LOW)   {     // remember that sensor is blocked     blocked = true;   }   // if sensor was blocked an is no longer blocked   if (blocked == true && digitalRead(WestIn) == HIGH)   {     // increment west-east counter     countWE++;     // remember that sensor is not blocked     blocked = false;   } }

/*   count cars leaving bridge to the east   assumes that the infrared sensor goes LOW when a car blocks the beam   decrements the global countWE variable */ void countEastOut() {   // indicator that car blocked sensor (or not); start with not blocked   static bool blocked = false;

  // if sensor was not blocked and is blocked now   if (blocked == false && digitalRead(EastOut) == LOW)   {     // remember that sensor is blocked     blocked = true;   }   // if sensor was blocked an is no longer blocked   if (blocked == true && digitalRead(EastOut) == HIGH)   {     // decrement west-east counter     countWE--;     // remember that sensor is not blocked     blocked = false;   } }

/*********************************************** east-west traffic counter functions ***********************************************/

/*   count cars entering bridge from the east   assumes that the infrared sensor goes LOW when a car blocks the beam   increments the global countEW variable */ void countEastIn() {   // indicator that car blocked sensor (or not); start with not blocked   static bool blocked = false;

  // if sensor was not blocked and is blocked now   if (blocked == false && digitalRead(EastIn) == LOW)   {     // remember that sensor is blocked     blocked = true;   }   // if sensor was blocked an is no longer blocked   if (blocked == true && digitalRead(EastIn) == HIGH)   {     // increment east-west counter     countEW++;     // remember that sensor is not blocked     blocked = false;   } }

/*   count cars leaving bridge to the west   assumes that the infrared sensor goes LOW when a car blocks the beam   decrements the global countEW variable */ void countWestOut() {   // indicator that car blocked sensor (or not); start with not blocked   static bool blocked = false;

  // if sensor was not blocked and is blocked now   if (blocked == false && digitalRead(WestOut) == LOW)   {     // remember that sensor is blocked     blocked = true;   }   // if sensor was blocked an is no longer blocked   if (blocked == true && digitalRead(WestOut) == HIGH)   {     // decrement east-west counter     countEW--;     // remember that sensor is not blocked     blocked = false;   } }

void loop() {   countWestIn();   countEastOut();   countEastIn();   countWestOut();   ...   ...

}



It compiles but is not tested. I hope this gets you on track.

thanks a lot friend. i will test it and i tell if it work well ;)

sterretje, i have to ask you a few things.

i forget to tell you which infra sensor i used.

the infra that stays on the bridge is TCRT5000L infrared sensor.

on the boat i have 2 different sensors. one is Sharp GP2Y0A21YK0F and the other is a TSOP 2238, which i

have a emissor and a receptor separaty.(the others they are together).

one the problem is about the counting boats to see if a boat cross the bridge to close bridge and

restart the traffic.

you can see the code here ( i add the comments to understand better the program)

#include <SharpIR.h>


int WestRed=1;
int WestYellow=2;
int WestGreen=3;
int EastRed=4;
int EastYellow=5;
int EastGreen=6;
int NorthRed=7;
int NorthGreen=8;
int SouthRed=9;
int SouthGreen=10;
const int WestIn=11;
const int EastOut=12;
int countWE = 0;
const int EastIn=13;
const int WestOut=14;
int countEW = 0;
#define NorthInfra A0
#define model 1080
SharpIR sharp(NorthInfra, 25, 93, model);
boolean done=false;
int SouthInfra=16;
int WestMotor=17;
int EastMotor=18;
int countNS;
int countSN;

void setup() { 
  pinMode(WestRed,OUTPUT);
  pinMode(WestYellow,OUTPUT);
  pinMode(WestGreen,OUTPUT);
  pinMode(EastRed,OUTPUT);
  pinMode(EastYellow,OUTPUT);
  pinMode(EastGreen,OUTPUT);
  pinMode(NorthRed,OUTPUT);
  pinMode(NorthGreen,OUTPUT);
  pinMode(SouthRed,OUTPUT);
  pinMode(SouthGreen,OUTPUT);
  pinMode(WestIn,INPUT);
  pinMode(EastOut,INPUT);
  pinMode(EastIn,INPUT);
  pinMode(WestOut,INPUT);
  pinMode(NorthInfra,INPUT);
  pinMode(SouthInfra,INPUT);
  pinMode(WestMotor,OUTPUT);
  pinMode(EastMotor,OUTPUT);
}
void countWestIn()
{
  // indicator that car blocked sensor (or not); start with not blocked
  static bool blocked = false;

  // if sensor was not blocked and is blocked now
  if (blocked == false && digitalRead(WestIn) == LOW)
  {
    // remember that sensor is blocked
    blocked = true;
  }
  // if sensor was blocked an is no longer blocked
  if (blocked == true && digitalRead(WestIn) == HIGH)
  {
    // increment west-east counter
    countWE++;
    // remember that sensor is not blocked
    blocked = false;
  }
}
void countEastOut()
{
  // indicator that car blocked sensor (or not); start with not blocked
  static bool blocked = false;

  // if sensor was not blocked and is blocked now
  if (blocked == false && digitalRead(EastOut) == LOW)
  {
    // remember that sensor is blocked
    blocked = true;
  }
  // if sensor was blocked an is no longer blocked
  if (blocked == true && digitalRead(EastOut) == HIGH)
  {
    // decrement west-east counter
    countWE--;
    // remember that sensor is not blocked
    blocked = false;
  }
}
void countEastIn()
{
  // indicator that car blocked sensor (or not); start with not blocked
  static bool blocked = false;

  // if sensor was not blocked and is blocked now
  if (blocked == false && digitalRead(EastIn) == LOW)
  {
    // remember that sensor is blocked
    blocked = true;
  }
  // if sensor was blocked an is no longer blocked
  if (blocked == true && digitalRead(EastIn) == HIGH)
  {
    // increment east-west counter
    countEW++;
    // remember that sensor is not blocked
    blocked = false;
  }
}
void countWestOut()
{
  // indicator that car blocked sensor (or not); start with not blocked
  static bool blocked = false;

  // if sensor was not blocked and is blocked now
  if (blocked == false && digitalRead(WestOut) == LOW)
  {
    // remember that sensor is blocked
    blocked = true;
  }
  // if sensor was blocked an is no longer blocked
  if (blocked == true && digitalRead(WestOut) == HIGH)
  {
    // decrement east-west counter
    countEW--;
    // remember that sensor is not blocked
    blocked = false;
  }
}

//traffic light function
void GreenYellow(int g, int y){ 
  digitalWrite(g, LOW);
  digitalWrite(y, HIGH);
}
void YellowRed(int y, int r){ 
  digitalWrite(y, LOW);
  digitalWrite(r, HIGH);
}
void RedGreen(int r, int g){ 
  digitalWrite(r, LOW);
  digitalWrite(g, HIGH);
}
void GreenRed(int g, int r){ 
  digitalWrite(g, LOW);
  digitalWrite(r, HIGH);
}

void yellow1(){ // i add this to make a realist traffic light
digitalWrite(WestYellow,HIGH);
delay(500);
digitalWrite(WestYellow,LOW);
delay(500);   
digitalWrite(WestYellow,HIGH);
delay(500);
digitalWrite(WestYellow,LOW);
delay(500);
digitalWrite(WestYellow,HIGH);
delay(500);
digitalWrite(WestYellow,LOW);
}
void yellow2(){
digitalWrite(EastYellow,HIGH);
delay(500);
digitalWrite(EastYellow,LOW);
delay(500);   
digitalWrite(EastYellow,HIGH);
delay(500);
digitalWrite(EastYellow,LOW);
delay(500);
digitalWrite(EastYellow,HIGH);
delay(500);
digitalWrite(EastYellow,LOW);
}

// time in seconds
void wait(int i){
  delay(i * 1000);
}

//when the ship blocks the infrared sensor

void Ship(){
if(NorthInfra<18){ // when the north sensor is blocked first
  GreenYellow(WestGreen,WestYellow);
  GreenYellow(EastGreen,EastYellow);
  yellow1();
  yellow2();
  YellowRed(WestYellow,WestRed);
  YellowRed(EastYellow,EastRed);
  }
if(countWE==0 && countEW==0){ // when the two counts for cars at the bridge are 0
  digitalWrite(WestMotor,HIGH);
  digitalWrite(EastMotor,HIGH);
  wait(5);                      // in here i gonna used relays 
  digitalWrite(WestMotor,LOW);
  digitalWrite(EastMotor,LOW);
  RedGreen(NorthRed,NorthGreen);
  RedGreen(SouthRed,SouthGreen);
      do{  // I add this to wait when the boat blocks the last infrared sensor to begin the void return
  delay(500);
} while (SouthInfra<18);
  NorthInfra++;   // that is a problem when you compile :/
  SouthInfra--;
  countNS=NorthInfra-SouthInfra;
}else if(SouthInfra<18){  // when the other infrared sensor blocks first
  GreenYellow(WestGreen,WestYellow);
  GreenYellow(EastGreen,EastYellow);
  yellow1();
  yellow2();
  YellowRed(WestYellow,WestRed);
  YellowRed(EastYellow,EastRed);
  }
if(countWE==0 && countEW==0){ // the same counting to cars
  digitalWrite(WestMotor,HIGH);
  digitalWrite(EastMotor,HIGH);
  wait(5);                    // use relays(again)
  digitalWrite(WestMotor,LOW);
  digitalWrite(EastMotor,LOW);
  RedGreen(NorthRed,NorthGreen);
  RedGreen(SouthRed,SouthGreen);
      do{       // here is the samething but the same problem at NorthInfra
  delay(500);
} while (NorthInfra<18);
  SouthInfra++;
  NorthInfra--;
  countSN=SouthInfra-NorthInfra;
}
}            


void Return(){    // when the ship is gone
  if(countNS==0 or countSN==0){
    GreenRed(NorthGreen,NorthRed);
    GreenRed(SouthGreen,SouthRed);
    digitalWrite(WestMotor,HIGH);
    digitalWrite(EastMotor,HIGH);
    wait(5);
    digitalWrite(WestMotor,LOW);
    digitalWrite(EastMotor,LOW);
    RedGreen(WestRed,WestGreen);
    RedGreen(EastRed,EastGreen);
  }
}


void loop() {
countWestIn();
countEastOut();
countEastIn();
countWestOut();
Ship();
Return();
}

hi guys, my newest problem on my program is this:

in the other topic that i start - see here at http://forum.arduino.cc/index.php?topic=381385.0

i have a problem with the infrared sensors, because i forget to tell you all which infra sensor i used.

the infra that stays on the bridge is TCRT5000L infrared sensor.

On the boat i have 2 different sensors. one is Sharp GP2Y0A21YK0F and the other is a TSOP IR sensor,

which i have a emissor and a receptor separaty.(the others they are together).

one the problem is about the counting boats to see if a boat cross the bridge to close bridge and

restart the traffic.

you can see the code here ( i add the comments to understand better the program)

#include <SharpIR.h>


int WestRed=1;
int WestYellow=2;
int WestGreen=3;
int EastRed=4;
int EastYellow=5;
int EastGreen=6;
int NorthRed=7;
int NorthGreen=8;
int SouthRed=9;
int SouthGreen=10;
const int WestIn=11;
const int EastOut=12;
int countWE = 0;
const int EastIn=13;
const int WestOut=14;
int countEW = 0;
#define NorthInfra A0
#define model 1080
SharpIR sharp(NorthInfra, 25, 93, model);
boolean done=false;
int SouthInfra=16;
int WestMotor=17;
int EastMotor=18;
int countNS;
int countSN;

void setup() { 
  pinMode(WestRed,OUTPUT);
  pinMode(WestYellow,OUTPUT);
  pinMode(WestGreen,OUTPUT);
  pinMode(EastRed,OUTPUT);
  pinMode(EastYellow,OUTPUT);
  pinMode(EastGreen,OUTPUT);
  pinMode(NorthRed,OUTPUT);
  pinMode(NorthGreen,OUTPUT);
  pinMode(SouthRed,OUTPUT);
  pinMode(SouthGreen,OUTPUT);
  pinMode(WestIn,INPUT);
  pinMode(EastOut,INPUT);
  pinMode(EastIn,INPUT);
  pinMode(WestOut,INPUT);
  pinMode(NorthInfra,INPUT);
  pinMode(SouthInfra,INPUT);
  pinMode(WestMotor,OUTPUT);
  pinMode(EastMotor,OUTPUT);
}
void countWestIn()
{
  // indicator that car blocked sensor (or not); start with not blocked
  static bool blocked = false;

  // if sensor was not blocked and is blocked now
  if (blocked == false && digitalRead(WestIn) == LOW)
  {
    // remember that sensor is blocked
    blocked = true;
  }
  // if sensor was blocked an is no longer blocked
  if (blocked == true && digitalRead(WestIn) == HIGH)
  {
    // increment west-east counter
    countWE++;
    // remember that sensor is not blocked
    blocked = false;
  }
}
void countEastOut()
{
  // indicator that car blocked sensor (or not); start with not blocked
  static bool blocked = false;

  // if sensor was not blocked and is blocked now
  if (blocked == false && digitalRead(EastOut) == LOW)
  {
    // remember that sensor is blocked
    blocked = true;
  }
  // if sensor was blocked an is no longer blocked
  if (blocked == true && digitalRead(EastOut) == HIGH)
  {
    // decrement west-east counter
    countWE--;
    // remember that sensor is not blocked
    blocked = false;
  }
}
void countEastIn()
{
  // indicator that car blocked sensor (or not); start with not blocked
  static bool blocked = false;

  // if sensor was not blocked and is blocked now
  if (blocked == false && digitalRead(EastIn) == LOW)
  {
    // remember that sensor is blocked
    blocked = true;
  }
  // if sensor was blocked an is no longer blocked
  if (blocked == true && digitalRead(EastIn) == HIGH)
  {
    // increment east-west counter
    countEW++;
    // remember that sensor is not blocked
    blocked = false;
  }
}
void countWestOut()
{
  // indicator that car blocked sensor (or not); start with not blocked
  static bool blocked = false;

  // if sensor was not blocked and is blocked now
  if (blocked == false && digitalRead(WestOut) == LOW)
  {
    // remember that sensor is blocked
    blocked = true;
  }
  // if sensor was blocked an is no longer blocked
  if (blocked == true && digitalRead(WestOut) == HIGH)
  {
    // decrement east-west counter
    countEW--;
    // remember that sensor is not blocked
    blocked = false;
  }
}

//traffic light function
void GreenYellow(int g, int y){ 
  digitalWrite(g, LOW);
  digitalWrite(y, HIGH);
}
void YellowRed(int y, int r){ 
  digitalWrite(y, LOW);
  digitalWrite(r, HIGH);
}
void RedGreen(int r, int g){ 
  digitalWrite(r, LOW);
  digitalWrite(g, HIGH);
}
void GreenRed(int g, int r){ 
  digitalWrite(g, LOW);
  digitalWrite(r, HIGH);
}

void yellow1(){ // i add this to make a realist traffic light
digitalWrite(WestYellow,HIGH);
delay(500);
digitalWrite(WestYellow,LOW);
delay(500);   
digitalWrite(WestYellow,HIGH);
delay(500);
digitalWrite(WestYellow,LOW);
delay(500);
digitalWrite(WestYellow,HIGH);
delay(500);
digitalWrite(WestYellow,LOW);
}
void yellow2(){
digitalWrite(EastYellow,HIGH);
delay(500);
digitalWrite(EastYellow,LOW);
delay(500);   
digitalWrite(EastYellow,HIGH);
delay(500);
digitalWrite(EastYellow,LOW);
delay(500);
digitalWrite(EastYellow,HIGH);
delay(500);
digitalWrite(EastYellow,LOW);
}

// time in seconds
void wait(int i){
  delay(i * 1000);
}

//when the ship blocks the infrared sensor

void Ship(){
if(NorthInfra<18){ // when the north sensor is blocked first
  GreenYellow(WestGreen,WestYellow);
  GreenYellow(EastGreen,EastYellow);
  yellow1();
  yellow2();
  YellowRed(WestYellow,WestRed);
  YellowRed(EastYellow,EastRed);
  }
if(countWE==0 && countEW==0){ // when the two counts for cars at the bridge are 0
  digitalWrite(WestMotor,HIGH);
  digitalWrite(EastMotor,HIGH);
  wait(5);                      // in here i gonna used relays 
  digitalWrite(WestMotor,LOW);
  digitalWrite(EastMotor,LOW);
  RedGreen(NorthRed,NorthGreen);
  RedGreen(SouthRed,SouthGreen);
      do{  // I add this to wait when the boat blocks the last infrared sensor to begin the void return
  delay(500);
} while (SouthInfra<18);
  NorthInfra++;   // that is a problem when you compile :/
  SouthInfra--;
  countNS=NorthInfra-SouthInfra;
}else if(SouthInfra<18){  // when the other infrared sensor blocks first
  GreenYellow(WestGreen,WestYellow);
  GreenYellow(EastGreen,EastYellow);
  yellow1();
  yellow2();
  YellowRed(WestYellow,WestRed);
  YellowRed(EastYellow,EastRed);
  }
if(countWE==0 && countEW==0){ // the same counting to cars
  digitalWrite(WestMotor,HIGH);
  digitalWrite(EastMotor,HIGH);
  wait(5);                    // use relays(again)
  digitalWrite(WestMotor,LOW);
  digitalWrite(EastMotor,LOW);
  RedGreen(NorthRed,NorthGreen);
  RedGreen(SouthRed,SouthGreen);
      do{       // here is the samething but the same problem at NorthInfra
  delay(500);
} while (NorthInfra<18);
  SouthInfra++;
  NorthInfra--;
  countSN=SouthInfra-NorthInfra;
}
}            


void Return(){    // when the ship is gone
  if(countNS==0 or countSN==0){
    GreenRed(NorthGreen,NorthRed);
    GreenRed(SouthGreen,SouthRed);
    digitalWrite(WestMotor,HIGH);
    digitalWrite(EastMotor,HIGH);
    wait(5);
    digitalWrite(WestMotor,LOW);
    digitalWrite(EastMotor,LOW);
    RedGreen(WestRed,WestGreen);
    RedGreen(EastRed,EastGreen);
  }
}


void loop() {
countWestIn();
countEastOut();
countEastIn();
countWestOut();
Ship();
Return();
}

I have asked the Moderator to merge this with your other Thread so everyone has easy access to all the info about your project.

Why don't you use titles that tell us what you want help with so we don't have to open the Thread to find out if we can help.

...R

ok you are right... i put the program with comments and there have the comment "that is a problem when you

compile :/" and below have the some problem. about the "NorthInfra" IR sensor

    NorthInfra++;   // that is a problem when you compile :/
    SouthInfra--;

Those are your pins; you're trying to change the pin that you read, not increment a counter.

Why don't you use the same approach as for the cars using functions (northIn / southOut and southIn / northOut) to count the ships?

ok i will try that. thanks :)