Hey together,
first I have to thank you so much for all the input you gave me on my project.
I am currently at a point where could manage to get the following working:
-
I have a "state machine" function that checks which Mode is currently selected.
-
These modes are represented with enums to get the code clear and readable
-
my main function currently checks if specific buttons have been pressed and then accordingly sets the corresponding enum.
-
depending on this enum value i am going to create subfunctions that will do other stuff then.
-
for "debugging" purposes i did a lot Serial.print commands which will be deleted in the final version once I have everything put into place.
I posted this video a few weeks ago, that is what should be controlled in the end: https://youtu.be/2DEnrEDu_PY
#######
my question is what do you think about the code.
- Is it organized or bs that needs serious rework
- What do you think about the function i created are they okay or should the be combined or broken down into smaller ones?
another question I want to ask you is on how to put separate code snippets like the print functions e.g.
print_buttonStates()
into separate code files.
I tried on a previous version using different tabs in the arduino IDE but i came to a point where the compiler got confused with linking the functions.
To condense everything: What do you think how I have written my code and what suggestions do you have to better organize it clean it up.
The code is currently working is it should to this point:
#include <Arduino.h>
#include <string.h>
#include <Button.h>
//Function Execution Timer
#include "NextExcecutionTime.h"
//DM13A classes
#include "DM13A_LedSegment.h"
#include "DM13A.h"
//FastLED
#include <FastLED.h>
/* std:array C++11 not allowed in arduino?
std::array<int, 5> scores ={0,1,2,3,4};
*/
// Define pin numbers as constants
const int PIN_WAND_TIP = 10;
const int PIN_START_UP1 = 8;
const int PIN_START_UP2 = 7;
const int PIN_INTENSIFY = 4;
const int PIN_BAR_GRAPH = 3;
const int PIN_VENT = 2;
// Button and Switches
Button SW_WandTip(PIN_WAND_TIP);
Button SW_StartUp1(PIN_START_UP1);
Button SW_StartUp2(PIN_START_UP2);
Button SW_Intensify(PIN_INTENSIFY);
Button SW_BarGraph(PIN_BAR_GRAPH);
Button SW_Vent(PIN_VENT);
int ButtonStates[6]; //-> löschen und code umbauen
//Cyclotron LEDs
constexpr int NUM_LEDS_CYCLOTRON = 28;
constexpr int CYCLOTRON_LED_PIN = A1;
CRGB cyclotron_leds[NUM_LEDS_CYCLOTRON];
//DM13A
constexpr byte LATCH_PIN = 12; // ST_CP // LAT -> for LED-Shiftregisters
constexpr byte CLOCK_PIN = 11; // SH_CP // SCLK -> for LED-Shiftregisters
constexpr byte DATA_PIN = 6; // DS // SIN -> for LED-Shiftregisters
constexpr byte CHAINED_CHIPS = 2;
DM13A DM13ALeds(LATCH_PIN, CLOCK_PIN, DATA_PIN, (2 * CHAINED_CHIPS));
constexpr byte n_Led[][2] = { 2, 7 };
DM13A_LedSegment n_Segment(n_Led);
constexpr byte k_Led[][2] = { 3, 1 };
DM13A_LedSegment k_Segment(k_Led);
constexpr byte l_Led[][2] = { 3, 0 };
DM13A_LedSegment l_Segment(l_Led);
//for testing purpose only
void PROGRAM_LED_ON(DM13A_LedSegment &setSegment) {
DM13ALeds.setOneBit(setSegment, 0);
DM13ALeds.outputRefresh_direct();
}
void PROGRAM_LED_OFF(DM13A_LedSegment &clearSegment) {
DM13ALeds.clearSegment(clearSegment);
DM13ALeds.outputRefresh_direct();
}
void setup() {
Serial.begin(9600);
// Buttons
SW_WandTip.begin();
SW_StartUp1.begin();
SW_StartUp2.begin();
SW_Intensify.begin();
SW_BarGraph.begin();
SW_Vent.begin();
DM13ALeds.begin();
}
void updateButtonStates() { //-> nur Hilfe für die print function
ButtonStates[0] = SW_WandTip.read();
ButtonStates[1] = SW_StartUp1.read();
ButtonStates[2] = SW_StartUp2.read();
ButtonStates[3] = SW_Intensify.read();
ButtonStates[4] = SW_BarGraph.read();
ButtonStates[5] = SW_Vent.read();
}
void print_buttonStates() {
Serial.print(ButtonStates[0]);
Serial.print("|");
Serial.print(ButtonStates[2]);
Serial.print(ButtonStates[1]);
Serial.print("|");
Serial.print(ButtonStates[3]);
Serial.print(ButtonStates[4]);
Serial.println(ButtonStates[5]);
}
enum class PROTON_PACK_STANDARD_MODES{ //-> used for standard mode has to be implamented
MODE_INITIATED,
MODE_BOOTED,
MODE_BAR_GRAPH_ON,
MODE_CYCLOTRON_ON,
FIRERING,
OVERHEADED,
};
enum class ProtonPackMainModes { // or MainMode?
MODE_PACK_OFF,
MODE_CONFIG,
MODE_STANDARD,
MODE_QUICK_BOOT,
MODE_PACK_SHUTTING_DOWN,
MODE_PACK_SHUTTING_DOWN_FAST,
MODE_MUSIC_PLAYER,
MODE_ERROR
};
ProtonPackMainModes updateCurrentState(); //-> needs to stay here otherwise Arduino messes compilation up "explicit inclusion"
ProtonPackMainModes currentMainProgram = ProtonPackMainModes::MODE_PACK_OFF;
void printCurrentState(const ProtonPackMainModes ¤tStandardMode); // kein plan wieso aber sonst wird nicht compiliert
void printCurrentState(const ProtonPackMainModes ¤tStandardMode) { //genaueren Functions namen
String modeString;
switch (currentStandardMode) {
case ProtonPackMainModes::MODE_PACK_OFF:
modeString = "Pack off";
break;
case ProtonPackMainModes::MODE_CONFIG:
modeString = "Config Menu";
break;
case ProtonPackMainModes::MODE_STANDARD:
modeString = "Standard";
break;
case ProtonPackMainModes::MODE_QUICK_BOOT:
modeString = "quick boot";
break;
case ProtonPackMainModes::MODE_PACK_SHUTTING_DOWN:
modeString = "shutting down";
break;
case ProtonPackMainModes::MODE_PACK_SHUTTING_DOWN_FAST:
modeString = "shutting down";
break;
case ProtonPackMainModes::MODE_MUSIC_PLAYER:
modeString = "Music Player";
break;
case ProtonPackMainModes::MODE_ERROR:
modeString = "ERROR";
break;
default:
Serial.println("SWITCH ERROR");
break; // It's good practice to include a break in the default case as well
}
Serial.print("Current Mode: ");
Serial.println(modeString);
}
void checkIfModeHasChanged() { // check and if it has changed print the new state
static ProtonPackMainModes lastState = ProtonPackMainModes::MODE_PACK_OFF;
if (lastState != currentMainProgram) {
printCurrentState(currentMainProgram);
lastState = currentMainProgram;
}
}
bool enterMODE_PackOff() {
return (!SW_Vent.read());
}
bool enterMODE_Config() {
return (SW_WandTip.read() && SW_Intensify.read() && SW_StartUp1.read() && SW_Vent.read() && SW_Vent.toggled()); // -> StartUP1 und 2 prüfen und besseren namen für die variable finden
}
bool enterMODE_Standard() {
return (SW_StartUp1.read() && SW_Vent.read() && SW_Vent.toggled()); // -> toggled notwendig?
}
bool enterMODE_QuickBoot() {
return (SW_WandTip.read() && SW_StartUp1.read() && SW_Vent.read() && SW_Vent.toggled());
}
bool enterMODE_PackShuttingDown_normal() {
return (!SW_Vent.read() && SW_Vent.toggled() && SW_StartUp1.read() && !SW_BarGraph.read()); //-> wenn alles aus ist und dann Vent ausgeschaltet wird
}
bool enterMODE_PackShuttingDown_emergency() { //-> egal wie die Stellung ist nur wenn Vent ausgeschaltet wird
return (!SW_Vent.read() && SW_Vent.toggled());
}
bool enterMODE_MusicPlayer() {
return (SW_WandTip.read() && SW_Intensify.read() && SW_StartUp2.read() && SW_Vent.read() && SW_Vent.toggled());
}
//bool enterMODE_ERROR ????
ProtonPackMainModes updateCurrentState() {
static ProtonPackMainModes currentState = ProtonPackMainModes::MODE_ERROR; //
if (currentMainProgram == ProtonPackMainModes::MODE_PACK_OFF) {
if (enterMODE_PackOff()) {
currentState = ProtonPackMainModes::MODE_PACK_OFF;
Serial.println("entered MODE : PackOff");
PROGRAM_LED_OFF(l_Segment);
PROGRAM_LED_OFF(n_Segment);
} else if (enterMODE_Config()) {
currentState = ProtonPackMainModes::MODE_CONFIG;
Serial.println("entered MODE : Config Mode");
Mode_Config();
} else if (enterMODE_Standard()) {
currentState = ProtonPackMainModes::MODE_STANDARD;
Serial.println("entered MODE : Standard");
Mode_Stadard();
}
// -> add the missing Modes
else {
currentState = ProtonPackMainModes::MODE_ERROR; //-> default
Serial.println("entered MODE : ERROR");
}
}
return currentState;
}
//-> only testing needs to be
void Mode_Stadard() { //-> only testing needs to be
PROGRAM_LED_ON(l_Segment);
}
void Mode_Config() { //-> only testing needs to be
PROGRAM_LED_ON(n_Segment);
}
//###############################
void loop() {
updateButtonStates();
currentMainProgram = updateCurrentState();
checkIfModeHasChanged();
if (!SW_Vent.read()) currentMainProgram = ProtonPackMainModes::MODE_PACK_OFF; // -> vernünftig herausschreiben
}