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;
}
}
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?
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
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?
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.