BREADBOARD:
CODE:
The method I used to get the photoresistor to change the speed of the flashes was to make the blinkDelay variable map the light value within a specific range. When the light is mapped it can be fine-tuned to the correct range of blinkDelay values you want.
The only issue I found was that the blinks would not gradually get faster, but it would quickly change its blink delay after it went through a cycle. So in essence, when a light is flashed on the photoresistor it would finish its cycle then update to the shorter blink delay.
This can be seen in the video above.
//Constants
const int pResistor = A0; // Photoresistor at Arduino analog pin A0
//Variables
int LED1 = 8;
int LED2 = 12;
int blinkDelay;
int lightVal;
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
Serial.begin(9600);
}
// the loop function runs over and over again forever
void loop() {
lightVal = analogRead(pResistor);
blinkDelay = map(lightVal, 200, 700, 400, 100);
Serial.println(lightVal);
for(int x = 0; x < 2; x++) {
digitalWrite(LED1, HIGH); // turn the LED on (HIGH is the voltage level)
delay(blinkDelay);
digitalWrite(LED1, LOW);
delay(blinkDelay);
}
delay (blinkDelay);
for(int x = 0; x < 2; x++) {
digitalWrite(LED2, HIGH);
delay(blinkDelay); // wait for a second
digitalWrite(LED2, LOW); // turn the LED off by making the voltage LOW
delay(blinkDelay); // wait for a second
}
}
No comments:
Post a Comment