Advertisement
inagantid20

RGB turning different colors at different lightings

Jun 19th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. /*
  2. *Divya Inaganti
  3. *June 14, 2017
  4. *different RGB colors
  5. */
  6. const int photoPin = A0;
  7. const int PinRed= 9;
  8. const int PinGreen=10;
  9. const int PinBlue= 11;
  10. int photoValue= 0;
  11. int lightVal= 0;
  12. const int pinButton = 4;
  13. int newButton = 0;
  14. int lastButton = 0;
  15. unsigned long timeVal;
  16. int minimum= 1000;
  17. int maximum= -1000;
  18. void setup()
  19.  
  20. {
  21. Serial.begin (9600);
  22. pinMode(photoPin, INPUT);
  23. pinMode (PinRed, OUTPUT);
  24. pinMode (PinGreen, OUTPUT);
  25. pinMode (PinBlue, OUTPUT);
  26. pinMode(pinButton, INPUT);
  27.  
  28.  
  29. analogWrite(PinRed, 255);
  30. analogWrite(PinGreen, 0);
  31. analogWrite(PinBlue,0);
  32. delay(200);
  33. analogWrite(PinRed, 0);
  34. analogWrite(PinGreen, 255);
  35. analogWrite(PinBlue,0);
  36. delay(200);
  37. analogWrite(PinRed, 0);
  38. analogWrite(PinGreen, 0);
  39. analogWrite(PinBlue,255);
  40. delay(200);
  41. analogWrite(PinRed, 0);
  42. analogWrite(PinGreen, 0);
  43. analogWrite(PinBlue,0);
  44.  
  45. }
  46. void calibrate()
  47. {
  48. maximum = -5000;
  49. minimum = 5000;
  50. analogWrite(PinGreen, 0);
  51. analogWrite(PinRed, 0);
  52. analogWrite(PinBlue, 0);
  53. delay(200);
  54. analogWrite(PinRed, 255);
  55. delay(200);
  56. analogWrite(PinRed, 0);
  57.  
  58. timeVal = millis();
  59. while(millis()- timeVal < 5000)
  60. {
  61. photoValue = analogRead(photoPin);
  62. if(photoValue > maximum)
  63. {
  64. maximum = photoValue;
  65. }
  66. else if(photoValue < minimum)
  67. {
  68. minimum = photoValue;
  69. }
  70. }
  71. Serial.println(minimum);
  72. Serial.println(maximum);
  73. analogWrite(PinGreen, LOW);
  74. delay(200);
  75. analogWrite(PinRed, LOW);
  76. }
  77. boolean debounce(boolean last)
  78. {
  79.  
  80. boolean current = digitalRead(pinButton);
  81. if (last != current)
  82. {
  83. delay(5);
  84. current = digitalRead(pinButton);
  85. }
  86. return current;
  87. }
  88. void loop()
  89. {
  90. newButton = debounce (lastButton);
  91. if (newButton == HIGH && lastButton == LOW)
  92. {
  93. calibrate();
  94. }
  95. lastButton = newButton;
  96. photoValue= analogRead (photoPin);
  97. lightVal= map(photoValue, minimum, maximum, 255, 0);
  98. lightVal= constrain(lightVal, 0, 255);
  99. if (photoValue>minimum && photoValue<(maximum-minimum)/2.5)
  100. {
  101. analogWrite(PinBlue, lightVal);
  102. analogWrite(PinRed, lightVal);
  103. analogWrite(PinGreen, 0);
  104. }
  105. {
  106. if (photoValue>(maximum-minimum)/2.5 && photoValue<2*(maximum-minimum)/2.5 )
  107. analogWrite(PinBlue, lightVal);
  108. analogWrite(PinRed, 0);
  109. analogWrite(PinGreen, lightVal);
  110. }
  111. {
  112. if (photoValue>2*(maximum-minimum)/2.5&&photoValue<maximum-minimum)
  113. analogWrite(PinBlue, 0);
  114. analogWrite(PinRed, lightVal);
  115. analogWrite(PinGreen, 0);
  116. }
  117. // analogWrite(PinBlue, lightVal);
  118. // analogWrite(PinRed, lightVal);
  119.  
  120. // Serial.print ("Photoresistor value = ");
  121. // Serial.print(photoValue);
  122. // Serial.print ("lightVal = ");
  123. // Serial.println(lightVal);
  124. // analogWrite(PinRed, lightVal);
  125. // analogWrite(PinBlue, lightVal);
  126. // analogWrite(PinGreen,0);
  127. // delay(100);
  128.  
  129.  
  130. //
  131. //
  132. //Read (photoPin);
  133. // lightVal= map(photoValue, 0, 1020, 255, 0);
  134. // Serial.print ("Photoresistor value = ");
  135. // Serial.print(photoValue);
  136. // Serial.print ("lightVal = ");
  137. // Serial.println(lightVal);
  138. // delay(100);
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement