/*******************************************************************************
* Arduino cookbook v.2 - page 15 - chapter 1.6 - 1.10.2023 *
* Sketch responsible for analog read from LDR (light dependent resistor) *
* module and maping read volume into a digital blink period. *
*******************************************************************************/
const int ledPin = 13; // LED connected to digital pin 13
const int sensorPin = 0; // connect sensor to analog input 0
//set maping values
int minDuration = 200;
int maxDuration = 800;
void setup() {
// put your setup code here, to run once:
pinMode(ledPin, OUTPUT); // enable output on the led pin
}
void loop() {
// put your main code here, to run repeatedly:
int rate = analogRead(sensorPin); // read the analog input
Serial.println(rate);
rate = map(rate, 200,800,minDuration, maxDuration); // convert to blink rate
// refer to https://www.arduino.cc/reference/en/language/functions/math/map/
digitalWrite(ledPin, HIGH); // set the LED on
delay(rate); // wait duration dependent on light level
digitalWrite(ledPin, LOW); // set the LED off
delay(rate);
}