Won't do task 2 unless task 1 is valid


#define RECEIVER1 2
#define RECEIVER2 6 //
#define BUZZER1 8 // 
#define BUZZER2 9


void setup() {
  Serial.begin(9600);
  Serial.println("TEST TEST");
  pinMode(RECEIVER1, INPUT);//define detect input pin
  pinMode(RECEIVER2, INPUT);
  pinMode(BUZZER1, OUTPUT);//define ACTION output pin
  pinMode(BUZZER2, OUTPUT);



}

void loop() {
     

  int detected = digitalRead(RECEIVER1);// read Laser sensor
 
  if( detected == HIGH)
  {
    digitalWrite(BUZZER1,HIGH);// set the buzzer ON
    Serial.println("NO LASER 1");

  }else{
    digitalWrite(BUZZER1,LOW); // Set the buzzer OFF
    Serial.println("LASER DETECTED 1");
     

  
int detected = digitalRead(RECEIVER2);

  if( detected == HIGH)
  {
    digitalWrite(BUZZER2,HIGH);// set the buzzer ON
    Serial.println("NO LASER 2");

  }else{
    digitalWrite(BUZZER2,LOW); // Set the buzzer OFF
    Serial.println("LASER DETECTED 2");
     
  }
  }
  delay(200);
}

hi, I'm working on my 1st Arduino project, and I have 10 pairs of KY-008 Laser Transmitter + Laser Receiver Sensor Modules. I have written the code, but it doesn't work as intended. Arduino won't check if the 2nd receiver is a "hit", unless the 1st receiver is a "hit." So, basically, it won't go to and check the value for receiver 2, unless the receiver 1 is exposed to the light from the transmitter.

and the 2nd question - is there any way to tell which transmitter is hitting the receive or not. I can obviously number number the receivers but how can I know which transmitter is hitting let's say receiver #1 ?

Thanks in advance, sorry if my question is super basic. I'm just starting (by the way it's an Arduino Uno R3 original that I'm using).

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

1 Like

wow, Thank you @Delta_G it solved my issue. Appreciate it much

ok, sure @UKHeliBob, didn't know there was such a thing

any ideas regarding the 2nd part of the question - is there any way to tell which transmitter is "hitting" the receiver?

If course not. You didn't read the forum guide before you posted. Go read it now, please. Then fix post #1 so we know you understand.

Thinking of copying & pasting your code 8 more times for the other receivers? Don't! Copy/pasting code is a sin. We can help you avoid it.

Yes, switch the transmitters on one at a time. If any receiver detects it, you know which transmitter it was from.

2 Likes

I made the change, it looks right now?

so, if I shouldn't just copy-paste for the other 8, what should I do instead?

Use arrays:

const byte receivers = 2;
const byte receiverPin[receivers] = {2, 6};
const byte buzzerPin[receivers] = {8, 9};

void setup() {
  Serial.begin(115200);
  Serial.println("TEST TEST");
  for (byte p=0; p<receivers; p++) {
    pinMode(receiverPin[r], INPUT);  //define detect input pin
    pinMode(buzzerPin[r], OUTPUT);  //define ACTION output pin
  }
}

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