From 87b1cb9acd39f8cdf366aff799cb8356478e67f5 Mon Sep 17 00:00:00 2001 From: jakeg00dwin Date: Sun, 23 Feb 2025 12:29:52 -0800 Subject: [PATCH] removed the duplicate bit shifting in favor of single operation in function. --- inc/main.h | 2 +- src/main.c | 25 ++++++++++++------------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/inc/main.h b/inc/main.h index 9644316..a5dde62 100644 --- a/inc/main.h +++ b/inc/main.h @@ -105,7 +105,7 @@ void InitProg(void); /** * @brief Reads the ADC pin from the fader. */ -uint16_t ReadFader(void); +uint8_t ReadFader(void); /** diff --git a/src/main.c b/src/main.c index 89176aa..92aaaac 100644 --- a/src/main.c +++ b/src/main.c @@ -104,7 +104,7 @@ uint8_t ReadSpeed(void) { } // change to ReadFader(void) -uint16_t ReadFader(void) { +uint8_t ReadFader(void) { // Initialize ADC ADMUX = (1 << MUX1) | (1 << MUX0); // Select ADC3 (PB3) ADCSRA = (1 << ADEN) | (1 << ADPS1) | (1 << ADPS0); // Enable ADC, prescaler 8 @@ -112,7 +112,7 @@ uint16_t ReadFader(void) { ADCSRA |= (1 << ADSC); // Start conversion while (ADCSRA & (1 << ADSC)) { } // Wait for conversion to finish - return ADC; + return (uint8_t)(ADC >> 2); } /* @@ -129,25 +129,24 @@ static inline uint8_t diff(uint8_t a, uint8_t b) { } void MotorSetSavePos(uint8_t *ADR) { - uint8_t pos = (uint8_t)(ReadFader() >> 2); - //eeprom_write_byte((uint8_t *)ADR, pos); - eeprom_update_byte((uint8_t *)ADR, pos); + uint8_t pos = ReadFader(); + eeprom_write_byte((uint8_t *)ADR, pos); return; } uint8_t MotorGetSavedPos(uint8_t *ADR) { - return (uint8_t)eeprom_read_byte((uint8_t *)POSITION1_ADR); + return (uint8_t)eeprom_read_byte((uint8_t *)ADR); } void MotorMoveTo(uint8_t target) { uint8_t on_delay = ReadSpeed(); - uint8_t pos = (uint8_t)(ReadFader() >> 2); + uint8_t pos = ReadFader(); uint8_t idx = 0; - while (diff(target, pos) > 8) { + while (diff(target, pos) > 4) { on_delay = ReadSpeed(); - pos = (uint8_t)(ReadFader() >> 2); + pos = ReadFader(); if (target > pos) { MotorMove(1); } else { @@ -270,14 +269,14 @@ static void UpdateButtonOutput(btn_state *b) { /*If the button pres was a short one.*/ else if (!b->is_long_pressed) { if (b->is_active) { - /*Save the current position into position 2 EEPROM*/ - MotorSetSavePos((uint8_t *)POSITION2_ADR); /*If already in saved position then go back to original pos.*/ - if(diff(MotorGetSavedPos((uint8_t *)POSITION1_ADR), ReadFader()) > 8){ - MotorMoveTo(MotorGetSavedPos((uint8_t *)POSITION1_ADR)); + if(diff(MotorGetSavedPos((uint8_t *)POSITION1_ADR), ReadFader()) < 4){ + MotorMoveTo(MotorGetSavedPos((uint8_t *)POSITION2_ADR)); } else{ + /*Save the current position into position 2 EEPROM*/ + MotorSetSavePos((uint8_t *)POSITION2_ADR); MotorMoveTo(MotorGetSavedPos((uint8_t *)POSITION1_ADR)); } } else {