Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 KB | None | 0 0
  1. int rotPin = 10;
  2. int gruenPin = 9;
  3. int blauPin = 11;
  4. int r = 0; // Variable Rot für die Farbmischung
  5. int g = 0; // Variable Grün für die Farbmischung
  6. int b = 0; // Variable Blau für die Farbmischung
  7. int fadeZeit = 8; // Ein höherer Wert verlangsamt den Farbwechsel
  8. boolean ersterDurchlauf = true; // Wird benötigt um den ersten Durchlauf des Programms zu erkennen
  9. // Used for generating interrupts using CLK signal
  10. const int PinA = 3;
  11. // Used for reading DT signal
  12. const int PinB = 2;
  13. const int PinSW = 8;
  14. // Keep track of last rotary value
  15. int lastCount = 0;
  16. // Updated by the ISR (Interrupt Service Routine)
  17. volatile int virtualPosition = 0;
  18. // ------------------------------------------------------------------
  19. // INTERRUPT
  20. // ------------------------------------------------------------------
  21. void isr () {
  22. static unsigned long lastInterruptTime = 0;
  23. unsigned long interruptTime = millis();
  24. // If interrupts come faster than 5ms, assume it's a bounce and ignore
  25. if (interruptTime - lastInterruptTime > 5) {
  26. if (digitalRead(PinB) == LOW)
  27. {
  28. if (virtualPosition < 1) {
  29. virtualPosition == 7;
  30. }
  31. else {
  32. virtualPosition-- ;
  33. }
  34. }
  35. else {
  36. if (virtualPosition > 6) {
  37. virtualPosition == 1;
  38. }
  39. else {
  40. virtualPosition++ ;
  41. }
  42. }
  43. // Keep track of when we were here last (no more than every 5ms)
  44. lastInterruptTime = interruptTime;
  45. }
  46. }
  47. // ------------------------------------------------------------------
  48. // SETUP SETUP SETUP SETUP SETUP SETUP SETUP
  49. // ------------------------------------------------------------------
  50. void setup() {
  51.  
  52. pinMode(rotPin, OUTPUT);
  53. pinMode(gruenPin, OUTPUT);
  54. pinMode(blauPin, OUTPUT);
  55. // Just whilst we debug, view output on serial monitor
  56. Serial.begin(9600);
  57. // Rotary pulses are INPUTs
  58. pinMode(PinA, INPUT);
  59. pinMode(PinB, INPUT);
  60. // Switch is floating so use the in-built PULLUP so we don't need a resistor
  61. pinMode(PinSW, INPUT_PULLUP);
  62. // Attach the routine to service the interrupts
  63. attachInterrupt(digitalPinToInterrupt(PinA), isr, LOW);
  64. // Ready to go!
  65. Serial.println("Start");
  66. }
  67. // ------------------------------------------------------------------
  68. // MAIN LOOP
  69. // ------------------------------------------------------------------
  70. void loop() {
  71.  
  72. if ((!digitalRead(PinSW))) {
  73. virtualPosition = 0;
  74. while (!digitalRead(PinSW))
  75. delay(10);
  76. Serial.println("Reset");
  77. }
  78. // If the current rotary switch position has changed then update everything
  79. if (virtualPosition != lastCount) {
  80. // Write out to serial monitor the value and direction
  81. Serial.print(virtualPosition > lastCount ? "Up :" : "Down:");
  82. Serial.println(virtualPosition);
  83. // Keep track of this new value
  84. lastCount = virtualPosition ;
  85. }
  86. delay(100);
  87.  
  88.  
  89. switch (virtualPosition) {
  90. case 0:
  91. setColor(0, 0, 0);
  92. break;
  93. case 1:
  94. setColor(000, 206, 209); //tuerkis
  95. break;
  96. case 2:
  97. setColor(070, 130, 180); //blau
  98. break;
  99. case 3:
  100. setColor(050, 205, 050); //gruen
  101. break;
  102. case 4:
  103. setColor(178, 034, 034); // rot
  104. break;
  105. case 5:
  106. setColor(255, 105, 180); // rosa
  107. break;
  108. case 6:
  109. setColor(153, 050, 204); // lila
  110. break;
  111. case 7:
  112. //-----------------farbwechsel------------------------------------------------------
  113. // Fragt ab, ob das der erste Durchlauf des Programms ist.
  114. // Wenn ja, wird die blaue LED hochgefahren
  115. if (ersterDurchlauf == true)
  116. {
  117. // Start mit Blau
  118. for (b = 0; b < 255; b++)
  119. {
  120. analogWrite(blauPin, b);
  121. delay(fadeZeit);
  122. }
  123. }
  124.  
  125. ersterDurchlauf = false; // Speichert, dass der erste Durchlauf volzogen ist
  126.  
  127. // Wechsel von Blau zu Lila
  128. for (r = 0; r < 255; r++)
  129. {
  130. analogWrite(rotPin, r);
  131. delay(fadeZeit);
  132. }
  133.  
  134. // Wechsel von Lila zu Rot
  135. for (b = 255; b > 0; b--)
  136. {
  137. analogWrite(blauPin, b);
  138. delay(fadeZeit);
  139. }
  140.  
  141. // Wechsel von Rot zu Gelb
  142. for (g = 0; g < 255; g++)
  143. {
  144. analogWrite(gruenPin, g);
  145. delay(fadeZeit);
  146. }
  147.  
  148. // Wechsel von Gelb zu Gruen
  149. for (r = 255; r > 0; r--)
  150. {
  151. analogWrite(rotPin, r);
  152. delay(fadeZeit);
  153. }
  154.  
  155. // Wechsel von Gruen zu Petrol
  156. for (b = 0; b < 255; b++)
  157. {
  158. analogWrite(blauPin, b);
  159. delay(fadeZeit);
  160. }
  161.  
  162. // Wechsel von Petrol zu Blau
  163. for (g = 255; g > 0; g--)
  164. {
  165. analogWrite(gruenPin, g);
  166. delay(fadeZeit);
  167. }
  168. //----------------------------------------------------
  169. break;
  170. }
  171. }
  172. //---------------einfach Farbe------------------------------
  173. void setColor(int redValue, int greenValue, int blueValue) {
  174. analogWrite(rotPin, redValue);
  175. analogWrite(gruenPin, greenValue);
  176. analogWrite(blauPin, blueValue);
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement