#include <Wire.h>
#include <LiquidCrystal_I2C.h>
int progress = 0;
#define ENCODER_CLK 2
#define ENCODER_DT 3
const int buttonpin = 4;
int currentmode = 1;
int newmode = 0;
int degreeround;
int pos = 0;
#define potPin A0
int globalPotValue = 0; // Rename the global variable
int previousPotValue = 0;
boolean isRecording = false;
const int numReadings = 50; // Number of readings to consider
int potValues[numReadings]; // Array to store potentiometer readings
int threshold = 10; // Adjust the threshold based on your requirements
int currVal;
int lastClk = HIGH;
int menustate = 0;
#define LCD_ADDRESS 0x27
#define LCD_COLUMNS 16
#define LCD_ROWS 2
void setup() {
Serial.begin(9600);
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
pinMode(potPin, INPUT);
pinMode(buttonpin, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
lcd.begin(LCD_COLUMNS, LCD_ROWS);
lcd.setBacklight(LOW);
}
void loop() {
int buttonstate = digitalRead(buttonpin);
if (digitalRead(buttonpin) == LOW) {
lcd.print("Mode: ");
if (menustate < 3){
menustate++;
} else if (menustate > 1){
menustate--;
menustate--;
}
delay(200); // Debounce delay
Serial.print(menustate);
}
switch (menustate) {
case 1:
lcd.setCursor(6,0);
lcd.print("1");
lcd.setCursor(0,1);
lcd.print("Club Mode");
delay(0);
while (digitalRead(buttonpin) == HIGH) {
// Wait for the button to be released
menu1();
}
lcd.clear();
break;
case 2:
lcd.setCursor(6,0);
lcd.print("2");
delay(0);
while (digitalRead(buttonpin) == HIGH) {
// Wait for the button to be released
menu2();
}
lcd.clear();
break;
case 3:
lcd.setCursor(6,0);
lcd.print("3");
lcd.setCursor(0,1);
lcd.print("Storing Swing");
delay(0);
while (digitalRead(buttonpin) == HIGH) {
// Wait for the button to be released
menu3();
}
lcd.clear();
break;
}
}
void menu1(){
lcd.setCursor(0,0);
//check encoder position
int newClk = digitalRead(ENCODER_CLK);
if (newClk != lastClk) {
// There was a change on the CLK pin
lastClk = newClk;
int dtValue = digitalRead(ENCODER_DT);
if (newClk == LOW && dtValue == HIGH) {
if (currentmode < 9) {
currentmode++; // Use pre-increment to update the value before using it
newmode = currentmode;
lcd.print(newmode);
lcd.println(" Iron");
lcd.setCursor(0,1);
lcd.print("Loft Angle: ");
int degreeround = round(13.5 + 2.1*newmode + 0.119*pow(newmode, 2));
lcd.print(degreeround);
lcd.print((char)223);
}
}
if (newClk == LOW && dtValue == LOW) {
if (currentmode > 2) {
currentmode--; // Use pre-decrement to update the value before using it
newmode = currentmode;
lcd.print(newmode);
lcd.println(" Iron");
lcd.setCursor(0,1);
lcd.print("Loft Angle: ");
int degreeround = round(13.5 + 2.1*newmode + 0.119*pow(newmode, 2));
lcd.print(degreeround);
lcd.print((char)223);
}
}
}
//set servo position
}
void menu2(){
lcd.setCursor(0,1);
lcd.print("Auto Correction");
}
void menu3(){
int currVal = analogRead(potPin); // Use a different name for the local variable
if (currVal < previousPotValue-threshold || currVal > previousPotValue+threshold) { // Check if the change is significant
isRecording = true;
unsigned long startTime = millis(); // Record the start time
lcd.setCursor(0,1);
lcd.print("swing stored");
for (int i = 0; i < numReadings; i++) {
potValues[i] = analogRead(potPin);
delay(0); // Adjust the delay based on your potentiometer reading rate
int elapsedTime = millis() - startTime; // Calculate the elapsed time
// Check if the potentiometer has stopped moving
if (abs(currVal - previousPotValue) <= threshold) {
isRecording = false;
break; // Exit the loop if the potentiometer stops moving
}
}
isRecording = false; // Set the recording flag to false after finishing the loop
}
previousPotValue = currVal;
delay(10);
}