-5

I have an Arduino Nano with an ATmega328P. I'm using Atmel Studio to program it in C and I need to have the following program:

I have 5 inputs (PC3-PC7), each should have their separate timer and each drive 2 (one red, one green) LEDs.

  • Each HIGH-level on an input pin (PC3-PC7) triggers a separate timer, which should be 10 minutes long.
  • The HIGH-level on the input pins should last over the course of these 10 minutes. If it changes to a LOW-level while running, something happens (LEDs blink, buzzer on).
  • If the timer has reached the 10-minute mark, something happens (red LED off, green LED on, buzzer on).

I think the time.h library is needed for this, but I have no idea how I could program this. Any help is appreciated.

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Geatrix
  • 113
  • 1
  • 1
  • 7

2 Answers2

1

There is probably no pre-made library for this.

What you need is to program some manner of Hardware Abstraction Layer (HAL) on top of your hardware peripheral timers. One hardware timer/RTC should be enough. Have it trigger with an interrupt every x time units, depending on what pre-scaler clock that is available.

From this timer interrupt, count the individual on-going tasks and see if it is time for them to execute. In pseudo code, it could look something like this:

void timer_ISR (void)  // triggers once every x time units
{
  // clear interrupt source etc here
  // set up timer again, if needed, here

  counter++; // some internal counter

 // possibly, enable maskable interrupts here. If I remember AVR, this is asm SEI.

  for(size_t i=0; i<timers; i++)
  {
    if(counter == timer[i].counter)
    {
      timer[i].execute();
      timer[i].enable = false;
    }
  }
}

where execute is a function pointer to a callback function.

Lundin
  • 195,001
  • 40
  • 254
  • 396
-1

The first thing you should do is set up a Timer interrupt. You can read timer interrupt

After setting the timer, you should use 1 or 2 variables to reach 10 minutes.

Like: timer int should increase Varb1 every time it gets in ISR (interrupt function). Every time Varb1 overflows, Varb2 should increase, and when Varb2 overflows, Varb3 should increase... and so on.

The reason you should have it is that a timer holds only really small times that are in milliseconds or microseconds.

dda
  • 6,030
  • 2
  • 25
  • 34