Advertisement
inagantid20

RGB turning red green blue then purple

Jun 15th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 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. analogWrite(PinGreen, HIGH);
  49. timeVal = millis();
  50. while(millis()- timeVal < 5000)
  51. {
  52. photoValue = analogRead(photoPin);
  53. if(photoValue > maximum)
  54. {
  55. maximum = photoValue;
  56. }
  57. else if(photoValue < minimum)
  58. {
  59. minimum = photoValue;
  60. }
  61. }
  62. Serial.println(minimum);
  63. Serial.println(maximum);
  64. analogWrite(PinGreen, LOW);
  65. }
  66. boolean debounce(boolean last)
  67. {
  68.  
  69. boolean current = digitalRead(pinButton);
  70. if (last != current)
  71. {
  72. delay(5);
  73. current = digitalRead(pinButton);
  74. }
  75. return current;
  76. }
  77. void loop()
  78. {
  79. newButton = debounce (lastButton);
  80. if (newButton == HIGH && lastButton == LOW)
  81. {
  82. calibrate();
  83. }
  84. lastButton = newButton;
  85. photoValue= analogRead (photoPin);
  86. lightVal= map(photoValue, minimum, maximum, 255, 0);
  87. lightVal= constrain(lightVal, 0, 255);
  88. analogWrite(PinBlue, lightVal);
  89. analogWrite(PinRed, lightVal);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement