Hi everyone,
I have a LED Matrix and 2 buttons on digital pins 3 and 4 and three pictogram's (icons) A,B,C (will be more than 3) to scroll trough by pressing button 1 (display next or forward) or 2 (display previous or backward).
With help on a old thread I manage to use button 1 to display the pictogram's from A to B to C for each press but I want to implement the button 2 to display the pictogram's on the reverse order.
Here is the code I have:
/*
This example code is in the public domain.
http://arduino.cc/en/Tutorial/ButtonStateChange
*/
//#include <TinyWireM.h>
#include <Wire.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
const int button1Pin = 3; // button pin on RePhone pad A1
const int button2Pin = 4; // button pin on RePhone pad E2
Adafruit_8x8matrix matrix = Adafruit_8x8matrix();
// Variables will change:
int button1PushCounter = 0; // counter for the number of button1 presses
int button1State = 0; // current state of the button1
int lastButton1State = 0; // previous state of the button1
int button2PushCounter = 0; // counter for the number of button2 presses
int button2State = 0; // current state of the button2
int lastButton2State = 0; // previous state of the button2
void setup() {
// initialize the button pin as a input:
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
matrix.begin(0x70); // pass in the address
}
static const uint8_t PROGMEM // X and square bitmaps
smile_bmp[] =
{ B00111100,
B01000010,
B10100101,
B10000001,
B10100101,
B10011001,
B01000010,
B00111100
},
hearth_bmp[] =
{ B00100100,
B01011010,
B10011001,
B10000001,
B10000001,
B01000010,
B00100100,
B00011000
},
flower_bmp[] =
{ B11000011,
B10100101,
B01100110,
B00011000,
B01111011,
B10101110,
B11001100,
B00001000
};
void loop() {
// read the pushbutton input pin:
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
// compare the buttonState to its previous state
if (button1State != lastButton1State) {
// if the state has changed, increment the counter
if (button1State == HIGH && lastButton1State == LOW) {
// if the current state is HIGH then the button
// went from off to on:
button1PushCounter++;
} else {
if (button1State != lastButton1State) {
// if the state has changed, increment the counter
if (button2State == HIGH && lastButton2State == LOW) {
// if the current state is HIGH then the button
// went from off to on:
button2PushCounter--;
}
}
// save the current state as the last state,
//for next time through the loop
lastButton1State = button1State;
lastButton2State = button2State;
// turns on the LED every four button pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
switch (button1PushCounter % 3)
{
case 0:
matrix.clear();
matrix.drawBitmap(0, 0, hearth_bmp, 8, 8, LED_ON);
matrix.writeDisplay();
button1State = HIGH;
break;
case 1:
matrix.clear();
matrix.drawBitmap(0, 0, smile_bmp, 8, 8, LED_ON);
matrix.writeDisplay();
button1State = HIGH;
break;
case 2:
matrix.clear();
matrix.drawBitmap(0, 0, flower_bmp, 8, 8, LED_ON);
matrix.writeDisplay();
button1State = HIGH;
}
}
Here is how I interpret in my logic (poor brain):
Variables are keeping track of the button presses for each buton
// Variables will change:
int button1PushCounter = 0; // counter for the number of button1 presses
int button1State = 0; // current state of the button1
int lastButton1State = 0; // previous state of the button1
int button2PushCounter = 0; // counter for the number of button2 presses
int button2State = 0; // current state of the button2
int lastButton2State = 0; // previous state of the button2
and I should use somehow this button2PushCounter--; to display the previous pictogram when button 2 is pressed. It works great in one direction but I can not make it to go to previous with button 2 press.
So I have a variable Counter that keeps track of button presses and now when I detect a button 2 press should change the number in Counter variable by -1 and then draw on the matrix the corresponding pictogram ...
Any hint or help is appreciated.
Thanks!