Simple LCD Based Kitchen Timer

Hi All,

For a while now i have been using my Blackberry Countdown Timer to let me know when the PCB exposure time is up.

Now I Ready to create a Attiny2313 LCD Based Version.

I have got all the Parts and written the Code to read the Rotary Encoder using interrupts and display data on a 16x2 LCD.

Where I get stuck: Writing a code that will count down the second.

Has anyone got any Pointers?

Thanks

Ddea

Sounds very simple for me.

Please show us your code.

Is this a program logic question, or C language related?

You need a timebase, not neccessarily using a crystal, but generating an interrupt every second.

Then you need some buttons and a simple menu to edit the preset, and a logic to start counting down from that preset.

Do you have a working PCB/skeleton source ready (LCD initialization, timer int, query buttons etc.)?

Do you only want to use rotary encoder or also pushbuttons? Only using rotary encoder is also possible I think, if you work the encoder, simply time will become increased (again).

Do you have an accurate millis() call? (Is the Arduino IDE supporting this target?)

If so, when starting, take the number of seconds to go, multiply by 1000L (long) and add millis(). Store it in a long variable callex targetTime.The number of seconds left is then (targetTime - millis())/1000. Check the counter at least 10 times a second to get a smoothly counting timer display.

Cheers for the responses.

At the moment I'm using a Event Library: it is listening for the Select Button and can also be used to check the countdown.

using your replies i made a start, but I'm racking my brain trying to figure out how to count down once the start button has been released

My code so far:

#include "EventManager.h" 
#define TimePlus_Pin 2
#define TimeMinus_Pin 3

volatile unsigned long startTime;
volatile unsigned long timerDuration;
volatile unsigned long DurationSelect;
int Timer_Active = 0; 

void setup() {
  Serial.begin (9600);
  pinMode(TimePlus_Pin, INPUT); 
  pinMode(TimeMinus_Pin, INPUT); 
  attachInterrupt(0, doEncoderA, CHANGE);
  attachInterrupt(1, doEncoderB, CHANGE);  
  InitEvents();
  AddEvent(DIGITAL_INPUT_LOW,ONE_SHOT,15,Timer_Start);
}

void loop(){
  ProcessEvents(millis());  
}

void doEncoderA(){
  if (digitalRead(TimePlus_Pin) == HIGH) {
    if (digitalRead(TimeMinus_Pin) == LOW) {  
      DurationSelect = DurationSelect + 1;         // CW
    } 
    else {
      DurationSelect = DurationSelect - 1;         // CCW
    }
  }  
  else
  {
    if (digitalRead(TimeMinus_Pin) == HIGH) {   
      DurationSelect = DurationSelect + 1;          // CW
    } 
    else {
      DurationSelect = DurationSelect - 1;          // CCW
    }
  }
  Serial.println (DurationSelect, DEC);          
} 

void doEncoderB(){
  if (digitalRead(TimeMinus_Pin) == HIGH) {   
    if (digitalRead(TimePlus_Pin) == HIGH) {  
      DurationSelect = DurationSelect + 1;    
    }          // CW

    else {
      DurationSelect = DurationSelect - 1;    
    }        // CCW         

  }
  else { 
    if (digitalRead(TimePlus_Pin) == LOW) {   
      DurationSelect = DurationSelect + 1;          // CW
    } 
    else {
      DurationSelect = DurationSelect - 1;          // CCW
    }
  }
  Serial.println (DurationSelect, DEC);          
} 

void Timer_Start(void){
  if (Timer_Active = 1){
    ; // need to cancel countdown
  }
  else{
    Timer_Active = 1;
  }
  AddEvent(DIGITAL_INPUT_LOW,ONE_SHOT,15,Timer_Start_R);
}

void  Timer_Start_R(void){
  if (Timer_Active = 1){
    if (DurationSelect == 0){
      ;
    }
    else {
      unsigned long timeSpent, remainingTime;
      startTime = millis(); 
      timeSpent = millis() - startTime;
      remainingTime = (timerDuration > timeSpent ? timerDuration - timeSpent : 0);
      AddEvent(DIGITAL_INPUT_LOW,ONE_SHOT,15,Timer_Start);
    }
  }
  else{
    // stop timer
  }
}

Thanks

Have you tried to draw a flow chart, rather thinking of a state machine, than device dependent "events processing"?

I tried to read your program, but I do not find something that starts producing "timer events" regularily.

You could start such an interrupt source anyway, only use it to take action if the "timer becomes active", otherwise do nothing.

It's interesting system, this event manager, reminds me on the windows loop (visual Studio). I'd normally use 16F PICs, where it is much more simple to set up relevant interrupts. But also examining this forum.

A state machine basically is a "finite state machine", the number of possible conditions is limited, and it traverses through them according to conditions. It really helps to draw it up on paper. There is a PDF about them, rather talking about digital logic, but it's the same for program logic.

http://pic.hitechworld.org/StateMachineDesign.pdf