пятница, 13 декабря 2019 г.

Звезда-трансформер на ёлку

// Documentation for this library can be found at:
// github.com/Stan-Reifel/TinyStepper_28BYJ_48
//
//
// This library requires that the stepper motor be connected to the Arduino
// using ULN2003 driver board.

// ***********************************************************************

#include <TinyStepper_28BYJ_48.h>
#include <Adafruit_NeoPixel.h>

// NEO Pixel configuration
#define PIN 0
#define LEDS_COUNT 5
#define RED strip.Color(255, 0, 0)
#define GREEN strip.Color(0, 255, 0)
#define BLUE strip.Color(0, 0, 255)
#define WHITE strip.Color(255, 255, 255)

// pin assignments, any digital pins can be used
#define MOTOR_IN1_PIN 1
#define MOTOR_IN2_PIN 2
#define MOTOR_IN3_PIN 3
#define MOTOR_IN4_PIN 4

// create the stepper motor object
TinyStepper_28BYJ_48 stepper;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LEDS_COUNT, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
// connect and configure the stepper motor to its IO pins
stepper.connectToPins(MOTOR_IN1_PIN, MOTOR_IN2_PIN, MOTOR_IN3_PIN, MOTOR_IN4_PIN);
// set the speed and acceleration rates for the stepper motor
stepper.setSpeedInStepsPerSecond(256);
stepper.setAccelerationInStepsPerSecondPerSecond(512);

strip.begin();
strip.show(); // Initialize all pixels to 'off'
}

void loop() {
// Rotate the motor in the forward direction one revolution
stepper.moveRelativeInSteps(410);
for (byte i = 0; i < 20; i++){
fadeInOut(0xff, 0x00, 0x00); // red
}

// rotate backward 1 rotation, then wait 1 second
stepper.moveRelativeInSteps(-410);
for (byte i = 0; i < 20; i++){
fadeInOut(0x00, 0x00, 0xff); // blue
}
}

void fadeInOut(byte red, byte green, byte blue){
float r, g, b;

for(int k = 0; k < 256; k=k+1) {
r = (k/256.0)*red;
g = (k/256.0)*green;
b = (k/256.0)*blue;
setAll(r,g,b);
strip.show();
delay(5);
}

for(int k = 255; k >= 0; k=k-2) {
r = (k/256.0)*red;
g = (k/256.0)*green;
b = (k/256.0)*blue;
setAll(r,g,b);
strip.show();
delay(5);
}
}

void setAll(byte red, byte green, byte blue) {
for(int i = 0; i < LEDS_COUNT; i++ ) {
strip.setPixelColor(i, strip.Color(red, green, blue));
}
strip.show();
}

Let's block ads! (Why?)

Комментариев нет:

Отправить комментарий