Duration control

Hi;

1.Not sure if I have used the code icon to set the sketch out correctly; if not please accept apologies and advise correctprocedure.

2. The sketch below works as a timer but it woud be helpful if the user could set the required duration using a rotary control. I can’t find anything in the Arduino Cookbook or libraries to fit in with the sketch; guidance would be appreciated please.

Thanks

Martyn

#include <LiquidCrystal.h>




const int numCols = 16;

const int numRows = 2;




LiquidCrystal lcd(12, 11, 5, 4, 3, 2);




void setup()

{

  lcd.begin(numCols, numRows);

  lcd.print("Egg Timer");

}




void loop()

{

  lcd.setCursor(0, 1);

  // clear the second row before printing

  lcd.print("                "); // 16 spaces

  lcd.setCursor(0, 1);




  unsigned long totalSeconds = millis() / 1000;

  unsigned int minutes = totalSeconds / 60;

  unsigned int seconds = totalSeconds % 60;




  lcd.print(minutes);

  lcd.print(" m ");

  if (seconds < 10) lcd.print('0');

  lcd.print(seconds);

  lcd.print(" s");




  delay(250); // modest update rate

}

In the Arduino IDE, if you first install the Rotary Encoder library, you will then find some examples under File -> Examples -> RotaryEncoder.

I have used EncoderButton for something like that, the advantage of that library is it also deals with the encoder knob pushbutton and any other buttons/switches you may have.

To be clear, did you want to use a rotary encoder, or would a rotary or slide potentiometer suit? If the latter, simply read the potentiometer and scale the read value using map() to a range of minimum to maximum values. So,

  • buy a 10 kohm potentiometer
  • attach potentiometer - one end to +5, other end to GND, wiper to analog input
  • read analog pin
  • scale value
  • proceed

however, Think this thru. Now its adjustable, so you'll want to see the range before you start it. so now you need two modes, and a button to switch between setup and run...
and, should it beep at end? Now you need a buzzer...
we can help with all this, but you'll need to participate.

2 Likes

You might later on see if it could work using the fewest number of buttons (lots of creative room) and leds just because those are cheaper and… look at cheap alarm clocks to remotes and how they reduce part counts in design. BUT FIRST do it like you thought and prove you can!

A potentiometer is good for analog dial adjuster and there is also a thing called a rotary dial that clicks digitally and is harder to read, easy to miss a click but turns all you want.

You might also one day look into various ways to make a linear sensor that uses resistance or capacitance or magnetism or pressure or something else to allow a range of choices like a slider. There are many different ways already published for Arduino with code on the web and in the Forum Archive including the SOLVED threads.

You could use two pots one for hours one for minutes. This will take @camsysca suggestion for a pot and make it more granular (easier to set). You can expand this any way you like.

look this over

// egg timer

const byte PinPot = A0;
const byte PinBut = A1;
const byte PinLed = 12;

enum { Off = HIGH, On = LOW };

unsigned long msecPeriod;
unsigned long msec0;

bool run;
int  sec;

char s [90];

// -----------------------------------------------------------------------------
void loop ()
{
    unsigned long msec = millis ();

    if (run)  {
        if (msec - msec0 >= msecPeriod)  {
            digitalWrite (PinLed, On);
            run = false;
        }

        sec = (msecPeriod - (msec - msec0)) / 1000;
        sprintf (s, "  %2d:%02d", sec/60, sec%60);
        Serial.println (s);
    }

    // ------------------------------------------------
    else  {
        int anlg = analogRead (PinPot);
        sec = map (anlg, 0, 1023, 9*60, 12*60);
        sprintf (s, " anlg %6d, time %2d:%02d", anlg, sec/60, sec%60);
        Serial.println (s);

        if (LOW == digitalRead (PinBut))  {
            if (On == digitalRead (PinLed))
                digitalWrite (PinLed, Off);
            else {
                msecPeriod = sec *100L;         // *1000L;  shorten for test
                msec0      = msec;
                run        = true;

                Serial.println ("run");
            }
        }
    }

    delay (500);
}

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (9600);

    pinMode (PinBut, INPUT_PULLUP);
    pinMode (PinLed, OUTPUT);
    digitalWrite (PinLed, Off);
}

I think I’d give the user time to set the interval and press a start button, not just follow one timeout with set and run the next.

But that does show beginners the first step first.

the timer doesn't start until the button is pressed

1 Like

Thanks for the responses; may I explain please that I am very much at stage one learning;I use AI to help with code, I guess its questionable but it has helped me to progress albeit the code provided as in the following example often doesnt work; the error message follows the code. I have installed Rotary but there are no apparent examples ; help please; thanks; Martyn





#include <Rotary.h>

 

Rotary r;

 

