I'm trying to run a sequence of blinks with a button push but can only manage to get it to go while the button is pushed. I just took the example Button code and added a digitalWrite(LOW) and delay() command and repeated it a few times manually by rewriting the lines (not a for loop). I also changed the else() command to HIGH. I wanted it to run through the entire sequence of blinks with a momentary press of the button, but instead it ran through it only as long as the button is pressed. Since that was the case I simplified the code to just one HIGH/LOW cycle and it blinked as long as the button is pressed which kind of works for this application.
Originally I thought I should use the Debounce example code as a framework, but couldn't figure out how to add a sequence of digitalWrite() blinks..
IDEALLY, the LED would be off when the microcontroller powers up, and then when the button is pressed it would blink for a few cycles then stay on.
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
delay(300);
digitalWrite(ledPin, LOW);
delay(300);
} else {
// turn LED off:
digitalWrite(ledPin, HIGH);
}
}
bool myFlag = false;
. . .
//if we are not currently handling a LED sequence, was the switch closed ?
if (myFlag == false && buttonState == HIGH)
{
//enable State Machine checking
myFlag = true;
}
//if enabled, check the State Machine
if (myFlag == true)
{
checkStateMachine() ;
}
Always show us a good schematic of your proposed circuit.
Iām not as bright as you think. Iāll PayPal you if you can write the code I need today though. It just needs to do one thing. When powered on the led is off. When the button is pressed it flashes 6x and then stays on. It should also use delay()s for the sequence so I can tweak it or add to it later. Easy money. Itāll go in a Nano Every but that shouldnāt matter.
Since it is a assignment and if you are allowed to pay for someone to do your work, get the moderator to move this to the Jobs and paid Consultancy forum.
Itās upsetting they only gave you 1 day to do this, but then itās only a 15 minute job.
Itās a āfrequency finder.ā The device has a button that when pushed the LED is supposed to blink for a few seconds and then stay on. Iām modifying an existing prop and canāt change the button because itās already been established.
Like, 6 blinks with a delay(300) between. That way I can tweak it later if needed.
if (buttonState == HIGH) {
// flash LED 5 times
for (int ii = 0; ii < 5; ii++) {
digitalWrite(ledPin, HIGH);
delay(300);
digitalWrite(ledPin, LOW);
delay(300);
}
digitalWrite(ledPin, HIGH); // now on permanent
while (1); // infinite loop hangs here until reset
}
Hit the reset button to start all over. You could wire a reset button of your own across to the REsET pin on your board.