Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int rotPin = 10;
- int gruenPin = 9;
- int blauPin = 11;
- int r = 0; // Variable Rot für die Farbmischung
- int g = 0; // Variable Grün für die Farbmischung
- int b = 0; // Variable Blau für die Farbmischung
- int fadeZeit = 8; // Ein höherer Wert verlangsamt den Farbwechsel
- boolean ersterDurchlauf = true; // Wird benötigt um den ersten Durchlauf des Programms zu erkennen
- // Used for generating interrupts using CLK signal
- const int PinA = 3;
- // Used for reading DT signal
- const int PinB = 2;
- const int PinSW = 8;
- // Keep track of last rotary value
- int lastCount = 0;
- // Updated by the ISR (Interrupt Service Routine)
- volatile int virtualPosition = 0;
- // ------------------------------------------------------------------
- // INTERRUPT
- // ------------------------------------------------------------------
- void isr () {
- static unsigned long lastInterruptTime = 0;
- unsigned long interruptTime = millis();
- // If interrupts come faster than 5ms, assume it's a bounce and ignore
- if (interruptTime - lastInterruptTime > 5) {
- if (digitalRead(PinB) == LOW)
- {
- if (virtualPosition < 1) {
- virtualPosition == 7;
- }
- else {
- virtualPosition-- ;
- }
- }
- else {
- if (virtualPosition > 6) {
- virtualPosition == 1;
- }
- else {
- virtualPosition++ ;
- }
- }
- // Keep track of when we were here last (no more than every 5ms)
- lastInterruptTime = interruptTime;
- }
- }
- // ------------------------------------------------------------------
- // SETUP SETUP SETUP SETUP SETUP SETUP SETUP
- // ------------------------------------------------------------------
- void setup() {
- pinMode(rotPin, OUTPUT);
- pinMode(gruenPin, OUTPUT);
- pinMode(blauPin, OUTPUT);
- // Just whilst we debug, view output on serial monitor
- Serial.begin(9600);
- // Rotary pulses are INPUTs
- pinMode(PinA, INPUT);
- pinMode(PinB, INPUT);
- // Switch is floating so use the in-built PULLUP so we don't need a resistor
- pinMode(PinSW, INPUT_PULLUP);
- // Attach the routine to service the interrupts
- attachInterrupt(digitalPinToInterrupt(PinA), isr, LOW);
- // Ready to go!
- Serial.println("Start");
- }
- // ------------------------------------------------------------------
- // MAIN LOOP
- // ------------------------------------------------------------------
- void loop() {
- if ((!digitalRead(PinSW))) {
- virtualPosition = 0;
- while (!digitalRead(PinSW))
- delay(10);
- Serial.println("Reset");
- }
- // If the current rotary switch position has changed then update everything
- if (virtualPosition != lastCount) {
- // Write out to serial monitor the value and direction
- Serial.print(virtualPosition > lastCount ? "Up :" : "Down:");
- Serial.println(virtualPosition);
- // Keep track of this new value
- lastCount = virtualPosition ;
- }
- delay(100);
- switch (virtualPosition) {
- case 0:
- setColor(0, 0, 0);
- break;
- case 1:
- setColor(000, 206, 209); //tuerkis
- break;
- case 2:
- setColor(070, 130, 180); //blau
- break;
- case 3:
- setColor(050, 205, 050); //gruen
- break;
- case 4:
- setColor(178, 034, 034); // rot
- break;
- case 5:
- setColor(255, 105, 180); // rosa
- break;
- case 6:
- setColor(153, 050, 204); // lila
- break;
- case 7:
- //-----------------farbwechsel------------------------------------------------------
- // Fragt ab, ob das der erste Durchlauf des Programms ist.
- // Wenn ja, wird die blaue LED hochgefahren
- if (ersterDurchlauf == true)
- {
- // Start mit Blau
- for (b = 0; b < 255; b++)
- {
- analogWrite(blauPin, b);
- delay(fadeZeit);
- }
- }
- ersterDurchlauf = false; // Speichert, dass der erste Durchlauf volzogen ist
- // Wechsel von Blau zu Lila
- for (r = 0; r < 255; r++)
- {
- analogWrite(rotPin, r);
- delay(fadeZeit);
- }
- // Wechsel von Lila zu Rot
- for (b = 255; b > 0; b--)
- {
- analogWrite(blauPin, b);
- delay(fadeZeit);
- }
- // Wechsel von Rot zu Gelb
- for (g = 0; g < 255; g++)
- {
- analogWrite(gruenPin, g);
- delay(fadeZeit);
- }
- // Wechsel von Gelb zu Gruen
- for (r = 255; r > 0; r--)
- {
- analogWrite(rotPin, r);
- delay(fadeZeit);
- }
- // Wechsel von Gruen zu Petrol
- for (b = 0; b < 255; b++)
- {
- analogWrite(blauPin, b);
- delay(fadeZeit);
- }
- // Wechsel von Petrol zu Blau
- for (g = 255; g > 0; g--)
- {
- analogWrite(gruenPin, g);
- delay(fadeZeit);
- }
- //----------------------------------------------------
- break;
- }
- }
- //---------------einfach Farbe------------------------------
- void setColor(int redValue, int greenValue, int blueValue) {
- analogWrite(rotPin, redValue);
- analogWrite(gruenPin, greenValue);
- analogWrite(blauPin, blueValue);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement