Hello, I could use some help w/ the TM1637.h library. It seems to randomely create issue when trying to save or upload sketches. Any help would be greatly appreciated. I've copied in my full code. To keep this clean, i'll post the error in a follow up message.
/*
Current version includes BLE Control code and Millis based clock.
*/
#include <ArduinoBLE.h>
int blueLedPin = 14;
int greenLedPin = 15;
int whiteLedPin = 16;
int redLedPin = 17;
byte signal1Value = 0;
byte oldSignal1Value = 0;
byte signal2Value = 1;
byte oldSignal2Value = 1;
const String periphID = "19B10000-E8F2-537E-4F6C-D104768A1214";
bool debug = 0; //Set to 1 and connect to serial monitor for debug mode
bool debug2 = 0; //Set to 1 to allow peripheral to continue sending values and for clock to keep going. 1 is the normal mode.
#include <TM1637.h>
int CLK = 3;
int DIO = 2;
TM1637 tm(CLK,DIO);
const int addMinute = 10;
const int subMinute = 11;
const int startWin = 12;
const int buzzer = 21;
int addMinuteCurrent = 1;
int subMinuteCurrent = 1;
int startWinCurrent = 1;
int addMinutePrior = 1;
int subMinutePrior = 1;
int startWinPrior = 1;
int startMin = 60;
int startSec = 00;
int minRemaining = 00;
int secRemaining = 00;
int countDown = 0;
int clkDelay = 1000;
unsigned long priorClockEvent = 0;
unsigned long buzzerStart = 0;
String str1 = String(startMin) + "00";
//*** SETTINGS ***
int gameStatus = 1; // 11 = Game Won, 10 = Jeapordy, 1 = Continue, 0 = Game Lost
const int autoWin = 1; // if 1, clock stops with solved game status
int clockSetupMode = 0;
int priorClockSetupMode = 0;
void setup() { //Clock Setup
pinMode(addMinute, INPUT_PULLUP);
pinMode(subMinute, INPUT_PULLUP);
pinMode(startWin, INPUT_PULLUP);
pinMode(buzzer, OUTPUT);
pinMode(blueLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(whiteLedPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
tm.init();
tm.setBrightness(5);
tm.colonOn();
tm.display(str1);
Serial.begin(9600);
if (debug) {while (!Serial);}
digitalWrite(redLedPin, HIGH);
while (!countDown) { //try moving setupClock to setup section of code
setupClock();
}
BLE.begin();// initialize the Bluetooth® Low Energy hardware
Serial.println("Bluetooth® Low Energy Central - LED control");
BLE.scanForUuid(periphID); // start scanning for peripherals
}
void loop(){
while (countDown) {
Serial.println("countdown timer on"); //Code update needed... Don't reprint this unless the timer status changes (happens if no device is connected and the clock doesn't run)
digitalWrite(whiteLedPin, LOW);
digitalWrite(redLedPin, HIGH);
BLEDevice peripheral = BLE.available(); // check if a peripheral has been discovered
if (peripheral) {
Serial.println("peripheral found");
BLE.stopScan();
readCharacteristics(peripheral);
BLE.scanForUuid(periphID); // peripheral disconnected, start scanning again
}
}
//DEV OPPORTUNITY: should this maintain peripheral connection even if timer is off???
}
void setupClock() {
if (round(millis()/1000)%2) {digitalWrite(whiteLedPin, HIGH);}
else (round(millis()/1000)%2) {digitalWrite(whiteLedPin, LOW);}
addMinuteCurrent = digitalRead(addMinute);
if (addMinuteCurrent != addMinutePrior) {
if (!addMinuteCurrent) {
startMin +=1;
Serial.println("add minute");
prntStr();
buzz(100);
delay(150);
}
}
addMinutePrior = addMinuteCurrent;
subMinuteCurrent = digitalRead(subMinute);
if (subMinuteCurrent != subMinutePrior) {
if (!subMinuteCurrent) {
startMin -=1;
Serial.println("subtract minute");
prntStr();
buzz(100);
delay(150);
}
}
subMinutePrior = subMinuteCurrent;
startWinCurrent = digitalRead(startWin);
if (startWinCurrent != startWinPrior) {
if (!startWinCurrent) {
buzz(250);
buzz(250);
buzz(250);
delay(150);
countDown = 1; //Count down started
digitalWrite(whiteLedPin, LOW);
Serial.println("start game");
priorClockEvent = millis();
}
}
startWinPrior = startWinCurrent;
}
void runClock() {
startWinCurrent=digitalRead(startWin);
if ((!startWinCurrent and gameStatus==11) or (!startSec and !startMin)) {
endGame();
}
if (millis() >= priorClockEvent + clkDelay) {
priorClockEvent += clkDelay;
if (!startSec and startMin) {
startMin-=1;
startSec=59;
prntStr();
} else {
startSec-=1;
prntStr();
}
}
}
void prntStr() {
if (startMin >= 10 and startSec >= 10) {
str1 = String(startMin) + String(startSec);
} else if (startMin >= 10 and startSec < 10) {
str1 = String(startMin) + "0" + String(startSec);
} else if (startMin < 10 and startSec >= 10) {
str1 = " " + String(startMin) + String(startSec);
} else {
str1 = " " + String(startMin) + "0" + String(startSec);
}
tm.display(str1);
}
void buzz(int ms) {
buzzerStart = millis();
while (millis() <= buzzerStart + ms) {
digitalWrite(buzzer,HIGH);
}
digitalWrite(buzzer,LOW);
}
void endGame() {
if (!debug2) {
buzz(150);
buzz(150);
}
prntStr();
countDown = 0;
blinkClock();
}
void blinkClock() {
while (debug2) {
buzzerStart = millis();
while (millis() <= buzzerStart + 750) {
tm.display(str1);
}
buzzerStart = millis();
while (millis() <= buzzerStart + 250) {
tm.display(" ");
}
}
}
void readCharacteristics(BLEDevice peripheral) {
peripheral.connect();
peripheral.discoverAttributes();
BLECharacteristic signal1 = peripheral.characteristic("19b10001-e8f2-537e-4f6c-d104768a1214");
BLECharacteristic signal2 = peripheral.characteristic("19b10002-e8f2-537e-4f6c-d104768a1214");
while (peripheral.connected()) {
signal1.readValue(signal1Value);
if (oldSignal1Value != signal1Value) {oldSignal1Value = signal1Value;}
Serial.print(signal1Value);
Serial.println(" = Signal 1 Value");
signal2.readValue(signal2Value);
if (oldSignal2Value != signal2Value) {oldSignal2Value = signal2Value;}
Serial.print(signal2Value);
Serial.println(" = Signal 2 Value");
if (signal1Value && signal2Value) { //WIN GAME
digitalWrite(blueLedPin, LOW);
digitalWrite(greenLedPin, HIGH);
digitalWrite(whiteLedPin, LOW);
digitalWrite(redLedPin, LOW);
gameStatus = 11;
Serial.println("gameStatus = 11 - Win Game");
if (autoWin) {endGame();}
clkDelay = 1000;
}
else if (signal1Value && !signal2Value) { //GAME AT RISK
digitalWrite(blueLedPin, HIGH);
digitalWrite(greenLedPin, LOW);
digitalWrite(whiteLedPin, HIGH);
digitalWrite(redLedPin, LOW);
gameStatus = 10;
Serial.println("gameStatus = 10 - Jeopardy");
clkDelay = 500;
}
else if (!signal1Value && signal2Value) { //CONTINUE GAME
digitalWrite(blueLedPin, LOW);
digitalWrite(greenLedPin, LOW);
digitalWrite(whiteLedPin, HIGH);
digitalWrite(redLedPin, LOW);
gameStatus = 1;
Serial.println("gameStatus = 01 - Continue Game");
clkDelay = 1000;
}
else if (!signal1Value && !signal2Value) { //LOSE GAME
digitalWrite(blueLedPin, LOW);
digitalWrite(greenLedPin, LOW);
digitalWrite(whiteLedPin, LOW);
digitalWrite(redLedPin, HIGH);
gameStatus = 0;
Serial.println("gameStatus = 00 - Lose Game");
buzz(1000);
endGame();
}
runClock();
}
Serial.println("Peripheral disconnected");
}