I have an LED display and I am trying to control it with a button in a digital input. Right now I have three patterns in the LED display. What I want to happen is anytime I press the button the display will advance one pattern. For example, it starts on pattern 1, when I press it it goes to pattern two, when pressed again it goes to pattern 3, and when pressed again it goes to pattern 1 again, etc.
With the code I have now it looks like it's just cycling super fast through all the patterns when the button is pressed and stops when i stop pressing it. Is there a way in code so that when I press the button it will only advance one pattern?
Thank you in advance for your help, I put my code below.
int buttonPin7 = 7;
int buttonState7 = 1;
int display1 = 1;
void setup()
{
#ifndef __AVR_ATtiny85__
// Serial.begin(9600);
// Serial.println("7 Segment Backpack Test");
#endif
matrix.begin(0x70);
pinMode(buttonPin7, INPUT);
}
void loop()
{
// unsigned long currentMillis = millis();
buttonState7 = digitalRead(buttonPin7);
if ( (buttonState7 == HIGH) && (display1 < 4) ) {
display1 = display1 + 1;
}
if ( (buttonState7 == HIGH) && (display1 > 4) )
{
display1 = 1;
}
if (display1 >= 4) {
display1 =1;
}
if (display1 == 1) {
matrix.clear();
matrix.writeDigitRaw(0,1); // LED pattern 1
matrix.writeDigitRaw(1,1);
matrix.writeDigitRaw(3,1);
matrix.writeDigitRaw(4,1);
matrix.writeDisplay();
}
if (display1 == 2)
{
matrix.clear();
matrix.writeDigitRaw(0,64);
matrix.writeDigitRaw(1,64); // LED pattern 2
matrix.writeDigitRaw(3,64);
matrix.writeDigitRaw(4,64);
matrix.writeDisplay();
}
if (display1 == 3)
{
matrix.clear();
matrix.writeDigitRaw(0,8);
matrix.writeDigitRaw(1,8); // LED pattern 3
matrix.writeDigitRaw(3,8);
matrix.writeDigitRaw(4,8);
matrix.writeDisplay();
}
}
I'm sure this isn't the best way to do this, but it's the way I've been doing it ever since I got my Arduino.
const int buttonPin = 2; // change this number to your button's input pin
void setup() {
pinMode(buttonPin, 0);
}
void loop() {
if (digitalRead(buttonPin) == LOW) {
Serial.println("Button was pressed");
delay(250); // this number can be changed if you experience 'double clicking'
}
}
You need to look for a cha he in the button state. This involves a variable often called lastState that you can use to compair if the button has changed since the last time you looked at it. There is an example of this change given in the IDE examples.
Using delay is just papering over the cracks, it will still repeat as long as the button is held down.
Especially the parts Detecting transitions, and Debouncing Without Delay
Then you will have to do something like this:
//top of your code
uint8_t sequenceId = 0;
//when button is pressed
if ( button_was_pressed )
{
// increase sequenceId, and reset if greater than 2 (so it goes 0, 1, 2, 0, 1, 2 etc... )
if ( ++sequenceId > 2 )
sequenceId = 0;
// here is where you should clear and write the matrix.
// this is a good idea to use a switch statement here, instead of your multiple if statements
}
Thank you everyone for your replies.
Grumpy_mike, I think the example you were referring to was this:
That example was really helpful, I basically modified it and was able to solve my issues with that. I put my code below.
Thanks again
#include <Wire.h> // Enable this line if using Arduino Uno, Mega, etc.
//#include <TinyWireM.h> // Enable this line if using Adafruit Trinket, Gemma, etc.
// D, or SDA, goes to analog 4
//C, or SCL, goes to analog 5
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
Adafruit_7segment matrix = Adafruit_7segment();
int buttonPin7 = 7; // the pin that the pushbutton is attached t0
// Variables will change:
int display1 = 0; // counter for the display pattern
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
#ifndef __AVR_ATtiny85__
#endif
matrix.begin(0x70);
// initialize the button pin as a input:
pinMode(buttonPin7, INPUT);
// initialize the LED as an output:
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin7);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
display1++;
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
if (display1 >= 4) {
display1 =1;
}
if (display1 == 1) {
matrix.clear();
matrix.writeDigitRaw(0,1); // LED pattern 1
matrix.writeDigitRaw(1,1);
matrix.writeDigitRaw(3,1);
matrix.writeDigitRaw(4,1);
matrix.writeDisplay();
}
if (display1 == 2)
{
matrix.clear();
matrix.writeDigitRaw(0,64);
matrix.writeDigitRaw(1,64); // LED pattern 2
matrix.writeDigitRaw(3,64);
matrix.writeDigitRaw(4,64);
matrix.writeDisplay();
}
if (display1 == 3)
{
matrix.clear();
matrix.writeDigitRaw(0,8);
matrix.writeDigitRaw(1,8); // LED pattern 3
matrix.writeDigitRaw(3,8);
matrix.writeDigitRaw(4,8);
matrix.writeDisplay();
}
}