#include <Stepper.h>
// for your motor
int ccwDirection = -1; // Set ccw motor direction as 1 or -1
// initialize the stepper library on pins 8 through 11:
// Use below line for the simulation
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
Stepper myStepper(stepsPerRevolution, 6, 7, 8, 9);
// Use Below line and steps with 28byj stepper motor
// const int stepsPerRevolution = 2048; // change this to fit the number of steps per revolution
// Stepper myStepper(stepsPerRevolution, 6, 8, 7, 9);
// ======================= CONFIGURATION =======================
// Pins
const int selectorPin = 2; // Selector switch (using INPUT_PULLUP)
const int limitSwitchPin = 12; // Limit switch for stepper motor zeroing
const int buttonPins[15] = {23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51}; // PB01 to PB15
// Green LEDs G1 - G12
const int greenLEDs[12] = {22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44};
// Orange LEDs O1 - O4
const int orangeLEDs[4] = {A0, A1, A2, A3};
// Timings
unsigned long flashDelay = 100; // Flash delay (ms)
unsigned long solidDuration = 5000; // Duration LEDs stay on (ms)
// LED Patterns - Use 0-11 for green led and 12-15 for orange led
const byte ledPatternLOW[15][3] = {
{4, 255, 14}, {0, 11, 12}, {7, 255, 15}, {2, 3, 14}, {2, 255, 14},
{2, 255, 12}, {1, 255, 12}, {1, 255, 12}, {8, 255, 15}, {8, 255, 15},
{9, 255, 15}, {9, 255, 12}, {10, 255, 12}, {10, 255, 12}, {11, 255, 12}
};
const byte ledPatternHIGH[15][3] = {
{5, 6, 13}, {1, 255, 14}, {6, 255, 13}, {4, 255, 14}, {3, 255, 13},
{3, 255, 14}, {2, 255, 14}, {2, 255, 14}, {6, 7, 13}, {7, 255, 13},
{8, 255, 13}, {8, 255, 15}, {9, 255, 15}, {9, 255, 15}, {10, 255, 15}
};
// State variables
int currentActiveButton = -1;
unsigned long actionStartTime = 0;
bool isActionRunning = false;
bool selectorPrevious = LOW;
// ======================= SETUP =======================
void setup() {
Serial.begin(9600);
// Configure pins
pinMode(selectorPin, INPUT_PULLUP);
pinMode(limitSwitchPin, INPUT_PULLUP);
for (int i = 0; i < 15; i++) pinMode(buttonPins[i], INPUT_PULLUP);
for (int i = 0; i < 12; i++) pinMode(greenLEDs[i], OUTPUT);
for (int i = 0; i < 4; i++) pinMode(orangeLEDs[i], OUTPUT);
// Stepper settings
// set the speed at 10 rpm:
myStepper.setSpeed(10);
// Stepper: move to limit switch to zero position
while (digitalRead(limitSwitchPin) == HIGH) {
myStepper.step(ccwDirection * 10);
}
delay(100); // wait for 100 milliseconds
Serial.println("Start Main loop");
}
// ======================= LOOP =======================
void loop() {
// Check for button press
for (int i = 0; i < 15; i++) {
if (digitalRead(buttonPins[i]) == LOW) {
if (currentActiveButton != i) {
cancelCurrentAction(); // Cancel any running LED pattern
performAction(i); // Start new one
}
}
}
// Stepper motor position handling
if (digitalRead(selectorPin) == HIGH && selectorPrevious == LOW) {
myStepper.step(- ccwDirection * stepsPerRevolution / 2); // Move stepper to 180 degree
selectorPrevious = HIGH;
Serial.println("Inside if condition");
} else if(digitalRead(selectorPin) == LOW && selectorPrevious == LOW) {
myStepper.step(ccwDirection * stepsPerRevolution / 2); // Move stepper back to 0
selectorPrevious = LOW;
Serial.println("Inside else condition");
}
// Check for timeout to turn off LEDs
if (isActionRunning && millis() - actionStartTime >= solidDuration) {
cancelCurrentAction();
}
}
// ======================= FUNCTIONS =======================
// Perform LED flash and hold pattern
void performAction(int buttonIndex) {
currentActiveButton = buttonIndex;
isActionRunning = true;
byte selectorState = digitalRead(selectorPin);
byte const* pattern = (selectorState == HIGH) ? ledPatternHIGH[buttonIndex] : ledPatternLOW[buttonIndex];
// Flash 3 times
for (int i = 0; i < 3; i++) {
setLEDs(pattern, true);
delay(flashDelay);
setLEDs(pattern, false);
delay(flashDelay);
}
// Turn on LEDs solid
setLEDs(pattern, true);
actionStartTime = millis();
}
// Turn off current LEDs
void cancelCurrentAction() {
if (currentActiveButton == -1) return;
byte selectorState = digitalRead(selectorPin);
byte const* pattern = (selectorState == HIGH) ? ledPatternHIGH[currentActiveButton] : ledPatternLOW[currentActiveButton];
setLEDs(pattern, false);
isActionRunning = false;
currentActiveButton = -1;
}
// Set LED states
void setLEDs(const byte pattern[3], bool state) {
for (int i = 0; i < 3; i++) {
if (pattern[i] == 255) continue;
if (pattern[i] <= 12) {
digitalWrite(greenLEDs[pattern[i] - 1], state);
} else {
digitalWrite(orangeLEDs[pattern[i] - 13], state);
}
}
}
12,11,10, 9, 8, 7, ,6, , 5, 4, 3, , 2, 1
O1,O2,O3,O4
PB1
PB2
PB3
PB4
PB5
PB6
PB7
PB8
PB9
PB10
PB11
PB12
PB13
PB14
PB15