BulldogLowell:
GoForSmoke:
Okay. Have you learned how to do multiple things at once through this or would like the rest?
You've not far to go unless you're already there, at least to this basic first level.
The code you have acts whenever Serial delivers a character. the rest of the time loop() goes around and other pieces of code triggered for other reasons (than Serial available) has a chance to run.
My standard to include is to have the pin 13 led blink as a status light. If the blink rate is 'off' then I know something in my code is hanging up. I can also change the blink rate (generally, to faster) as a means of error notification.
This real time coding will let you run sensors, motors and user I/O without weaving the code for any one part into the same block as another part. Instead of a sensor read in an if changing the running of a motor, the sensor code sets a state that the motor code reads and acts upon. Then if you change one part, sensor or motor, you can rip that code out cleanly and insert code for the new thing to read and pass states as the old did rather than hunting for lines and pieces of function calls all through the whole sketch and moving functionality piece by piece with extra debugging for the bits missed or not quite done right. You won't just save hours over a year, more like weeks.
Here's another of Nick's blogs. It's kind of his BlinkWithoutDelay with a full easy teaching explanation. It shows how to make things happen on time and is very clear about what is blocking code and why you don't want that.
How to do multiple things at once ... like cook bacon and eggs
Gammon Forum : Electronics : Microprocessors : How to do multiple things at once ... like cook bacon and eggs
this is very interesting to me. I am using a slave arduino pro mini to control some weather 'skits' with flashing LEDs and flashing temperatures and message indicators. I have attached the code but you can see it is the prototype of what you just described.
Let me look through Nick's blog on this and perhaps it can help me develop a more robust code. I understand quite well using the mills() function to eliminate delays, however the attached will show you why i think that is a daunting task considering what I have going on in here.
Looking forward to learning...
Look up more on state machines, or the full name is finite state machines but I like to keep it simple.
For responsive real time code, loop needs to run often with time-critical tasks up top and human-speed tasks at the bottom. People don't notice much above 30 FPS except for flashes and then persistence of vision works the opposite way. Try blinking a led 995 ms ON and 5 ms OFF, you won't see it OFF but 995 ms OFF and 5 ms ON, you will. Or measure a button touch where it take 10 ms to debounce, the human "just touched" it. Low Priority.
So inside loop() there's like separate programs to read or control devices and the data and algorithm parts, you keep them separate and if need be break them into pieces that run fast, each changes a state variable to trigger the next when it's time to run that piece, the sub-task.
Especially break a function that has a delay() or other blocking code in it and change the block into a check in the new sub-task so that something else can run of that one is not ready.
Make it run fast. If some process does take a lot of cycles then do a return (to loop()) at the end so that some other trigger doesn't hold up critical time checks. It might only be the human who can't notice being kept for less than about 50-100ms.
If some task has a loop inside of a trigger then turn that loop into a sub-task that runs until it changes the meaningfully named task state to run the sub-task that continues the process next time through loop() maybe.
Write you code as if each piece will be gotten to quickly and you will want to keep it that way.
Triggers: time, inputs, state values and flags.
Time can be millis() or micros(). Micros() is always rounded to 4. You have to count loops to get smaller.
Inputs are pins and ports. I/O makes Arduino Real World. MCU's are designed to be RWRT and so is Arduino.
Each piece of code communicates to others through values and flags rather than using intertwined code. The latter starts out simpler in its directness but once things get complicated, it becomes spaghetti aka can of worms.
BTW looking through your code, is shiftout and shiftin for SPI because I don't find a #include.
Which Arduino are you using? Not an UNO.
also these
int pwmPin = 40; //PartlyCloudy
int pwmPin2 = 41; //PartlyCloudy
int pwmPin3 = 42; //OverCast
int pwmPin4 = 43; //Overcast
int pwmPin5 = 44; //Thunderstorms
int pwmPin6 = 45; //Thunderstorms
int pwmPin7 = 46; //Sunny
int pwmPin9 = 47; //Cloudy
int pwmPin10 = 48; //Cloudy
int pwmPin11 = 49; //Rain
int pwmPin12 = 50; //Rain
int pwmPin13 = 51; //Snow
int pwmPin14 = 52; //Snow
could be this and save on RAM
const byte pwmPin0 = 39; // and the pins are pwmPin0 + pin #
All of these ... soft-SPI blocks. The only difference is the data. You only need one function, without the delay().
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, 0x00);
shiftOut(dataPin, clockPin, LSBFIRST, 0x00);
shiftOut(dataPin, clockPin, LSBFIRST, 0x01);
digitalWrite(latchPin, HIGH);
delay(100);
And you have a lot of functions that could be generalized and run on fed data.
What's the functional difference in the 2 below?
if (masterMessage[0] >= 'A' && masterMessage[0] <= 'E' || masterMessage[0] == 'F'){
if (masterMessage[0] >= 'A' && masterMessage[0] <= 'F' ){