Matt,
I glanced back at your other request and realized it would be pretty simple to add to my ATTiny10 example as well, so here are the changes you'd need for that, still referencing the base code I posted here
//add a variable to time button presses unsigned int press_timer;
//change debounce routine to: //time button presses and //throw new press flag after switch is released instead of after it is pressed void debounce(void) { static char port_copy=0xff; #define switch_mask 0b00000001 //this selects PB0 as the switch
if((PINB&switch_mask)==port_copy) //if the current state matches previous state { if(--switch_count==0) //count down samples. if 10 consecutive samples matched { switch_count=10; //reset sample counter if(PINB&switch_mask){ //if the state is high, if(pressed) new_press=1; //if the last state was pressed, the switch was just released. indicate new complete press pressed=0; //indicate switch is up } else //else switch is down. check for new press { //if(pressed==0) new_press=1; //if last state of pressed was 0, this is a new press if(pressed==0) press_timer=0; //restart the timer if the switch just went down pressed=1; //switch is now down press_timer++; //count up time while switch is held down } } } else //state doesn't match, { switch_count=10; //reset sample counter port_copy=(PINB&switch_mask); //get new sample }
}
//change mode change hanlder in main loop to distinguish between press lengths if(new_press){ new_press=0; if(press_timer<30){ //if held less than 300ms (my time limit, change if desired) mode++; //increment mode if(mode>max_mode) mode=0; //limit mode variable to available values } else if(press_timer<60){ //if held >300ms and <600ms, mode--; //decrement mode if(mode>max_mode) mode=mode_max; //limit mode variable to available values } else //else, it was held a long time mode=off; initialize_mode(); }