Goodday all,
I've been working with the arduino for quite some time now and I managed to build me an RGB LED stairway. This has worked for months, untill my mother-in-law thought the sensor was a button ::) ::) :( . Now as i'm typing this thinking the sensor is probably dirty thats why it starten tripping, but ever since it did not work quite as well anymore. So I thought a great moment to rethink my code.
The code is pretty simple, it uses an distance sensor pointing at the wall to measure the distance and when it reaches a less then a certain distance (eg. somebody's leg passes by) it turns on the TLC5940 driven led sequence.
To do this I created a couple of functions for the loop to call when the sensor is triggerd.
example of the code:
void loop(){
int LDRR = analogRead(LDR_Pin);
Serial.print("LDR: ");
Serial.println(LDRR);
CheckBsens();
if(bClimbStarted && LDRR <= 100) {
Serial.println("Firing climbing sequence.");
Climbing();
}
else {
CheckTsens();
Not so long ago I came across the term "state machine" which uses switch case to control the flow. I was thinking of typing in something in the lines of:
Case:
- Check LDR
LDR high > 2 - Check sensor
top > 3
bottom > 4
LDR low > 1 - run top/down (on)
run down/top (off)
state 1 - run down/top (on)
run top/down (off)
state 1
Now i'm wondering which is the best/preferd way to minimize use of resources and is the least error prone, working with function or with a (few) switch case(s). And of course also thinking about the future when I might want to add optional modes, (test)buttons, display, you name it (No, really name it, I love ideas).
Thanks in advance