Arduino C — types, variables et constantes (20 exercices)
Types Arduino : int, unsigned long, float. 20 exercices.
Illustration générée par IA
Types Arduino : int, unsigned long, float. 20 exercices.
Réponse directe (octobre 2026) — Sur Arduino, déclarez le type (
int,float,bool,char[]) avant usage ;constpour les broches ;unsignedsi pas de signe. Les noms de variables sont sensibles à la casse ; un;manque bloque toute la compilation.
En robotique, tu manipules beaucoup de nombres (vitesses, distances, temps) et des états (allumé/éteint). En Arduino C, choisir le bon type rend ton programme plus clair et évite des erreurs.
int : entier (compteur, vitesse, seuil).unsigned long : temps millis() (important : millis() renvoie ce type).float : nombre à virgule (mesure “réelle”, moyenne, calcul).bool : vrai/faux (état, drapeau).char : un caractère (ex. 'A').const char* : texte (chaîne) constant, pratique pour afficher un message.Bon réflexe : si tu comptes des intrusions, c’est un entier →
int, pasfloat.
Schéma :
int global; ← visible dans setup() et loop()
void loop() {
int local; ← visible seulement dans loop()
}
const int LED_PIN = 13;
const int BAUD = 9600;
const int DELAI_MS = 100;
Bonne pratique : mets des noms qui disent pourquoi ce nombre existe.
int n = 3; float f = n; OK (3 → 3.0)float f = 3.7; int n = (int)f; tronque (3.7 → 3)Pour des textes, on affiche souvent sans stocker de String global : on garde des nombres en int, et on affiche avec Serial.print.
Exercice 1 — Déclare une constante const int BAUD = 9600; et utilise-la dans Serial.begin. Simple
const int BAUD = 9600;
void setup() { Serial.begin(BAUD); }
void loop() {}
Exercice 2 — Déclare int compteur = 0; et incrémente-le dans loop(). Simple
int compteur = 0;
void setup() {}
void loop() { compteur++; }
Exercice 3 — Déclare bool alarme = false; puis mets-la à true dans setup(). Simple
bool alarme = false;
void setup() { alarme = true; }
void loop() {}
Exercice 4 — Déclare unsigned long t0 = 0; et stocke millis() dans setup(). Simple
unsigned long t0 = 0;
void setup() { t0 = millis(); }
void loop() {}
Exercice 5 — Affiche le type “implicite” : affiche une phrase avec un int et un float (avec Serial.print). Simple
int n = 7;
float f = 3.5;
void setup() {
Serial.begin(9600);
Serial.print("n=");
Serial.print(n);
Serial.print(" f=");
Serial.println(f);
}
void loop() {}
Exercice 6 — Évite les nombres magiques : remplace delay(250) par const int DELAI_MS = 250;. Intermédiaire
const int DELAI_MS = 250;
void setup() {}
void loop() { delay(DELAI_MS); }
Exercice 7 — Portée : déclare une variable locale int x dans loop() et essaye (mentalement) de l’utiliser dans setup() : explique pourquoi ça ne marche pas (commentaire). Intermédiaire
void setup() {
// int x; ici n'existe pas si x est déclaré dans loop()
// Portée : une variable locale n'est visible que dans son bloc.
}
void loop() {
int x = 0;
x++;
}
Exercice 8 — Déclare un texte : const char* msg = \"OK\"; et affiche-le au démarrage. Intermédiaire
const char* msg = "OK";
void setup() {
Serial.begin(9600);
Serial.println(msg);
}
void loop() {}
Exercice 9 — Conversion : avec float d = 12.9; calcule int n = (int)d; et affiche n (doit afficher 12). Intermédiaire
void setup() {
Serial.begin(9600);
float d = 12.9;
int n = (int)d;
Serial.println(n); // 12
}
void loop() {}
Exercice 10 — Écris une fonction int clampInt(int v, int minV, int maxV) qui force v à rester entre min et max. Intermédiaire
int clampInt(int v, int minV, int maxV) {
if (v < minV) return minV;
if (v > maxV) return maxV;
return v;
}
Exercice 11 — Corrige un mauvais type : un compteur d’événements ne doit pas être un float. Remplace par int et affiche-le. Avancé
int intrusions = 0;
void setup() { Serial.begin(9600); }
void loop() {
intrusions++;
Serial.println(intrusions);
delay(1000);
}
Exercice 12 — Explique pourquoi millis() doit être stocké dans unsigned long (commentaire + exemple). Avancé
void setup() {
// millis() renvoie un grand nombre positif (ms depuis le démarrage)
// => unsigned long évite les débordements signés et garde la bonne plage.
unsigned long t = millis();
Serial.begin(9600);
Serial.println(t);
}
void loop() {}
Exercice 13 — Écris une fonction bool isNear(int distanceCm, int seuil) qui renvoie true si distanceCm est <= seuil. Avancé
bool isNear(int distanceCm, int seuil) {
return distanceCm <= seuil;
}
Exercice 14 — Constantes nommées : crée const int DIST_STOP_CM = 15; et utilise-la dans un test. Avancé
const int DIST_STOP_CM = 15;
int distance_cm = 12;
void setup() {
Serial.begin(9600);
if (distance_cm <= DIST_STOP_CM) Serial.println("STOP");
}
void loop() {}
Exercice 15 — Texte + DRY : déclare const char* message = \"MBOT\"; et affiche-le 3 fois sans recopier “MBOT” en dur. <span class=“exo-badge exo-badge—advanced”>Avancé
const char* message = \"MBOT\";
void setup() {
Serial.begin(9600);
for (int i = 0; i < 3; i++) Serial.println(message);
}
void loop() {}
Exercice 16 — Crée deux variables : int vitesse = 80; et int vitesse_min = 30; puis calcule int v = vitesse - vitesse_min; et affiche v. <span class=“exo-badge exo-badge—advanced”>Avancé
void setup() {
Serial.begin(9600);
int vitesse = 80;
int vitesse_min = 30;
int v = vitesse - vitesse_min;
Serial.println(v);
}
void loop() {}
Exercice 17 — static : crée une fonction qui compte combien de fois elle a été appelée (retourne un int). <span class=“exo-badge exo-badge—advanced”>Avancé
int countCalls() {
static int c = 0;
c++;
return c;
}
Exercice 18 — Écris un petit “état” : bool enVeille = true; puis passe en false après 5 secondes (avec millis()). <span class=“exo-badge exo-badge—advanced”>Avancé
bool enVeille = true; unsigned long t0 = 0;void setup() { Serial.begin(9600); t0 = millis(); }
void loop() { if (enVeille && millis() - t0 >= 5000) { enVeille = false; Serial.println(“GO”); } }
Exercice 19 — Écris une fonction float toM(float cm) qui convertit des cm en mètres. <span class=“exo-badge exo-badge—advanced”>Avancé
float toM(float cm) {
return cm / 100.0;
}
Exercice 20 — Mini-projet : fais une variable int distance_cm = 42; et affiche Distance: 42 cm (formatage simple). <span class=“exo-badge exo-badge—advanced”>Avancé
void setup() {
Serial.begin(9600);
int distance_cm = 42;
Serial.print(\"Distance: \");
Serial.print(distance_cm);
Serial.println(\" cm\");
}
void loop() {}
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.