Illustration : microcontrôleur relié à un capteur de lumière, un bouton, un servomoteur et un buzzer, un arbre de décision lumineux au-dessus

Illustration générée par IA

Mis à jour le Par Emmanuel SAUVAGE
Enregistrer

Arduino C : conditions (if/else) capteurs et actionneurs

if/else Arduino : seuils, capteurs et actionneurs simulés, bonnes pratiques. 20 exercices corrigés.

Les conditions (if, else if, else) décident quoi faire selon l’état du monde : distance mesurée, bouton pressé, lumière faible, etc.

En robotique, tu écris très souvent des règles du type :

SI distance <= 15 cm → STOP
SINON → AVANCER

1) Comparaisons (les bases)

  • == égal (attention : deux = en C !)
  • != différent
  • <, >, <=, >=

2) Combiner des conditions

  • && : ET
  • || : OU
  • ! : NON

Exemple :

if (distance_cm <= 15 && vitesse > 0) {
  // obstacle proche + robot en mouvement
}

3) Bonnes pratiques pour des if lisibles

  • utilise des constantes : const int DIST_STOP = 15;
  • évite les “pyramides” de if imbriqués trop profondes : découpe en fonctions (leçon plus tard)
  • mets des noms “verbe + sens” : isNear, isPressed

Exercices (20)

Pour ces exercices, si tu n’as pas de capteur sous la main, tu peux simuler une lecture :

int distance_cm = 42; // valeur test

Niveau simple

Exercice 1 — Avec int distance_cm = 12;, affiche STOP si distance_cm <= 15, sinon GO. Simple

Afficher la solution
void setup() {
  Serial.begin(9600);
  int distance_cm = 12;
  if (distance_cm <= 15) Serial.println("STOP");
  else Serial.println("GO");
}
void loop() {}

Exercice 2 — Crée const int DIST_WARN = 35; et affiche WARN si distance_cm <= DIST_WARN. Simple

Afficher la solution
const int DIST_WARN = 35;
void setup() {
  Serial.begin(9600);
  int distance_cm = 30;
  if (distance_cm <= DIST_WARN) Serial.println("WARN");
}
void loop() {}

Exercice 3 — Teste l’égalité : si mode == 1 affiche MODE 1, sinon AUTRE. Simple

Afficher la solution
void setup() {
  Serial.begin(9600);
  int mode = 1;
  if (mode == 1) Serial.println("MODE 1");
  else Serial.println("AUTRE");
}
void loop() {}

Exercice 4 — Trois zones : distance >= 70 → SILENCE, distance >= 35 → BIP LENT, sinon BIP RAPIDE. Simple

Afficher la solution
void setup() {
  Serial.begin(9600);
  int d = 50;
  if (d >= 70) Serial.println("SILENCE");
  else if (d >= 35) Serial.println("BIP LENT");
  else Serial.println("BIP RAPIDE");
}
void loop() {}

Exercice 5 — Écris bool pressed = true; puis affiche CLICK si pressed est vrai. Simple

Afficher la solution
void setup() {
  Serial.begin(9600);
  bool pressed = true;
  if (pressed) Serial.println("CLICK");
}
void loop() {}

Niveau intermédiaire

Exercice 6 — Évite != sur du bruit : considère intrusion si abs(mesure - reference) > 3. (Tu peux coder une fonction int absInt(int v).) Intermédiaire

Afficher la solution
int absInt(int v) { return v < 0 ? -v : v; }

void setup() { Serial.begin(9600); int reference = 40; int mesure = 44; if (absInt(mesure - reference) > 3) Serial.println(“INTRUSION”); else Serial.println(“RAS”); } void loop() {}

Exercice 7 — Combine ET : affiche DANGER si distance <= 15 ET vitesse > 0. Intermédiaire

Afficher la solution
void setup() {
  Serial.begin(9600);
  int distance = 12;
  int vitesse = 80;
  if (distance <= 15 && vitesse > 0) Serial.println("DANGER");
}
void loop() {}

Exercice 8 — Combine OU : affiche ALERTE si distance <= 15 OU son_detecte == true. Intermédiaire

Afficher la solution
void setup() {
  Serial.begin(9600);
  int distance = 50;
  bool son_detecte = true;
  if (distance <= 15 || son_detecte) Serial.println("ALERTE");
}
void loop() {}

Exercice 9 — Refactor : écris une fonction bool isStop(int d) qui encapsule d <= 15. Intermédiaire

