Arduino code problem : change scrolling text when button is pressed (THANK YOU)

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 :slight_smile:

  • 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);
  }
}

Good guess, your for-loops block for 128x24x20 milliseconds.

There are a few topics on the forum that can help you to solve the puzzle. The keyword is millis().

Your topic has been moved to a more suitable location on the forum.

You have a classmate that was doing the exact same thing last week.

Yep. Here it is, complete with a simulation. Do you two attend the same class?

(Oops, still searching)

Thank You so much for these answers, guys ! Right now, I'm trying to modify the code but it doesn't seems to work. I'll work on it -but i admit, for now, i don't understand why i'm doing :slight_smile:
If you have anymore advices or code, it would be so cool !

THANK YOU so much

Not the same class :wink:

Thank you for searching ! :slight_smile:

Oh, really. Here's "not the same class - bonus" sim I made for him.
https://wokwi.com/projects/357363115155713025

And you posted, in another topic, a link to the sim I made, and called it your sim...

which is plagiarism, but I do free stuff all the time because it's fun for me. Let's get what you need working in ONE thread.

Ahh... right... it isn't "a friend" doing the same thing... you have TWO login names.

//https://forum.arduino.cc/t/arduino-code-problem-change-scrolling-text-when-button-is-pressed-thank-you/1093339

/*
----- a ledring (neopixel 24) will play cool lights (mainly orange and red types).

WHEN Bonus is activate via a button :
----- Matrix 7219 will write a text (for example "Winner").
------ Ledring will make a cool rainbow template (or anything coolest).
------ A sound will be play via an ISD1820 (or even better, 2 or 3 random sounds).
------- The servo motor will sweep from "x" degrees slowy and then will come back
*/

#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("Bonus!", 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(100);
}

// 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);
  }
}

Oh yes, that was my message ! Strange that i have 2 logins. I didn't know.

I worked on the code last night but still no improvements. I continue :slight_smile:

Really sorry, it was not plagiarism just a mistake. Cut/paste problem or something, i don't know.
Since this first message i have worked a lot on the code but still cannot figure it out ...

Hello,

I'm quite new at this and i do have issues with a code...

I'm trying to use a MDMAX7219 Matrix to display texts.
That's quite ok when I use Parola library but i don't understand how to make the text change when i activate a pushbutton.

The idea is to display differents texts and then when button is pushed, display a new text.
Then, back to initials text.

The other issue is that my text have to be displayed vertically.
animated text would be cool too...

Can you help me ?

Here is the link...

https://wokwi.com/projects/348937583067136596

Thank You so much for any help...

Hi.

I put the link in my text. It's a wokwi link.
Can you see it or did i make something wrong ?

Cheers

Posting to Wokwi will attract a certain small segment of the helpers here. The rest, not so much. A schematic(pencil on paper, post photo) is fine to start with. Post your first attempts at code, and we can go from there.
You're not flagged as a new member, but this might help you anyway:
How to get the best out of this forum

Thank you really muc for your answer !
i will work on the code a little bit more and will draw schematics.

For now, i'm at this point :slight_smile:
When i push the button, it change the text but what i need is that the new text ("hello") stand for 3-4seconds before getting back to the initial text.

And add some animations.... later i guess !

Thanks a lot for the advices ; i'm understanding Arduino is not for me ; i should go back to my wellknown job (carpentry).

#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>

const uint16_t WAIT_TIME = 1000;

#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
#define MAX_DEVICES 4

#define CLK_PIN   13
#define DATA_PIN  11
#define CS_PIN    10

int button = 3;
int etatbutton = 0;

// Hardware SPI connection
MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);

// Global data
struct sCatalog
{
  textEffect_t  effect;   // text effect to display
  const char *  psz;      // text string nul terminated
  uint16_t      speed;    // speed multiplier of library default
  uint16_t      pause;    // pause multiplier for library default
};

sCatalog catalog[] =
{
#if ENA_OPNCLS
  { PA_OPENING, "OPEN", 3, 1 },
#endif
};

