
to make your sketch more professional
The reason for using:
Arduino’s standard sketch is a linear(plain) program. This means that all actions are performed sequentially. When we program the controller, there are situations when one sensor gives its values constantly but other one require our immediate reaction. The sketch may have no time to read the value and lost it. To prevent this we can use pseudo multitasking with timer functions.
Where to get:
There is simple and useful timer library called Timer by Simon Monk. It placed at Arduino’s Playground Libraries Timing (the first one):
This timer library use callback functions which called after some events. We can originate calls every time period or once after needed time. This two simple functions are very useful to organize pseudo multitasking in our sketch and it use few resources of the controller.
For example:
We will use DS18b20 temperature sensor F1 parameter and one other test sensor D1 parameter.
Algorithm of DS18b20:
- Request to calculate temperature;
- Read the temperature after 750ms-1000ms.
Algorithm of D1 test parameter:
- Most time of the D1 is LOW but some time it going to HIGH. We need read it very often (ones per 10ms) to check this high value.
Implementation:
To implement it in our normal sketch we will read D1 inside the one loop with delay 10ms, and will use timers after function to read DS18b20 values. In this case we will get all D1 HIGH values and read DS18b20 according its algorithm: read one value per second.
#include "SPI.h"
#include "EthernetUdp.h"
#include "OneWire.h" // one wire library to connect ds18b20
#include "Timer.h" // timer library
#include "ksDS18B20.h" // subclass to read ds18b20 values
#include "KSduino.h" // KSduino Library
// DS18B20 address
uint8_t devAddr1[] = { 0x28, 0x16, 0x05, 0xB2, 0x03, 0x00, 0x00, 0x00 };
// Define OneWire, DS18B20 & Timer classes
Timer t;
OneWire ds(7); // OneWire on pin 7
ksDS18B20 ds18b20_1 (&ds, devAddr1);
// KSduino User definition -----------------------------------------
// This Arduino ID and Password
// Change this values to yours
unsigned int deviceID = 0000; // set your Device ID
unsigned int devicePwd = 1111; // set your Device Password
// This Arduino MAC, IP,DNS, Gateway and port
// Change this values to yours
byte mac[] = { 0x74,0x69,0x69,0x2D,0x00,0x00 };
byte ip[] = { 192, 168, 1, 222 };
//byte dns[] = { 192, 168, 1, 1 };
//byte gateway [] = { 192, 168, 1, 1 };
//byte subnet [] = { 255, 255, 255, 0 };
unsigned int port = 40000 + deviceID;
// KSduino Server definition ----------------------------------------
// Server address & port
byte serverIp[] = { 178,63,53,233 }; // Global IP address
unsigned int serverPort = 9930; // Port number
// ----------------------------------------------------------------
// Define KSduino class
KSduino ksd (deviceID, devicePwd, serverIp, serverPort);
// Define some KSduino parameters with KSduinoParameter class
KSduinoParameter d1 (&ksd, "d1", 0.5, 15*60);
KSduinoParameter f1 (&ksd, "f1", 0.5, 15*60);
//------------------------------------------------------------------
void setup()
{
ksd.begin(mac, ip, port); // start KSduino
// ksd.begin(mac, ip, dns, gateway, port);
// ksd.begin(mac, ip, dns, gateway, subnet, port);
t.after (0, ksd_loop); // start main loop
t.after (0, readDS18B20); // start ds18b20 loop
}
//------------------------------------------------------------------
void loop()
{
// Update timers functions
t.update();
// KSduino update function
ksd.update ();
}
//------------------------------------------------------------------
/*
* Read D1 value & Send Parameters to KSduino server
*/
void ksd_loop()
{
#define WAIT_FOR_ANSWER 2
// Create D1 test value
int d1_generator = random(1,1000);
byte d1_value = d1_generator < 10 ? 1 : 0; // will high ones per second
// Send values to KSduino if something changed
if (d1.check (d1_value) || ds18b20_1.getTemp () && f1.check(ds18b20_1.celsius))
{
ksd.beginPacket ();
d1.addParameter (d1_value);
f1.addParameter( ds18b20_1.celsius);
ksd.endPacket (WAIT_FOR_ANSWER);
}
// Loops delay
t.after (10, ksd_loop);
}
//------------------------------------------------------------------
/*
* Read thermometer value
*/
void readDS18B20 (void)
{
static byte cnt = 0;
ds18b20_1.readTermometer();
t.after (1000, readDS18B20);
}
To use this example you need one DS18b20 sensor:
https://ksdu.in/o/3h
This sketch is using ksDS18B20 class. You can download this example with ksDS18B20 class at:
http://ksdu.in/o/3iBest regards,
Kirill Scherba
The founder and main developer of
KSduino project.
Sea also:
How to skip repeated parameter values?