#define ENCODER_CLK 2 //Pin number for Encoder_CLK
#define ENCODER_DT 3 //Pin number for Encoder_DT
#define ENCODER_SW 4 //Pin number for Encoder Button
const int buttonA = 5; //Pin number for button A
const int buttonB = 6; //Pin number for button B
const int buttonC = 7; //Pin number for button C
const int W1Pin = 31; //Pin number for Way 1
const int W2Pin = 32; //Pin number for Way 2
const int W3Pin = 33; //Pin number for Way 3
const int W4Pin = 34; //Pin number for Way 4
const int W5Pin = 35; //Pin number for Way 5
const int W6Pin = 36; //Pin number for Way 6
const int W7Pin = 37; //Pin number for Way 7
const int W8Pin = 38; //Pin number for Way 8
const int W9Pin = 39; //Pin number for Way 9
int lastButtonPushed = 0; //Default Value for lasButtonPushed container on Startup
int lastLedLit = 0; //Default Value for lastLedLit container on Startup
int currentEncoderPin = W1Pin; //Default Value for currentEncoderPin on Startup
int lastClk = HIGH; //Default Value for lastClk on Startup so encoder comparitor can work
int encoderCounter = 0; //Default Value for Encoder Counter on Startup
void setup() {
// put your setup code here, to run once:
Serial.begin(115200); //Serial Monitor Setup
pinMode(W1Pin, OUTPUT); //SocaPex W1 Pin Output
pinMode(W2Pin, OUTPUT); //SocaPex W2 Pin Output
pinMode(W3Pin, OUTPUT); //SocaPex W3 Pin Output
pinMode(W4Pin, OUTPUT); //SocaPex W4 Pin Output
pinMode(W5Pin, OUTPUT); //SocaPex W5 Pin Output
pinMode(W6Pin, OUTPUT); //SocaPex W6 Pin Output
pinMode(W7Pin, OUTPUT); //Ceeform W1 Pin Output
pinMode(W8Pin, OUTPUT); //Ceeform W2 Pin Output
pinMode(W9Pin, OUTPUT); //Ceeform W3 Pin Output
//encoder pin setup
pinMode(2, INPUT); //'Encoder_CLK' Input Pullup Resister Disabled
pinMode(3, INPUT); //'Encoder_DT' Input Pullup Resister Disabled
pinMode(4, INPUT_PULLUP); //'Encoder Push Button' Input + Pullup Resister Enabled
//pushbutton pin setup
pinMode(5, INPUT_PULLUP); //'Button A' Input Pullup Resister Enabled
pinMode(6, INPUT_PULLUP); //'Button B' Input Pullup Resister Enabled
pinMode(7, INPUT_PULLUP); //'Button C' Input Pullup Resister Enabled
}
void loop() {
// put your main code here, to run repeatedly:
if(lastButtonPushed == 0) {digitalWrite(W7Pin,HIGH); //Select Default LED on Startup
lastLedLit=W7Pin; //Update container to keep track of lit LED
}
if (digitalRead(buttonA) == LOW) {lastButtonPushed=buttonA;} // Update Container for last button pushed if A is pushed
if (digitalRead(buttonB) == LOW) {lastButtonPushed=buttonB;} // Update Container for last button pushed if B is pushed
if (digitalRead(buttonC) == LOW) {lastButtonPushed=buttonC;} // Update Container for last button pushed if C is pushed
if (digitalRead (ENCODER_SW) == LOW) {lastButtonPushed=ENCODER_SW;} // Update Container for last button pushed if Encoder button is pushed
if (lastButtonPushed == ENCODER_SW) // Encoder State Machine
{
digitalWrite (lastLedLit, LOW); // Switch off last lit LED whilst in this state (Updates automatically)
digitalWrite(currentEncoderPin,HIGH); // Light up Current Encoder LED whilst in this state (Updates automatically)
lastLedLit = currentEncoderPin; // Update container to keep track of lit LED once new one switches on (Updates automatically)
// Encoder Wheel Logic________________________________________________________________________________________________________________
int newClk = digitalRead(ENCODER_CLK); //Read Encoder_CLK pin and store value in container
if (newClk != lastClk) // Compare new value to previous or default value and if it is different then...
{
// There was a change on the CLK pin
lastClk = newClk; // Update lastClk to be same as newClk
int dtValue = digitalRead(ENCODER_DT); // Read Encoder_DT pin and store value in container
if (newClk == LOW && dtValue == HIGH) // If both pins indicate clockwise movement then...
{
Serial.println("Rotated clockwise ⏩"); //Serial Monitor Breakpoint to determine clockwise motion
if (encoderCounter<18){encoderCounter++;} //Increase encoder counter by one increment to max 18.
Serial.print(encoderCounter); //Serial Monitor Breakpoint to check encoder counter total
if (encoderCounter < 3) {currentEncoderPin=W1Pin; } //Set conditions for currentEncoderPin = W1 (1/6 of total)
if (encoderCounter >3 && encoderCounter < 6) {currentEncoderPin=W2Pin;} //Set conditions for currentEncoderPin = W2 (2/6 of total)
if (encoderCounter >6 && encoderCounter < 9) {currentEncoderPin=W3Pin;} //Set conditions for currentEncoderPin = W3 (3/6 of total)
if (encoderCounter >9 && encoderCounter < 12) {currentEncoderPin=W4Pin;} //Set conditions for currentEncoderPin = W4 (4/6 of total)
if (encoderCounter >12 && encoderCounter < 15) {currentEncoderPin=W5Pin;} //Set conditions for currentEncoderPin = W5 (5/6 of total)
if (encoderCounter > 15 && encoderCounter < 18) {currentEncoderPin=W6Pin;} //Set conditions for currentEncoderPin = W6 (6/6 of total)
}
if (newClk == LOW && dtValue == LOW) // If both pins indicate clockwise movement then...
{
Serial.println("Rotated counterclockwise ⏪"); //Serial Monitor Breakpoint to determine clockwise motion
if (encoderCounter>0){encoderCounter--;} //Decrease encoder counter by one increment to min of 0.
Serial.print(encoderCounter); //Serial Monitor Breakpoint to check encoder counter total
if (encoderCounter < 3) {currentEncoderPin=W1Pin; } //Set conditions for currentEncoderPin = W1 (1/6 of total)
if (encoderCounter > 3 && encoderCounter < 6) {currentEncoderPin=W2Pin;} //Set conditions for currentEncoderPin = W2 (2/6 of total)
if (encoderCounter >6 && encoderCounter < 9) {currentEncoderPin=W3Pin;} //Set conditions for currentEncoderPin = W3 (3/6 of total)
if (encoderCounter >9 && encoderCounter < 12) {currentEncoderPin=W4Pin;} //Set conditions for currentEncoderPin = W4 (4/6 of total)
if (encoderCounter >12 && encoderCounter < 15) {currentEncoderPin=W5Pin;} //Set conditions for currentEncoderPin = W5 (5/6 of total)
if (encoderCounter > 15 && encoderCounter < 18) {currentEncoderPin=W6Pin;} //Set conditions for currentEncoderPin = W6 (6/6 of total)
}
}
}
//______________________________________________________________________________________________________________________________________________
//State Machine for Buttons
if (lastButtonPushed==buttonA) //Enter 'Button A' Statemachine
{
currentEncoderPin=W1Pin; //Resets Encoder Pin to Default Starting Position
encoderCounter=0; //Resets Encoder Counter to Default and clamps it at 0 whilst in this state
digitalWrite(lastLedLit,LOW); //Switches Previous LED off
digitalWrite(W7Pin,HIGH); //Switches Designated LED for this state ON
(lastLedLit = W7Pin);} //Updates Container as LED switches ON
if (lastButtonPushed==buttonB) //Enter 'Button B' Statemachine
{
encoderCounter=0; //Resets Encoder Counter to Default and clamps it at 0 whilst in this state
currentEncoderPin=W1Pin; //Resets Encoder Pin to Default Starting Position
digitalWrite(lastLedLit,LOW); //Switches Previous LED off
digitalWrite(W8Pin,HIGH); //Switches Designated LED for this state ON
(lastLedLit = W8Pin);} //Updates Container as LED switches ON
if (lastButtonPushed==buttonC) //Enter 'Button C' Statemachine
{
encoderCounter=0; //Resets Encoder Counter to Default and clamps it at 0 whilst in this state
currentEncoderPin=W1Pin; //Resets Encoder Pin to Default Starting Position
digitalWrite(lastLedLit,LOW); //Switches Previous LED off
digitalWrite(W9Pin,HIGH); //Switches Designated LED for this state ON
(lastLedLit = W9Pin);}} //Updates Container as LED switches ON