so my goal for this is to have ledPin3 light up and tern of after set interval time when the button is pushed. As well as switching between to leds when the button is pushed using count.
here is the full code
const int ledPin1 = 12;
const int ledPin2 = 11;
const int ledPin3 = 4;
int count = 0;
int buttonState = 0;
bool lastButtonState;
const int buttonPin = 2;
unsigned long previousMillis = 0;
const unsigned long interval = 50;
void setup() {
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode (ledPin3, OUTPUT) ;
}
void loop() {
unsigned long currentMillis = millis();
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH && lastButtonState == LOW) {
lastButtonState = buttonState;
count++;
}
else {
lastButtonState = buttonState;
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
}
if (count == 0) {
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin3, HIGH);
if (currentMillis - previousMillis >= interval) {
digitalWrite(ledPin3, LOW);
previousMillis = currentMillis;
}
}
if (count == 1) {
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
if (currentMillis - previousMillis >= interval) {
digitalWrite(ledPin3, LOW);
previousMillis = currentMillis;
}
}
if (count == 2) {
(count = 0);
}
else {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
}
}
and here is the part that dose not work
if (count == 1) {
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
if (currentMillis - previousMillis >= interval) {
digitalWrite(ledPin3, LOW);
previousMillis = currentMillis;
}
}
when I run the code the first if statement works but the second one does not.
I would like the second if statement to tern ledPin3 off after set interval time.
thanks for any help
Please post your full code here. If it is too large to post directly then attach it to a post
We can't see what changes count. Maybe the first time this is called count equals 1 so the if is true and the millis timer is evaluated to false. Maybe the next time count is not 1 so the millis timer is not evaluated. Impossible to know if you don't share the rest of your code.
Most homework is random operations like this. Is this homework or is there some real reason for wanting to do this.
Why are you declaring previousmillis = currentmillis?
You should use currentmillis = millis(); instead of that. If you use previousmillis = currentmillis, then your while function would never exit! : D
..Arnav
ArnavPawarAA:
Why are you declaring previousmillis = currentmillis?
You should use currentmillis = millis(); instead of that. If you use previousmillis = currentmillis, then your while function would never exit! : D
..Arnav
I would guess that the OP does currentmillis = millis() prior to what is shown, in code that isn't shown. There are different styles of terminology, "previous" sometimes getting confused with "current" for example. "last" sometimes pops up. But the main point is to keep the ones you use consistent with each other.
Looking at the code, enclosing a time check inside a conditional statement needs some extra caution. There is some risk the condition would stall the time dependent code inside.
I use a quick and dirty if(debug) though...
UKHeliBob:
Please post your full code here. If it is too large to post directly then attach it to a post
I have added the full code in the post
Grumpy_Mike:
Most homework is random operations like this. Is this homework or is there some real reason for wanting to do this.
no this is not homework. I am trying to get better at coding Arduino and have nothing else to do so I am just coming up with different functions and trying to make the Arduino do them. i have been trying to get this one to work for a couple of days but can't. This is my first time using millis in code like this so I don't have much experience to help me trouble shoot.
after lots and lots of try's I got it working.
thanks to anyone who tried to help ; )
Are you going to share the problem and solution with the forum ?
Doing so might help someone else
ArnavPawarAA:
If you use previousmillis = currentmillis, then your while function would never exit! : D
a) it's a "while loop", not a "while function"
b) there is no while loop in the OP's code.
Try
if(millis() - previousMillis > amount-of-time-you-want) {
//do something
}