Sorry i am a beginner and amateur when it comes to coding, can somebody help me to make the code inside the loop() less "monkey-like" by using a proper function?
I have tried a lot and at some point i am running out of ideas about how to even phrase the question on google.
My main problem when writing a function for this case is that i don't know how to pass a variable into a function which has to be written out as part of a Variable but also as part of Char Strings if that makes sense.
If you look at the code inside the loop() i actually only had to pass the number to the function, like "3" to change some Variables and Char Strings accordingly. I would be able to solve the issue with those Char Strings but i am completely clueless how to handle the Variable (rmd3, rmd4...).
"3" as in: Variable: rmd3, Char String "rmd3 = 1", Variable Value: rotary_mode = 3 and Char String: "rotary_mode = 3"
This is a simplified version of my code but boils down to where i am stuck at. It refers to a rotary selector i am using which has 6 Positions (rmd1 - rmd6). However if i knew how to solve this issue i could use it in many other situations as well.
Bounce rmd3 = Bounce(45,10); //rotary selector on pos.3
Bounce rmd4 = Bounce(44,10); //rotary selector on pos.4
int rotary_mode = 0; //Rotary Mode
void setup() {
Serial.begin(11520);
pinMode(45, INPUT_PULLUP); //pin of pos.3
pinMode(44, INPUT_PULLUP); //pin of pos.4
}
void loop() {
rmd3.update();
if (rmd3.fallingEdge()) {
Serial.print("rmd3 = 1");
rotary_mode = 3;
Serial.print("rotary_mode = 3");
}
if (rmd3.risingEdge()) {
Serial.print("rmd3 = 0");
rotary_mode = 0;
Serial.print("rotary_mode = 0");
}
rmd4.update();
if (rmd4.fallingEdge()) {
Serial.print("rmd4 = 1");
rotary_mode = 4;
Serial.print("rotary_mode = 4");
}
if (rmd4.risingEdge()) {
Serial.print("rmd4 = 0");
rotary_mode = 0;
Serial.print("rotary_mode = 0");
}
}´´´
Pseudocode:
´´´void loop() {
int rmd_int; //Mode numbers as int from 1 to 6
char rmd_char = [2]; //Mode numbers as char from "1" to "6"
rmd3.update();
rmd_int = 3;
rmd_char = "3";
void selected_mode(rmd_int, rmd_char);
}
void selected_mode(int rmd_int, char rmd_char ){
if (rmd[rmd_int].fallingEdge()) {
Serial.print("rmd[rmd_char] = 1");
rotary_mode = [rmd_int];
Serial.print("rotary_mode = [rmd_char]");
}
if (rmd[rmd_int].risingEdge()) {
Serial.print("rmd[rmd_char] = 0");
rotary_mode = 0;
Serial.print("rotary_mode = 0");
}
}```
loop()
andsetup()
functions. And the description unfortunately is a mess. I understand, that it is difficult to explain, when you may have misconceptions or not understanding enough to find the correct words. Maybe you can show us directly with your code, what you are trying to do.