void setup(void)

{
     pinMode (button, INPUT) ;
{
  P.begin();

  for (uint8_t i=0; i<ARRAY_SIZE(catalog); i++)
  {
    catalog[i].speed *= P.getSpeed();
    catalog[i].pause *= 500;
  }
}}

void loop(void)
{
  static textPosition_t just = PA_LEFT;
  static uint8_t i = 0;   // text effect index
  static uint8_t j = 0;   // text justification index

  if (P.displayAnimate()) 
  {
    // progress the justification if needed
    if (i == ARRAY_SIZE(catalog))
    {
      j++;
      if (j == 3) j = 0;

      switch (j)
      {
      case 0: just = PA_LEFT;    break;
      case 1: just = PA_CENTER;  break;
      case 2: just = PA_RIGHT;   break;
      }

      i = 0;  // reset loop index
    }

    // set up new animation
    P.displayText(catalog[i].psz, just, catalog[i].speed, catalog[i].pause, catalog[i].effect, catalog[i].effect);

    i++;  
  }
{
  P.print ("Boom");
  etatbutton = digitalRead (button);  
  
  if (etatbutton==HIGH)
  

  P.print("Hello");
  delay (100);
 
}}

Do a careful inventory of all you know about carpentry.  How long did it take to learn all this?  A week?  A month?  A year?

Electronics and programming is no different.  You have to stick with it build a bit at a time.

1 Like

Hello again everyone !

someone to help ? :slight_smile:

When you said that, I presumed you were giving up, walking away. I still can't visualize either your schematic, or your project in any detailed form. While I'd like to help, I'd prefer if you actually tried to explain what you want in excruciating detail, so we have a better chance of guiding you, instead of misguiding you.
Ball's in your court.

1 Like

Are the asterisks in the expressions below means multiplications of the previous speed data to the new one? is it what you intended?

1 Like

You want to begin here. A button press has only a momentary effect. However, you want the action to last several seconds. The normal way to do that is to have a status variable which "remembers" that the button was pushed, let's call it wasPushed and another (a timer) which remembers the time at which the button was pushed. Let's call that wasPushedAtMs.
So you set wasPushed and wasPushedAtMs when the button is pressed. During the time WasPushed is true you display the new text. When the timer wasPushedAtMs expires, say after 3000ms , you make wasPushed false. When wasPushed becomes false, the original text is displayed. You test if the timer has expired by comparing its value with the current value of millis().

1 Like

Hello guys,

Thank You so much for the answers. You guys are really cool to take time to help.

I will work on the code tonight and try to figure it out.

Here's what i need to do : (is it detailed enough ?).

1- During the game (all the time actually), i would like the Matrix to display diferent (random or not) TEXTS ans ANIMATION.
For exemple : "WIN WIN WIN" followed bv $ signs followed by scrolling texts,etc...

2- WHEN BUTTON IS PRESSED :
Matrix display an animated text "BONUS" (+ signs ?) for 8 seconds :

  • At second 0 = "BONUS" animated text is displayed.

  • At second 2 = A servo motor will sweep from 0 to 120 and back.
    (Bonus animation is still running on Matrix until :

  • At second 8 : no more "BONUS" text and BACK to the initial text and animations.

Is it clear enough ? If not, tell me, i'll make something clearer.

This general idea/final project is actually this :slight_smile:
https://forum.arduino.cc/t/coin-pusher-help-servo-button-led-matrix-sound/1056333

I will work on the code but any advices are welcomed because i'm beginning at this and just do "Copy and paste" to make it work. But actually I don't understand all the things i do in the code.

Hope it's clear enough.

Again, thanks a lot everyone !! Really.

Thank for your answer !

Actually, i did a lot of "Copy&Paste" to make this code work.... But, i don't always no what it means :slight_smile:

I don't need speed differences.

I detailed just before answering You what i would like to do. Maybe it will help you understand.

THANK YOU
Sorry, i'll try to figure it out and work on it.