I would like to use two buttons to alternate between two scrolling message on an LED Matrix display (Arduino Nano & Max72xx Matrix) can anybody help?
Welcome to the forum
What hardware are you using and what code have you written so far ?
welcome to the arduino-forum.
I'm pretty sure that you agree and will follow the way how to solve your problem mimimum 200 minutes faster.
This requires to invest 20 minutes of your precious time to read how to speedup solving your problems.
Directly after registering you got presented informations how to speed up solving your problem.
You should really read it.
https://www.google.com/search?q=arduino+max7219+scrolling+text+code
Here is a tutorial about switches and buttons
Take one of the demo-codes and make an own attempt.
best regards Stefan
You did not read the how to get the best out of this forum.
Otherwise you would already now how to post code as a code-section
#include <MD_Parola.h>
#include <MD_MAX72XX.h>
#include <SPI.h>
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4
#define CS_PIN 10
MD_Parola myDisplay = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
char msg1[] = "Andy is IN";
char msg2[] = "Andy is OUT";
void setup() {
myDisplay.begin();
myDisplay.setIntensity(0);
myDisplay.displayClear();
}
void loop() {
if (myDisplay.displayAnimate()) {
myDisplay.displayScroll(msg1, PA_CENTER, PA_SCROLL_LEFT, 40);
myDisplay.displayReset();
}
}
type or paste code here
OK very good you posted your attempt how to code it as a code-section.
Well the way this forum works is that you describe what your code does
and that you write what you want the code do instead.
And of course you can always ask specific questions.
So as a starting point: How would you add reading in a single button?
best regards Stefan
Would you consider using one button? I put your two character arrays in one two-dimension array. The button press changes states of the subscript to point to phrase one or phrase two...
#include <MD_Parola.h>
#include <SPI.h> // 10 = CS, 11 = MOSI, 12 = MISO, 13 = CLK
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4
#define CS_PIN 10
#define CLK_PIN 13 // not used in the sketch, here for wiring reference
#define MOSI_PIN 11 // not used in the sketch, here for wiring reference
#define SPEED 40
MD_Parola myDisplay = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
char message[][12] = {"Andy is IN\0", "Andy is OUT\0"};
byte msg;
int buttonPin = A0;
void setup() {
myDisplay.begin();
myDisplay.setIntensity(0);
myDisplay.displayClear();
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
if (!digitalRead(buttonPin)) { // read the button
delay(150); // debounce
msg = !msg; // change subscript state to choose array element
}
// MD_Parola
if (myDisplay.displayAnimate()) {
myDisplay.displayScroll(message[msg], PA_CENTER, PA_SCROLL_LEFT, SPEED);
myDisplay.displayReset();
}
}
This is the wokwi diagram.json
{
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{
"type": "wokwi-arduino-nano",
"id": "nano",
"top": -137.2,
"left": 174.1,
"rotate": 270,
"attrs": {}
},
{
"type": "wokwi-max7219-matrix",
"id": "matrix1",
"top": -229.8,
"left": -146.16,
"attrs": { "chain": "4", "color": "cyan", "layout": "fc16" }
},
{
"type": "wokwi-pushbutton",
"id": "btn1",
"top": -176.2,
"left": 316.8,
"attrs": { "color": "green" }
}
],
"connections": [
[ "matrix1:V+", "nano:5V", "red", [ "h115.2", "v67.2" ] ],
[ "matrix1:GND", "nano:GND.1", "black", [ "h105.6", "v38.4" ] ],
[ "matrix1:DIN", "nano:11", "blue", [ "h28.8", "v144" ] ],
[ "matrix1:CS", "nano:10", "gold", [ "h19.2", "v124.8" ] ],
[ "matrix1:CLK", "nano:13", "green", [ "h9.6", "v163.2", "h96", "v-28.8" ] ],
[ "nano:A0", "btn1:2.l", "green", [ "h19.2", "v-77" ] ],
[ "nano:GND.1", "btn1:1.l", "black", [ "h0" ] ]
],
"dependencies": {}
}
This is awesome, thank you very much
There are better methods of reading a button... I used a poor example, but functional. Try to find and write two more sketches to...
- Read a button using "millis()" to "debounce" the pushbutton rather than "delay()"
- Read a button using a hardware interrupt (hint: with Uno/Nano, only pins 2 and 3 have hardware interrupts)
You will use pushbuttons very often in your projects. Ask questions when you need. Have fun learning.
[edit]
Also read about:
- long press versus short press
- multiple presses
#include <MD_Parola.h>
#include <SPI.h>
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4
#define CS_PIN 10
#define CLK_PIN 13
#define MOSI_PIN 11
#define SPEED 50
MD_Parola myDisplay = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
#define buttonPin A0
int buttonVal;
int buttonState;
int LEDPin = 2;
int message = 0;
int lastButtonState = 1;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup() {
myDisplay.begin();
myDisplay.setIntensity(0);
myDisplay.displayClear();
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
buttonVal = digitalRead(buttonPin);
if (buttonVal != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (buttonVal != buttonState) {
buttonState = buttonVal;
if (buttonState == 0) { message = !message; }
}
}
if (message == 0) {
if (myDisplay.displayAnimate()) {
myDisplay.displayScroll("Andy is IN.", PA_CENTER, PA_SCROLL_LEFT, SPEED);
myDisplay.displayReset();
}
}
if (message == 1) {
if (myDisplay.displayAnimate()) {
myDisplay.displayScroll("Andy is OUT.", PA_CENTER, PA_SCROLL_LEFT, SPEED);
myDisplay.displayReset();
}
}
lastButtonState = buttonVal;
}
This seems to work okay, I modified the Arduino 'Debouncing' example from the IDE and applied what I have learned to this project. I'm sure it can be improved but as a beginner I still have a lot to learn, what do you think?
Very good. Does the button behave as you expect?
Yes, it does, I am very pleased with the result, and have learned a lot, however I am now going to use this project to explore and learn about characters, strings and things and try to apply them to this project. Thanks for your help
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.