const int ledPin = 2; // PWM-capable pin for LED
const int ldrPin = 35; // Analog input pin for LDR
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
}
void loop() {
// Read the analog value from the LDR
int ldrValue = analogRead(ldrPin);
// Map the LDR value to the range of LED brightness (0-255)
int brightness = map(ldrValue, 0, 4095, 0, 255);
// Use analogWrite to control the LED brightness
analogWrite(ledPin, brightness);
// Print LDR value and adjusted brightness to Serial Monitor
Serial.print("LDR Value: ");
Serial.print(ldrValue);
Serial.print(", Adjusted Brightness: ");
Serial.println(brightness);
delay(100); // Adjust the delay based on your requirements
}
Output:
Compilation error: 'analogWrite' was not declared in this scope
The above program that the instruction analogWrite() brings up the error
'analogWrite' was not declared in this scope
when I click on compile.
So I loaded the analoginoutserial sketch from the analog section of examples and get the same error?
Any idea what I am doing wrong?
I am using arduino IDE 2.2.1
2 posts - 2 participants