A Mode State Machine

typedef enum _SM_USER_MODE
{
    UM_POWERUP,
    UM_IDLE
} SM_USER_MODE;

uint8_t CurrentMode = UM_POWERUP;
	static uint8_t CurrentModeLast = 0xff;
	uint8_t JustEnteredNewMode = 0;



	//------------------------------
	//----- CHECK FOR NEW MODE -----
	//------------------------------
	JustEnteredNewMode= 0;
	if (CurrentMode != CurrentModeLast)
	{
		//Flag that we've just entered a new mode
		JustEnteredNewMode = 1;
		CurrentModeLast = CurrentMode;
	}


	//------------------------------------
	//------------------------------------
	//----- PROCESS THE CURRENT MODE -----
	//------------------------------------
	//------------------------------------
	switch (CurrentMode)
	{
	case UM_POWERUP:
		//-------------------
		//-------------------
		//----- POWERUP -----
		//-------------------
		//-------------------
		if (JustEnteredNewMode)
		{
			//----- JUST ENTERED THIS MODE -----

		}
		//------------------------
		//----- PROCESS MODE -----
		//------------------------

		
		break;



	case UM_IDLE:
		//-------------------------------
		//-------------------------------
		//----- IDLE - DEFAULT MODE -----
		//-------------------------------
		//-------------------------------
		if (JustEnteredNewMode)
		{
			//----- JUST ENTERED THIS MODE -----


		}
		//------------------------
		//----- PROCESS MODE -----
		//------------------------



		break;





//<<<<< ADD OTHER MODES HERE



	} //switch (CurrentMode)
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.