Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. My Project
  3. Timer Help

Timer Help

Scheduled Pinned Locked Moved My Project
10 Posts 3 Posters 2.2k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Bruno CunhaB Offline
    Bruno CunhaB Offline
    Bruno Cunha
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • dbemowskD Offline
      dbemowskD Offline
      dbemowsk
      wrote on last edited by
      #2

      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.

      Vera Plus running UI7 with MySensors, Sonoffs and 1-Wire devices
      Visit my website for more Bits, Bytes and Ramblings from me: http://dan.bemowski.info/

      1 Reply Last reply
      0
      • Bruno CunhaB Offline
        Bruno CunhaB Offline
        Bruno Cunha
        wrote on last edited by
        #3
        
        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

        1 Reply Last reply
        0
        • dbemowskD Offline
          dbemowskD Offline
          dbemowsk
          wrote on last edited by
          #4

          Thank you.. Much easier to read.

          Vera Plus running UI7 with MySensors, Sonoffs and 1-Wire devices
          Visit my website for more Bits, Bytes and Ramblings from me: http://dan.bemowski.info/

          1 Reply Last reply
          0
          • dbemowskD Offline
            dbemowskD Offline
            dbemowsk
            wrote on last edited by
            #5

            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.

            Vera Plus running UI7 with MySensors, Sonoffs and 1-Wire devices
            Visit my website for more Bits, Bytes and Ramblings from me: http://dan.bemowski.info/

            1 Reply Last reply
            0
            • Bruno CunhaB Offline
              Bruno CunhaB Offline
              Bruno Cunha
              wrote on last edited by
              #6

              Well that remains a problem, the relay keeps turning in and off even if i dont press the button...

              1 Reply Last reply
              0
              • dbemowskD Offline
                dbemowskD Offline
                dbemowsk
                wrote on last edited by
                #7

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

                Vera Plus running UI7 with MySensors, Sonoffs and 1-Wire devices
                Visit my website for more Bits, Bytes and Ramblings from me: http://dan.bemowski.info/

                1 Reply Last reply
                0
                • Bruno CunhaB Offline
                  Bruno CunhaB Offline
                  Bruno Cunha
                  wrote on last edited by
                  #8

                  https://circuits.io/circuits/4669781-the-unnamed-circuit

                  Well is something like that

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    Topdawg2881
                    wrote on last edited by
                    #9

                    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

                    dbemowskD 1 Reply Last reply
                    1
                    • T Topdawg2881

                      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

                      dbemowskD Offline
                      dbemowskD Offline
                      dbemowsk
                      wrote on last edited by
                      #10

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

                      Vera Plus running UI7 with MySensors, Sonoffs and 1-Wire devices
                      Visit my website for more Bits, Bytes and Ramblings from me: http://dan.bemowski.info/

                      1 Reply Last reply
                      1
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      25

                      Online

                      11.7k

                      Users

                      11.2k

                      Topics

                      113.1k

                      Posts


                      Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • MySensors
                      • OpenHardware.io
                      • Categories
                      • Recent
                      • Tags
                      • Popular