Étape 4: IDE Arduino Code
Le codage n’est pas trop difficile de mettre sur pied. La première section du code inclut les bibliothèques requises, définit ce que NIP seront utilisés, initialise certaines valeurs et crée les objets de chaque côté à contrôler.
#include <Adafruit_Neopixel.h> #include <avr/power.h> #include <StepperAK.h> #include <IRremote.h>
#define LEDPin 5 //LED strip pin 5 #define gearratio 64 //1:64 gear ratio for stepper motor #define NUMPIXELS 60 // How many NeoPixels are attached to the Arduino #define Pin1 8 //These set the pins for the Stepper Motor #define Pin2 9 #define Pin3 10 #define Pin4 11 Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, LEDPin, NEO_GRB + NEO_KHZ800);//This creates the Neopixel object strip int delayt = 500; // delay for half a second const int stepsPerRevolution = 2048;//2048 steps turn the motor one revolution Stepper myStepper(stepsPerRevolution, Pin1, Pin2, Pin3, Pin4);//Creates the Stepper Motor object int RECV_PIN = 3; //IR Receiver connected to Pin 3 IRrecv irrecv(RECV_PIN); //Create a IR receiver object decode_results results; //Create a decode_results object
La partie du programme d’installation du code simplement permet le capteur IR, engrenages vers le bas le moteur pas à pas et Initialise toutes les LEDs à un état d’arrêt.
void setup() { irrecv.enableIRIn(); //Enables the IR device myStepper.setSpeed(0.15*gearratio); //the motor appears to be geared down 1/64, strip.begin(); strip.show(); // Initialize all pixels to 'off' }
Dans la section de la boucle, j’ai utilisé imbriqué si des déclarations pour chaque fois qu’un signal a été reçu par le capteur infrarouge, il décrypte ce que signal était et effectue l’opération correspondante à ce signal. Pour chaque résultat de signal si déclaration, j’ai un couple logique ou opérateurs, le premier est pour le kit Arduino distant, les deuxième et troisième sont pour la télécommande de la Xbox 360. J’ai deux signaux différents pour la 360 distant est parce qu’un signal différent est mis si vous maintenez le bouton plutôt que simplement en appuyant sur on. Je l’ai codé dans le programme pour tenir compte de cette anomalie de signal. Les fonctions en bas sont fournies par Adafruit, et ils peuvent être facilement modifiés en fonction de ce que vous cherchez à faire avec la bande de LED.
void loop() { if (irrecv.decode(&results)) {//has a transmission been received? if (results.value == 2148529177 || results.value == 2148496409 || results.value == 16738455){ //If the "0" button or the "stop" button is pressed, the LEDs all turn off. colorWipe(strip.Color(0,0,0), 20); } irrecv.resume(); // Receive the next value from the IR Sensor } if (irrecv.decode(&results)) {//has a transmission been received? if (results.value == 2148529164 || results.value == 2148496396 || results.value == 16753245){ //If the power button is pressed the LEDs all initialize to red colorWipe(strip.Color(255,0,0), 20); } irrecv.resume(); // Receive the next value from the IR Sensor } if (irrecv.decode(&results)) {//has a transmission been received? if (results.value == 2148529171 || results.value == 2148496403 || results.value == 16724175){ //If the "1" Button or the "x" button is pressed do the following: colorWipe(strip.Color(0,0,255), 20); } irrecv.resume(); // Receive the next value from the IR Sensor } if (irrecv.decode(&results)) { if (results.value == 2148496422 || results.value == 2148529190 || results.value == 16718055){ //If the "2" or "y" Button is pressed do the following: colorWipe(strip.Color(255,255,255), 20); } irrecv.resume(); } if (irrecv.decode(&results)) { if (results.value == 2148529185 || results.value == 2148496417 || results.value == 16743045) { //If the "3" button or "right arrow" is pressed do the following: theaterChaseRainbow(50); } irrecv.resume(); } if (irrecv.decode(&results)) { if (results.value == 2148529184 || results.value == 2148496416 || results.value == 16716015) { //If the "4" button or "left arrow" button is pressed do the following: rainbow(50); } irrecv.resume(); }
if (irrecv.decode(&results)) { if (results.value == 2148496418 || results.value == 2148529186 || results.value == 16726215) { //If the "5" button or "Ok" is pressed do the following: myStepper.step(stepsPerRevolution); //Rotates the stepper motor 1 revolution myStepper.step(stepsPerRevolution); } irrecv.resume(); } } // Fill the dots one after the other with a color<br>void colorWipe(uint32_t c, uint8_t wait) {
for(unit16_t I=0; I<strip.numPixels(); I++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
Style théâtre rampant s’allume avec arc-en-ciel effet Sub theaterChaseRainbow (uint8_t attente) {pour (int j = 0; j < 256; j ++) {/ / cycle tous les 256 couleurs dans la roue de (q int = 0; q < 3; q ++) {pour (int j’ai = 0; j’ai < strip.numPixels(); i = i + 3) {strip.setPixelColor (i + q, roue ((i + j) % 255)); //turn chaque pixel troisième sur} //Theatre-style crawling lights with rainbow effect void theaterChaseRainbow(uint8_t wait) { for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel for (int q=0; q < 3; q++) { for (int i=0; i < strip.numPixels(); i=i+3) { strip.setPixelColor(i+q, Wheel( (i+j) % 255));//turn every third pixel on } strip.show(); delay(wait); for (int i=0; i < strip.numPixels(); i=i+3) { strip.setPixelColor(i+q, 0); //turn every third pixel off } } } } // 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); } else if(WheelPos < 170) { WheelPos -= 85; return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); } else { WheelPos -= 170; return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); } } void rainbow(uint8_t wait) { uint16_t i, j; for(j=0; j<256; j++) { for(i=0; i<strip.numPixels(); i++) { Delay(Wait) ; pour (int i = 0; j’ai < strip.numPixels(); i = i + 3) {strip.setPixelColor (i + q, 0); //turn chaque pixel troisième hors strip.setPixelColor(i, Wheel((i+j) & 255); } } } / / Entrée d’une valeur de 0 à 255 pour obtenir une valeur de couleur. Les couleurs sont un transition r - g - b - retour à r. roue d’uint32_t (WheelPos octets) {WheelPos = 255 - WheelPos ; if(WheelPos < 85) {bande de retour. Couleur (255 - WheelPos * 3, 0, WheelPos * 3) ; strip.show(); d’autre if(WheelPos < 170) {WheelPos-= 85 ; bande de retour. Couleur (0, WheelPos * 3, 255 - WheelPos * 3) ; delay(wait); else {WheelPos-= 170 ; bande de retour. Couleur (WheelPos * 3, 255 - WheelPos * 3, 0) ; }
}