SHARE
TWEET

Coding 7 Colors on RGB with Button as Switch

jmyean Apr 9th, 2019 95 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. * Jung Min Yean
  3. * 04/09/19
  4.  * Learning how used the analog Read and Write
  5. * Calibrating your sensor
  6. * debouncing your button
  7. * Coding for 7 Colors on RGB
  8. * Using SwitchCase
  9. */
  10. const int Button = 2;
  11. const int LedPin1 = 9;
  12. const int LedPin2 = 10;
  13. const int LedPin3 = 11;
  14. const int PhotoPin = A1;
  15. const int LedPinRed = 3;
  16. const int LedPinGreen = 5;
  17. const int LedPinBlue = 6;
  18. int PhotoVal=0;
  19. unsigned long timeStart=0;
  20. int Max=0;
  21. int Min =1000;
  22. int randy;
  23. boolean currentVal;
  24. boolean lastVal;
  25.  
  26. void setup()
  27. {
  28.   pinMode(LedPin1, OUTPUT);
  29.   pinMode(LedPin2, OUTPUT);
  30.   pinMode(LedPin3, OUTPUT);
  31.   pinMode(PhotoPin, INPUT);
  32.   pinMode(LedPinRed, OUTPUT);
  33.   pinMode(LedPinGreen, OUTPUT);
  34.   pinMode(LedPinBlue, OUTPUT);
  35.   Serial.begin(9600);
  36.   digitalWrite(LedPin1, HIGH);
  37.   delay(100);
  38.   digitalWrite(LedPin1, LOW);
  39.    calibrate();
  40. }
  41. boolean debounce(boolean last)
  42. {
  43.   boolean current = digitalRead(Button);
  44.   if (last != current)
  45.   {
  46.     delay(5);
  47.     current = digitalRead(Button);
  48.     Serial.println(current);
  49.   }
  50.   return current;
  51. }
  52. void calibrate()
  53. {
  54.   Max = 0;
  55.   Min = 1000;
  56.     analogWrite(LedPin1,200);
  57.   analogWrite(LedPin2, 0);
  58.    analogWrite(LedPin3,0);
  59.    delay(200);
  60.      analogWrite(LedPin1, 0);
  61.   analogWrite(LedPin2, 0);
  62.    analogWrite(LedPin3,0);
  63.   timeStart = millis();
  64.   while (millis() -timeStart <= 5000)
  65.   {
  66.     delay(5);
  67.     PhotoVal= analogRead(PhotoPin);
  68.     Serial.println(PhotoVal);
  69.    
  70.     if(PhotoVal > Max)
  71.     {
  72.       Max = PhotoVal;
  73.     }
  74.         if(PhotoVal < Min)
  75.     {
  76.       Min = PhotoVal;
  77.     }  
  78.   }
  79.   Serial.print("The Max value is  ");
  80.   Serial.println(Max);
  81.   Serial.print("The Min value is  ");
  82.   Serial.println(Min);
  83. }
  84. void loop()
  85. {
  86.   currentVal = debounce(lastVal);
  87.   if (lastVal == LOW && currentVal == HIGH)    // Checks if the button has been pressed
  88. {
  89.   randy = random(1,8);
  90.   calibrate();
  91. }
  92.   PhotoVal= analogRead(PhotoPin);
  93.   int MapVal = map(PhotoVal,Min,Max,100,0) ;
  94.   MapVal = constrain(MapVal, 0,100);
  95.   double percent = MapVal/100.0;
  96.   Serial.println(MapVal);
  97.   analogWrite(LedPin1, MapVal);
  98.   analogWrite(LedPin2, MapVal);
  99.   analogWrite(LedPin3, MapVal);
  100.  
  101.   switch(randy)
  102.   {
  103.     case 1:
  104.       analogWrite(LedPinRed, 255*percent);
  105.       analogWrite(LedPinGreen, 255*percent);
  106.       analogWrite(LedPinBlue, 255*percent);
  107.       break;
  108.  
  109.     case 2:
  110.       analogWrite(LedPinRed,255*percent);
  111.       analogWrite(LedPinGreen, 0);
  112.       analogWrite(LedPinBlue, 0);
  113.       break;
  114.      
  115.     case 3:
  116.       analogWrite(LedPinRed,255*percent);
  117.       analogWrite(LedPinGreen, 255*percent);
  118.       analogWrite(LedPinBlue, 0);
  119.       break;
  120.  
  121.     case 4:
  122.       analogWrite(LedPinRed, 153*percent);
  123.       analogWrite(LedPinGreen, 255*percent);
  124.       analogWrite(LedPinBlue, 153*percent);
  125.       break;
  126.  
  127.     case 5:
  128.       analogWrite(LedPinRed, 102*percent);
  129.       analogWrite(LedPinGreen, 102*percent);
  130.       analogWrite(LedPinBlue, 255*percent);
  131.       break;
  132.  
  133.     case 6:
  134.       analogWrite(LedPinRed,255*percent);
  135.       analogWrite(LedPinGreen, 0);
  136.       analogWrite(LedPinBlue, 255*percent);
  137.       break;
  138.  
  139.     case 7:
  140.       analogWrite(LedPinRed,255*percent);
  141.       analogWrite(LedPinGreen, 102*percent);
  142.       analogWrite(LedPinBlue, 178*percent);
  143.       break;
  144.  
  145.     case 8:
  146.       analogWrite(LedPinRed,0);
  147.       analogWrite(LedPinGreen, 0);
  148.       analogWrite(LedPinBlue, 0);
  149.       break;
  150. }
  151.   lastVal = currentVal;
  152. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top