#include /* changed from #defined constants to variables 2015-12-15 Mel Lester Jr. The following two lines are all you may need to change for your project */ // Pin reliée a la DATA du bandeau LED const byte DataLED_pin = 6; // Arduino PWM data pin D6 int pixels = 5; // number of Neopixels // Parameter 1 = number of pixels in strip // Parameter 2 = Arduino pin number (most are valid) // Parameter 3 = pixel type flags, add together as needed: // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products) // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) Adafruit_NeoPixel strip = Adafruit_NeoPixel(pixels, DataLED_pin, NEO_GRB + NEO_KHZ800); // IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across // pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input // and minimize distance between Arduino and first pixel. Avoid connecting // on a live circuit...if you must, connect GND first. // Les valiables volatiles sont utilisees pour echanger des informations entre les programme principal // et les fonctions déclenchees sur interruption volatile byte seqA = 0b0000; volatile byte seqB = 0b0000; volatile byte NumProg = 0; volatile boolean right = false; volatile boolean left = false; volatile boolean button = false; volatile byte state = LOW; volatile byte brightness = 255; // Pin utilisees pour lire l'état des switchs du selecteur rotatif const byte RotarySelector_Button = A5; const byte RotarySelector_A_Signal = A3; const byte RotarySelector_B_Signal = A4; void setup() { pinMode(DataLED_pin, OUTPUT); // Enable internal pull-up resistors pinMode(RotarySelector_Button, INPUT_PULLUP); //Bouton pinMode(RotarySelector_A_Signal, INPUT_PULLUP); //Rotary A pinMode(RotarySelector_B_Signal, INPUT_PULLUP); //Rotary B // Interruptions sur les pin A3, A4 et A5 pour le selecteur rotatif PCICR = 0b00000010; // 1. PCIE1: Pin Change Interrupt Enable 1 PCMSK1 = 0b00111000; // Enable Pin Change Interrupt for A5, A4, A3 Serial.begin(115200); Serial.println("Start"); //Extinction des LEDs strip.begin(); strip.show(); // Initialize all pixels to 'off' } void loop() { //Serial.println(NumProg); switch (NumProg) { case 0: AfficheRVB(); break; case 1: colorWipe(strip.Color(255, 0, 0), 50); // Red break; case 2: colorWipe(strip.Color(0, 255, 0), 50); // Green break; case 3: colorWipe(strip.Color(0, 0, 255), 50); // Blue break; case 4: rainbow(20); break; case 5: rainbowCycle(20); break; case 6: theaterChase(strip.Color( 0, 0, 127), 50); // Blue break; case 7: theaterChase(strip.Color(127, 127, 127), 50); // White; break; case 8: theaterChase(strip.Color(127, 0, 0), 50); // Red break; default: NumProg = 0; break; } // Some example procedures showing how to display to the pixels: // Take action if a new command received from the encoder if (left) { left = false; Serial.println("Droite"); } if (right) { right = false; Serial.println("Gauche"); } if (button) { button = false; Serial.println("Bouton"); } } //************************************************ // Fonctions d'affichage des LEDs //************************************************ // Affiche R - - V - - B void AfficheRVB() { strip.setPixelColor(0, 255, 0, 0); strip.setPixelColor(1, 0, 0, 0); strip.setPixelColor(2, 0, 255, 0); strip.setPixelColor(3, 0, 0, 0); strip.setPixelColor(4, 0, 0, 255); strip.show(); } void SwapRVB(uint8_t wait) { unsigned long previousMillis = 0; bool loopagain = true; for (uint8_t i = 0; i < 4; i++) { // Tout en R for (uint16_t i = 0; i < strip.numPixels(); i++) { strip.setPixelColor(i, 255, 0, 0); } strip.show(); //remplace le delay pour permettre l'interruption previousMillis = millis(); do { } while ((millis() - previousMillis < wait) && !button); } } // Fill the dots one after the other with a color void colorWipe(uint32_t c, uint8_t wait) { unsigned long previousMillis = 0; bool loopagain = true; for (uint16_t i = 0; i < strip.numPixels(); i++) { strip.setPixelColor(i, c); strip.show(); //remplace le delay pour permettre l'interruption previousMillis = millis(); do { } while ((millis() - previousMillis < wait) && !button); } } //Theatre-style crawling lights. void theaterChase(uint32_t c, uint8_t wait) { unsigned long previousMillis = 0; bool loopagain = true; for (int j = 0; j < 10; j++) { //do 10 cycles of chasing for (int q = 0; q < 3; q++) { for (int i = 0; i < strip.numPixels(); i = i + 3) { strip.setPixelColor(i + q, c); //turn every third pixel on } strip.show(); //remplace le delay pour permettre l'interruption previousMillis = millis(); do { } while ((millis() - previousMillis < wait) && !button); for (int i = 0; i < strip.numPixels(); i = i + 3) { strip.setPixelColor(i + q, 0); //turn every third pixel off } } } } void rainbow(uint8_t wait) { uint16_t i, j; unsigned long previousMillis = 0; bool loopagain = true; for (j = 0; j < 256; j++) { for (i = 0; i < strip.numPixels(); i++) { strip.setPixelColor(i, Wheel((i + j) & 255)); } strip.show(); //remplace le delay pour permettre l'interruption previousMillis = millis(); do { } while ((millis() - previousMillis < wait) && !button); } } // Slightly different, this makes the rainbow equally distributed throughout void rainbowCycle(uint8_t wait) { uint16_t i, j; unsigned long previousMillis = 0; bool loopagain = true; for (j = 0; j < 256; j++) { // 1 cycles of all colors on wheel for (i = 0; i < strip.numPixels(); i++) { strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255)); } strip.show(); //remplace le delay pour permettre l'interruption previousMillis = millis(); do { } while ((millis() - previousMillis < wait) && !button); } } // Input a value 0 to 255 to get a color value. // The colours are a transition r - g - b - back to r. uint32_t Wheel(byte WheelPos) { WheelPos = 255 - WheelPos; if (WheelPos < 85) { return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); } if (WheelPos < 170) { WheelPos -= 85; return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); } WheelPos -= 170; return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); } //************************************************ // Interruption //************************************************ ISR (PCINT1_vect) { // If interrupt is triggered by the button if (!digitalRead(RotarySelector_Button)) { button = true; NumProg++; } // Else if interrupt is triggered by encoder signals else { // Read A and B signals boolean A_val = digitalRead(RotarySelector_A_Signal); boolean B_val = digitalRead(RotarySelector_B_Signal); /* Serial.print("Val A : "); Serial.print(A_val, BIN); Serial.print(" - Val B : "); Serial.println(B_val, BIN); */ // Record the A and B signals in seperate sequences //Decalabe a gauche d'un bit https://www.arduino.cc/en/Reference/Bitshift seqA <<= 1; // OU bit a bit pour inserer la valeur mesuree https://www.arduino.cc/en/Reference/BitwiseAnd seqA |= A_val; seqB <<= 1; seqB |= B_val; /* Serial.print("Avant : seqA : "); Serial.print(seqA,BIN); Serial.print(" - seqB : "); Serial.println(seqB,BIN); */ // Pour ne conserver que les cinq derniers etats // Mask the MSB four bits seqA &= 0b10001111; seqB &= 0b10001111; /* Serial.print("Apres : seqA : "); Serial.print(seqA,BIN); Serial.print(" - seqB : "); Serial.println(seqB,BIN); Serial.println(""); */ // Compare the recorded sequence with the expected sequence if (seqA == 0b00001001 && seqB == 0b00000011) { left = true; if (brightness < 255) { if (brightness < 220) { brightness = brightness + 10; } else { brightness++; } } } if (seqA == 0b00000011 && seqB == 0b00001001) { right = true; if (brightness > 0) { if (brightness > 20) { brightness = brightness - 10; } else { brightness--; } } } Serial.println(brightness); strip.setBrightness(brightness); strip.show(); } }