# need c program for 5 minutes timer [closed]

i am building a project for a cycling bath in which the motor goes up and down. the micrlimit switch senses up & down position. once switch is activated motor cuts off and timer starts. after 5 minutes the motor starts in reverse direction and moves till it trips the switch theis continues indefinitly. i had written a program as below

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdbool.h>

int main(void){
DDRD=0x00;// for input port-UP & DOWN Micro Switch
DDRC=0xff;// for output port- UP & DOWN Motor
while(1){
if(PIND==0xff)// checking the status of PIN PD5 & PD6, if both is '1', turns ON the down Motor
_delay_ms(100); // for debouncing of switch
PORTC=0x40;// turns ON down motor

if(PIND==0xbf)// checking the status of PIN PD6 (Dn sw), if it is '0', turns off the down Motor
{
_delay_ms(100); // for debouncing of switch
PORTC=0x00;   // Turning off the down Motor
int32_t second;
for (second=0; second <5LL*60;++second)
{
if (second > 5)
{
PORTC=0X80; // turn ON UP motor
} else
{ PORTC = 0x00;}
}
{
if(PIND==0xdf);// checking the status of PIN PD5 (UP SW), if it is '0' turns off the UP motor,
_delay_ms(100); // For debouncing of switch
PORTC=0x00;// Turning off the UP Motor
int32_t second;
for (second=0; second <5LL*60;++second)
{
if (second > 5)
{
PORTC=0X40; // turn ON DOWN motor
} else
{ PORTC = 0x00;}
}

}

{
if(PIND==0x9f);// checking the status of PIN PD5 & 6 if both 0 then stop motor
_delay_ms(100); // for debounicng of switch
PORTC=0x00; // trun off up/down motor

}
}
}
}


## closed as too broad by brhans, Chris Stratton, winny, Dmitry Grigoryev, laptop2dAug 10 '18 at 20:40

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• I fear the answer but: Why don't you run the program on it's environment and see what happens? – Alexander von Wernherr Aug 9 '18 at 13:00
• You need to fix the formatting, particularly the indentation, of your code. – Elliot Alderson Aug 9 '18 at 13:23
• Probably not, as you've neglected to put any timing delay in the seconds loop. – Chris Stratton Aug 9 '18 at 13:56
• sleep(300); // // – mkeith Aug 9 '18 at 14:32

Your code does not get the style award. It might compile, but I will not work as intended.

First you need to define what you want to do. Create a flowchart.

Then you look up how you create a state machine in C. Because this looks like a perfect job for a state machine.
Create a small dummy application perhaps, on the pc.

You'll also want to look up how to compare, for set inputs.
if(PIND==0xdf); is not the way. Try if(PIND & (1<<PIN).

Then you will have learned a lot, and perhaps your able to run this on target.

Or, you get a Siemens LOGO (PLC) and program it with an IEC 61131-3 variant.
It will provide you with the building blocks to build this reliably and quickly. At the small cost of the hardware.

To answer your title, to create a wait time this long on an embedded platform a software timer is used. One way to do this is with a system tick.

uint32_t stim1 = 0; // unsigned 32 bit software timer 1

const unsigned int interval = 20; // Interval of hardware timer in milliseconds

// Timer 1 Compare interrupt, constant interval, 20 ms for example.
ISR (TIMER1_COMPA_vect)
{
// Saturating increment software timer1
if(stim1 < (0xFFFFFFFF-interval))
stim1 += interval;
}

int main(void){
init_tim1();

while(1){
if(stim1 > 1000){      // wait 1 second
PORTB ^= (1 << 0);   // toggle led
stim1 = 0;           // reset software timer
}
}
}

• And remove magic numbers like 0xdf – Alexander von Wernherr Aug 9 '18 at 14:02