Laser trip wire coding help


#1

Could someone please help me on coding this on two arduino/ genuino 101 boards. There is a picture attached displaying when all the components are


Arduino 1 is the master
Arduino 2 is the slave

Arduino 1 is connected to laser 1 and 2
Arduino 2 is connected to laser 3 and 4

If laser 1 is broken and then laser 2 then the lights trigger.
If laser 4 is broken and then laser 3 then the lights trigger.
If laser 2 is broken and then laser 1 then the lights will not trigger.
If laser 3 is broken and then laser 4 then the lights will not trigger.
If both lasers 1 and 2 or both lasers 3 and 4 are triggered at the same time then the lights trigger- means a heavy flow of traffic, the same if all four lasers are triggered at the same time

Once triggered the light will turn from green to red and flash __ flashes per minute

The idea at the moment is to have ‘people on crossing’ integer which is added to when laser 1 is broken before 2 or laser 4 is broken before 3, and subtracted from when laser 2 is broken before 1 or laser 3 is broken before 4

Existing code:
//Setup for pins that measure the photoresitors attached to lasers.

const int sensorPin_1 = 1;
const int sensorPin_2 = 2;
const int sensorPin_3 = 3;
const int sensorPin_4 = 4;

//Setup for pins to control the colour of the leds
const int ledPinRed = 10;
const int ledPinGreen = 11;

// counter for the number of people on the crossing.
// Must equal zero for the light to be green.
int peopleOnCrossing = 0;

//Levels for Photoresistors - will also need mapping - see other file
int lightLevel_1, high_1 = 200, low_1 = 700;
int lightLevel_2, high_2 = 200, low_2 = 700;
int lightLevel_3, high_3 = 200, low_3 = 700;
int lightLevel_4, high_4 = 200, low_4 = 700;

//Boolean conditions for individual laser triggers
boolean laser1Triggered = false;
boolean laser2Triggered = false;
boolean laser3Triggered = false;
boolean laser4Triggered = false;

//Boolean conditions to determine if somebody has entered the crossing.
boolean enteredFrom1_2 = false;
boolean enteredFrom4_3 = false;

//Boolean conditions to determine if somebody has exited the crossing.
boolean exitFrom1_2 = false;
boolean exitFrom4_3 = false;

void setup() {
// put your setup code here, to run once:

Serial.begin(9600);
pinMode(ledPinRed, OUTPUT);
pinMode(ledPinGreen, OUTPUT);

}

void loop() {

//if the photoresistor gives a value of less than 700, the laser beam has been broken.
//the level of each photoresistor is being given by the lightLevel_1, 2, 3 and 4.

// The following analogRead functions are used to get a value from the photoresistor attached to each laser.

lightLevel_1 = analogRead(sensorPin_1);
lightLevel_2 = analogRead(sensorPin_2);
lightLevel_3 = analogRead(sensorPin_3);
lightLevel_4 = analogRead(sensorPin_4);

Serial.println(“lightLevel_1 =”); //Print the name of laser1 to the serial monitor
Serial.println(lightLevel_1); //Print the value of laser1 to the serial monitor
Serial.println(“lightLevel_2 =”); //Print the name of laser2 to the serial monitor
Serial.println(lightLevel_2); //Print the value of laser2 to the serial monitor
Serial.println(“lightLevel_3 =”); //Print the name of laser3 to the serial monitor
Serial.println(lightLevel_3); //Print the value of laser3 to the serial monitor
Serial.println(“lightLevel_4 =”); //Print the name of laser4 to the serial monitor
Serial.println(lightLevel_4); //Print the value of laser4 to the serial monitor

//check if any lasers have been triggered (write in a function)
if (lightLevel_1<700);
laser1Triggered = true;
if (lightLevel_2<700);
laser2Triggered = true;
if (lightLevel_3<700);
laser3Triggered = true;
if (lightLevel_4<700);
laser4Triggered = true;

// if laser 1 then laser 2 are broken, somebody has entered the left crossing
if(laser1Triggered && !laser2Triggered)
{
enteredFrom1_2 = true;
}
if(laser1Triggered && laser2Triggered && enteredFrom1_2)
{
peopleOnCrossing++;

//reset triggers
laser1Triggered = false;
laser2Triggered = false;
enteredFrom1_2 = false;

}

// if laser 2 and then laser 1 are broken, somebody has exited the left crossing
if(laser2Triggered && !laser1Triggered)
{
exitFrom1_2 = true;
}
if(laser1Triggered && laser2Triggered && exitFrom1_2)
{
peopleOnCrossing–;
//reset triggers
laser1Triggered = false;
laser2Triggered = false;
exitFrom1_2 = false;
}

// if laser 4 then laser 3 are broken, somebody has entered the right crossing
if(laser4Triggered && !laser3Triggered)
{
enteredFrom4_3 = true;
}
if(laser4Triggered && laser3Triggered && enteredFrom4_3)
{
peopleOnCrossing++;
//reset triggers
laser4Triggered = false;
laser3Triggered = false;
enteredFrom4_3 = false;
}

// if laser 3 and then laser 4 are broken, somebody has exited the right crossing
if(laser3Triggered && !laser4Triggered)
{
exitFrom4_3 = true;
}
if(laser3Triggered && laser4Triggered && exitFrom4_3)
{
peopleOnCrossing–;

//Print the status of peopleOnCrossing to the serial monitor
Serial.print (“People on Crossing =”);
Serial.print (peopleOnCrossing);

//reset triggers
laser3Triggered = false;
laser4Triggered = false;
exitFrom4_3= false;

}

// control traffic lights
if(peopleOnCrossing > 0)
{
//change to red light
digitalWrite(ledPinGreen, LOW);
digitalWrite(ledPinRed, HIGH);
Serial.print (“I’m on red”);
}
else
{
//change to green light
digitalWrite (ledPinRed, LOW);
digitalWrite (ledPinGreen, HIGH);
Serial.print (“I’m on Green”);
}
}


#2

Your code lost all the indentation, please use three backticks above and below it to make it readable.

Problem with a counter of people on crossing is that it can be unreliable.

Also, “at the same time” is quite hard to determine.

The Arduino library has a millis() function which tells you the time in milliseconds since reset.

I would code the slave to transmit the state of laser 3 and 4 twice a second.

I would code the master to;

  • receive messages from the slave, and remember the time, and automatically flash the lights if the time was more than a second ago, a sort of fail to safe mode,
  • when any of the four laser beams are broken, remember the off time for that photodetector,
  • when any of the four laser beams are seen, remember the on time for that photodetector,
  • decide whether to flash the lights based on the remembered times.

For example, on the master;

void loop() {

    /* get the current time */
    now = millis();

    /* receive messages and set last_receive to now */
    ...

    /* detect loss of reception and turn lights on */
    if (now > last_receive + 1000) {
        lights_on();
        last_on = now;
        return;
    }

    /* notice changed to the photodetectors and 
       set beam_{1,2,3,4}_{on,off} */
    ...

    /* if beam 1 and beam 2 are broken, and
       beam 2 was broken after beam 1 */
    if (beam_1_off > beam_1_on &&
        beam_2_off > beam_2_on &&
        beam_2_off > beam_1_off) {

        lights_on();
        last_on = now;
        return;
    }

    if (beam_3_off > beam_3_on &&
        beam_4_off > beam_4_on &&
        beam_4_off > beam_3_off) {

        lights_on();
        last_on = now;
        return;
    }

    /* turn the lights off after N seconds */
    if (now > last_on + 60000) {
        lights_off();
    }
}

You may also have to code something special to handle the 50 day limit for millis(); either reboot or do extra calculations.