Objective
The objective of this tutorial is to reproduce the native Arduino function named Delay(). This
function must not be used with the CrcDuino since it blocks the rapid execution of the program.
That being said, it might be necessary to ask the CrcDuino to perform a certain task during a
certain amount of time. This tutorial shows how to do this.
...
Objectif
L’objectif de ce tutoriel est de reproduire la fonction native à Arduino nommée delay(). Cette fonction ne peut être utilisée avec la CrcLib, puisqu’elle bloque l’exécution du programme. Ceci dit, il pourrait être intéressant de demander au CRCduino d’effectuer une action pour une durée limitée. Ce tutoriel montre comment faire cela.
Schéma électrique et présentation complète
View file |
---|
name | CrcDuino_Tuto2_PlansElectriques_20201119.pdf |
---|
|
Code
Download the Arduino IDE .ino fileTéléchargez le code de démonstration:
View file |
---|
name | Tutoriel02_DelayAlternativeAlternativeDelay_20201118.ino |
---|
|
This is a transcript of the codeQui est ici retranscrit:
Code Block |
---|
|
//**************************************************************************************************************************
// Tutoriel CrcDuino Tutorial 2 - AlternativeReproduire to the native Arduino function Delay()
// Inspired by: https://www.norwegiancreations.com/2017/09/arduino-tutorial-using-millis-instead-of-delay/
// and https://www.norwegiancreations.com/2018/10/arduino-tutorial-avoiding-the-overflow-issue-when-using-millis-and-micros/
// The code below uses the millis() native Arduino function, a command that returns the number of milliseconds since the board started running its current sketch, to measure elapsed time.
// NOTE: This version of the code does not handle correctly the case where the BTN_RUN is maintained for more than the time specified by delay1
// Not a priority, might be corrected some day. Feel free to send your own solution to that problem at info.crc@sciencetech.ca
//la fonction delay()
//**************************************************************************************************************************
#include <CrcLib.h> // Obligatoire
//CrcLib programC'est requirementpratique
//Rename IOs with meaningful name (good practice)d'avoir un nom pour chaque port de contrôle
#define SERVO_STAND_1 CRC_PWM_6
#define BTN_RUNAVANCE CRC_DIG_1
//Declare Déclaration programdes variablesconstantes (valuequi willont changeune duringvaleur executionfixe)
// *Generally, you should use "const unsigned long" forDELAIS_MS variables that hold time= 5000; // *Thedurée valuede willcontrôle quicklydu becomeservo too(en largemillisecondes, for an int to store
unsigned long time1Capture = 0; //will store last time the time was recorded
bool btnHasBeenPressed = LOW; //tells us we pressed the button or not
//Declare program constants (value can't change during execution)
const unsigned long delay1 = 5000; // time for which we send the servo command (milliseconds)donc 5s)
// Déclaration des variables (elle changeront de valeur, mais se "souviennent" de leur valeur)
Timer timer; // Pour chronometrer
bool enAttente = false; // Pour se souvenir de si nous sommes en attente
//**************************************************************************************************************************
void setup() {
// putMettez yourvotre setupcode coded'initialisation here, to run once at the beginning:ici, il s'exécutera une seule fois au début du programme
CrcLib::Initialize(); //Sets upInitialise thele CrcDuinoCRCduino
CrcLib::InitializePwmOutput(SERVO_STAND_1, 500, 2500); //Sets Configure upla thesortie motordu outputmoteur, weavec specifydes thedurées minde andpulsations maxexplicites, pulsetelles lenghtque asdictées mentionedpar by the servo manufacturerle manufacturier
CrcLib::SetDigitalPinMode(BTN_RUNAVANCE, INPUT); //Sets upConfigure thele digitalbouton portcomme as anentrée
Input
Serial.begin(96001000000);
}
//**************************************************************************************************************************
void loop() {
// putMettez yourici mainle code here, to run repeatedly de la boucle principale, qui s'exécutera à l'infini:
CrcLib::Update(); //Refreshes the CrcDuino Tâches de routine du CRCduino
if ( !(CrcLib::GetDigitalInput(BTN_RUNAVANCE) == HIGHLOW )&& !enAttente) //When the button is pressed (GND applied to the input)
{
btnHasBeenPressed = HIGH; //we wantQuand tole rememberbouton it happened, so that we go on even if released
}
if(btnHasBeenPressed == HIGH ) //If the button has been pressed
{
//we don't want to update time1Capture so that our timer loop below is monitored
CrcLib::SetPwmOutput(SERVO_STAND_1, -64); //But we want to send the servo to the desired position
}
else //If the button has NOT been pressed
{
time1Capture = millis(); //we want to update time1Capture if the button has not been pressed, so that we don't enter the loop below...
est appuyé (GND est connecté à l'entrée, donc CrcLib::GetDigitalInput() retourne LOW), et que nous ne sommes pas en attente
timer.Start(DELAIS_MS); // on démarre le compte à rebours
CrcLib::SetPwmOutput(SERVO_STAND_1, 0-64); //we reseton theallume positionle commandservo
} enAttente // This loop will be entered when more than "delay1" ms will have elapsed since the last time the "time1Capture" value has been updated
// If the button has not been pressed, we continuously update time1Capture, so we will never enter the loop...
// If the button has been pressed, we've stopped continuously updating time1Capture, so we will someday enter the loop!
if(millis() - time1Capture > delay1)
{
= true; // on se souvient que nous sommes en attente
}
if(enAttente && timer.IsFinished()) { // Quand le compte à rebours s'achève
CrcLib::SetPwmOutput(SERVO_STAND_1, 0); //we On resetréinitialise thela position commanddu servo
btnHasBeenPressedenAttente = LOWfalse; // On est prêts à recommencer }le tout
}
}
//************************************************************************************************************************** |