Hi All,
My first post! I've been playing with Arduinos for a while now and feel okay doing basic stuff. I'm scratching my head a bit on this one though and I am hoping someone can assist please?
I am using a sketch posted on this forum - MAX7219 Message Selector - #14 by StriderTR but I want the display to be blank to start with and then play the first message once when the button is pressed. Once that message has played, then clear the display and wait for the button to be pressed again to display the next message and so on...
I thought I could call a function to run the display animation called dismess() but I seem to have fallen at the first hurdle getting that to work!
Thanks in advance!
// Program to demonstrate the MD_Parola library
// button select canned messages
// MD_MAX72XX library can be found at https://github.com/MajicDesigns/MD_MAX72XX
// by groundFungus AKA c. goulding
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
const byte buttonPin = 2; // the pin that the pushbutton is attached to
// Define the number of devices we have in the chain and the hardware interface
// NOTE: These pin numbers will probably not work with your hardware and may
// need to be adapted
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4
#define CLK_PIN 13
#define DATA_PIN 11
#define CS_PIN 10
MD_Parola P = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
// individual messages in strings
const char msg_1[] = "1111";
const char msg_2[] = "2222";
const char msg_3[] = "3333";
const char msg_4[] = "4444";
const char msg_5[] = "5555";
const char msg_6[] = "6666";
// an array of pointers to the strings
char *messages[] = {msg_1, msg_2, msg_3, msg_4, msg_5, msg_6};
byte messageNum = sizeof(messages) / sizeof(messages[0]);
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup(void)
{
Serial.begin(115200);
Serial.println("\nParola pick a message program\n");
P.begin();
pinMode(buttonPin, INPUT_PULLUP);
}
void loop()
{
while(digitalRead(buttonPin)==HIGH)
{
; // wait for the button to be pressed
dismess() //call dismess() function
}
void dismess() {
if (P.displayAnimate()) // time to show next frame?
{
P.displayText(messages[buttonPushCounter], PA_CENTER, 30, 0, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
}
checkButton();
}
void checkButton()
{
static unsigned long timer = 0;
unsigned long interval = 50;
if (millis() - timer >= interval)
{
timer = millis();
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState)
{
if (buttonState == LOW)
{
// if the current state is LOW then the button
// went from off to on:
buttonPushCounter++; // add one to counter
// if counter over number of messages, reset the counter to message 0
if (buttonPushCounter >= messageNum)
{
buttonPushCounter = 0;
}
//Serial.println(buttonPushCounter);
}
}
lastButtonState = buttonState;
}
}
}
It seems I didn't word that well - Currently, the code starts running the first message as soon as it is powered up. I want it to wait until the button is pressed...
Then put the look for the button press inside the setup function. Only end the setup function once the button is pressed.
You might want to use a 'while' structure here.
// Program to demonstrate the MD_Parola library
// button select canned messages
// MD_MAX72XX library can be found at https://github.com/MajicDesigns/MD_MAX72XX
// by groundFungus AKA c. goulding
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
const byte buttonPin = 2; // the pin that the pushbutton is attached to
// Define the number of devices we have in the chain and the hardware interface
// NOTE: These pin numbers will probably not work with your hardware and may
// need to be adapted
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4
#define CLK_PIN 13
#define DATA_PIN 11
#define CS_PIN 10
MD_Parola P = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
// individual messages in strings
const char msg_1[] = "1111";
const char msg_2[] = "2222";
const char msg_3[] = "3333";
const char msg_4[] = "4444";
const char msg_5[] = "5555";
const char msg_6[] = "6666";
// an array of pointers to the strings
char *messages[] = {msg_1, msg_2, msg_3, msg_4, msg_5, msg_6};
byte messageNum = sizeof(messages) / sizeof(messages[0]);
int counter = 0; // counter for the number of button presses
//-------------------------------------------------------------------
void setup(void)
{
Serial.begin(115200);
Serial.println("\nParola pick a message program\n");
P.begin();
pinMode(buttonPin, INPUT_PULLUP);
}
//-------------------------------------------------------------------
void loop()
{
while (digitalRead(buttonPin) == LOW) {
if (digitalRead(buttonPin) == HIGH)
{
for (counter = 0; counter < 4096; counter++)
{
if (P.displayAnimate()) // time to show next frame?
{
P.displayText(messages[counter], PA_CENTER, 30, 0, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
}
}
P.begin();
}
}
}
Why a while loop inside of loop()?
Why does counter goes to from 0 to 4095 but the messages array only has 6 members?
Why do you call P.begin() in the loop() function?
Thank you! I found another example on the same site and have created this from it which almost works the way I want it to. Just a bit more thought required...