Timer Help
-
Hi,´
I have this timer, where when a button is pressed it activates a relay for some time, the duration is regulated by a potenciometer.int potPin = A1;
const int buttonPin = A2;void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);pinMode(buttonPin, INPUT);
}
void loop()
{
int buttonState = digitalRead(buttonPin);int val = analogRead(potPin);
long tempo = (val*19.550342131);
Serial.print(tempo);
Serial.print("tempo:");// read the value from the sensor
if (buttonState==HIGH) {digitalWrite(ledPin, HIGH);
delay(tempo);
digitalWrite(ledPin, LOW);
} else {}; // turn the ledPin offdelay(500);
}Thats the code, my problem is that when i press de button, the arduino stops reading the code while it is counting, can anyone show me a way to keep running the code, but counting the time?
Thank you -
-
int potPin = A1; const int buttonPin = A2; void setup() { Serial.begin(9600); pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT); } void loop() { int buttonState = digitalRead(buttonPin); int val = analogRead(potPin); long tempo = (val*19.550342131); Serial.print(tempo); Serial.print("tempo:"); // read the value from the sensor if (buttonState==HIGH) { digitalWrite(ledPin, HIGH); delay(tempo); digitalWrite(ledPin, LOW); } else {}; // turn the ledPin off delay(500); }Like this
-
What if you did something like this:
int potPin = A1; const int buttonPin = A2; int counter = 0; void setup() { Serial.begin(9600); pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT); } void loop() { int buttonState = digitalRead(buttonPin); int val = analogRead(potPin); long tempo = (val*19.550342131); Serial.print(tempo); Serial.println("tempo:"); // read the value from the sensor if (buttonState==HIGH) { digitalWrite(ledPin, HIGH); } //While the ledPin/relay is on, increment and check the counter if (digitalRead(ledPin) == HIGH) { counter ++; //Only reset if the counter is over tempo, otherwise continue with loop if (counter > tempo) { digitalWrite(ledPin, LOW); //reset the counter counter = 0; } } delay(500); }Notice how the above code is double space indented. Having it in the code block does help readability, but indenting helps even more.
-
Well that remains a problem, the relay keeps turning in and off even if i dont press the button...
-
https://circuits.io/circuits/4669781-the-unnamed-circuit
Well is something like that
-
The main problem is that you are delaying the tempo time. When you use "delay" everything stops for that amount of time. Try using millis. compare current millis to val and if it is greater than a set amount do something else.
Thomas -
The main problem is that you are delaying the tempo time. When you use "delay" everything stops for that amount of time. Try using millis. compare current millis to val and if it is greater than a set amount do something else.
Thomas@Topdawg2881 that's where I was going with my example. There is still the delay(500) that I left in from his code, but that's a short delay.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login