#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <Adafruit_NeoPixel.h>
#include <Servo.h>
#include <SPI.h>
#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
#define MAX_DEVICES 4
#define CLK_PIN 13
#define DATA_PIN 11
#define CS_PIN 10
#define PIN 2 // input pin Neopixel is attached to
#define BUTTON_PIN 3 // input pin for button
#define SERVO_PIN 9 // pin for servo motor
#define NUMPIXELS 24 // number of neopixels in strip
MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
Servo myServo;
bool servoSwept = false;
const uint16_t SCROLL_SPEED = 25;
const textEffect_t SCROLL_EFFECT = PA_SCROLL_LEFT;
const textPosition_t SCROLL_ALIGN = PA_LEFT;
const uint16_t SCROLL_PAUSE = 2000;
char message[] = "Hello, world!";
void setup() {
P.begin();
P.displayText(message, SCROLL_ALIGN, SCROLL_SPEED, SCROLL_PAUSE, SCROLL_EFFECT, SCROLL_EFFECT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
myServo.attach(SERVO_PIN);
pixels.begin();
}
void loop() {
if (P.displayAnimate()) {
P.displayReset();
}
// Play random colors on the neopixel
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(random(0, 256), random(0, 256), random(0, 256)));
}
pixels.show();
// Check if the button is pressed
if (digitalRead(BUTTON_PIN) == LOW) {
// Play a moving rainbow cycle for 10 seconds
unsigned long startTime = millis();
servoSwept = false; // reset the servoSwept flag
while (millis() - startTime < 10000) {
for (int j = 0; j < 128; j++) { // cycle first half of the colors in the wheel
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, Wheel((i + j) & 255));
}
pixels.show();
delay(20); // wait a little bit before displaying the next color
}
// Move servo to 90 degrees and back
if (!servoSwept) {
myServo.write(90);
delay(500);
myServo.write(0);
delay(500);
myServo.write(90); // return the servo to its initial position
delay(500);
servoSwept = true; // set the servoSwept flag to true
}
for (int j = 128; j < 256; j++) { // cycle second half of the colors in the wheel
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, Wheel((i + j) & 255));
}
pixels.show();
delay(20); // wait a little bit before displaying the next color
}
}
}
// Add a delay to slow down the rate of updating the random colors
delay(500);
}
// 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) {
if(WheelPos < 85) {
return pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if(WheelPos < 170) {
WheelPos -= 85;
return pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}