Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int sound = 8;
- const int photoPin = A0;
- int readVal;
- int minSense = 1000;
- int maxSense = 0;
- unsigned long myTime;
- void setup() {
- // put your setup code here, to run once:
- pinMode (photoPin, INPUT);
- pinMode (sound, OUTPUT);
- Serial.begin (9600);
- calibrate ();
- }
- void calibrate ()
- {
- myTime = millis ();
- while (millis() - myTime < 5000)
- {
- readVal = analogRead (photoPin);
- if (readVal > maxSense)
- {
- maxSense = readVal;
- }
- if (readVal < minSense)
- {
- minSense = readVal;
- }
- }
- Serial.print (" max Value ");
- Serial.print (maxSense);
- Serial.print ("min Value ");
- Serial.print (minSense);
- }
- void loop()
- {
- readVal = analogRead(photoPin);
- readVal = map(readVal, minSense, maxSense, 40, 4000);
- readVal = constrain(readVal, 4000, 40);
- tone (sound, readVal, 20);
- delay (10);
- noTone (sound);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement