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 off

    delay(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



  • It would help if your code was indented and put in a code block. When posting, click the </> icon and paste your indented code, or paste it in and put a line before and one after your code that has 3 backticks ( ` ). That is how to signify a code block.



  • 
    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



  • Thank you.. Much easier to read.



  • 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...



  • Can we see how you have things hooked up? Show us your schematic.





  • 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.


Log in to reply
 

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.