I work on sections at a time. This part of my program is for two sets of strobe patterns. Each set has 6 functions in it. They are connected to pen 7 and pen 8. I know the patterns work great as they have been tested.
What I was looking for was if you pressed button on pen 10 for 3 seconds it would start a set up where it takes the first pattern in the set and run it 20 seconds then pen 7 to high for 3 seconds and then run the next pattern and continue until the button is pressed. at the time it would stop and write that pattern to epprom. now if you hold th button for 6 seconds it would do all this but witht he second set on pen 8
What I get is when I hold the button for 3 seconds pen 7 LED is on and stays on till i hit the button then its off. If I do it for 6 I get LED on pen 8 on with same results. Its not running the pattersn.
#include <EEPROM.h>
// Pin Definitions
const int stro1led = 7;
const int stro2led = 8;
const int BUTTON_PIN = 10;
// EEPROM Memory Addresses
const int EEPROM_ADDR_SP1 = 0;
const int EEPROM_ADDR_SP2 = 1;
// Timing Constants (in milliseconds)
const unsigned long HOLD_S1_TIME = 3000; // 3 seconds
const unsigned long HOLD_S2_TIME = 6000; // 6 seconds
const unsigned long RUN_DURATION = 20000; // 20 seconds
const unsigned long PAUSE_DURATION = 3000; // 3 seconds
// State Machine Definitions
enum SystemState { IDLE, RUNNING_S1, PAUSING_S1, RUNNING_S2, PAUSING_S2 };
SystemState currentState = IDLE;
// Execution Tracking
int currentFunctionIndex = 0;
unsigned long stateStartTime = 0;
unsigned long buttonPressStartTime = 0;
bool buttonWasPressed = false;
// strobe 1 patterns
//--------------------------------------------------------------------------------------------------------------
void s1pattern1() {
static int s1currentState = 0; // Keeps state between calls
unsigned long s1currentMillis = millis();
static unsigned long s1previousMillis = 0; // Keeps time between calls
const unsigned long s1p1intervals[4] = {15, 100, 15, 1370};
const int s1p1ledStateValues[4] = {HIGH, LOW, HIGH, LOW};
if (s1currentMillis - s1previousMillis >= s1p1intervals[s1currentState]) {
s1previousMillis = s1currentMillis; // Save the time of the change
// Move to the next state (loops back to 0 after 3)
s1currentState = (s1currentState + 1) % 4;
// Apply the new LED state
digitalWrite(stro1led, s1p1ledStateValues[s1currentState]);
}
}
void s1pattern2() {
static int s1currentState = 0; // Keeps state between calls
unsigned long s1currentMillis = millis();
static unsigned long s1previousMillis = 0; // Keeps time between calls
const unsigned long s1p2intervals[2] = {15, 1485}; // pattern 2
const int s1p2ledStateValues[2] = {HIGH, LOW}; // pattern 2
if (s1currentMillis - s1previousMillis >= s1p2intervals[s1currentState]) {
s1previousMillis = s1currentMillis; // Save the time of the change
// Move to the next state (loops back to 0 after 1)
s1currentState = (s1currentState + 1) % 2;
// Apply the new LED state
digitalWrite(stro1led, s1p2ledStateValues[s1currentState]);
}
}
void s1pattern3() {
static int s1currentState = 0; // Keeps state between calls
unsigned long s1currentMillis = millis();
static unsigned long s1previousMillis = 0; // Keeps time between calls
const unsigned long s1p3intervals[2] = {15, 985}; // pattern 3
const int s1p3ledStateValues[2] = {HIGH, LOW}; // pattern 2
if (s1currentMillis - s1previousMillis >= s1p3intervals[s1currentState]) {
s1previousMillis = s1currentMillis; // Save the time of the change
// Move to the next state (loops back to 0 after 1)
s1currentState = (s1currentState + 1) % 2;
// Apply the new LED state
digitalWrite(stro1led, s1p3ledStateValues[s1currentState]);
}
}
void s1pattern4() {
static int s1currentState = 0; // Keeps state between calls
unsigned long s1currentMillis = millis();
static unsigned long s1previousMillis = 0; // Keeps time between calls
const unsigned long s1p4intervals[2] = {15, 735}; // pattern 2
const int s1p4ledStateValues[2] = {HIGH, LOW}; // pattern 2
if (s1currentMillis - s1previousMillis >= s1p4intervals[s1currentState]) {
s1previousMillis = s1currentMillis; // Save the time of the change
// Move to the next state (loops back to 0 after 1)
s1currentState = (s1currentState + 1) % 2;
// Apply the new LED state
digitalWrite(stro1led, s1p4ledStateValues[s1currentState]);
}
}
void s1pattern5() {
static int s1currentState = 0; // Keeps state between calls
unsigned long s1currentMillis = millis();
static unsigned long s1previousMillis = 0; // Keeps time between calls
const unsigned long s1p5intervals[2] = {15, 585}; // pattern 2
const int s1p5ledStateValues[2] = {HIGH, LOW}; // pattern 2
if (s1currentMillis - s1previousMillis >= s1p5intervals[s1currentState]) {
s1previousMillis = s1currentMillis; // Save the time of the change
// Move to the next state (loops back to 0 after 1)
s1currentState = (s1currentState + 1) % 2;
// Apply the new LED state
digitalWrite(stro1led, s1p5ledStateValues[s1currentState]);
}
}
void s1pattern6() {
digitalWrite(stro1led, LOW);
}
// strobe 2 patterns
//--------------------------------------------------------------------------------------------------------------
void s2pattern1() {
static int s2currentState = 0; // Keeps state between calls
unsigned long s2currentMillis = millis();
static unsigned long s2previousMillis = 0; // Keeps time between calls
const unsigned long s2p1intervals[4] = {15, 100, 15, 1270};
const int s2p1ledStateValues[4] = {HIGH, LOW, HIGH, LOW };
if (s2currentMillis - s2previousMillis >= s2p1intervals[s2currentState]) {
s2previousMillis = s2currentMillis; // Save the time of the change
// Move to the next state (loops back to 0 after 3)
s2currentState = (s2currentState + 1) % 4;
// Apply the new LED state
digitalWrite(stro2led, s2p1ledStateValues[s2currentState]);
}
}
void s2pattern2() {
static int s2currentState = 0; // Keeps state between calls
unsigned long s2currentMillis = millis();
static unsigned long s2previousMillis = 0; // Keeps time between calls
const unsigned long s2p2intervals[5] = {750, 15, 100, 15, 620};
const int s2p2ledStateValues[5] = {LOW, HIGH, LOW, HIGH, LOW};;
if (s2currentMillis - s2previousMillis >= s2p2intervals[s2currentState]) {
s2previousMillis = s2currentMillis; // Save the time of the change
// Move to the next state (loops back to 0 after 1)
s2currentState = (s2currentState + 1) % 5;
// Apply the new LED state
digitalWrite(stro2led, s2p2ledStateValues[s2currentState]);
}
}
void s2pattern3() {
static int s2currentState = 0; // Keeps state between calls
unsigned long s2currentMillis = millis();
static unsigned long s2previousMillis = 0; // Keeps time between calls
const unsigned long s2p3intervals[3] = {750, 15, 735};
const int s2p3ledStateValues[3] = {LOW, HIGH, LOW};
if (s2currentMillis - s2previousMillis >= s2p3intervals[s2currentState]) {
s2previousMillis = s2currentMillis; // Save the time of the change
// Move to the next state (loops back to 0 after 1)
s2currentState = (s2currentState + 1) % 3;
// Apply the new LED state
digitalWrite(stro2led, s2p3ledStateValues[s2currentState]);
}
}
void s2pattern4() {
static int s2currentState = 0; // Keeps state between calls
unsigned long s2currentMillis = millis();
static unsigned long s2previousMillis = 0; // Keeps time between calls
const unsigned long s2p4intervals[3] = {500, 15, 485};
const int s2p4ledStateValues[3] = {LOW, HIGH, LOW};
if (s2currentMillis - s2previousMillis >= s2p4intervals[s2currentState]) {
s2previousMillis = s2currentMillis; // Save the time of the change
// Move to the next state (loops back to 0 after 1)
s2currentState = (s2currentState + 1) % 3;
// Apply the new LED state
digitalWrite(stro2led, s2p4ledStateValues[s2currentState]);
}
}
void s2pattern5() {
static int s2currentState = 0; // Keeps state between calls
unsigned long s2currentMillis = millis();
static unsigned long s2previousMillis = 0; // Keeps time between calls
const unsigned long s2p5intervals[3] = {375, 15, 360};
const int s2p5ledStateValues[3] = {LOW, HIGH, LOW};
if (s2currentMillis - s2previousMillis >= s2p5intervals[s2currentState]) {
s2previousMillis = s2currentMillis; // Save the time of the change
// Move to the next state (loops back to 0 after 1)
s2currentState = (s2currentState + 1) % 3;
// Apply the new LED state
digitalWrite(stro2led, s2p5ledStateValues[s2currentState]);
}
}
void s2pattern6() {
static int s2currentState = 0; // Keeps state between calls
unsigned long s2currentMillis = millis();
static unsigned long s2previousMillis = 0; // Keeps time between calls
const unsigned long s2p6intervals[3] = {300, 15, 285};
const int s2p6ledStateValues[3] = {LOW, HIGH, LOW};
if (s2currentMillis - s2previousMillis >= s2p6intervals[s2currentState]) {
s2previousMillis = s2currentMillis; // Save the time of the change
// Move to the next state (loops back to 0 after 1)
s2currentState = (s2currentState + 1) % 3;
// Apply the new LED state
digitalWrite(stro2led, s2p6ledStateValues[s2currentState]);
}
}
// Function Pointers for the 2 Sets
void (*set1_functions[6])() = {s1pattern1, s1pattern2, s1pattern3, s1pattern4, s1pattern5, s1pattern6};
void (*set2_functions[6])() = {s2pattern1, s2pattern2, s2pattern3, s2pattern4, s2pattern5, s2pattern6};
void setup() {
pinMode(stro1led, OUTPUT);
pinMode(stro2led, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP); // Assumes button connects pin to GND
digitalWrite(stro1led, LOW);
digitalWrite(stro2led, LOW);
Serial.begin(9600);
}
void loop() {
int buttonState = digitalRead(BUTTON_PIN);
unsigned long currentMillis = millis();
// --- BUTTON PRESS LOGIC ---
if (buttonState == LOW) { // Button is being pressed
if (!buttonWasPressed) {
buttonPressStartTime = currentMillis;
buttonWasPressed = true;
}
} else { // Button is released
if (buttonWasPressed) {
unsigned long pressDuration = currentMillis - buttonPressStartTime;
buttonWasPressed = false;
if (currentState != IDLE) {
// Immediate Stop if already running a sequence
stopAndSave();
} else {
// Start sequences from IDLE state
if (pressDuration >= HOLD_S2_TIME) {
startSequence(RUNNING_S2, EEPROM_ADDR_SP2);
} else if (pressDuration >= HOLD_S1_TIME) {
startSequence(RUNNING_S1, EEPROM_ADDR_SP1);
}
}
}
}
// --- SEQUENCE EXECUTION STATE MACHINE ---
if (currentState != IDLE) {
unsigned long elapsedTime = currentMillis - stateStartTime;
switch (currentState) {
case RUNNING_S1:
digitalWrite(stro1led, HIGH);
set1_functions[currentFunctionIndex](); // Continuously fire current function
if (elapsedTime >= RUN_DURATION) {
goToPauseState(PAUSING_S1);
}
break;
case PAUSING_S1:
digitalWrite(stro1led, HIGH); // Pin stays HIGH during pause
if (elapsedTime >= PAUSE_DURATION) {
goToNextFunction(RUNNING_S1, EEPROM_ADDR_SP1);
}
break;
case RUNNING_S2:
digitalWrite(stro2led, HIGH);
set2_functions[currentFunctionIndex]();
if (elapsedTime >= RUN_DURATION) {
goToPauseState(PAUSING_S2);
}
break;
case PAUSING_S2:
digitalWrite(stro2led, HIGH); // Pin stays HIGH during pause
if (elapsedTime >= PAUSE_DURATION) {
goToNextFunction(RUNNING_S2, EEPROM_ADDR_SP2);
}
break;
default:
break;
}
}
}
// --- STATE HELPER FUNCTIONS ---
void startSequence(SystemState targetState, int eepromAddr) {
currentState = targetState;
// Read saved progress from EEPROM
int savedIndex = EEPROM.read(eepromAddr);
// If memory is empty (255) or out of bounds, start from 0
if (savedIndex >= 6 || savedIndex < 0) {
currentFunctionIndex = 0;
} else {
currentFunctionIndex = savedIndex;
}
Serial.print("Starting sequence. Resuming at function index: ");
Serial.println(currentFunctionIndex);
stateStartTime = millis();
}
void goToPauseState(SystemState pauseState) {
currentState = pauseState;
stateStartTime = millis();
}
void goToNextFunction(SystemState runState, int eepromAddr) {
currentFunctionIndex++;
if (currentFunctionIndex >= 6) {
// Finished all 6 functions cleanly: reset index in memory to 0
EEPROM.update(eepromAddr, 0);
Serial.println("Sequence complete. Resetting EEPROM memory to 0.");
resetSystem();
} else {
currentState = runState;
stateStartTime = millis();
}
}
void stopAndSave() {
if (currentState == RUNNING_S1 || currentState == PAUSING_S1) {
EEPROM.update(EEPROM_ADDR_SP1, currentFunctionIndex);
Serial.print("Stopped S1. Saved progress: ");
Serial.println(currentFunctionIndex);
} else if (currentState == RUNNING_S2 || currentState == PAUSING_S2) {
EEPROM.update(EEPROM_ADDR_SP2, currentFunctionIndex);
Serial.print("Stopped S2. Saved progress: ");
Serial.println(currentFunctionIndex);
}
resetSystem();
}
void resetSystem() {
currentState = IDLE;
digitalWrite(stro1led, LOW);
digitalWrite(stro2led, LOW);
}