int strokecount = 0;
int lock = 3;
int feed = 4;
int button = 10;
int buttonstate = 0;
int lastButtonState = LOW;
int strokestillfinish = 0; // Total strokes dynamically calculated
int feednum = 0;
// Editable
int totalcycles = 1; // Number of feeding cycles to complete
int minfeedtime = 2;
int maxstrokeincrease = 5;
int maxFeedTimeRegular = 8; // Max feed time for regular cycles (in seconds)
int maxFeedTimeHardMode = 4; // Max feed time for hard mode hold (in seconds)
int hardMode = 0; // Enable or disable hard mode (1 = enabled, 0 = disabled)
// Not editable
int feedtime = 0;
int holdTime = 0; // Time in seconds the button must be held (dynamic: maxFeedTimeHardMode + 2)
bool hardModeFailed = false;
bool holdMessageDisplayed = false;
bool warningMessageDisplayed = false;
void setup() {
Serial.begin(9600);
pinMode(lock, OUTPUT);
pinMode(feed, OUTPUT);
pinMode(button, INPUT);
digitalWrite(lock, HIGH); // Turn the lock on initially
randomSeed(analogRead(0)); // Seed random number generator
strokestillfinish = random(5, 15); // Initialize strokestillfinish for the first cycle
feedtime = minfeedtime * 1000; // Initialize feedtime in milliseconds
holdTime = maxFeedTimeHardMode + 2; // Set holdTime dynamically
// Print initial values for debugging
Serial.print("Initial strokes required for cycle: ");
Serial.println(strokestillfinish);
}
void loop() {
// Read the current button state
buttonstate = digitalRead(button);
// Detect button press (transition from LOW to HIGH)
if (buttonstate == HIGH && lastButtonState == LOW) {
strokecount++; // Increment the strokecount
Serial.print("strokecount: ");
Serial.println(strokecount);
// Display warning message on second-to-last stroke during the last cycle
if (hardMode == 1 && feednum == totalcycles - 1 && strokecount == strokestillfinish - 1 && !warningMessageDisplayed) {
Serial.println("Warning: The next stroke is the last one!");
warningMessageDisplayed = true;
}
// Display hold message only during the last cycle and on the last stroke
if (hardMode == 1 && feednum == totalcycles - 1 && strokecount == strokestillfinish && !holdMessageDisplayed) {
Serial.print("Stroke number ");
Serial.print(strokecount);
Serial.println(" is a hold. Prepare to hold the button!");
holdMessageDisplayed = true;
}
delay(250); // Simple debounce delay
}
// Update the last button state
lastButtonState = buttonstate;
// Check if strokecount has reached strokestillfinish
if (strokecount >= strokestillfinish) {
if (hardMode == 1 && feednum == totalcycles - 1 && !hardModeFailed) {
// Hard mode logic during the final feed
Serial.print("Hard mode active! You must hold the button for ");
Serial.print(holdTime);
Serial.println(" seconds.");
// Wait for button press to start the hold
Serial.println("Press and hold the button to begin the final feed.");
while (digitalRead(button) == LOW) {
// Wait until the button is pressed
}
Serial.println("Button pressed! Hold it down...");
// Require button to be held during the feed process
unsigned long startTime = millis();
unsigned long holdDuration = holdTime * 1000;
bool buttonHeld = true;
digitalWrite(feed, HIGH); // Start feeding
while (millis() - startTime < holdDuration) {
if (digitalRead(button) == LOW) {
buttonHeld = false; // Button was released
break;
}
}
digitalWrite(feed, LOW); // End feeding
if (buttonHeld) {
Serial.println("Button held successfully! Final finish complete.");
feednum++;
holdMessageDisplayed = false; // Reset message flag
} else {
Serial.println("Button hold failed! Additional finish required.");
hardModeFailed = true;
holdMessageDisplayed = false; // Reset message flag for proper state transition
}
} else {
// Normal feeding process or retry after a failed hold
feednum++;
digitalWrite(feed, HIGH);
delay(random(minfeedtime * 1000, maxFeedTimeRegular * 1000)); // Simulate feeding process
digitalWrite(feed, LOW);
delay(1000); // Allow time before resetting
}
// Reset strokecount and update strokestillfinish
strokecount = 0;
warningMessageDisplayed = false; // Reset warning flag
if (!hardModeFailed) {
strokestillfinish += random(1, maxstrokeincrease + 1); // Increment strokestillfinish
Serial.print("Updated strokes required for next cycle: ");
Serial.println(strokestillfinish);
} else {
// Reset hard mode failure state after additional finish
hardModeFailed = false;
}
}
// Check if feednum has reached totalcycles
if (feednum >= totalcycles) {
digitalWrite(lock, LOW); // Unlock
}
}