I am slowly building my code up to have a fully functioning, adjustable, multi-mode chess clock; right now I have 2 led displays and 2 buttons hooked up.
I am just trying to make it so that when I press a button, one of the screens stops and the other starts; and this doesn't reverse until the other button is pressed.
Right now button1 only works if its pressed first and even then it stops working after button2 is pressed. Button2 responds normally according to the serial output. I'm not sure what part of the code is important to show so I will show it all.
#include <Metro.h>
Metro disp1Metro = Metro(1000);
Metro disp2Metro = Metro(1000);
int dataPin = 2; // connect to MOSI on both displays
int clockPin =3; // connect to SCK on both displays
int CS1 = 4; // connect to CS on first display
int CS2 = 5; // connect to CS on second display
int num1 = 0; //number to be displayed on CS1
int num2 = 0; //number to be displayed on CS2
int inPin1 = 7; // switch connected to digital pin 7
int inPin2 = 8; // switch connected to digital pin 8
int button1 = 0; // variable to store the read value of button1
int button2 = 0; // variable to store the read value of button2
int hold1 = 0; // holds compounded button value
int hold2 = 0; // holds compounded button value
void setup (void) {
pinMode (dataPin, OUTPUT);
pinMode (clockPin, OUTPUT);
pinMode (CS1, OUTPUT);
pinMode (CS2, OUTPUT);
digitalWrite (CS2, HIGH); // deselect second display
digitalWrite (CS1, LOW); // select first display
shiftOut(dataPin, clockPin, MSBFIRST, num1);
shiftOut(dataPin, clockPin, MSBFIRST, num1);
shiftOut(dataPin, clockPin, MSBFIRST, num1);
shiftOut(dataPin, clockPin, MSBFIRST, num1);
//shiftOut(dataPin, clockPin, MSBFIRST, 0x7A); special character for setting brightness
//shiftOut(dataPin, clockPin, MSBFIRST, 0x00); full brightness (0x00) and dimmest (0xFF)
digitalWrite (CS1, HIGH); // deselect first display
digitalWrite (CS2, LOW); // select second display
shiftOut(dataPin, clockPin, MSBFIRST, num2);
shiftOut(dataPin, clockPin, MSBFIRST, num2);
shiftOut(dataPin, clockPin, MSBFIRST, num2);
shiftOut(dataPin, clockPin, MSBFIRST, num2);
//shiftOut(dataPin, clockPin, MSBFIRST, 0x7A); special character for setting brightness
//shiftOut(dataPin, clockPin, MSBFIRST, 0x00); full brightness (0x00) and dimmest (0xFF)
pinMode(inPin1, INPUT); // sets digital pin 7 as input
pinMode(inPin2, INPUT); // sets digital pin 8 as input
Serial.begin(9600);
}
void loop (void) {
button1 = digitalRead(inPin1); // read the input pin for button 1
button2 = digitalRead(inPin2); // read the input pin for button 2
hold1 += button1;
hold2 += button2;
if(button1==1){
Serial.println("Button 1 pressed");
Serial.println(hold1);
}
if(button2==1){
Serial.println("Button 2 pressed");
Serial.println(hold2);
}
if(hold2 >= 1){
hold1 = 0;
}
if(hold1 >= 1){
hold2 = 0;
}
if(hold1 >= 1){
if(disp1Metro.check() == 1) {
digitalWrite (CS2, HIGH); // deselect second display
digitalWrite (CS1, LOW); // select first display
shiftOut(dataPin, clockPin, MSBFIRST, num1);
shiftOut(dataPin, clockPin, MSBFIRST, num1);
shiftOut(dataPin, clockPin, MSBFIRST, num1);
shiftOut(dataPin, clockPin, MSBFIRST, num1);
num1++;
if(num1 == 10){
num1 = 0;
}
}
}
if(hold2 >= 1){
if(disp2Metro.check() == 1) {
digitalWrite (CS1, HIGH); // deselect first display
digitalWrite (CS2, LOW); // select second display
shiftOut(dataPin, clockPin, MSBFIRST, num2);
shiftOut(dataPin, clockPin, MSBFIRST, num2);
shiftOut(dataPin, clockPin, MSBFIRST, num2);
shiftOut(dataPin, clockPin, MSBFIRST, num2);
num2++;
if(num2 == 10){
num2 = 0;
}
}
}
}