I made a sketch which worked perfect. However, i decided i want a pushbutton to switch two functions. This kinda works. However it seems that the push is not always registered. Another thing i noticed is when i keep the button pressed it keeps toggling the functions. I would expect it to toggle when i release the button. Can someone point me in the right direction?
#include <TM1637Display.h>
#include <SoftwareSerial.h>
#include <TinyGPS++.h>
#include <Timezone.h>
#define CLK 2
#define DIO 3
#define rxPin 5
#define txPin 6
#define BUTTON_PIN 7
#define LED_PIN LED_BUILTIN
// Change these two rules corresponding to your timezone, see https://github.com/JChristensen/Timezone
//Central European Time (Frankfurt, Paris) 120 = +2 hours in daylight saving time (summer).
// Central European Time
TimeChangeRule CEST = { "CEST", Last, Sun, Mar, 2, 120 }; // Central European Summer Time
TimeChangeRule CET = { "CET ", Last, Sun, Oct, 3, 60 }; // Central European Standard Time
Timezone CE(CEST, CET);
TimeChangeRule *tcr;
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
TM1637Display display = TM1637Display(CLK, DIO);
const uint8_t SEG_GPS[] = {
SEG_D, // _
SEG_A | SEG_C | SEG_D | SEG_E | SEG_F, // G
SEG_A | SEG_B | SEG_E | SEG_F | SEG_G, // P
SEG_A | SEG_C | SEG_D | SEG_F | SEG_G // S
};
const uint8_t SEG_TEST[] = {
SEG_F | SEG_G | SEG_E | SEG_D, // _
SEG_A | SEG_D | SEG_E | SEG_F | SEG_G, // G
SEG_A | SEG_C | SEG_D | SEG_F | SEG_G, // P
SEG_F | SEG_G | SEG_E | SEG_D, // S
};
TinyGPSPlus gps;
int ldrpin = 0;
int value = 0;
//int button = 7;
int status = false;
int led = 13;
void setup() {
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
pinMode(LED_PIN, OUTPUT);
mySerial.begin(9600);
//mySerial.begin(57600);
display.setBrightness(7);
display.setSegments(SEG_GPS);
setTime(00, 00, 00, 01, 01, 1970);
}
void loop() {
value = analogRead(ldrpin);
value = constrain(value, 500, 1500);
value = map(value, 500, 1500, 1, 7);
display.setBrightness(value);
//updateTime();
// run over and over
//while (mySerial.available() > 0)
//if (gps.encode(mySerial.read())){
// setDisplay();
static byte toggle_sw_memmory = 0;
// Check for keypress
if (!digitalRead(BUTTON_PIN)) { // Pulled up so zero = pushed.
delay(200);
}
if (!digitalRead(BUTTON_PIN)) { // if it is still pushed after a delay.
toggle_sw_memmory = !toggle_sw_memmory;
}
if (toggle_sw_memmory) {
digitalWrite(LED_PIN, HIGH);
while (mySerial.available() > 0)
if (gps.encode(mySerial.read())){
// setDisplay();
setDisplayTime();}
}
if (!toggle_sw_memmory){
digitalWrite(LED_PIN, LOW);
while (mySerial.available() > 0)
if (gps.encode(mySerial.read())){
// setDisplay();
setDisplay();}
}
}
void updateTime(void) {
if (gps.time.isValid() && gps.date.isValid()) {
setTime(gps.time.hour(), gps.time.minute(), gps.time.second(), gps.date.day(), gps.date.month(), gps.date.year());
}
}
void setDisplay() {
if (gps.speed.isValid()) {
double speed = gps.speed.kmph();
if (speed > 10) {
display.showNumberDec(speed, false, 4, 0);
return;
}
}
if (gps.time.isValid()) {
time_t utc = now();
time_t local = CE.toLocal(utc, &tcr);
int minutes = gps.time.minute();
display.showNumberDec(minutes, true, 2, 2);
int hours = gps.time.hour();
updateTime();
hours = hours + 1;
//hours = hours+1;
if (hours > 24)
hours = hours - 24;
// if-else to blink te colon
int seconds = gps.time.second();
if ((seconds % 2) == 0) {
display.showNumberDecEx(hour(local), 0b01000000, true, 2, 0);
} else {
display.showNumberDecEx(hour(local), 0, true, 2, 0);
}
}
}
void setDisplayTime() {
if (gps.time.isValid()) {
time_t utc = now();
time_t local = CE.toLocal(utc, &tcr);
int minutes = gps.time.minute();
display.showNumberDec(minutes, true, 2, 2);
int hours = gps.time.hour();
updateTime();
hours = hours + 1;
//hours = hours+1;
if (hours > 24)
hours = hours - 24;
// if-else to blink te colon
int seconds = gps.time.second();
if ((seconds % 2) == 0) {
display.showNumberDecEx(hour(local), 0b01000000, true, 2, 0);
} else {
display.showNumberDecEx(hour(local), 0, true, 2, 0);
}
}
}