Created
February 18, 2024 03:20
-
-
Save shahzaib-sheikh/e756e34d7dcbd944d9755fc3a2cf83e5 to your computer and use it in GitHub Desktop.
Timer on Arduino BCD implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <FiniteStateMachine.h> | |
| #include <Button.h> | |
| int input1[4] = {10, 13, 12, 11}; | |
| int input2[4] = {6, 9, 8, 7}; | |
| int input3[4] = {2, 5, 4, 3}; | |
| int input4[4] = {17, 14, 15, 16}; | |
| byte BCD[16][4] = { | |
| {0, 0, 0, 0}, | |
| {1, 0, 0, 0}, | |
| {0, 1, 0, 0}, | |
| {1, 1, 0, 0}, | |
| {0, 0, 1, 0}, | |
| {1, 0, 1, 0}, | |
| {0, 1, 1, 0}, | |
| {1, 1, 1, 0}, | |
| {0, 0, 0, 1}, | |
| {1, 0, 0, 1}}; | |
| const byte NUMBER_OF_STATES = 3; | |
| int seconds; | |
| int minutes; | |
| void resetTime() | |
| { | |
| seconds = 0; | |
| minutes = 0; | |
| } | |
| void incrementTime() | |
| { | |
| seconds++; | |
| if (seconds == 60) | |
| { | |
| minutes++; | |
| seconds = 0; | |
| } | |
| } | |
| void printMinutes() | |
| { | |
| int localMinutes = minutes; | |
| int remainder = localMinutes % 10; | |
| for (int c = 0; c < 4; c++) | |
| { | |
| digitalWrite(input2[c], BCD[remainder][c]); | |
| } | |
| localMinutes /= 10; | |
| remainder = localMinutes % 10; | |
| for (int c = 0; c < 4; c++) | |
| { | |
| digitalWrite(input1[c], BCD[remainder][c]); | |
| } | |
| Serial.print(minutes); | |
| } | |
| void printSeconds() | |
| { | |
| int localSeconds = seconds; | |
| int remainder = localSeconds % 10; | |
| for (int c = 0; c < 4; c++) | |
| { | |
| digitalWrite(input4[c], BCD[remainder][c]); | |
| } | |
| localSeconds /= 10; | |
| remainder = localSeconds % 10; | |
| for (int c = 0; c < 4; c++) | |
| { | |
| digitalWrite(input3[c], BCD[remainder][c]); | |
| } | |
| Serial.print(seconds); | |
| } | |
| void printTime() | |
| { | |
| printMinutes(); | |
| Serial.print(":"); | |
| printSeconds(); | |
| Serial.println(""); | |
| } | |
| void state1Function() | |
| { | |
| printTime(); | |
| } | |
| void ReadyStateFunction() | |
| { | |
| resetTime(); | |
| printTime(); | |
| } | |
| State ReadyState = State(ReadyStateFunction); | |
| // State State2 = State(state2Function); | |
| // State State3 = State(state3Function); | |
| FSM TimerStateMachine = FSM(ReadyState); | |
| Button buttonR = Button(19); | |
| Button buttonS = Button(18); | |
| // byte buttonPresses = 0; //counter variable, hols number of button presses | |
| int inc = 1; | |
| void setup() | |
| { | |
| Serial.begin(9600); | |
| for (int a = 0; a < 4; a++) | |
| { | |
| pinMode(input1[a], OUTPUT); | |
| pinMode(input2[a], OUTPUT); | |
| pinMode(input3[a], OUTPUT); | |
| pinMode(input4[a], OUTPUT); | |
| } | |
| } | |
| void loop() | |
| { | |
| bool isButtonRIsPressed = buttonR.isPressed(); | |
| bool isButtonSIsPressed = buttonS.isPressed(); | |
| Serial.print(isButtonRIsPressed); | |
| Serial.print(isButtonSIsPressed); | |
| // if (pressed){ | |
| // buttonPresses = ++buttonPresses % NUMBER_OF_STATES; | |
| // switch (buttonPresses){ | |
| // case 0: TimerStateMachine.transitionTo(State1); break; | |
| // case 1: TimerStateMachine.transitionTo(State2); break; | |
| // case 2: TimerStateMachine.transitionTo(State3); break; | |
| // } | |
| // } | |
| TimerStateMachine.update(); | |
| printTime(); | |
| incrementTime(); | |
| } | |
| // | |
| // void state3Function(){ | |
| // number++; | |
| // if(number>=9999){ | |
| // TimerStateMachine.transitionTo(State2); | |
| // } | |
| // printNumber(number); | |
| // delay(1000); | |
| //} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment