I am a new arduino user. I have a project where I need to blink my LED 8 times in a second. I wrote the following code for this. Although LED blinks succesfully it fails to close at the end of designated time (1 minute) it sometimes stay open or closes (one by one) I could not see the problem on my code so I am asking for help from you. I appreciate any suggestions.
Thank you
Code:
const byte buttonPin = 7; // the pin that the pushbutton is attached to
// constants won't change. Used here to set a pin number:
const int ledPin = 8; // the number of the LED pin
unsigned long keepBlinkingUntil = millis();
boolean ledState = LOW;
boolean previousButtonState = HIGH;
boolean buttonState = LOW;
boolean serialCheck = LOW;
long duration = 0;
unsigned long previousMillis = 0;
// constants won't change 62.5 is for 8 hz if 4 hz use 125 :
const long interval = 62.5;
void setup()
{
Serial.begin(9600);
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
pinMode(buttonPin,INPUT);
}
void loop()
{
Serial.print("do not worry i am working");
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (currentMillis < keepBlinkingUntil){
if (ledState == LOW) {
ledState = HIGH;
}
else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
if (millis() >= duration && buttonState == HIGH) {
previousButtonState = buttonState;
buttonState = LOW;
}
}
if(digitalRead(buttonPin) == HIGH) {
Serial.print("buttonPin suan HIGH");
if(previousButtonState == HIGH) {
buttonState = HIGH;
Serial.print("buttonState HIGH yaptik");
}
else {
buttonState = LOW;
Serial.print("buttonState LOW yaptik");
}
delay(5); //debouncing
duration = millis() + 60000;
keepBlinkingUntil = duration;
}
}
8 blinks per second, that is 62.5 milliseconds on and the same time off.
For a minute it should blink 480 times.
There is only one timing thing to do, therefor I see a single millis-timer (or micros-timer) that counts up to 480.
Let's bend the rules, and turn the led on for 62 milliseconds and then off for 63 milliseconds.
This is with a single millis-timer:
// Blink a led 8 times per second for a minute
//
// This sketch bends the rules, by using millis() and 62 and 63 for the blink.
// Blinking 8 times per second would be a interval of 62.5 ms.
//
// The blinking is almost too fast to be visible in Wokwi.
//
const unsigned long onTime = 62;
const unsigned long offTime = 63;
unsigned long previousMillis;
unsigned long interval = offTime; // start with led off
const int ledPin = 13;
int ledState = LOW; // start with low
const int buttonPin = 2;
bool blinkingEnabled = false; // don't blink to start with.
int count;
const int maxCount = 8 * 60; // 8 blinks per second, 60 seconds
void setup()
{
Serial.begin(115200);
Serial.println( "Press the button to start");
pinMode( ledPin, OUTPUT);
pinMode( buttonPin, INPUT_PULLUP);
}
void loop()
{
unsigned long currentMillis = millis();
if( digitalRead( buttonPin) == LOW)
{
blinkingEnabled = true;
count = 0;
}
if( blinkingEnabled)
{
if( currentMillis - previousMillis >= interval)
{
previousMillis += interval; // clock-accurate millis-timer
if( ledState == HIGH) // led was on ?
{
interval = offTime;
ledState = LOW;
if( count >= maxCount) // blinked all the blinks ?
{
blinkingEnabled = false; // turn of this millis-timer
}
}
else // led was off
{
interval = onTime;
ledState = HIGH;
count++; // count the number the led is turned on
}
digitalWrite( LED_BUILTIN, ledState); // set new led state
}
}
}
Now is the time for a schematic of your project.
Can we please have a circuit diagram?
An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.
hey . Thanks for your comment. I installed the libraries and it worked. As you mentioned total number of blink is 475 right now. Is there a way to increase it to 480?
Then it would account for jitter due to printing or other processing that delays the trigger--If something runs long one cycle, it makes up for it in the next cycle.