Whenever I upload the following program I am getting error message showing "expected initializer before interval".
Kindly Help me to solve out this probleme.
const byte button=2;
const byte LED=10;
bool blinking =false;//defines when blinking should occur
unsigned long blink Interval=250;// number of milliseconds for blink
unsigned long currentMillis;// variables to track millis()
unsigned long previousMillis;
void setup()
{
pinMode(button,INPUT);
pinMode(LED,OUTPUT);
}
void loop()
{
// this code blinks the LED
if(blinking) {
currentMillis =millis();// better to store in variable, for less jitter
if((unsignedlong)(currentMillis-previousMillis) >=blinkInterval) { // enough time passed yet?
digitalWrite(LED,!digitalRead(LED)); // shortcut to toggle the LED
previousMillis =currentMillis;// sets the time we wait "from"
}
} else {
digitalWrite(LED,LOW); // force LED off when not blinking
}
int reading=digitalRead(button);
delay(50); // crude de-bouncing
if(reading==LOW) // buttons with pull-up are pressed when LOW
{ blinking =true; }// start blinking
else
{ blinking =false; }// stop blinking
}
asked Dec 27, 2017 at 8:42
2 Answers 2
It is blinkInterval
, not blink Interval
. You have an space in the name in the declaration:
unsigned long blinkInterval=250; // number of milliseconds for blink
// ....
if((unsigned long)(currentMillis-previousMillis) >= blinkInterval) {
-
@Josephazim. That's not a problem; that's your code. When button goes HIGH, you put
blinking=false
. You have to introduce a waiting time there, but that is a new problem.user31481– user314812017年12月27日 09:14:30 +00:00Commented Dec 27, 2017 at 9:14 -
Thanks!It worked as you suggest but now my problem is that when I keep holding the button led is blinking and when I release the button it stop.I want it to be when I hold the push button for 5 sec, then led should start blinking for 10 minute or according to the certain time.Kindly help me to solve this problem.Josephazim– Josephazim2017年12月27日 09:54:55 +00:00Commented Dec 27, 2017 at 9:54
-
@Josephazim Check out my answer. It will helpful for you.Hasan– Hasan2017年12月27日 10:51:47 +00:00Commented Dec 27, 2017 at 10:51
-
@NickGammon I think you have to check this comment section. I answered according to that problem.Hasan– Hasan2017年12月28日 07:47:43 +00:00Commented Dec 28, 2017 at 7:47
The fully working sketch
const byte button=2;
const byte LED=10;
bool blinking =false;
unsigned long blinkInterval=250;
unsigned long currentMillis;
unsigned long previousMillis;
void setup()
{
pinMode(button,INPUT);
pinMode(LED,OUTPUT);
}
void loop()
{
if(blinking) {
currentMillis =millis();
unsigned long blinkInterval=100;
if((unsigned long)(currentMillis-previousMillis) >= blinkInterval) {
digitalWrite(LED,!digitalRead(LED)); LED
previousMillis =currentMillis;
}
} else {
digitalWrite(LED,LOW);
}
int reading=digitalRead(button);
delay(100);
if(reading==LOW)
{ blinking =true; }
else
{ blinking =false; }
}
VE7JRO
2,51519 gold badges27 silver badges29 bronze badges
default