I need some help combining some loops, creating a Cricket scoreboard using 74HC595N's and 7 segment displays. I have some code running that counts from 0-999 and wish to add another two digits but separate from the original 3 digits. I have declared different pins so that they operate independently but cannot work out how to make a loop in a loop.
Potentially I want to have 4 separate routines running in a loop, i.e. 3 digits for Score, 2 digits for Overs, 1 digit for Wickets, 3 digits for Other score. Eventually i want to try and increment the scores using remote xy app and either bluetooth or wireless modules. However little steps first and am just trying to make the 4 counters. I have started with some code from Pauls Electronics and amended the code to just count up and that works well.
void loop()
{
handle_abc() ;
handle_def() ;
....
handle_xyz() ;
}
void handle_abc ()
{
if (<some condition>)
handle_abc_foo () ;
if (<some other condition>)
handle_abc_bar () ;
}
...
[code]
So basically each part of the program has all its code trigger by an event handler for that part of
the program, and all loop() has to do is poll every part of the program for events to process.
Then its trivial to combine different bits of functionality - just call all the right handlers from loop()
There are some good replies here. I personally prefer to use the VariableTimedAction library for "multi-threading".
Here is an example:
// VariableTimedAction - Version: 1.2.2
#include <VariableTimedAction.h>
//define class Counter
class Counter : public VariableTimedAction {
private:
//it will keep track of a number
int count = 0;
//this method will be called every 500ms
unsigned long run() {
//increment the counter
count++;
//TODO: remove above code and add your functionality here!
return 0;
}
public:
//this method is used for getting the current count
int getCount() {
return count;
}
};
const int counter_amt = 4;
//create 4 Counter objects
Counter counters[counter_amt];
//Printer class to output counter values; remove for production code
class Printer : public VariableTimedAction {
private:
unsigned long run() {
for (int i = 0; i < counter_amt; i++) {
Serial.print("Counter ");
Serial.print(i);
Serial.print(":\t");
Serial.println(counters[i].getCount());
}
return 0;
}
} printer;
void setup() {
Serial.begin(9600);
Serial.println("STARTING...");
for (int i = 0; i < counter_amt; i++) {
//each counter is started to fire at 500ms intervals
//the argument in start defines how often the run() method is executed
counters[i].start(500);
}
//printer run() method to execute every 1 second
printer.start(1000);
}
void loop() {
//do not place anything else in loop()
VariableTimedAction::updateActions();
}
The library is located in the LibraryManager and is on Github. Full disclosure: I am its author.
If you're inclined towards a timed action system, you can consider RIOS. With great success I have implemented the Riverside-Irving preemptive kernel on a 328p. It uses the inherent capabilities of the hardware for context switching and doesn't allow a task to interrupt itself. There's also a traditional tickless option which is just another round-robin scheduler. http://www.riosscheduler.org/
Sorry for not replying sooner, I was waiting on an email to show someone had replied to me. I just checked on the off chance on my post and have lots to be reading. Thank you for your replies I will do some reading and be back.
Milhouse123:
Sorry for not replying sooner, I was waiting on an email to show someone had replied to me.
If you make a habit of checking the Forum (rather than waiting for an email) it gives you the opportunity to browse other Threads to see if you could help someone else.