CrcLib Usage Warnings
This page presents must-know information about the use of CrcLib functions.
Rules to follow
Make sure to respect the following rules to ensure the proper execution of a code containing CrcLib functions:
Do not use the Arduino native delay() function.
Do not use sensors outputting only a variable-width pulse, also called TTL pulse, like the Parallax PING and its knockoffs like the HC-SR04.
Use while(), do…while() and for() functions with caution. If the program execution is intentionally kept for a long time inside a loop, make sure to include a
CrcLib::Update()
call in it so that data used by CrcLib is correctly refreshed at an appropriate rate.
Why follow these rules?
The delay() function stops the code execution for a given time, and most of the time it’s a bad thing. An Arduino code that runs CrcLib should, like the vast majority of micro-controller code, always be looping through a set of instructions at a certain rate.
For CrcLib functions to work correctly, the CrcLib::Update()
function must be called very, very often: every 0 to 10 milliseconds.
In a way, how CrcLib works forces users not to resort to bad programming practices, the most notable being blocking the program execution.
Learn how to safely reproduce the Delay() function when you don’t want to stop the continuous looping of your code. The pizza analogy described here gives a great exemple of what it’s like to use delay().
TTL pulse sensors won’t work for the same reason described above. The Arduino Sketch provided with these sensors use the Arduino native pulseIn() function, which use the basically stops the execution of the code while the sensor response is timed.
Code example of intentional infinite while() loop
One of the easiest way to launch the “running” part of a code some time after the board has been powered up is to use a button that makes the code fall into an infinite loop.
Here, the Normally Open contact of a button, that we’ve called BUTTON_LAUNCH_PROGRAM
has been physically wired to make electrical continuity between the SIG
pin of the CRC_PWM_1
port and a GND
pin. Grounding the pin will make the digital input fall to LOW
since it will read 0 Volts. As soon as the input falls to low, the code will enter the if(CrcLib::GetDigitalInput(BUTTON_LAUNCH_PROGRAM)==LOW)
condition, and immediately after fall into the while(1==1)
loop. Since 1 will always be equal to 1, the code execution will never leave the while(1==1)
loop.
The “Out of the loop” call to CrcLib::Update()
will allow data refreshing whilst the code has not yet entered the while(1==1)
loop, and the “In loop” call will take care of data refreshing when the code has been trapped inside this part of the code.
#include <CrcLib.h>
#define BUTTON_LAUNCH_PROGRAM CRC_DIG_1 //This renames CRC_DIG_1 to BUTTON_LAUNCH_PROGRAM
void setup() {
CrcLib::Initialize();
CrcLib::SetDigitalPinMode(CRC_DIG_1,INPUT);
Serial.begin(2000000);
}
void loop() {
CrcLib::Update(); //Out of the loop call
Serial.println("Out of the loop");
Serial.println(CrcLib::GetDigitalInput(BUTTON_LAUNCH_PROGRAM));
if (CrcLib::GetDigitalInput(BUTTON_LAUNCH_PROGRAM)==LOW) //The button is pressed
{
while(1==1)
{
Serial.println("Program executes this loop until board reset or power loss");
CrcLib::Update(); //In loop call
//Insert the robot code here.
}
}
}
Related articles
-
-
-
Debugging - CrcLib Error Codes (Système CrcDuino System)