int numbers[][7] = {
{0, 0, 0, 0, 0, 0, 1}, // zero
{1, 0, 0, 1, 1, 1, 1}, // one
{0, 0, 1, 0, 0, 1, 0}, // two
{0, 0, 0, 0, 1, 1, 0}, // three
{1, 0, 0, 1, 1, 0, 0}, // four
{0, 1, 0, 0, 1, 0, 0}, // five
{0, 1, 0, 0, 0, 0, 0}, // six
{0, 0, 0, 1, 1, 1, 1}, // seven
{0, 0, 0, 0, 0, 0, 0}, // eight
{0, 0, 0, 0, 1, 0, 0} // nine
};
// constants
const int sensorPin = A0; // input pin of the photoresistor
int digitPins[] = {2, 3, 4}; // output pins for the digit pins of the 7 segment display
int segmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13}; // output pins for the segment pins of the 7 segment display
// Variables
int sensorValue = 0; // the value from the sensor
int percentage = 0; // percentage value to display
// function for initializing the pins
void intializePins() {
for (int i = 0; i < 7; i++) {
pinMode(segmentPins[i], OUTPUT);
}
for (int i = 0; i < 3; i++) { // Changed 4 to 3 because there are 3 digits.
pinMode(digitPins[i], OUTPUT);
}
}
// function for turning off the display
void turnOffDisplay() {
for (int i = 0; i < 7; i++) {
digitalWrite(segmentPins[i], 1);
}
}
// function for displaying a number on a desired digit
void displayNumber(int number, int digitPin) {
digitalWrite(digitPins[digitPin], 1);
for (int i = 0; i < 7; i++) {
digitalWrite(segmentPins[i], numbers[number][i]);
}
digitalWrite(digitPins[digitPin], 0);
turnOffDisplay();
}
// function for displaying to all of the digits
void displayToThreeDigit(int num) {
int hundreds = num / 100;
int tens = (num / 10) % 10;
int ones = num % 10;
displayNumber(hundreds, 0);
displayNumber(tens, 1);
displayNumber(ones, 2);
}
// function to read the sensor and calculate percentage
void readSensor() {
sensorValue = analogRead(sensorPin); // Read the value from the photoresistor
percentage = map(sensorValue, 0, 1023, 0, 100); // Convert the sensor value to percentage (0-100)
}
void setup() {
intializePins();
turnOffDisplay();
Serial.begin(9600);
}
void loop() {
readSensor(); // Read sensor value
displayToThreeDigit(percentage); // Display the percentage on the 3-digit 7-segment display
delay(10); // Small delay for stability
}