I know you guys are harsh to noobies who don't format their posts, pls be kind to me, I'll provide everything I can and I really appreciate the help!!
I'm writing code for a clock, the clock aspect is fully functional I'm adding another 'mode' as a countdown however whenever I call 'getTime()' it executes as shown in the serial monitor, it's executing but doesn't update the values of the curtime I cannot understand why, in the serial you can see it's 20:53 when in the corner the actual time is 20:56. If I switch it back to the clock it works no worries.
also pls forgive the spaghetti code.
#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
const unsigned char byte1[10] = {
0x83, 0x80, 0x8B, 0x8A, 0x8A, 0x0A, 0x0B, 0x80, 0x8B, 0x8A
};
const unsigned char byte2[10] = {
0xFF, 0xE1, 0xBF, 0xFF, 0xE5, 0xFF, 0xFF, 0xE7, 0xFF, 0xE7
};
byte tm_buffer[10] = {0x83, 0xED, 0x83, 0xFF, 0x0B, 0x3F, 0x8B, 0xED, 0x81, 0x0C};
// | Grid 1 | Grid 2 | Grid 3 | Grid 4 | Grid 5 |
//byte tm_buffer[10] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x0C};
// | Grid 1 | Grid 2 | Grid 3 | Grid 4 | Grid 5 |
int tm_dio = 9;
int tm_clk = 8;
int tm_stb = 7;
tmElements_t tm;
//INPUT BUTTONS
int button1 = 10;
int button2 = 11;
void setup() {
pinMode(13, OUTPUT);
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
Serial.begin(9600);
initial(2);
Serial.println("initialised");
writeBuffer();
delay(500);
}
int curTime[4] = {0,0,0,0}; //0,1,2
int AP = 1;
int mode = 0;
int var = 0;
int count = 0;
int longPress = 0;
void loop() { // 100Hz, 10Hz, 1Hz
if (mode == 0) { //clock
updateTimeDisplay();
}
if (mode == 1) { //timer
Countdown();
}
if (mode == 2) { //temp
}
if (digitalRead(button1) == LOW && var == 0) {
if (mode == 1 && digitalRead(button1) == LOW) {
longPress++;
}
if (mode != 1 || longPress == 30) { //Change this value to change how long to hold button for. 30*delay(50) gives ALSO MUST ALTER the value for confirming minutes in COUNTDOWN();
changeMode();
var = 1;
longPress = 0;
}
}
else if (digitalRead(button1) == HIGH) {
var = 0;
longPress = 0;
}
}
/*void put2digits(int number, int pos) {
if (number >=0 && number < 10) {
putDigitAt(number, pos);
putDigitAt(0, pos+1);
}
}*/
int timertime[4] = {0,0,0,0};
int fintime[4] = {0,0,0,0};
int timerrunning = 0;
int minutes = 0;
int milliscompare[2] = {0,0};
int change = 0;
void Countdown() {
if (timerrunning == 0) {
if (digitalRead(button2) == LOW) {
minutes++;
}
clearBuffer();
putDigitAt(minutes % 10,1);
putDigitAt((minutes / 10) % 10,0);
putDigitAt((minutes) / 100 % 10,2);
writeBuffer();
if (digitalRead(button1) == LOW) {
milliscompare[0] = millis();
change = 1;
}
else if (digitalRead(button1) == HIGH && change == 1) {
milliscompare[1] = millis();
if (milliscompare[1] - milliscompare[0] < 30 * 50) {
int overshot = 0;
timerrunning = 1;
timertime[3] = minutes % 10; //00:01
timertime[2] = (minutes / 10) % 10; //00:10
if (minutes > 59) {
overshot = (minutes / 60);
timertime[2] = ((minutes - (60 * overshot)) / 10) % 10; //00:10
timertime[1] = overshot % 10; //01:00
timertime[0] = (overshot / 10) % 10; //10:00
}
if (RTC.read(tm)) {
fintime[3] = (tm.Minute % 10) + timertime[3]; // 00:01
fintime[2] = ((tm.Minute / 10) % 10) + timertime[2]; // 00:10
fintime[1] = ((tm.Hour) % 10) + timertime[1]; // 01:00
fintime[0] = (((tm.Hour) / 10) % 10) + timertime[0]; // 10:00
if (fintime[3] > 9) {
fintime[2]++;
fintime[3] = fintime[3] - 10;
}
if (fintime[2] > 5) {
fintime[2] = fintime[2] - 6;
fintime[1]++;
}
if (fintime[1] > 9) {
fintime[1] = fintime[1] - 10;
fintime[0]++;
}
if (fintime[0] > 1 && fintime[1] > 3) {
fintime[0] = fintime[0] - 2;
fintime[1] = fintime[1] - 4;
}
}
}
change = 0;
}
}
else if (timerrunning == 1) {
getTime(24);
Serial.print(curTime[3]);
Serial.print(curTime[2]);
Serial.print(curTime[0]);
Serial.println(curTime[1]);
putDigitAt(fintime[0] - curTime[3], 3); // 10:00
putDigitAt(fintime[1] - curTime[2], 2); // 01:00
putDigitAt(fintime[2] - curTime[0], 0); // 00:10
putDigitAt(fintime[3] - curTime[1], 1); // 00:01
writeBuffer();
delay(1000);
//for (int i = 0
}
delay(50);
}
void changeMode() {
if (mode < 1) {
mode++;
}
else{
mode = 0;
}
while (digitalRead(button1) == LOW) {
}
}
void getTime(int type) {
Serial.println(type);
if (type == 12) {
if (tm.Hour > 12) {
curTime[2] = (tm.Hour - 12) % 10;
curTime[3] = ((tm.Hour - 12) / 10) % 10;
AP = 1;
}
else {
curTime[2] = (tm.Hour) % 10;
curTime[3] = ((tm.Hour) / 10) % 10;
AP = 0;
}
if (tm.Hour == 0) {
curTime[2] = 2;
curTime[3] = 1;
}
}
if (type == 24) {
Serial.println("WHY ARE YOU NOT EXECUTING THIS");
curTime[2] = tm.Hour % 10; // 01:00
curTime[3] = (tm.Hour / 10) % 10; // 10:00
AP = 3;
}
curTime[1] = tm.Minute % 10; // 00:01
curTime[0] = (tm.Minute / 10) % 10; // 00:10
}
void updateTimeDisplay() {
clearBuffer();
if (RTC.read(tm)) {
getTime(12);
if (AP == 3) {
}
else if (AP == 1 || tm.Hour == 12) {
bitWrite(tm_buffer[9], 6, 1);
}
else {
bitWrite(tm_buffer[9], 5, 1);
}
for (int i = 0; i < 4; i++) {
if (i == 3 && curTime[3] == 0) {}
else {
putDigitAt(curTime[i], i);
}
}
bitWrite(tm_buffer[4], 4, 1); //turn on centre dots
writeBuffer();
delay(1000);
}
}
void putDigitAt(int digit, int pos) {
tm_buffer[pos*2] = byte1[digit];
tm_buffer[(pos*2) + 1] = byte2[digit];
if (digit <=9 && digit >=4 && digit !=7 || digit == 0){
switch (pos){
case 0:
bitWrite(tm_buffer[8],7,1);
break;
case 1:
bitWrite(tm_buffer[9],2,1);
break;
case 2:
bitWrite(tm_buffer[9], 3, 1);
break;
case 3:
bitWrite(tm_buffer[8], 0, 1);
break;
}
}
else {
switch (pos) {
case 0:
bitWrite(tm_buffer[8],7,0);
break;
case 1:
bitWrite(tm_buffer[9],2,0);
break;
case 2:
bitWrite(tm_buffer[9], 3, 0);
break;
case 3:
bitWrite(tm_buffer[8], 0, 0);
break;
}
}
}
void initial(int intensity) {
pinMode(tm_dio, OUTPUT);
pinMode(tm_clk, OUTPUT);
pinMode(tm_stb, OUTPUT);
digitalWrite(tm_stb, HIGH);
digitalWrite(tm_clk, HIGH);
// delay(200);
tm_sendCommand(0x40); // command 2
digitalWrite(tm_stb, LOW);
tm_sendByte(0xc0); // command 3
for (int i = 0; i < 16; i++)
tm_sendByte(0x00); // clear RAM
digitalWrite(tm_stb, HIGH);
tm_sendCommand(0x03); // command 1
setIntensity(intensity);
}
void clearBuffer() {
for (int i = 0; i < 10; i++) {
tm_buffer[i] = 0x00;
}
}
void setIntensity(int intensity)
{
if (intensity < 0)
{
tm_sendCommand(0x80); // command 4
return;
}
tm_sendCommand(0x88 | (intensity % 8));
}
void writeBuffer() {
tm_sendCommand(0x40); // command 2
digitalWrite(tm_stb, LOW);
tm_sendByte(0xc0); // command 3
for (int i = 0; i < 16; i++){
tm_sendByte(tm_buffer[i]); // set RAM
}
digitalWrite(tm_stb, HIGH);
}
void tm_sendCommand(byte data)
{
digitalWrite(tm_stb, LOW);
tm_sendByte(data);
digitalWrite(tm_stb, HIGH);
}
void tm_sendByte(byte data)
{
for (int i = 0; i < 8; i++)
{
digitalWrite(tm_clk, LOW);
digitalWrite(tm_dio, data & 1 ? HIGH : LOW);
data >>= 1;
digitalWrite(tm_clk, HIGH);
}
}
void dp() {
bitWrite(tm_buffer[0], 4, 1);
}