First code troubleshooting

Hey All,
I have a code where i want my arduino nano to look for a closed circuit on pins d2-d11, if any of the pins are closed for more than 5 seconds i want it to sound an alarm (or turn on an LED) connected to pin d12.

ive got the turn on part working, but i want the led to turn off immediately when circuit open again. currently there seems to be a 5 second delay for the led to also turn off.

Ive tried to get chat GPT to write it and fix it but went round in circles.

it wouldnt be my physical setup would it? i have pin 12 going to a resistor into the led then to ground, for the switches i just have going from ground to any of the d2-d11 pins. ?

heres the code:

// Define the pins to monitor
const int startPin = 2;  // Start pin (D2)
const int endPin = 11;   // End pin (D11)

// Define the LED pin
const int ledPin = 12;   // D12 pin on Arduino Nano

void setup() {
  // Initialize the LED pin as an output
  pinMode(ledPin, OUTPUT);

  // Initialize the digital pins as inputs with pull-up resistors
  for (int pin = startPin; pin <= endPin; ++pin) {
    pinMode(pin, INPUT_PULLUP);
  }
}

void loop() {
  // Flag to track if any pin forms a circuit for more than 5 seconds
  bool circuitDetected = false;

  // Loop through each pin to check if it's connected to ground
  for (int pin = startPin; pin <= endPin; ++pin) {
    // If the pin is connected to ground (LOW), set the flag and break the loop
    if (digitalRead(pin) == LOW) {
      // Wait for 5 seconds to ensure the circuit is continuously closed
      delay(5000);
      if (digitalRead(pin) == LOW) {
        circuitDetected = true;
        break;
      }
    }
  }

  // If a circuit is detected, turn on the LED
  if (circuitDetected) {
    digitalWrite(ledPin, HIGH);
  } else {
    // If no circuit is detected, turn off the LED
    digitalWrite(ledPin, LOW);
    delay(10); // Ensure the LED turns off immediately
  }
}

No, that's all ok. It's your code. Specifically it's that delay(5000). You need to be using millis() for the timing.

thank you, i tried taking small steps and everything worked till adding the 5 second timer.

will do some learning and try another night, my attempt tonight came back with a code error so will read through this thread and try getting it working.

you might benefit from studying state machines. Here is a small introduction to the topic: Yet another Finite State Machine introduction

Consider this:
What happens when you hold a button down for longer than 5 seconds, say 5.1 seconds?

You don't actually need 10 pins for that, you could connect all 10 buttons to the same pin.

Here's my attempt, if you want to see it:
// Define the pins to monitor
const int startPin = 2;  // Start pin (D2)
const int endPin = 11;   // End pin (D11)

// Define the LED pin
const int ledPin = 12;   // D12 pin on Arduino Nano

void setup() {
  // Initialize the LED pin as an output
  pinMode(ledPin, OUTPUT);

  // Initialize the digital pins as inputs with pull-up resistors
  for (int pin = startPin; pin <= endPin; ++pin) {
    pinMode(pin, INPUT_PULLUP);
  }
}

void loop() {
  // Flag to track if any pin forms a circuit for more than 5 seconds
  bool circuitDetected = false;
  static bool timerStarted = false;
  static unsigned long detectionTime;

  // Loop through each pin to check if it's connected to ground
  for (int pin = startPin; pin <= endPin; ++pin) {
    // If the pin is connected to ground (LOW), set the flag and break the loop
    if (digitalRead(pin) == LOW) {
      circuitDetected = true;
      break;
    }
  }

  // If a circuit is detected, turn on the LED
  if (circuitDetected) {
    if (timerStarted) {
      if (millis() - detectionTime >= 5000) {
        digitalWrite(ledPin, HIGH);
      }
    } else {
      // Start a 5 second timer
      detectionTime = millis();
      timerStarted = true;
    }
  } else {
    // If no circuit is detected, turn off the LED
    digitalWrite(ledPin, LOW);
    timerStarted = false;
  }
}

amazing, thank you this works perfect!

Looking forward to exploring a few more projects.

The reason I wanted 10 pins, was because i have 10 contactors activated by 10 photoelectric sensors and was worried about inadvertently linking them together.

It would be fun to take this up with chatGPT. Who knows, maybe it could solve the problem.

One quick change my HI came up with, which is worth as much, is to condition the five second delay on its being the first time you make the LED go on, viz:

void loop() {

  static bool circuitDetected;
  int pin:

  for (pin = startPin; pin <= endPin; ++pin) {

    if (digitalRead(pin) == LOW) {

      if (not circuitDetected) delay(5000);
      if (digitalRead(pin) == LOW) {
        circuitDetected = true;
        break;
      }
    }

    if (pin > endPin) circuitDetected = false;
  }


// rest of loop unchanged 

I can't test this at the moment. Show chatGPT both versions of the complete loop()and ask it to explain it to you.

a7

It got tested it and a mistake or two fixed, thank her very much...

This is the complete loop. I commented out the delay(10) for the nonsense it made to me.

void loop() {
  static bool circuitDetected;
  int pin;

  for (pin = startPin; pin <= endPin; ++pin) {

    if (digitalRead(pin) == LOW) {
      if (not circuitDetected) delay(5000);
      if (digitalRead(pin) == LOW) {
        circuitDetected = true;
        break;
      }
    }
  }

  if (pin > endPin) circuitDetected = false;

  if (circuitDetected) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
// ???    delay(10); // Ensure the LED turns off immediately
  }
}

HTH and sry, shoulda waited until I coulda tested (and fized) it myself.

a7

I need a little more help, I have progressed from lighting up a LED to wanting a horn to run via a relay module, however when hooking up the module it instantly turns on regardless of a signal.

The LED waits the 5 seconds as programmed, but when I connect the signal wire (In terminal) of the relay module it clicks the relay as soon as the wire is connected.

I have confirmed the wiring is correct through programming a simple program listed below and it activates the relay as expected.

Any tips?

int relayPin = 12;

void setup() {
  // put your setup code here, to run once:
  pinMode(relayPin , OUTPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
digitalWrite(relayPin , HIGH ); // off light
delay(8000);
digitalWrite(relayPin , LOW ); // On Light
delay(3000);
}

Maybe the relay is triggered by a LOW signal from the Arduino pin? Relay modules which include opto-isolators are often like this.

When a pin is set to OUTPUT, it defaults to LOW.

I don't see any 5 second delay in the code you posted.

The code I posted was just to test if i was connecting it right so and the relay turned on after 8s and off after 3s.

I was trying to use the code you had sent which was where the 5000 delay was.
Just strange is does whats expected with the LED, but then the relay triggered as soon as a wire was connected to pin 12.

Good news is I have since fiddled with the code posted by alto777 and got it to work, it was initially inverted, switch on when i needed it off, but I swapped it and it works well.

I am still super grateful for your help though! early days in my journey and have a few other ideas to get happening.

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