Light Level Bar
Part of the Displays and Sensors section of Coddy's Arduino journey. Lesson 22 of 24.
The ring can show a number as a bar: the bigger the number, the more pixels lit. Here the number is the light level.
As in the night light lesson, the light sensor's reading on A0 falls as the light rises. Turn it round first, so a bigger number means more light:
int light = 1023 - analogRead(A0);
int count = map(light, 0, 1023, 0, 16);Then clear the ring and light pixels 0 up to count minus 1:
ring.clear();
for (int i = 0; i < count; i++) {
ring.setPixelColor(i, yellow);
}
ring.show();The clear() lets the bar shrink: without it, pixels lit when it was bright stay lit when it gets dark.
Challenge
MediumThe light sensor is on A0 and the ring on pin 6. Make a light bar:
- turn the reading round so more light gives a bigger number
- map it onto 0 to 16 pixels
- clear the ring, light that many pixels from pixel 0, and show it
Press Run, change the light on the board, then Check.
Try it yourself
#include <Adafruit_NeoPixel.h>
// 16 pixels, data on pin 6
Adafruit_NeoPixel ring(16, 6, NEO_GRB + NEO_KHZ800);
uint32_t yellow = ring.Color(255, 160, 0);
void setup() {
ring.begin();
}
void loop() {
int reading = analogRead(A0); // falls as the light rises
// light more pixels the brighter it is
delay(50);
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Displays and Sensors
1Seven-Segment Displays
Seven-Segment DisplayDigit Patterns in BytesCount on the DisplayCountdown to ZeroDice RollerPractice on your own: Arduino playground