Advertisement
coltonspastebin

Real button calibrate

Dec 8th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1.  
  2. const int ledpin = 12;
  3. const int photopin = A0;
  4. const int button = 5;
  5. unsigned long timeValue = 0;
  6. int minVal = 1000;
  7. int maxVal = 0;
  8. int photoValue;
  9. bool check = false;
  10.  
  11.  
  12. void setup()
  13. {
  14. pinMode (photopin, INPUT);
  15. pinMode (ledpin, OUTPUT);
  16. Serial.begin (9600);
  17. }
  18.  
  19. void calibrate()
  20. {
  21. if (digitalRead (button) == HIGH);
  22. digitalWrite (ledpin, HIGH);
  23. timeValue = millis();
  24. while(millis()-timeValue<5000)//sets the time it takes to callibrate 5 seconds
  25. {
  26. photoValue = analogRead (photopin);//This is used to callibrate the photoresistor (place your hand on and off of the photoresistor to get the resistor to see the max and min
  27. if (photoValue<minVal)
  28. {
  29. minVal = photoValue;
  30. }
  31. if (photoValue>maxVal)
  32. {
  33. maxVal = photoValue;
  34. }
  35. }
  36. digitalWrite (ledpin, LOW);
  37. Serial.println ("I am in");
  38. }
  39.  
  40. void loop()
  41. {
  42. photoValue = digitalRead (button);
  43. Serial.println (photoValue);
  44. if (check==false)
  45. {
  46. Serial.println ("check is true");
  47. if (button==1)
  48. {
  49. Serial.println ("button is read");
  50. calibrate();
  51. }
  52. else
  53. {
  54. Serial.println ("check is false");
  55. check = true;
  56. }
  57. }
  58. photoValue = analogRead (photopin);
  59. photoValue = constrain(photoValue, 0, 255);//makes min and max values 0 and 120
  60. photoValue = map (photoValue, minVal, maxVal, 255, 0);//maps 255-0 along 0-120
  61. Serial.println (photoValue);
  62. analogWrite (ledpin, photoValue);
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement