I am creating a 20x15 LED Matrix using and Arduino Nano. My goal is to switch between different "scenes," if you will, using 4 buttons (I just need to run different code to make it display something different). The four buttons share a ground, and they go into digital pins (I don't have resistors on the ground because of this article). Then in the code (under void loop
) there is one big if-then tree. For example, if (button1State == HIGH) {} else {if (button2State == HIGH)}
and so on and so forth.
When I connect the buttons, the lights flicker and glitch. Without the buttons, it goes to the last else
completely fine, so I know my light code works. To me, the code looks fine, but I want to see if there is something that I am doing wrong. I wonder if putting the code in voids instead of them in the if-thens in the loop would be better, but I don't know how I would invoke them.
If anyone could figure out what I am doing wrong or something that could make my code better, I would be super grateful.
Here is my full, current code (ignore all of the comments, I am designing the code so someone with no coding knowledge could understand it)
// HEY! LOOK UP HERE! I know it looks confusing, but don't worry, I'll help you! I'm Clippy!
// _________________
// < I can help you! >
// -----------------
// \
// \
// __
// / \
// | |
// @ @
// | |
// || |/
// || ||
// |\_/|
// \___/
//As you know, there are 4 buttons. When they are pressed, they display something different. We can make them display text as well.
//Below are VARIABLES, which control what text is displayed when buttons are pressed. Put what you want them to display between the
//quotation marks. If you don't want anything in a button, just put a space inbetween them.
//Note: only button 1 and 3 can display text, so lets only put text in those :)
String buttonText1 = " "; //Again, put what you want button 1 to display in text
//String buttonText2 = " ";
String buttonText3 = " ";
//String buttonText4 = " ";
String defaultText = " Text for the panel"; //This is what will show up when you first start it up
//Here we can adjust what the brightness of the panel is. We use a switch to do this. You can use values from 0 (lowest) to 255 (highest)
int switchLevel1 = 10;
int switchLevel2 = 200;
//Here we can control how many colors are on the backgrounds at one time. We can customize this to get some variety in our displays.
int colorsOnscreen1 = 5; //For button 1
//int colorsOnscreen2 = 3
int colorsOnscreen3 = 10; //For button 3
//int colorsOnscreen4 = 5;
int colorsOnscreenDefault = 3; //For when you start it up
int lastButton = 0; //Last button pressed stuff, gets fed to buttons
//----------------------------------------------------------------You Probably Don't Have To Worry About The Rest----------------------------------------------------------------
//This stuff you don't have to worry about, its for the board
#include <Adafruit_NeoPixel.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SPITFT.h>
#include <Adafruit_SPITFT_Macros.h>
#include <gfxfont.h>
#include <Adafruit_NeoMatrix.h>
#include <gamma.h>
#include "RGB.h"
#ifndef PSTR
#define PSTR
#include smileytongue24.h
#include yellowsmiley24.h
#endif
#define PIN 3 // This is the pin where the data is coming out (you don't have to worry about this)
const int button1 = 4; // the number of the arcade button pins
const int button2 = 5;
const int button3 = 6;
const int button4 = 7;
const int brightnessSwitch = 8;
// variables will change:
int button1State = 0; // variable for reading the pushbutton status
int button2State = 0;
int button3State = 0;
int button4State = 0;
int switchState = 0;
int panelBrightness = 0;
//This stuff tells the board how the lights are arranged. In our case, it starts at the top left, and
//the signal goes right, and zigzags the rest of the way. It also tells it that it is 20 by 15 LEDs
Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(20, 15, PIN,
NEO_MATRIX_TOP + NEO_MATRIX_RIGHT +
NEO_MATRIX_ROWS + NEO_MATRIX_ZIGZAG,
NEO_GRB + NEO_KHZ800);
void setup() {
// initialize the buttons as inputs:
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(brightnessSwitch, INPUT);
matrix.begin();
matrix.setTextWrap(false);
Serial.begin(9600);
Serial.print("The LED Matrix has started. Default will be activated soon");
}
String panelText = buttonText1; //This is what text gets fed to the panel
int x = matrix.width();
int pass = 0;
void loop() {
matrix.setBrightness(panelBrightness); //This sets the overall brightness. 255 is the highest, while 0 is the lowest
// read the state of the button:
button1State = digitalRead(button1);
button2State = digitalRead(button2);
button3State = digitalRead(button4);
button4State = digitalRead(button4);
switchState = digitalRead(brightnessSwitch);
if (switchState == HIGH){panelBrightness = switchLevel1;} else {panelBrightness = switchLevel2;} //Brightness Logic
// if-then statements
if (button1State == HIGH){ //BUTTON 1 CODE START
static int i = 0;
i++;
for(int y = 0; y < 15; y++)
for(int x = 0; x < 20; x++)
if(y & 1)
matrix.setPixelColor(19 - x + y * 20, matrix.gamma32(matrix.ColorHSV((x + y) * 256 * colorsOnscreen1 + i * 128 * 2)));
else
matrix.setPixelColor(x + y * 20, matrix.gamma32(matrix.ColorHSV((x + y) * 256 * colorsOnscreen1 + i * 128 * 2)));
matrix.setTextColor(matrix.Color(255, 255, 255)); //This determines the text color. If you want to set a text color, search in google "Color Picker" and use the RGB values in place of the numbers in (a,a,a)
matrix.setCursor(-((millis() / 40) & 127), 4);
matrix.print(buttonText1);
matrix.show();
}
//BUTTON 1 CODE END
else {
if (button2State == HIGH){ //BUTTON 2 CODE START
//PUT WHAT HAPPENS FOR BUTTON 2 HERE
//BUTTON 2 CODE END
} else {
if (button3State == HIGH){ //BUTTON CODE 3 START
static int i = 0;
i++;
for(int y = 0; y < 15; y++)
for(int x = 0; x < 20; x++)
if(y & 1)
matrix.setPixelColor(19 - x + y * 20, matrix.gamma32(matrix.ColorHSV((x - y) * 200 * colorsOnscreen3 + i * 128 * 2)));
else
matrix.setPixelColor(x + y * 20, matrix.gamma32(matrix.ColorHSV((x - y) * 256 * colorsOnscreen3 + i * 128 * 2)));
matrix.setTextColor(matrix.Color(255, 255, 255)); //This determines the text color. If you want to set a text color, search in google "Color Picker" and use the RGB values in place of the numbers in (a,b,c)
matrix.setCursor(-((millis() / 40) & 127), 4);
matrix.print(buttonText3);
matrix.show();
}
//BUTTON 3 CODE END
else {
if (button4State == HIGH){ //BUTTON 4 CODE START
//PUT WHAT HAPPENS FOR BUTTON 4 HERE
//BUTTON 4 CODE END
} else {
//WHAT HAPPENS UNTIL A BUTTON IS PRESSED
static int i = 0;
i++;
//matrix.fillScreen(0);
for(int y = 0; y < 15; y++)
for(int x = 0; x < 20; x++)
if(y & 1)
matrix.setPixelColor(19 - x + y * 20, matrix.gamma32(matrix.ColorHSV((x + y) * 256 * colorsOnscreenDefault + i * 128 * 2)));
else
matrix.setPixelColor(x + y * 20, matrix.gamma32(matrix.ColorHSV((x + y) * 256 * colorsOnscreenDefault + i * 128 * 2)));
matrix.setTextColor(matrix.Color(255, 255, 255)); //This determines the text color. If you want to set a text color, search in google "Color Picker" and use the RGB values in place of the numbers in (a,b,c)
matrix.setCursor(-((millis() / 70) & 127), 4);
matrix.println(defaultText);
matrix.show();
}}}}
}
Again, Thank You
-Micah