Skip to content

Instantly share code, notes, and snippets.

@okiwan
Created June 4, 2023 16:23
Show Gist options
  • Select an option

  • Save okiwan/ba4766edc10dbcde29732dae0a7fdbe6 to your computer and use it in GitHub Desktop.

Select an option

Save okiwan/ba4766edc10dbcde29732dae0a7fdbe6 to your computer and use it in GitHub Desktop.
Small refactor on motor method
// Podemos separar la definición de las constantes
// en un archivo .H a parte e incluir después
#define MODE_SHIT 0
#define MODE_STORAGE 1
#define KEYDOWN_THRESHOLD 1500
#define LCD_DISPLAY_DELAY 1000
void display(int x, int y, char *string) {
lcd.setCursor(x, y);
lcd.print(string);
delay(LCD_DISPLAY_DELAY);
}
void mode_storage() {
int pressed_key = get_key()
float keydown_time = millis();
bool already_stored = false;
char *display_string;
float *location;
void *motor_method;
switch(pressed_key) {
case UP:
location = &aLocation;
motor_method = &gotoA;
display_string = "Store A ";
break;
case DOWN:
location = &bLocation;
motor_method = &gotoB;
display_register = "Store B ";
break;
default:
break;
}
while(get_key() == pressed_key) {
if !(already_stored) && (millis() - keydown_time > KEYDOWN_THRESHOLD) {
*location = counts;
display(0, 0, display_string);
already_stored = true;
}
}
// If we did not reach the threshold, we set the motor position
if(millis() - keydown_time < KEYDOWN_THRESHOLD){
*motor_method();
}
}
void main() {
// ...
switch(mode) {
case MODE_SHIT:
break;
case MODE_STORAGE:
mode_storage();
break;
default:
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment