0Pricing
Arduino & IoT Academy · Leçon

Animer un graphique à barres

Représentez un capteur variable avec une barre animée.

Animer un graphique à barres est une leçon Arduino & IoT Academy gratuite sur CoddyKit. Ceci est la leçon 4 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Arduino & IoT Academy, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Arduino & IoT Academy comprend 4 leçons au total.

Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.

Why a Bar Graph

Numbers are precise, but a bar tells the story instantly. A growing bar shows a rising sensor value far faster than reading digits. 📊

Start From a Reading

Animation begins with fresh data. Each loop you grab a new sensor value, for example an analogRead that lands between 0 and 1023.

int value = analogRead(A0);

Map to Bar Width

The raw range rarely matches the screen. Use map to rescale 0-1023 into 0-128 pixels so the value fits your display width.

int barWidth = map(value, 0, 1023, 0, 128);

Clamp the Result

A noisy reading can overshoot. Wrap it in constrain so the bar never draws past the screen edge and corrupt the layout.

barWidth = constrain(barWidth, 0, 128);

Clear Each Frame

Animation is just redrawing fast. Every frame starts with clearDisplay so the old bar vanishes before the new length is drawn.

display.clearDisplay();

Draw the Bar

Now paint the bar with fillRect, using your mapped width. A fixed x, y, and height keep it steady while the width breathes with the data.

display.fillRect(0, 28, barWidth, 8, WHITE);

Add a Frame

Outline the full track with drawRect so users see the maximum. The fill grows inside this border, giving the bar clear context.

display.drawRect(0, 28, 128, 8, WHITE);

Label the Value

Pair the bar with the exact number. Set the cursor above it and print the reading so users get both the shape and the precise figure.

display.setCursor(0, 0);
display.print(value);

Push the Frame

Send the finished frame with display.display(). Because the whole buffer updates at once, the bar moves cleanly with no half-drawn flicker.

display.display();

Pace the Animation

Redrawing as fast as possible looks jittery. A small delay of 50 milliseconds smooths motion and steadies a twitchy sensor reading.

delay(50);

The Full Loop

Put it together: read, map, clear, draw, display, pause. That cycle in loop() is the engine behind every live OLED animation you build.

Quick Check

Your bar shows ghost trails from previous frames.

Recap

You turned readings into a live bar graph: read, map, constrain, clear, fill, frame, label, and display in a paced loop. That is real-time visualization.

Questions Fréquemment Posées

La leçon « Animer un graphique à barres » est-elle gratuite ?

Oui — le texte complet de « Animer un graphique à barres » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Arduino & IoT Academy, passe à CoddyKit PRO. Le cours Arduino & IoT Academy comprend 4 leçons au total.

Qu'est-ce que j'apprendrai dans « Animer un graphique à barres » ?

Représentez un capteur variable avec une barre animée. Tu pratiques Arduino & IoT Academy avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.

Dois-je avoir de l'expérience pour commencer Arduino & IoT Academy ?

Aucune expérience préalable n'est requise. Arduino & IoT Academy sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 4 sur 4.

Combien de temps prend la leçon « Animer un graphique à barres » ?

La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.

Peux-tu écrire et exécuter du code dans cette leçon Arduino & IoT Academy ?

Oui. Chaque leçon Arduino & IoT Academy inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.

Toutes les leçons de ce cours

  1. Câbler et initialiser un OLED SSD1306
  2. Afficher du texte en plusieurs tailles
  3. Lignes, rectangles et cercles
  4. Animer un graphique à barres
← Retour à Arduino & IoT Academy