Hello everyone ! Sorry for my english....
I built a cool "coin pusher machine" for my best friend's birthday. It took me a long time but everyt mechanic parts are ok and working.
Well.... I do have a problem with arduino. I'm not really good at it and i don't have time anymore to learn more...
I used Wokwi, arduino forums, examples, libraires, generators, etc to try to create a code. It does work on simulator (more or less!) but i'm stuck...
Here's the main problem : when button is hit, scrolling text stops. What i would like is this
- When button is pressed : Replace the scrolling text by "BONUS" + a cool light effect on the neopixelring. Few seconds after, servo motor moves.
Then back to normal !
I guess it's something with non blocking code ?!
Here's the main problem but i do haveother ones to check after (not so responsive button + add sound when bonus is activated, etc....) Well, one step at a time i guess...
THANK YOU SO MUCH. Really !`
#include <Adafruit_NeoPixel.h>
#include <Servo.h>
#include <MD_Parola.h>
#include <MD_MAX72xx.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
// Hardware SPI connection
MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
// Arbitrary output pins
// MD_Parola P = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
Servo myServo;
bool servoSwept = false;
void setup()
{
pinMode(BUTTON_PIN, INPUT_PULLUP);
myServo.attach(SERVO_PIN);
pixels.begin();
P.begin();
}
void loop()
{
if (P.displayAnimate())
P.displayText("Hello !", PA_CENTER, 50, 1000, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
// 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);
}
}