so i have been working on this project (led marquee fanny pack for my wife) and cant seem to get it figured out and would love to get some coding help i am very very new to this and only have until 5/15/17 to make this work
i have a ws2812b matrix 24w x 7h starting in top left serpentine layout which i can get to scroll text.
what i am trying to accomplish is be able to change the message at any time by pressing the button currently it will only change messaged if i catch it at the right time at the end of the current cycle.
below is my full code and i would love some help in getting it right and working as desired.
#include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h>
#include <Adafruit_NeoPixel.h>
// MATRIX DECLARATION:
// Parameter 1 = width of NeoPixel matrix
// Parameter 2 = height of matrix
// Parameter 3 = pin number (most are valid)
// Parameter 4 = matrix layout flags, add together as needed:
// NEO_MATRIX_TOP, NEO_MATRIX_BOTTOM, NEO_MATRIX_LEFT, NEO_MATRIX_RIGHT:
// Position of the FIRST LED in the matrix; pick two, e.g.
// NEO_MATRIX_TOP + NEO_MATRIX_LEFT for the top-left corner.
// NEO_MATRIX_ROWS, NEO_MATRIX_COLUMNS: LEDs are arranged in horizontal
// rows or in vertical columns, respectively; pick one or the other.
// NEO_MATRIX_PROGRESSIVE, NEO_MATRIX_ZIGZAG: all rows/columns proceed
// in the same order, or alternate lines reverse direction; pick one.
// See example below for these values in action.
// Parameter 5 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
int matrixW = 24;
int matrixH = 7;
#define PIN 1 // OUTPUT PIN FROM ARDUINO TO MATRIX D-In
#define BUTTON_PIN 2
Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(matrixW, matrixH, PIN,
NEO_MATRIX_TOP + NEO_MATRIX_LEFT +
NEO_MATRIX_ROWS + NEO_MATRIX_ZIGZAG,
NEO_GRB + NEO_KHZ800);
bool oldState = HIGH;
int showType = 0;
const uint16_t colors[] = {
matrix.Color(255, 0, 0), matrix.Color(0, 255, 0), matrix.Color(255, 255, 0), matrix.Color(0, 0, 255), matrix.Color(255, 0, 255), matrix.Color(0, 255, 255), matrix.Color(255, 255, 255)
};
#define arr_len( x ) ( sizeof( x ) / sizeof( *x ) ) // Calculation of Array Size;
int pixelPerChar = 5; // Width of Standard Font Characters is 8X6 Pixels
int x = matrix.width(); // Width of the Display
int pass = 0; // Counter
int i = 0; // Counter
int clr = 0; // Counter for Indexing Array of Colors
char msg[] = "-------------------"; // BLANK Message of Your Choice;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
matrix.begin();
matrix.setTextWrap(false);
matrix.setBrightness(5);
matrix.setTextColor(colors[0]);
matrix.show();
}
void loop() {
// Get current button state.
bool newState = digitalRead(BUTTON_PIN);
// Check if state changed from high to low (button press).
if (newState == LOW && oldState == HIGH) {
// Short delay to debounce button.
delay(20);
// Check if button is still low after debounce.
newState = digitalRead(BUTTON_PIN);
if (newState == LOW) {
showType++;
if (showType > 2)
showType = 0;
startShow(showType);
}
}
startShow(showType);
// Set the last button state to the old state.
oldState = newState;
}
void startShow(int i) {
switch (i) {
case 0: Text();
break;
case 1: Text1();
break;
case 2: Text2();
break;
}
}
void Text() {
writeText("Happy");
}
void Text1() {
writeText("Sad");
}
void Text2() {
writeText("Fine"); // SCROLLING Message of Your Choice;
}
void writeText(String msg) {
int arrSize = arr_len( colors ); // Array of Text Colors;
int msgSize = (msg.length() * pixelPerChar) + (2 * pixelPerChar); // CACULATE message length;
int scrollingMax = (msgSize) + matrix.width(); // ADJUST Displacement for message length;
x = matrix.width(); // RESET Cursor Position and Start Text String at New Position on the Far Right;
clr = 0; // RESET Color/Repeat Index;
while (clr <= arrSize) {
/* Change Color with Each Pass of Complete Message */
matrix.setTextColor(colors[clr]);
matrix.fillScreen(0); // BLANK the Entire Screen;
matrix.setCursor(x, 0); // Set Starting Point for Text String;
matrix.print(msg); // Set the Message String;
/* SCROLL TEXT FROM RIGHT TO LEFT BY MOVING THE CURSOR POSITION */
if (--x < -scrollingMax ) {
/* ADJUST FOR MESSAGE LENGTH */
// Decrement x by One AND Compare New Value of x to -scrollingMax;
// This Animates (moves) the text by one pixel to the Left;
x = matrix.width(); // After Scrolling by scrollingMax pixels, RESET Cursor Position and Start String at New Position on the Far Right;
++clr; // INCREMENT COLOR/REPEAT LOOP COUNTER AFTER MESSAGE COMPLETED;
}
matrix.show(); // DISPLAY the Text/Image
delay(30); // SPEED OF SCROLLING or FRAME RATE;
}
clr = 0; // Reset Color/Loop Counter;
}