Help with programme writing

I am struggleing writing a code to control a valve output controlled by a button input with an emergency stop also written in.!!

The Format of the test is as followed

Button Pressed

System Primed - which involved 10 x 2sec of output causing the valve to open with 2 sec rest intervals inbetwwen.

this is then followed by 5 mins of valve being open

close valve

END - wait for button to be pressed to comense test again....

with an emergency stop built in to end the process straight away!!!

ANY HELP WOULD BE VERY MUCH APRIECIATED!!!!!

L JOHNSTON

Time: So you would need to start with a clock.

States: Would imply a state machine. (This is basically a switch/case statement which monitors a variable which keeps track of what “state” the process is at).
[edit]
Here is a clock which runs on a state machine: add states for each thing you want your code to do.

enum TimerState {

No_Update,
 Update_seconds,
 Update_minutes,
 Update_hours,
};

/******************************************************

  • Class for keeping time
    ******************************************************/
    class TimeDateKeeper {
    public:
     char seconds;
     char minutes;
     char hours;
     TimeDateKeeper(char _sec_in, char _min_in, char _hour_in);
     int Update_seconds();
     int Update_minutes();
     int Update_hours();
    };

//constructor for class
TimeDateKeeper::TimeDateKeeper(char _sec_in, char _min_in, char _hour_in) {
 seconds = _sec_in;
 minutes = _min_in;
 hours = _hour_in;
}

//if resetting, return increment error. Otherwise increment seconds
int TimeDateKeeper::Update_seconds() {
 if(seconds >= 59) {
   seconds = 0;
   return 0;
 }
 seconds++;
 return 1;
}

//if resetting, return increment error. Otherwise increment minutes
int TimeDateKeeper::Update_minutes() {
 if(minutes >= 59) {
   minutes = 0;
   return 0;
 }
 minutes++;
 return 1;
}

//if resetting, return increment error. Otherwise increment hours
int TimeDateKeeper::Update_hours() {
 if(hours >= 23) {
   hours = 0;
   return 0;
 }
 hours++;
 return 1;
}
/******************************************************/

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

SetupTimerISR();
}

unsigned long int_count = 0;
unsigned long max_count = 100000;
TimeDateKeeper myTimer(0,0,0);
TimerState State = No_Update;

void loop()
{

switch (State) {
 case No_Update:
   {
     if (int_count >= max_count) {
       int_count = 0;
       State = Update_seconds;
       DisplayTime();
     }
     break;
   }
 case Update_seconds:
   {
     //if seconds reset to 0, update minutes. Else, return to no update status
     !myTimer.Update_seconds() ? State = Update_minutes : State = No_Update;
     break;
   }
 case Update_minutes:
   {
     //if minutes reset to 0, update hours. Else, return to no update status
     !myTimer.Update_minutes() ? State = Update_hours : State = No_Update;
     break;
   }
 case Update_hours:
   {
     //Update hours and return to “no update” status.
     myTimer.Update_hours();
     State = No_Update;

//for future development…
     //!myTimer.Update_hours() ? State = Update_date : State = No_Update;

break;
   }

default:
   {
     //if error state is found, return to “no update” status
     State = No_Update;
     break;
   }

}
}

//This is where the user chooses how to display
//the clock time…
void DisplayTime() {
 Serial.print(myTimer.hours, DEC);
 Serial.print(":");
 Serial.print(myTimer.minutes, DEC);
 Serial.print(":");
 Serial.println(myTimer.seconds, DEC);
}
void SetupTimerISR() {
 // Disable interrupts while setting registers
 cli();

// Reset control registers
 TCCR2A = 0;
 TCCR2B = 0;

// Clear Timer on Compare Match (CTC) Mode
 TCCR2A |= (1 << WGM21);

// Prescaler x1
 TCCR2B |= (1 << CS20);

// Interrupt every 160/16e6 = 10 usec
 OCR2A = 159;

// Use system clock for Timer/Counter2
 ASSR &= ~(1 << AS2);

// Reset Timer/Counter2 Interrupt Mask Register
 TIMSK2 = 0;

// Enable Output Compare Match A Interrupt
 TIMSK2 |= (1 << OCIE2A);

// Enable interrupts once registers have been update
 sei();
}

// This ISR is run when Timer/Counter2 reaches OCR2A
ISR(TIMER2_COMPA_vect)
{
 int_count++;
}

Apologies for cross post[/edit]

You also need to NOT cross post.