Limit switch state change detection

Good afternoon all - I am relatively new to arduino code.

I am currently in the process of making an automated hockey net with 4 x 12V LED illuminated targets. I am using limit switches to detect whether a target has been hit - if the target is hit it will de-energize and the next LED target will activate and wait for a shot to strike it's limit switch.

I am having difficulties with the limit switch code, I had originally tried to force the LED target low once the limit switch went from a high to low signal which proved to be ineffective. I have since attempted to use state change detection with no success. I am using a 4-channel relay to do the 12V switching required and the limit switches are wired in a normally open configuration.

I am looking for any advice on to how to write the code for these limit switches to make the LED targets go from on to off when the target is hit and illuminate the next LED target. The program seems very simple yet I am lost as to where to go from here.

Any help is greatly appreciated.

 int LEDTarget1 = 2; // LED Strip Relay Pin
int LEDTarget2 = 3; // LED Strip Relay Pin
int LEDTarget3 = 4; // LED Strip Relay Pin
int LEDTarget4 = 5; // LED Strip Relay Pin
int limitSwitch1 = 10; // Target Limit Switch 1
int limitSwitch2 = 11; // Target Limit Switch 2
int limitSwitch3 = 12; // Target Limit Switch 3
int limitSwitch4 = 13; // Target Limit Switch 4
void setup() {
    pinMode(LEDTarget1, OUTPUT);
  pinMode(LEDTarget2, OUTPUT);
  pinMode(LEDTarget3, OUTPUT);
  pinMode(LEDTarget4, OUTPUT);
  pinMode(limitSwitch1, INPUT);
  digitalWrite(limitSwitch1, LOW);
  pinMode(limitSwitch2, INPUT);
  digitalWrite(limitSwitch2, HIGH);
  pinMode(limitSwitch3, INPUT);
  digitalWrite(limitSwitch3, HIGH);
  pinMode(limitSwitch4, INPUT);
  digitalWrite(limitSwitch4, HIGH);


void loop() {
  digitalWrite(LEDTarget1, HIGH);
  if (digitalRead(limitSwitch1 = HIGH) )
    { digitalWrite(limitSwitch1, LOW);
    digitalWrite(LEDTarget1, LOW);
    digitalWrite(LEDTarget2, LOW);
    digitalWrite(LEDTarget4, LOW); 
    digitalWrite(limitSwitch3, HIGH); 
    digitalWrite(limitSwitch2, LOW);
    digitalWrite(limitSwitch1, LOW);   
    digitalWrite(LEDTarget3, HIGH);


Any help is greatly appreciated.

First post your code so we can see what you are talking about.

And PLEASE use the code button </> so it looks like this and is easy to select and copy to a text editor


Sorry about that.

I've added a portion of the code just to hopefully give you a rough idea of what I am trying to accomplish, I hope it helps somewhat.

Again, I appreciate any input.

Troy C

Is that portion a complete working program ? If not please post the whole program.

Also please don't amend earlier posts in such a way as to make nonsense of subsequent comments. It makes it very difficult for others to follow the conversation. Just add new stuff in a new Post.

You can't use digitalWrite() to set an INPUT pin LOW. That must be done by the external hardware. And don't change the pinMode for that pin to OUTPUT in case you cause a short circuit and damage your Arduino.

How is the switch connected to your Arduino? Make a pencil drawing showing all the wiring and post a photo of the drawing.

It is a good idea from an electrical point of view to use INPUT_PULLUP and have the switch wired so, when pressed, it pulls the input pin LOW.

If this was my project I would have a function to read all the switches and save their states. Then I would have another function (or several functions) to do things based on the save states. Have a look at planning and implementing a program

You should use millis() to manage timing (also illustrated in planning and implementing) rather than delay() because the Arduino can do nothing during a delay() interval and the program will be unresponsive. It is essential to ensure loop() repeats hundreds or thousands of times per second.

Finally I cannot relate your description of the problem to your code. You have not said what the code actually does at the moment and what it should be doing