Timed sequencer

I mocked up a little FET control board to control some pneumatic valves for a project. I’ve got 6 channels I need to control in a certain sequence, not a problem. I’m drawing a blank when it comes timing everything. I’m using Timer2 on my atmega48p and this is what I’m currently doing.

Clock 8MHz

Timer2 Prescaler 1024

I have a Timer2 overflow every 10ms. Working fine. And in the overflow routine I have a counter to count 100 times for 1 second. At that time it will increment a variable called seconds. That is my seconds counter.

What I’m doing is this when I go into a sequence.

void sequence1(void){
	uint8_t temp_time;
	temp_time=seconds;
	G1_open;
	while (seconds!=temp_time+1);
	
	G4_open;
	while (seconds!=temp_time+3);

	G4_close;
	G3_open;
	while (seconds!=temp_time+4);

	G3_close;
	while (seconds!=temp_time+6);

	G2_open;
	while (seconds!=temp_time+8);

	G1_close;
	G6_open;
	while (seconds!=temp_time+10);

	G6_close;
	G2_close;
}

Basically recording the time at the start of the sequence, then timing everything after that based on comparing that value to the real time. This works alright except for the very first event. If the event stars in the middle of the second then the timing is messed up (the first gate is only open for the remainder of the second when the sequence was started) . I feel like there’s probably a better way to do this. And I’m just not seeing it.

the “project” in question

http://picasaweb.google.com/chupa0/Cann … YiG8f-WOA#

my dog would appreciate your help!

The timing of the first stage will be variable; it depends on how far into that second you are when you enter the routine. How about using delay_ms(1000); for a 1-second wait? Or if startup time isn’t critical, add a wait for the first second to roll over after “temp_time = seconds;”.

/mike