A single modii cation has been made; yield() has been added after the if
loop. When the sketch reaches this point, it releases control of loop3() and
looks to see if any other function needs CPU time. Now all the functions are
cooperative, and the sketch functions as needed.
Cooperative multitasking is an excellent way of making reliable multitasking
code, without the need for a heavy operating system. However, care must be
taken to make sure that the threads are cooperative, by adding yield() func-
tions or delay() statements.
This example will be an aquarium temperature sensor, one that will monitor the
temperature and control a lighting system and control the temperature depending
on the result. Every few seconds, the sensor will send the temperature by serial.
Aquariums can be expensive, and enthusiasts often welcome devices that
can help them monitor certain aspects of the water; temperature, acidity, water
hardness, and oxygen levels are all critical to the well-being of the i sh they
contain. A mistake can often be disastrous.
The temperature sensor is simple; as with the previous chapter, you will be
using an LM35 temperature sensor. Tropical i sh require precise temperatures,
and this application can help you achieve that. Most heating elements auto-
regulate themselves, but for exotic i sh, or for breeding conditions, you may
want to regulate the temperature; it should be warmer in the day and slightly
cooler at night. Bala sharks, also known as silver sharks, are a beautiful addition
to large aquariums—and my personal favorite. They are peaceful creatures but
are difi cult to please, requiring a temperature between 22 and 28°C. For this
application, the heater will be turned off at 26 and turned on at 24.
Also, lighting conditions are important, especially when breeding. Most lighting
turns on rather violently in the morning and turns off entirely at night, instead
of a more natural cycle of slowly brightening the light and slowly dimming.
This sketch enables you to change that. Figure 19-3 shows the lighting strategy.
Figure 19-3: Lighting control
The light regulator will use the Arduino Due's digital to analog converter.
It will be a single task; one that will wait for hours before changing the light