Sapin de Noël bâtons de colle *

 

 

    Prérequis :

Matériel :

  • 1 x Carte Arduino Nano
  • 1 x L298N
  • 1 x Moteur PAP bipolaire
  • 1 x DHT11
  • 4 x  strips Led (6 leds WS2812B)
  • 1 x Alimentation 12v
  • Fils de connexion
  • 1 x Breadboard

Version IDE :

Bibliothèque :

  • Adafruit_NeoPixel.h

 

 

Vidéo de démonstration :

 

Schéma de câblage :

 

Fichiers SolidWorks et STL :

 

Code :

#include <Stepper.h>
const int stepsPerRevolution = 200;
// Initialise le moteur sur les broches 2 à 5
Stepper myStepper(stepsPerRevolution, 2, 3, 4, 5);
int motorSpeed = 20;
#include <Adafruit_NeoPixel.h>
#define LED_PIN 9     //Définit le pin du strip des Leds
#define LED_COUNT  6  //Définit le nombre de Leds
int BRIGHTNESS = 30;  // Set BRIGHTNESS to about 1/5 (max = 255)

Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
unsigned long currentTime = 0;
unsigned long previousTime = 0;
unsigned long previousAnim = 0;
int anim;
int tpsanim[] = {5000, 5000, 5000, 5000, 5000, 10000}; //Temps par animation
int positionLed;
int prevpositionLed;
int etat;
int color[4][3] = {{255, 0, 0}, {0, 255, 0}, {0, 0, 255}, {255, 255, 255}};
int coloranim5[6] = {0, 1, 2, 3, 0, 1};
int coloranim6[7][6] = {{255, 0, 0}, {0, 255, 0}, {0, 0, 255}, {255, 0, 0}, {0, 255, 0}, {0, 0, 255}, {0, 0, 0, 0, 0, 0}};
int paracolor;

void setup() {
  Serial.begin(9600);
  strip.begin();
  strip.show();
  strip.setBrightness(BRIGHTNESS);
}

void loop() {
  currentTime = millis();
  myStepper.setSpeed(motorSpeed);
    myStepper.step(1);
  if ((currentTime - previousAnim) > tpsanim[anim]) {
    previousAnim = currentTime;
    reset();
    anim++;
    }
    switch (anim) {
    case 0:
      anim1(60);
      break;
    case 1:
      anim2(60);
      break;
    case 2:
      anim3(80);
      break;
    case 3:
      anim4(5);
      break;
    case 4:
      anim5(5);
      break;
    case 5:
      anim6(1);
      break;
    case 6:
      anim = 0;
      break;
    default:
      // statements
      break;
    }
}

void reset() {
  BRIGHTNESS = 30;
  positionLed = 0;
  prevpositionLed = 0;
  etat = 0;
  paracolor = 0;
}

void anim6(int vitesse) {
  if ((currentTime - previousTime) > vitesse) {
    previousTime = currentTime;
    for (int i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, coloranim6[i][0],   coloranim6[i][1],   coloranim6[i][2]);
      strip.show();
      strip.setBrightness(BRIGHTNESS);
    }
    for (int b = 0; b < strip.numPixels(); b++) {
      for (int i = 0; i < 3; i++) {
        if (coloranim6[b][i] < 255 && coloranim6[6][0] == 0) {
          coloranim6[b][i]++;
        }
        if (coloranim6[b][i] > 0 && coloranim6[6][0] == 1) {
          coloranim6[b][i]--;
        }
        if (coloranim6[b][i] == 255 && coloranim6[6][0] == 0) {
          coloranim6[6][0] = 1;
        }
        if (coloranim6[b][i] == 0 && coloranim6[6][0] == 1) {
          coloranim6[6][0] = 0;
        }
      }
    }
  }
}

void anim5(int vitesse) {
  if ((currentTime - previousTime) > vitesse) {
    previousTime = currentTime;
    if (BRIGHTNESS > 0 && etat == 0) {
      BRIGHTNESS--;
    }
    if (BRIGHTNESS < 50 && etat == 1) {
      BRIGHTNESS++;
    }
    for (int i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, color[coloranim5[i]][0],   color[coloranim5[i]][1],   color[coloranim5[i]][2]);
      strip.show();
      strip.setBrightness(BRIGHTNESS);
    }
    if (BRIGHTNESS == 0) {
      etat = 1;
      for (int i = 0; i < strip.numPixels(); i++) {
        if (coloranim5[i] < 3) {
          coloranim5[i]++;
        } else {
          coloranim5[i] = 0;
        }
      }
    }
    if (BRIGHTNESS == 30) {
      etat = 0;
    }
  }
}


void anim4(int vitesse) {
  if ((currentTime - previousTime) > vitesse) {
    previousTime = currentTime;
    if (BRIGHTNESS > 0 && etat == 0) {
      BRIGHTNESS--;
    }
    if (BRIGHTNESS < 100 && etat == 1) {
      BRIGHTNESS++;
    }
    for (int i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, color[paracolor][0],   color[paracolor][1],   color[paracolor][2]);
      strip.show();
      strip.setBrightness(BRIGHTNESS);
    }
    if (BRIGHTNESS == 0) {
      etat = 1;
      if (paracolor < 2) {
        paracolor++;
      } else {
        paracolor = 0;
      }
    }
    if (BRIGHTNESS == 100) {
      etat = 0;
    }
  }
}

void anim3(int vitesse) {
  if ((currentTime - previousTime) > vitesse) {
    previousTime = currentTime;
    if (positionLed < strip.numPixels() && etat == 0) {
      strip.setPixelColor(positionLed, color[paracolor][0],   color[paracolor][1],   color[paracolor][2]);
      strip.show();
      positionLed++;
      if (positionLed >= strip.numPixels()) {
        etat = 1;
        if (paracolor < 2) {
          paracolor++;
        } else {
          paracolor = 0;
        }
      }
    }
    if (positionLed > 0 && etat == 1) {
      strip.setPixelColor(positionLed, color[3][0],   color[3][1],   color[3][2]);
      strip.show();
      positionLed--;
      if (positionLed <= 0) {
        etat = 0;
      }
    }
  }
}

void anim2(int vitesse) {
  if ((currentTime - previousTime) > vitesse) {
    previousTime = currentTime;
    strip.clear();
    positionLed = random(0 , strip.numPixels());
    paracolor = random(0 , 4);
    strip.setPixelColor(positionLed, color[paracolor][0],   color[paracolor][1],   color[paracolor][2]);
    strip.show();
  }
}


void anim1(int vitesse) {
  if ((currentTime - previousTime) > vitesse) {
    previousTime = currentTime;
    if (positionLed < strip.numPixels()) {
      strip.setPixelColor(positionLed, color[paracolor][0],   color[paracolor][1],   color[paracolor][2]);
      strip.show();
      positionLed++;
    }
    else {
      positionLed = 0;
      strip.clear();
      if (paracolor < 3) {
        paracolor++;
      } else {
        paracolor = 0;
      }
    }
  }
}

 


 

Laisser un commentaire