I need help adding something in my code. It's urgent

Hi, I'm a complete beginner to Arduino coding and It's one of the first codes I've written. The code that I have under turns at my LED on every time my laser is cut. I want my serial monitor to count every time the laser is cut, but I can't seem to make that work.

int ldrPin = A4; // the cell and 10K pulldown are connected to a0
int sirenPin = 11; //pin 3 selected!
int ledPin = A0;
int ldrValue1, ldrValue2;

const int pinLaser = 2; // output signal pin of laser module/laser pointer
const int pinReceiver = 3; // input signal pin of receiver/detector (the used module does only return a digital state)
const int BUTTON_PIN = 2;//Pin2 connected to Button
const int LED_PIN = 13;//Pin3 connected to a LED

void setup() {
pinMode (sirenPin,OUTPUT); // set the siren pin as output
pinMode (ledPin,OUTPUT); // set the siren pin as output
pinMode (ldrPin,INPUT); // set the siren pin as output
//Serial.begin(9600);
pinMode(pinLaser, OUTPUT); // set the laser pin to output mode
pinMode(pinReceiver, INPUT); // set the laser pin to output mode
digitalWrite(pinLaser, HIGH); // emit red laser
}

void loop(void) {
ldrValue1 = analogRead(ldrPin);
delay(10);
ldrValue2 = analogRead(ldrPin);

if (ldrValue1-ldrValue2 > 20){
digitalWrite(sirenPin,HIGH);
digitalWrite(ledPin,HIGH);
delay(140);
}
else{
digitalWrite(sirenPin,LOW);
digitalWrite(ledPin,LOW);
}
if (

}

Why is it urgent? Why do we care?
Where is the rest of the code?

Please post your code correctly.

1 Like

Can you explain what you want to do, and how things are connected?

Please remember to use code tags when posting code.

I suppose one of them's got to be the right comment

1 Like

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 </> icon above the compose window) to make it easier to read and copy for examination

Fix your comments and make your code legible, and then maybe we'll help. When you edit your post, put the text in code tags.

const int pinLaser = 2; // output signal pin of laser module/laser pointer
const int BUTTON_PIN = 2; //Pin2 connected to Button

I don't think you want your button and your laser on the same pin.

When you run this sketch, what values get displayed on Serial Monitor when the laser hits the LDR and what values get displayed on Serial Monitor when the laser is blocked?

int LDRPin = A4;
const int pinLaser = 2; // Laser power

void setup()
{
  Serial.begin(115200);

  // Turn on the laser
  pinMode(pinLaser, OUTPUT);
  digitalWrite(pinLaser, HIGH); // emit red laser
}

void loop(void)
{
  // Report the reading from the LDRPin
  Serial.println(analogRead(LDRPin));
  delay(1000);
}

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