diff --git a/src/main.c b/src/main.c index de0638b..1d632f4 100644 --- a/src/main.c +++ b/src/main.c @@ -73,8 +73,7 @@ void InitProg(void) { // Checks against a bit pattern we defined to represent the start of data. if (eeprom_read_byte((uint8_t *)ROM_SP_ADR) == START_PATTERN) { - // Reads the two bytes representing the two states. - btn1.is_active = eeprom_read_byte((uint8_t *)ROM_SS1_ADR); + MotorMoveTo(eeprom_read_byte((uint8_t *)ROM_SS1_ADR)); } else { // otherwise we write the init values for the start pattern and states. eeprom_write_byte((uint8_t *)ROM_SP_ADR, START_PATTERN); @@ -119,23 +118,23 @@ uint8_t MotorGetSavedPos(void) { } void MotorMoveTo(uint8_t target) { - uint8_t pos = 0; - while (target != pos) { + + uint8_t pos = (uint8_t)(ReadADC() >> 2); + + while (diff(target, pos) > 8) { pos = (uint8_t)(ReadADC() >> 2); - if (diff(target, pos) < 8){ - MotorCoast(); - break; - } - else if (target > pos) { + if (target > pos) { MotorMove(1); } else { MotorMove(0); } + // The delay ratio controlls the PWM waveforms. _delay_ms(5); MotorCoast(); _delay_ms(5); } + return; } @@ -213,6 +212,7 @@ static void UpdateButtonInput(btn_state *b) { static void UpdateButtonOutput(btn_state *b) { /*If the button is actually pressed.*/ if (b->is_pressed) { + b->is_active = 1; /*If this is a new event.*/ if (!b->is_long_pressed && !b->timer_enabled) { @@ -242,8 +242,14 @@ static void UpdateButtonOutput(btn_state *b) { } /*If the button pres was a short one.*/ else if (!b->is_long_pressed) { - MotorMoveTo(MotorGetSavedPos()); + if(b->is_active) { + MotorMoveTo(MotorGetSavedPos()); + } + else{ + MotorCoast(); + } } + b->is_active = 0; ClearButtonTimer(b); } }