Arduino Counter Issue

Hi there,

We are doing a project with an Arduino Nano Every where a red LED is supposed to blink 10 times when activated by a switch. After the LED blinked 10 times you can put the switch back to the "off-position" and activate the LED again when put back to the "on-position". The problem is that exactly this last step does not work as expected. After the initial blinking sequence the LED won't start blinking 10 times again. Somehow there is an issue with the counter and we can't quite figure it out. We also haven't used a pull-down/up resistor for the switch. Could that be part of the problem? Thanks for your help.

int ignitionSwitch = 12;
int ledRed = 10;
int ledGreen = 8;
int readIgnitionSwitch = 0;
int count = 0;

void setup() {
  pinMode(ledGreen, OUTPUT);
  pinMode(ledRed, OUTPUT);
  pinMode(ignitionSwitch, INPUT);
}

void loop() {

  digitalWrite(ledGreen, HIGH);
  readIgnitionSwitch = digitalRead(ignitionSwitch);

  if (readIgnitionSwitch == HIGH && count == 0) {
    for (int i = 1; i <= 10; i++) {
      digitalWrite(ledRed, HIGH);
      delay(500);
      digitalWrite(ledRed, LOW);
      delay(500);
    }
    count++;
  } else if (readIgnitionSwitch == LOW) {
    count = 0;
  }
}

how is it wired then?

(you could use a bool to say that you are waiting for release instead of a counter that will only go to 1)

You haven't got a pulldown resistor in place keeping the switch pin in a known state when the switch is not active. So it will be floating at an unknown voltage, maybe HIGH, maybe LOW, maybe changing

Consider using INPUT_PULLUP in pinMode() to activate the built in pullup resistor, change the wiring to take the pin LOW when the switch is on and detect LOW instead of HIGH in the sketch

Thanks for your help. When we use INPUT_PULLUP in our code the LED won't blink anymore. We also changed the wiring but this doesn't work either. Do you have any other ideas/advice why it still doesn't work?

Thanks for your answer. Could you specify what you mean by "waiting for release"?

Please post the revised code and a schematic of your project. A picture of a hand drawn schematic is good enough

what you do there is waiting for the button to be released (switch back to the "off-position" )

but as we don't know how this is wired nor if you have bouncing...

Hi,

Sorry for our bad schematic. I hope you understand how the schematic is structured. You can find the revised code down below.

int ignitionSwitch = 12;
int ledRed = 10;
int ledGreen = 8;
int readIgnitionSwitch = 0;
int count = 0;

void setup() {
  pinMode(ledGreen, OUTPUT);
  pinMode(ledRed, OUTPUT);
  pinMode(ignitionSwitch, INPUT);
  // TODO change wiring to detect LOW to start blinking instead of HIGH
}

void loop() {

  digitalWrite(ledGreen, HIGH);
  readIgnitionSwitch = digitalRead(ignitionSwitch);

  if (readIgnitionSwitch == LOW && count == 0) {
    for (int i = 1; i <= 10; i++) {
      digitalWrite(ledRed, HIGH);
      delay(500);
      digitalWrite(ledRed, LOW);
      delay(500);
    }
    count++;
  } else if (readIgnitionSwitch == HIGH) {
    count = 0;
  }
}

your switch connected on D12 is not correctly wired. when the switch is open, D12 is just hanging there with no set voltage, its wire will act as an antenna and you'll get HIGH a LOW depending on what's in the air ...

set it as a INPUT_PULLUP and wire D12 ---- Switch ---- GND. it will be LOW when activated

also don't try to do "photorealistic" drawing of a circuit, just use outlines and standard elements

your drawing would be easier to read (although still wrong) if drawn like this

it could even be hand drawn (with a ruler for straight lines please)
and bonus point if you add more info such as the type of Arduino :slight_smile:

may be something like this

Sorry for this stupid question but why didn't you use the 3.3 V pin on the ignition switch? If we don't connect the ignition switch with the 3.3 V pin we won't be able to detect if the INPUT is HIGH or LOW, right?

read about INPUT_PULLUP :slight_smile:

And compare your circuit with the examples in this graphic:

I may be sun blind, but it looks like @J-M-L's version of @herrquellwasser's schematic is not faithful.

I see a traditionally pulled-high input with a switch that grounds the input, connecting the pin and the pull-up to ground when closed.

a7

First of all thanks a lot for your help. We just found out that the pin 12 doesn't detect if the signal is HIGH or LOW. By using the serial monitor we found out that it only detects HIGH independently of the actual state of the switch. When we tried another pin it didn't work either. Then we tried unsing the same switch but another board (Arduino UNO) instead of the Arduino Nano Every and it somehow worked every single time we tested it. Now we're not sure wether the Arduino Nano Every is actually broken or we just made another mistake... Thanks again for your help.

int ignitionSwitch = 12;
int ledRed = 10;
int ledGreen = 8;
int readIgnitionSwitch = 0;
int count = 0;

void setup() {
  pinMode(ledGreen, OUTPUT);
  pinMode(ledRed, OUTPUT);
  pinMode(ignitionSwitch, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {

  digitalWrite(ledGreen, HIGH);
  readIgnitionSwitch = digitalRead(ignitionSwitch);

  if (readIgnitionSwitch == HIGH) {
    Serial.println("HIGH");
  } 
  else if (readIgnitionSwitch == LOW) {
    Serial.println("LOW");
  }

Whenever you post test results, please also post the sketch that produced them. Use the lower diagram in reply #9 as the hardware reference, it's the most likely to work.

Edit - thanks for adding it, but next time please add in a new message. Editing previous posts makes the thread conversation hard to follow.

We used it.

I don't see pin 12 in use.

Yeah, because we used pin 5 to check if it's just pin 12 that doesn't work. I forgot to change it. Edit: Now i did.

But what wiring did you use on pin 5?

I connected pin 5 with the switch and the switch with GND.