EasyManua.ls Logo

SunFounder 3in1 Kit - Page 193

Default Icon
351 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
SunFounder 3in1 Kit
Note:
Open the 5.14.calibration.ino file under the path of 3in1-kit\learning_project\5.14.
calibration.
Or copy this code into Arduino IDE.
After the code is uploaded successfully, the LED will light up, and we will have 5 seconds to calibrate the detection
range of the photoresistor. This is because we may be in a different light environment each time we use it (e.g. the light
intensity is different between midday and dusk).
At this time, we need to swing our hands up and down on top of the photoresistor, and the movement range of the hand
will be calibrated to the playing range of this instrument.
After 5 seconds, the LED will go out and we can wave our hands on the photoresistor to play.
How it works?
1. Set the initial values and pins of all components.
const int buzzerPin = 9;
const int ledPin = 8;
const int photocellPin = A0; //photoresistor attach to A2
int lightLow = 1023;
int lightHigh = 0;
int sensorValue = 0; // value read from the sensor
int pitch = 0; // sensor value converted into LED 'bars'
unsigned long previousMillis = 0;
const long interval = 5000;
2. Set up a calibration process in setup().
void setup()
{
pinMode(buzzerPin, OUTPUT); // make buzzer output
pinMode(ledPin, OUTPUT); // make the LED pin output
/* calibrate the photoresistor max & min values */
previousMillis = millis();
digitalWrite(ledPin, HIGH);
while (millis() - previousMillis <= interval) {
sensorValue = analogRead(photocellPin);
if (sensorValue > lightHigh) {
lightHigh = sensorValue;
}
if (sensorValue < lightLow) {
lightLow = sensorValue;
}
}
digitalWrite(ledPin, LOW);
}
The work flow is as follows.
4.5. 5. More Syntax 189

Related product manuals