制作动态柱状图
用移动的柱条显示不断变化的传感器数据
制作动态柱状图 是 CoddyKit 上的免费 Arduino & IoT Academy 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Arduino & IoT Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Arduino & IoT Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
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.
常见问题解答
「制作动态柱状图」课时是免费的吗?
是的 — 「制作动态柱状图」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Arduino & IoT Academy 课程的其余内容,请升级到 CoddyKit PRO。 Arduino & IoT Academy 课程共包含 4 节课。
「制作动态柱状图」这节课中我会学到什么?
用移动的柱条显示不断变化的传感器数据 你通过在浏览器中直接运行的动手代码来练习 Arduino & IoT Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Arduino & IoT Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Arduino & IoT Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「制作动态柱状图」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Arduino & IoT Academy 课中编写并运行代码吗?
能。每节 Arduino & IoT Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。