Make sound on laser interrupt

hi,
I'm totally new in working with Arduino and it is so interesting to me.
So I have a project about to make a traffic lights (red, yellow, green). It need to have lights for cars and pedestrian. about cars it is classic, three colors .
About pedestrian there are two lights red and green.
All with lights is ok, they are working fine. But the problem is about two lasers and photo resistors. So both of lasers are on right and left side of the pedestrian walkway. The task is if the pedestrian cross the right laser to beep three times, if he cross the left laser, beep two times.
I have Arduino mega 2560
I tried to do it with analog or digital read of third pin of the photo resistors,
the result is the same, it pass trough the loop and beep (2 or 3 times), but not catch if I interrupt the laser light with my hand to the PR.
Photo resistors are KY-018 KY-018 Photoresistor - SensorKit
See the code:

// define variables about duration of lights
int SvetiChK = 6000;  // pedestrian walk
int SvetiJk = 3000;
int SvetiZK = 5000;  // cars driving
int Izgasen = 400;
int MigaZK = 500;
int KolkoDaBipka;  // define count of beeping 3-right(0,1,2) , 2 left (0,1)
//int Rpin =A0;      // right PR, analogov signal
//int Lpin = A1;      // left PR, analogov signal
int Rpin = 22;      // right PR, digital signal
int Lpin = 24;      // left PR, digital signal

int RPRvalue = 0;      // variable about right photo resistor RPR
int LPRvalue = 0;      // variable about left photo resistor LPR


void setup() {
  Serial.begin(9600);
  pinMode(2, OUTPUT);  // configure pin 2 about output RED Koli ChK
  pinMode(3, OUTPUT);  // configure pin 3 about output yellow Koli JK
  pinMode(4, OUTPUT);  // configure pin 4 about output green Koli ZK

  pinMode(8, OUTPUT);  // configure pin 8 about output red Peshehodci ChP
  pinMode(9, OUTPUT);  // configure pin 9 about output green  Peshehodci ZP

  pinMode(10, OUTPUT);  // configure pin 10 about output Zumer Zum

  pinMode(11, OUTPUT);  // configure pin 11 about output both Lazers
  pinMode(12, OUTPUT);  // configure pin 12 about output Photo Rezistori LPR
  pinMode(13, OUTPUT);  // configure pin 13 about output Photo Rezistori RPR

    pinMode(Rpin, INPUT);  // konfigurirame D22 VHOD Photo Rezistori LPR
    pinMode(Lpin, INPUT);  // konfigurirame D24 Vhod Photo Rezistori RPR
}

void loop() {             // start loop about lights rotation
  digitalWrite(2, HIGH);  // Turn on ChK
  digitalWrite(9, HIGH);  // Turn on ZP
  // Turn on lasers and PhotoResistors
  digitalWrite(11, HIGH);  // Turn on Lazers
  digitalWrite(12, HIGH);  // Turn on LPR
  digitalWrite(13, HIGH);  // Zahranva RPR
  delay(SvetiChK);

  // START LASERS AND BEEP MANAGMENT 

  // READ RIGHT PR
 RPRvalue = digitalRead(Rpin);
  //RPRvalue = analogRead(Rpin);  // read value of Rpin right photorezistor
  Serial.println(RPRvalue);
  //RPRvalue /= 4;                // konvertira 0-1023 kym 0-255
  //Serial.println(RPRvalue);
  if (RPRvalue == 0) {
    
    noTone(10);
    tone(10, 9000, 100);
      delay(400);
    tone(10, 9000, 100);
      delay(400);
    tone(10, 9000, 100);
      delay(400);
    noTone(10);
    
  }
  

  // CHETE LYV PR
  LPRvalue = digitalRead(Lpin);
  //LPRvalue = analogRead(Lpin);  // read value of Lpin left photorezistor
  Serial.println(LPRvalue);
  //LPRvalue /= 4;     
  //  Serial.println(LPRvalue);           // konvertira 0-1023 kym 0-255
 if (LPRvalue ==1) {

    noTone(10);
    tone(10, 100, 100);
      delay(400);
    tone(10, 100, 100);
      delay(400);
    noTone(10);
  }
  
// END LASERS AND BEEP MANAGMENT 
  
  digitalWrite(9, LOW);   // Turn off  ZP
  digitalWrite(11, LOW);  // Turn off  Lazers
  digitalWrite(12, LOW);  // Turn off  LPR
  digitalWrite(13, LOW);  // Turn off  RPR
  delay(Izgasen);

  digitalWrite(3, HIGH);  // Turn on JK
  digitalWrite(8, HIGH);  // Turn on ChP
  delay(SvetiJk);

  digitalWrite(3, LOW);  // Turn off  JK
  digitalWrite(2, LOW);   // Turn off  ChK
  //digitalWrite(8, LOW);  // Turn off  ChP
  delay(Izgasen);

  digitalWrite(4, HIGH);  // Turn on ZK
  delay(SvetiZK);

  digitalWrite(4, LOW);  // Turn off  ZK
  delay(MigaZK);

  for (int i = 0; i < 3; i++)  // deklarira na vutreshen cikul za miganeto na ZK
  {

    digitalWrite(4, HIGH);  // Turn on ZK
    delay(MigaZK);

    digitalWrite(4, LOW);  // Turn off  ZK
    delay(MigaZK);
  }

  digitalWrite(4, LOW);  // Turn off  ZK

    digitalWrite(3, HIGH);  // Turn on JK
    delay(SvetiJk);
    digitalWrite(3, LOW);  // Turn off  JK
    digitalWrite(8, LOW);  // Turn off  ChP

  delay(Izgasen);
}

Thanks in advance.

You must show a wiring diagram.

You should use descriptive variable names, for example, not this:

but this

int REDkolichk = 2;
.
.
void setup() {
  pinMode(REDkolichk, OUTPUT);  // configure pin 2 about output RED Koli ChK`
.
.
}

The use of fifteen delay();commands is causing the blocking of reading the LASER/PhotoResistor.

Read about timing multiple events... (also has a video)..

Please post a diagram so we can see the third pin of the photo resistors.

Are they photo transistors? Provide also their part numbers if available

A good rule of thumb for troubleshooting is to test and understand each component type separately. So if everything is working except the laser/sensor pair, test them separately so you can tell if the problem is in the components, their wiring or in the software.

Liberal use of delay() is not recommended, as pointed out by @xfpd

You need to connect the photoresistor "S" pin to and analog input and use analogRead() to read the signal. Then you need to set a threshold to see if the signal is above or below it.
I see you did have them connected to A0 and A1 and used analogRead() but commented it out.

Go back to the analog and set if (RPRvalue >=800)


Hi,
I'm attaching the wiring diagram, I hope it will help.
would you like to propose, how to avoid delay command.
tomorrow I will test it back with analog read and let you know.

thanks in advance.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.