Advertisement
Guest User

Untitled

a guest
Dec 19th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. int pResistor = A0;
  2. int red = 6;
  3. int yellow = 5;
  4. int green = 3;
  5.  
  6. int intervalSec = 3; // TODO
  7.  
  8. int previousMillis;
  9. int currentMillis;
  10.  
  11.  
  12. int prevPhoResVal;
  13. int currPhoResVal;
  14. int prevPhoResTime;
  15. int currPhoResTime;
  16. double addVal;
  17.  
  18. void setup()
  19. {
  20. // put your setup code here, to run once:
  21.  
  22. Serial.begin(9600);
  23.  
  24. pinMode(red, OUTPUT);
  25. pinMode(yellow, OUTPUT);
  26. pinMode(green, OUTPUT);
  27.  
  28. pinMode(pResistor, INPUT);
  29.  
  30. prevPhoResVal = analogRead(pResistor);
  31. prevPhoResTime = millis();
  32. }
  33.  
  34. void loop()
  35. {
  36. changeLights();
  37. }
  38.  
  39. void changeLights(){
  40. previousMillis = millis();
  41. for(currentMillis = millis() ; currentMillis - previousMillis < 4000 ; currentMillis = millis())
  42. {
  43. // green on
  44. analogWrite(green, photoresistorRatio());
  45. analogWrite(yellow, LOW);
  46. analogWrite(red, LOW);
  47.  
  48. //delay(2);
  49. }
  50.  
  51. previousMillis = millis();
  52. for(currentMillis = millis() ; currentMillis - previousMillis < 1000 ; currentMillis = millis())
  53. {
  54. // yellow on
  55. analogWrite(yellow, photoresistorRatio());
  56. analogWrite(green, LOW);
  57. analogWrite(red, LOW);
  58.  
  59. //delay(2);
  60. }
  61.  
  62. previousMillis = millis();
  63. for(currentMillis = millis() ; currentMillis - previousMillis < 6000 ; currentMillis = millis())
  64. {
  65. // red on
  66. analogWrite(red, photoresistorRatio());
  67. analogWrite(green, LOW);
  68. analogWrite(yellow, LOW);
  69.  
  70. //delay(2);
  71. }
  72.  
  73. previousMillis = millis();
  74. for(currentMillis = millis() ; currentMillis - previousMillis < 1000 ; currentMillis = millis())
  75. {
  76. // red & yellow on
  77. analogWrite(yellow, photoresistorRatio());
  78. analogWrite(red, photoresistorRatio());
  79. analogWrite(green, LOW);
  80.  
  81. //delay(2);
  82. }
  83. }
  84.  
  85. // map the value from the photoresistor to a
  86. // matching value of the output.
  87. int photoresistorRatio()
  88. {
  89. currPhoResVal = analogRead(pResistor);
  90. currPhoResTime = millis();
  91.  
  92. double diffVal = abs(prevPhoResVal - currPhoResVal);
  93. double interval = double(currPhoResTime - prevPhoResTime) / 3000;
  94. addVal = diffVal * interval;
  95. double newVal = prevPhoResVal;
  96.  
  97. if(currPhoResVal > prevPhoResVal) {
  98. newVal = prevPhoResVal + addVal;
  99. }
  100. else if (currPhoResVal < prevPhoResVal) {
  101. newVal = prevPhoResVal - addVal;
  102. }
  103. else {
  104. newVal += prevPhoResVal;
  105. }
  106.  
  107. Serial.println(newVal);
  108. int mapVal = map(newVal, 0, 1023, 255/2, 255);
  109. delay(200);
  110.  
  111. prevPhoResVal = currPhoResVal;
  112. prevPhoResTime = currPhoResTime;
  113.  
  114. return mapVal;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement