#include <Wire.h>
#include <EEPROM.h>
#include <LiquidCrystal_I2C.h>
#define PULSE_PIN 9
#define MOTOR_PIN 3
#define SPEAKER_PIN 4
#define CALIBRATION_DISTANCE_CM 100
#define MAX_ROLL_OUT_METERS 25
LiquidCrystal_I2C lcd(0x27, 16, 2);
volatile unsigned long pulseCount = 0;
unsigned long pulsesPerMeter;
unsigned long rollOutPulses;
unsigned long targetPulseCount;
bool systemOn = false;
int meters = 0;
int setmeters = 25;
int pulses = 0;
bool buttonPressed = false; // Flag to indicate if the button is pressed
unsigned long startTime; // Variable to store the start time
void handlePulse() {
pulseCount++;
}
void setup() {
lcd.init();
lcd.backlight();
lcd.print("display ok");
delay(500);
pinMode(PULSE_PIN, INPUT_PULLUP);
pinMode(MOTOR_PIN, OUTPUT);
pinMode(SPEAKER_PIN, OUTPUT);
digitalWrite(MOTOR_PIN, LOW);
digitalWrite(SPEAKER_PIN, LOW);
attachInterrupt(digitalPinToInterrupt(PULSE_PIN), handlePulse, FALLING);
// Load the stored pulses per meter value from EEPROM
pulsesPerMeter = EEPROM.read(0);
if (pulsesPerMeter == 0) {
// If the EEPROM value is 0, set a default value
pulsesPerMeter = 1000;
}
// Calculate the number of pulses for the maximum roll-out distance
rollOutPulses = pulsesPerMeter * MAX_ROLL_OUT_METERS;
// Display the default value on the LCD
lcd.setCursor(0, 0);
lcd.print("Amount of meters?");
lcd.setCursor(0, 1);
lcd.print(MAX_ROLL_OUT_METERS);
}
void ready() {
digitalWrite(MOTOR_PIN, LOW);
lcd.clear();
lcd.print("ready");
tone(SPEAKER_PIN, 1000, 2000);
delay (2500);
meters = 0;
loop();
}
void rollout() {
while (meters < setmeters) {
lcd.clear();
lcd.print(meters);
delay(50);
meters++;
digitalWrite(MOTOR_PIN, HIGH);
}
ready();
}
void calibrate() {
lcd.clear();
lcd.print("Calibrate mode");
delay (1500);
lcd.clear();
lcd.print("press start when ready");
delay (1500);
while (digitalRead(10) == HIGH) {
delay(50); // Wait for 50ms before checking again
// Start the loop when the start button is pressed
while (digitalRead(10) == LOW) {
while (pulses < 250) {
lcd.clear();
lcd.print(pulses);
delay(1);
pulses++;
digitalWrite(MOTOR_PIN, HIGH);
}}
if (pulses >=250) {
digitalWrite(MOTOR_PIN, LOW);
lcd.clear();
lcd.print("ready");
tone(SPEAKER_PIN, 1000, 2000);
delay (2500);
meters = 0;
pulses = 0;
lcd.clear();
lcd.print("please input");
lcd.setCursor(0, 1);
lcd.print("length");
delay (1500);
lcd.clear();
lcd.blink();
delay (500);
lcd.blink();
}
}
}
void loop() {
lcd.print("loop");
if (digitalRead(PULSE_PIN) == LOW) {
delay (100);
pulseCount++;
lcd.clear();
lcd.print(pulseCount);
}
//if (digitalRead(10) == LOW) {
// pulseCount++;
// lcd.clear();
// lcd.print("ten");
// delay (300);
// lcd.clear();
// }
if (digitalRead(10) == LOW) {
lcd.clear();
lcd.print("roll");
delay (300);
lcd.clear();
rollout(); // Call the rollout function
}
if (digitalRead(11) == LOW) {
lcd.clear();
lcd.print("eleven");
delay (300);
lcd.clear();
}
if (digitalRead(12) == LOW) {
lcd.clear();
lcd.print("twelve");
delay (300);
lcd.clear();
}
if (digitalRead(13) == LOW && !buttonPressed) { // If the button is pressed and not already pressed
buttonPressed = true; // Set the buttonPressed flag
startTime = millis(); // Store the start time
}
if (digitalRead(13) == HIGH) {
buttonPressed = false;
}
if (buttonPressed && millis() - startTime >= 1000) { // If the button has been held down for 1000ms
calibrate(); // Jump to calibrate function
lcd.clear();
buttonPressed = false; // Reset the buttonPressed flag
}
}