Hello all,
Though I'm quite technical (in software and systems), my experience with Arduino is all of a week for me, and I'm seriously going loopy from it. For 3 days I've been trying to understand the best way (or any way) to use a pushbutton as a toggle. When pressed, an external LED flashes a repeating pattern (think aircraft wing lights - a double strobe). When pressed again, it should stop the flashing.
I've tried two ways - using a very modified example sketch, and listening to Google's Gemini - needless to say, neither works. They either flash all the time, or never. I've even tried adding some text output on them to try to understand what state the strobe light is in (on or off) via the Serial Monitor.
Can anyone please, please help me see the error in my ways? I'd be most appreciative!
My code is here:
// Firstly, we set the constants. Constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change. Here we set the timings of the flashes and which pi
int buttonState = 0; // variable for reading the pushbutton status
bool strobeOn = LOW; //This allows a pushbutton to act as a switch (True=on, false=off)
int i = 0; // used in the "for" loop
int ontime = 100; // how long to keep the LED on in ms
int offtime = 50; // how long to keep the LED off in ms
int seqcount = 1; //number of times the strobe flashes during a sequence
int pausetime = 1000; // how long between flash sequences
void setup() {
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:
pinMode(buttonPin, INPUT_PULLUP); // initialize the pushbutton pin as an input:
Serial.begin(9600);
}
void loop() {
// First, read the state of the pushbutton value:
int buttonState = digitalRead(buttonPin);
// if the button is pressed (ie buttonState is HIGH)
// then change strobeOn to opposite of current state.
if (buttonState == LOW) {
strobeOn = !strobeOn;
delay(10); //to debounces button press
Serial.print("strobeOn value is: ");
Serial.println(strobeOn);
}
// else{
// strobeOn = strobeOn;
// }
while (strobeOn=1) {
for (int i = 0; i < seqcount; i++) {
digitalWrite(ledPin, HIGH);
delay(ontime); // wait for "delay in ms"
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
delay(offtime); // wait for "delay in ms"
digitalWrite(ledPin, HIGH);
delay(ontime); // wait for "delay in ms"
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
delay(offtime); // wait for "delay in ms"
}
delay(pausetime);
int buttonState = digitalRead(buttonPin);
// if the button is pressed (ie buttonState is HIGH)
// then change strobeOn to opposite of current state.
if (buttonState == LOW) {
strobeOn = !strobeOn;
delay(10); //to debounces button press
Serial.print("strobeOn value is: ");
Serial.println(strobeOn);
}
}
}
and Gemini's attempt (I understand where it's going, but it still doesn't work) is here:
const int buttonPin = 2; // Button connected to digital pin 2
const int ledPin = 13; // the number of the LED pin
int buttonState = LOW; // variable for reading the pushbutton status
bool strobeOn = true; //This allows a pushbutton to act as a switch (True=on, false=off)
int i = 0; // used in the "for" loop
int ontime = 100; // how long to keep the LED on in ms
int offtime = 50; // how long to keep the LED off in ms
int seqcount = 1; //number of times the strobe flashes during a sequence
int pausetime = 1000; // how long between flash sequences
void setup() {
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:
pinMode(buttonPin, INPUT_PULLUP); // Use internal pull-up resistor
Serial.begin(9600);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading == LOW) { // Button pressed
delay(50); // Debounce button press
buttonState = !buttonState; // Toggle button state
// strobeOn = !strobeOn;
if (buttonState == 1) {
for (int i = 0; i < seqcount; i++) {
strobeSequence();
}
}
}
else {
}
delay(pausetime);
Serial.print("buttonState value is: ");
Serial.println(buttonState);
}
void strobeSequence() {
digitalWrite(ledPin, HIGH);
delay(ontime); // wait for "delay in ms"
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
delay(offtime); // wait for "delay in ms"
}