Afficher la solution
const int DIST_STOP = 15;
bool isStop(int d) { return d <= DIST_STOP; }

Exercice 10 — Écris int vitesse = 80; puis si distance <= 50 mets vitesse = 30. Affiche la vitesse. Intermédiaire

Afficher la solution
void setup() {
  Serial.begin(9600);
  int distance = 40;
  int vitesse = 80;
  if (distance <= 50) vitesse = 30;
  Serial.println(vitesse);
}
void loop() {}

Niveau avancé

Exercice 11 — Écris une petite “machine à états” avec int etat = 0; : si etat==0 affiche VEILLE, si etat==1 affiche ALERTE. Avancé

Afficher la solution
void setup() {
  Serial.begin(9600);
  int etat = 0;
  if (etat == 0) Serial.println("VEILLE");
  else if (etat == 1) Serial.println("ALERTE");
  else Serial.println("INCONNU");
}
void loop() {}

Exercice 12 — Ajoute un compteur : si intrusion, intrusions++, puis affiche intrusions. Avancé

Afficher la solution
int intrusions = 0;
int absInt(int v) { return v < 0 ? -v : v; }

void setup() { Serial.begin(9600); }

void loop() { int reference = 40; int mesure = 44; if (absInt(mesure - reference) > 3) intrusions++; Serial.println(intrusions); delay(1000); }

Exercice 13 — Écris une fonction int zoneRadar(int d) qui renvoie 0/1/2 selon les zones (silence/lent/rapide). Avancé

Afficher la solution
int zoneRadar(int d) {
  if (d >= 70) return 0;
  if (d >= 35) return 1;
  return 2;
}

Exercice 14 — Utilise zoneRadar(d) pour afficher le texte correspondant. Avancé

Afficher la solution
int zoneRadar(int d) {
  if (d >= 70) return 0;
  if (d >= 35) return 1;
  return 2;
}

void setup() { Serial.begin(9600); int d = 20; int z = zoneRadar(d); if (z == 0) Serial.println(“SILENCE”); else if (z == 1) Serial.println(“BIP LENT”); else Serial.println(“BIP RAPIDE”); } void loop() {}

Exercice 15 — Évite une répétition : remplace deux tests d <= 15 par une seule fonction isStop(d). Avancé

Afficher la solution
const int DIST_STOP = 15;
bool isStop(int d) { return d <= DIST_STOP; }

Exercice 16 — Écris une condition “plage” : affiche OK si d est entre 35 et 70 inclus. Avancé

Afficher la solution
void setup() {
  Serial.begin(9600);
  int d = 50;
  if (d >= 35 && d <= 70) Serial.println("OK");
}
void loop() {}

Exercice 17 — Explique (commentaire) la différence = et ==, puis écris un test correct. Avancé

Afficher la solution
void setup() {
  Serial.begin(9600);
  int mode = 2;
  // = affecte une valeur ; == compare deux valeurs
  if (mode == 2) Serial.println("MODE 2");
}
void loop() {}

Exercice 18 — Écris un “watchdog” simple : si distance_cm vaut 0, affiche MESURE INVALIDE. Avancé

Afficher la solution
void setup() {
  Serial.begin(9600);
  int distance_cm = 0;
  if (distance_cm == 0) Serial.println("MESURE INVALIDE");
}
void loop() {}

Exercice 19 — Ajoute une “zone rouge” : si distance <= 10 affiche ROUGE, sinon si distance <= 35 affiche ORANGE, sinon VERT. Avancé

Afficher la solution
void setup() {
  Serial.begin(9600);
  int d = 22;
  if (d <= 10) Serial.println("ROUGE");
  else if (d <= 35) Serial.println("ORANGE");
  else Serial.println("VERT");
}
void loop() {}

Exercice 20 — Mini-projet : écris une règle “ralentir” : si distance <= 50 alors vitesse = 40, sinon vitesse = 80. Affiche vitesse. Avancé

Afficher la solution
void setup() {
  Serial.begin(9600);
  int distance = 60;
  int vitesse = 0;
  if (distance <= 50) vitesse = 40;
  else vitesse = 80;
  Serial.println(vitesse);
}
void loop() {}

Cette page vous a-t-elle été utile ?

Une question sur cet article ou besoin d'un conseil ? Posez-la ici

Vous hésitez entre deux modèles ou vous avez besoin d'aide sur un montage ? Écrivez-nous, nous vous répondons rapidement par e-mail.