Project 5.03 Light and Sound II
This project is similar to Project 5.02 except the light intensity measured by the phototransistor is reported by changing the rate at which the piezo clicks. The effect is something like a Geiger counter.
Concepts: analogRead, map function
Circuits:
Concepts: analogRead, map function
Circuits:
Like in our past projects, we don’t want to use the delay statement to control the frequency of clicks. We want to keep measuring the phototransistor and updating the time between clicks. We use the variable lastClick to keep track of when we last sounded a click:
long lastClick = 0;
We measure the photoTran pin and map the values between 1 and 500. The higher the light level, the shorter the time between clicks. We cast clickTime to a long variable type since we want to add it to millis(), which also returns a long value:
reading = analogRead(photoTran);
clickTime = (long) map(reading,0,1023,500,1);
We found that the effect is more interesting if we create a longer delay between clicks when little light hits the phototransistor. So we set a longer delay when clickTime is greater than 450:
if(clickTime > 450) clickTime = 5000;
At the bottom of the loop() block we test to see if enough time has elapsed between clicks:
if(millis() > lastClick + clickTime){
If so, we sound a click and then reset lastClick before returning to the top of the loop() block.
piezoTone(4000,5);
lastClick = millis();
}
long lastClick = 0;
We measure the photoTran pin and map the values between 1 and 500. The higher the light level, the shorter the time between clicks. We cast clickTime to a long variable type since we want to add it to millis(), which also returns a long value:
reading = analogRead(photoTran);
clickTime = (long) map(reading,0,1023,500,1);
We found that the effect is more interesting if we create a longer delay between clicks when little light hits the phototransistor. So we set a longer delay when clickTime is greater than 450:
if(clickTime > 450) clickTime = 5000;
At the bottom of the loop() block we test to see if enough time has elapsed between clicks:
if(millis() > lastClick + clickTime){
If so, we sound a click and then reset lastClick before returning to the top of the loop() block.
piezoTone(4000,5);
lastClick = millis();
}