Advertisement
coltonspastebin

Photoresistor callibrate

Dec 7th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. const int ledpin = 5;
  2. const int photopin = A0;
  3. unsigned long timeValue = 0;
  4. int minVal = 120;
  5. int maxVal = 0;
  6. int photoValue;
  7.  
  8. void setup()
  9. {
  10. pinMode (photopin, INPUT);
  11. pinMode (ledpin, OUTPUT);
  12. Serial.begin (9600);
  13. digitalWrite (ledpin, HIGH);
  14. timeValue = millis();
  15. while(millis()-timeValue<5000)
  16. {
  17. photoValue = analogRead (photopin);
  18. if (photoValue<minVal)
  19. {
  20. minVal = photoValue;
  21. }
  22. if (photoValue>maxVal)
  23. {
  24. maxVal = photoValue;
  25. }
  26. }
  27. digitalWrite (ledpin, LOW);
  28. }
  29.  
  30. void loop()
  31. {
  32. photoValue = analogRead (photopin);
  33. photoValue = constrain(photoValue, 0, 120);//makes min and max values 40 and 120
  34. photoValue = map (photoValue, 0, 120, 255, 0);//maps 255-0 along 40-120
  35. Serial.println (photoValue);
  36. analogWrite (ledpin, photoValue);
  37. // put your main code here, to run repeatedly:
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement