[Débutant] Les interruptions
-
<p style="text-align:center;"><u><span style="font-size:24px;">Les interruptions c'est quoi ?</span></u></p>
<p style="text-align:center;"> </p>
<p style="text-align:center;">C'est une fonction qui est définis sur une broche. Lorsque la broche change d'état (paramètrable dans le code), le code en cour est stoppé et le sous programme mis en argument vas être démarré.</p>
<p style="text-align:center;"> </p>
<p style="text-align:center;">Utilité: Je devais faire un chrono (pour la course en cours, si des gens connaissent).</p>
<p style="text-align:center;"> </p>
<p style="text-align:center;">Seulement, il fallait ce dire: la voiture peut partir à tout moment.</p>
<p style="text-align:center;">Alors au début j'avais dans l'idée de mettre une condition dans le bloc while(), mais très mauvaise idée! Surtout que la course est au ms près!</p>
<p style="text-align:center;"> </p>
<p style="text-align:center;">Alors j'ai découvert les interruptions
.</p>
<p style="text-align:center;"> </p>
<p style="text-align:center;"><u><span style="font-size:24px;">Mise en place:</span></u></p>
<p style="text-align:center;"> </p>
<p style="text-align:center;">La fonction de arduino est: attachInterrupt( pin, ISR, mode);</p>
<p style="text-align:center;"> </p>
<p style="text-align:center;">pin -> entre 0 et 5, elle correspond à des broches définis suivant le type de carte:</p>
<p style="text-align:center;"> </p>
<p style="text-align:center;"> </p>
<p style="text-align:center;"><img alt="i4PPgwH.png" src="https://i.imgur.com/i4PPgwH.png"/></p>
<p style="text-align:center;"> </p>
<p style="text-align:center;">ISR -> C'est le sous programme à executé, il dois être sans argument.</p>
<p style="text-align:center;"> </p>
<p style="text-align:center;">Mode -> Mode de détection: LOW, CHANGE, RISING, FALLING, HIGH.</p>
<p style="text-align:center;"> </p>
<p style="text-align:center;"><u><span style="font-size:24px;">Exemple de code:</span></u></p>
<p style="text-align:center;"> </p>
<p style="text-align:center;"><img alt="VbPWF2c.png" src="https://i.imgur.com/VbPWF2c.png"/></p>
<p style="text-align:center;"> </p>
<p style="text-align:center;">Mon code de chronométre "normal":</p>
<pre class="ipsCode prettyprint">
#include <Wire.h>
#include <Adafruit_RGBLCDShield.h>Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
volatile int time_start = 0;
volatile int time_stop = 0;
volatile int time_final = 0;
volatile boolean isrunning = false;
volatile boolean isended = false;
boolean display_wait = false;
boolean display_running = false;
boolean display_ended = false;void setup() {
lcd.begin(16, 2);
attachInterrupt(0, count_start, RISING);
attachInterrupt(1, count_stop, RISING);
lcd.print("En attente...");
}void loop() {
if (isrunning == true){
if (display_running == false){
display_wait = false;
display_ended = false;
display_running = true;
lcd.clear();
lcd.print("Comptage...");
}
}else if (isended == true){
if (display_ended == false){
display_wait = false;
display_running = false;
display_ended = true;
lcd.clear();
lcd.print("Temps : " + time_final);
lcd.print(" ms");
}
}else{
if (display_wait == true){
display_running = false;
display_ended = false;
display_wait = true;
lcd.clear();
lcd.print("En attente...");
}
}
}void count_start() {
if (isrunning == false) {
display_running = false;
display_ended = false;
display_wait = false;
time_start = millis();
isended = false;
isrunning = true;
}
}void count_stop(){
if (isrunning == true){
time_stop = millis();
time_final = time_stop - time_start;
isended = true;
isrunning = false;
}
}</pre>
<p style="text-align:center;">Mon code de chronométre "POO"(plus légé et plus rapide):</p>
<pre class="ipsCode prettyprint">
#include <Wire.h>
#include <Adafruit_RGBLCDShield.h>enum Status{
isNull,
isWaiting,
isRunning,
isEnded
};struct Time {
int Start;
int Stop;
int Final = Stop - Start;
};Status chrono_status;
Status display_status;
volatile Time chrono_time;
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();void setup(){
lcd.begin(16, 2);
attachInterrupt(0, chrono_start, RISING);
attachInterrupt(1, chrono_stop, RISING);
display_status = isNull;
chrono_status = isWaiting;
}void loop(){
switch (chrono_status) {
case isWaiting:
if (display_status != isWaiting){
lcd.clear();
lcd.print("En attente...");
display_status = isWaiting;
}
break;
case isRunning:
if (display_status != isRunning){
lcd.clear();
lcd.print("Comptage...");
display_status = isRunning;
}
break;
case isEnded:
if (display_status != isEnded){
lcd.clear();
lcd.print("Temps: ");
lcd.print(chrono_time.Final);
lcd.print(" ms");
display_status = isEnded;
}
break;
}
}void chrono_start() {
if (chrono_status != isRunning){
chrono_time.Start = millis();
chrono_status = isRunning;
}
}void chrono_stop() {
if (chrono_status == isRunning){
chrono_time.Stop = millis();
chrono_status = isEnded;
}
}</pre>
<p style="text-align:center;">Voila, j'espère que ce petit tuto vous a plus!</p>
<p style="text-align:center;"> </p>
<p style="text-align:center;">Pour plus d'informations: <a href="http://www.arduino.cc/en/Reference/AttachInterrupt" rel="external nofollow">http://www.arduino.cc/en/Reference/AttachInterrupt</a></p> -
<blockquote class="ipsQuote" data-cite="Soulalex" data-ipsquote="" data-ipsquote-contentclass="forums_Topic" data-ipsquote-contentcommentid="16314" data-ipsquote-contentid="1575" data-ipsquote-contenttype="forums" data-ipsquote-timestamp="1431280546" data-ipsquote-username="Soulalex"><div>
<div>
<p>Merci pour ce petit tutoriel
</p>
<p>Un jour il faudrait que je m'achète un Arduino ou un Raspberry pour faire quelques bidouilles ^^</p>
</div>
</div></blockquote>
<p> </p>
<p>Je te conseil le Arduino pour commencer, c'est le plus simple, je trouve.</p> -
<blockquote class="ipsQuote" data-cite="Brokeos" data-ipsquote="" data-ipsquote-contentclass="forums_Topic" data-ipsquote-contentcommentid="16315" data-ipsquote-contentid="1575" data-ipsquote-contenttype="forums" data-ipsquote-timestamp="1431282586" data-ipsquote-username="Brokeos"><div>
<div>
<p>Je te conseil le Arduino pour commencer, c'est le plus simple, je trouve.</p>
</div>
</div></blockquote>
<p>D'accord merci
Je te solliciterais surement si j'en achète une
</p>
<p></p><blockquote class="ipsQuote" data-cite="Brokeos" data-ipsquote="" data-ipsquote-contentclass="forums_Topic" data-ipsquote-contentcommentid="16361" data-ipsquote-contentid="1575" data-ipsquote-contenttype="forums" data-ipsquote-timestamp="1431528015" data-ipsquote-username="Brokeos"><div>
<div>
<p>Rajout du code du chronométre sous 2 version : Normal, et POO(plus légère et plus rapide)</p>
</div>
</div></blockquote>
<p>Merci du partage
</p> -
<blockquote class="ipsQuote" data-cite="Soulalex" data-ipsquote="" data-ipsquote-contentclass="forums_Topic" data-ipsquote-contentcommentid="16366" data-ipsquote-contentid="1575" data-ipsquote-contenttype="forums" data-ipsquote-timestamp="1431535001" data-ipsquote-username="Soulalex"><div>
<div>
<p>D'accord merci
Je te solliciterais surement si j'en achète une
</p>
<p></p><p>Merci du partage
</p>
</div>
</div></blockquote>
<p> </p>
<p>Aucun soucis.</p>
<p></p><p>Si vous avez du mal à comprendre le code je peux vous l'expliquer !</p>
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login