int lastPos = 0;

 

void setup() {

  Serial.begin(9600);

  r.begin(2, 3); // **pin A = 2, pin B = 3** adjust to your wiring

}

 

void loop() {

  // Many versions expose a position/read method — try read() or getPosition()

  // First try read()

  int pos = r.read(); // if your library doesn't have read(), see instructions below

  if (pos != lastPos) {

    Serial.print("Position: ");

    Serial.println(pos);

    lastPos = pos;

  }

  delay(10);

}

 

FQBN: arduino:avr:mega

Using board 'mega' from platform in folder: C:\Users\marty\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6

Using core 'arduino' from platform in folder: C:\Users\marty\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6

 

Detecting libraries used...

C:\Users\marty\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -IC:\Users\marty\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\cores\arduino -IC:\Users\marty\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\variants\mega C:\Users\marty\AppData\Local\arduino\sketches\0124073ED5FB0D4C3A0AD3204DBF393C\sketch\sketch_oct23a.ino.cpp -o nul

Alternatives for Rotary.h: [Rotary@1.0.0]

ResolveLibrary(Rotary.h)

  -> candidates: [Rotary@1.0.0]

C:\Users\marty\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -IC:\Users\marty\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\cores\arduino -IC:\Users\marty\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\variants\mega -Ic:\Users\marty\Documents\Arduino\libraries\Rotary\src C:\Users\marty\AppData\Local\arduino\sketches\0124073ED5FB0D4C3A0AD3204DBF393C\sketch\sketch_oct23a.ino.cpp -o nul

C:\Users\marty\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -IC:\Users\marty\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\cores\arduino -IC:\Users\marty\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\variants\mega -Ic:\Users\marty\Documents\Arduino\libraries\Rotary\src c:\Users\marty\Documents\Arduino\libraries\Rotary\src\Rotary.cpp -o nul

Generating function prototypes...

C:\Users\marty\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -IC:\Users\marty\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\cores\arduino -IC:\Users\marty\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\variants\mega -Ic:\Users\marty\Documents\Arduino\libraries\Rotary\src C:\Users\marty\AppData\Local\arduino\sketches\0124073ED5FB0D4C3A0AD3204DBF393C\sketch\sketch_oct23a.ino.cpp -o C:\Users\marty\AppData\Local\Temp\3776757081\sketch_merged.cpp

C:\Users\marty\AppData\Local\Arduino15\packages\builtin\tools\ctags\5.8-arduino11/ctags -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives C:\Users\marty\AppData\Local\Temp\3776757081\sketch_merged.cpp

 

Compiling sketch...

"C:\\Users\\marty\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR "-IC:\\Users\\marty\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\cores\\arduino" "-IC:\\Users\\marty\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\variants\\mega" "-Ic:\\Users\\marty\\Documents\\Arduino\\libraries\\Rotary\\src" "C:\\Users\\marty\\AppData\\Local\\arduino\\sketches\\0124073ED5FB0D4C3A0AD3204DBF393C\\sketch\\sketch_oct23a.ino.cpp" -o "C:\\Users\\marty\\AppData\\Local\\arduino\\sketches\\0124073ED5FB0D4C3A0AD3204DBF393C\\sketch\\sketch_oct23a.ino.cpp.o"

C:\Users\marty\AppData\Local\Temp\.arduinoIDE-unsaved2025923-5160-1ypgt8d.y1y5\sketch_oct23a\sketch_oct23a.ino: In function 'void loop()':

C:\Users\marty\AppData\Local\Temp\.arduinoIDE-unsaved2025923-5160-1ypgt8d.y1y5\sketch_oct23a\sketch_oct23a.ino:15:15: error: 'class Rotary' has no member named 'read'

   int pos = r.read(); // if your library doesn't have read(), see instructions below

               ^~~~

Using library Rotary at version 1.0.0 in folder: C:\Users\marty\Documents\Arduino\libraries\Rotary

exit status 1

 

Compilation error: 'class Rotary' has no member named 'read'

Which one exactly?

I searched Rotary in libraries and was given one apparent option’ Rotary’ which I installed, thanks, Martyn

In the IDE, go to file/examples/rotary.

You should see something resembling this screenshot:

Those are examples provided with the library.

Hi, @Sam1Bird2

Search for encoder.

Tom.... :smiley: :+1: :coffee: :australia:

Hint about using a dial potentiometer… they don’t need a library.

1 Like

Thanks for the input from all; I am going to exit this for now as I need do some research before I can go forward.

Thanks again

Martyn

@Sam1Bird2 Please, when you return, continue on with this thread, as it makes retrieving the context very easy. If you start a new thread, many questions will be required to reconstitute what could be gleaned from the 16 posts in this thread.

1 Like

???