// Pin setup for analog pins A22 to A29 on Arduino Mega using PORTA
#define LED_PORT PORTA // Define PORTA as LED output
#define LED_DDR DDRA // Define Data Direction Register for PORTA
const int photoresistorPin = A0; // Pin where the photoresistor is connected
const int numLeds = 8; // Number of LEDs
void setup() {
// Set all PORTA pins (A22 to A29) as output
LED_DDR = 0xFF; // Set all pins of PORTA as output (A22 to A29)
// Initialize the serial monitor for debugging
Serial.begin(9600);
}
void loop() {
// Read the value from the photoresistor (range 0 to 1023)
int lightLevel = analogRead(photoresistorPin);
// Print the light level for debugging purposes
Serial.print("Light Level: ");
Serial.println(lightLevel);
// Map the light level (0-1023) to the number of LEDs (0-8)
int ledsToLight = map(lightLevel, 0, 1023, 0, numLeds);
// Light up the LEDs based on the light level from A22 to A29
// We manipulate the bits in PORTA to turn on or off the LEDs
if (ledsToLight > 0) {
LED_PORT = (1 << (ledsToLight)) - 1; // Turn on LEDs from A22 to A29 based on light level
} else {
LED_PORT = 0x00; // Turn off all LEDs
}
// Add a small delay before reading again
delay(100);
}