Hey,
I have a new problem :
My aim is to make a led flash by pushing a fisrt button (here b2) and to turn the led off by pushing an other button (b). I want to do it by using the function millis(),
Actually, when I use this code, my led does not react with the buttons,
Can someone help me ?
Here is my code :
const byte bPin = 2;
const byte b2Pin = 3;
const int led = 8;
bool bState;
bool b2State;
bool ledState;
long time;
void setup() {
// put your setup code here, to run once:
pinMode(led, OUTPUT);
pinMode(bPin, INPUT);
pinMode(b2Pin, INPUT);
ledState = 1;
digitalWrite(led, ledState);
bState = HIGH;
bState = HIGH;
}
void loop() {
// put your main code here, to run repeatedly:
bState = digitalRead(bPin);
b2State = digitalRead(b2Pin);
time = millis();
bool i = 0;
if (b2State == false) {
i = 0;
}
if (i = 0) {
while (millis - time < 250) {
digitalWrite(led, ledState);
time = millis();
}
if (millis() - time > 250) {
ledState = !ledState;
digitalWrite(led, ledState);
}
}
if (bState == false) {
i = !i;
digitalWrite(led, HIGH);
}
}
You're doing the timing with a while loop. So execution stays in the while loop until it is done. All you have done is recreated the delay function. You might as well use delay with this code.
Go study blink without delay. Instead of being stuck in a while loop it just checks the time once in an if and moves on if it's not time.
I tried with delay but my code seams false again :
here is my loop (I did not change the rest) :
void loop() {
// put your main code here, to run repeatedly:
bState = digitalRead(bPin);
b2State = digitalRead(b2Pin);
bool i = 0;
if (b2State == false) {
i = 0;
}
if (i = 0) {
digitalWrite(led, ledState);
delay(250);
ledState = !ledState;
digitalWrite(led, ledState);
delay(250);
}
if (bState == false) {
i = !i;
digitalWrite(led, HIGH);
}
}