NEW AND NEED LED CODE HELP!

Hi everyone. I am very new to this and need coding help. Here is what I would like my project to do.

I am making a Cornhole board. When a bag goes through the hole, I want it to break the IR beam, and flash one LED for 3 seconds then turn off and be ready for the next input.

I’m sure this is simple, but like I said I am very new to coding here. Any help would be greatly appreciated!! I have all the parts, just need the code. :slight_smile:

Thanks so much!

// Define pins for LED
int redLED = 13;
 
// Input from Motion Sensor
int pirPin = 7; 
 
// Variable for Motion Detected
int motionDetected = 0;
 
// Variable to store value from PIR
int pirValue; 

// Variable for counter
int counter = 0;
 
 
void setup() {
  
 // Setup LEDs as Outputs
 pinMode(redLED, OUTPUT);
  
 // Setup PIR as Input
 pinMode(pirPin, INPUT);
 
}


void loop() {
 
 // Get value from motion sensor
 pirValue = digitalRead(pirPin);
 // See if motion Detected
 if (pirValue == 1){
 // Display Triggered LED 10 times
 for (counter = 0; counter < 10; ++counter)
 digitalWrite(redLED, HIGH);
 motionDetected = 1;
 delay(200);
 digitalWrite(redLED, LOW);
 delay(200);
 } else {
 digitalWrite(redLED, LOW);
 }

 }

So far, this is what I came up with. Can anyone tell me if this looks correct?? I want the motion sensor to trip the LED and flash 10 times, then reset. I feel like I have the code for the "count" incorrectly added.

THANKS!

You're missing some braces in your for loop. I expect this was what you intended:

    for (counter = 0; counter < 10; ++counter)
      {
      digitalWrite(redLED, HIGH);
      motionDetected = 1;
      delay(200);
      digitalWrite(redLED, LOW);
      delay(200);
      }

That motionDetected variable isn't doing anything for you (yet?).

int detectedLED = 13;
int counter = 0;
int pirPin = 7;
int motionDetected = 0;
int pirValue;

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(detectedLED, OUTPUT);
  pinMode(pirPin, INPUT);
  
}

// the loop function runs over and over again forever
void loop() {
  pirValue = digitalRead(pirPin);
  if (pirValue == 0)
  for (counter = 0; counter < 5; ++counter)
  {
  digitalWrite(detectedLED, HIGH); 
  motionDetected = 0; 
  delay(30);                      
  digitalWrite(detectedLED, LOW);   
  delay(30);
  digitalWrite(detectedLED, HIGH);  
  delay(30);                      
  digitalWrite(detectedLED, LOW);   
  delay(30);
  digitalWrite(detectedLED, HIGH);  
  delay(30);                      
  digitalWrite(detectedLED, LOW);   
  delay(30);
  digitalWrite(detectedLED, HIGH);  
  delay(30);                      
  digitalWrite(detectedLED, LOW);   
  delay(100);
  digitalWrite(detectedLED, HIGH);  
  delay(500);   
  digitalWrite(detectedLED, LOW);   
  delay(30);                      

   }else{
  digitalWrite(detectedLED, LOW);
   motionDetected == 1;
}
}

WildBill, thanks a ton! Worked with a few tweaks and I am running as hoped! This is my first project and I am excited to work on more! Figured I would share the code that worked for me. Ultimately, I wanted the LED to flash like a strobe 5 times.