115 lines
4.4 KiB
C++
115 lines
4.4 KiB
C++
// MicroController Test Board with 4-Digit-Display, one button and SD card module
|
|
// v0.3
|
|
// https://github.com/faustech/mctestboard
|
|
// License: MIT License
|
|
|
|
#include <Arduino.h>
|
|
#include <Time.h>
|
|
#include <Thread.h>
|
|
#include <SD.h> // Micro SD card
|
|
#include <SevenSegmentExtended.h> // DisplaySetting // Extended TM1637 library https://github.com/bremme/arduino-tm1637
|
|
const byte PIN_CLK = 2; // DisplaySetting // Define CLK pin (for 4-Digit Display)
|
|
const byte PIN_DIO = 3; // DisplaySetting // Define DIO pin (for 4-Digit Display)
|
|
bool displayOn = false; // DisplaySetting // Boolian for store display power status
|
|
SevenSegmentExtended display(PIN_CLK, PIN_DIO); // DisplaySetting // Display setup
|
|
|
|
const byte PIN_SDCS = 10; // MicroSDcardSetting // Define SD card SPI CS PIN
|
|
File file_log; // MicroSDcardSetting // File object for store log
|
|
|
|
const byte buttonPin = 4; // ButtonSetting // Define button pin (a 10K resistor is necessary)
|
|
long buttonPressed = 0; // ButtonSetting // Button ms pressed counter
|
|
long previousButtonMillis = 0; // ButtonSetting // Button previous press counter
|
|
byte buttonView = 0; // ButtonSetting // Button viewing state
|
|
int buttonInterval = 10000; // ButtonSetting // Interval for highlighting 4-Digit Display after pressing button
|
|
int buttonLongPress = 2500; // ButtonSetting // Interval for long press button action
|
|
int buttonCheck = 200; // ButtonSetting // Interval for checking button state
|
|
|
|
// Threads:
|
|
Thread pressButtonThread = Thread(); // Create thread for button state checking
|
|
|
|
void setup() {
|
|
Serial.begin(9600); // Initializes the Serial connection @ 9600 baud for debug
|
|
Serial.println("start");
|
|
display.begin(); // DisplaySetting // Initializes the display
|
|
displayOn = true; // Display On
|
|
display.setBacklight(100); // Display On - set the brightness to 100 %
|
|
display.print("INIT-INIT"); // Display "INIT"x2 on the display
|
|
pinMode(buttonPin, INPUT); // ButtonSetting
|
|
pinMode(PIN_SDCS, OUTPUT); // MicroSDcardSetting // switch SC pin to Output
|
|
if (!SD.begin(PIN_SDCS)) { // MicroSDcardSetting // Initialize SD card
|
|
Serial.println("no_SD");
|
|
}
|
|
|
|
// Button state cheking thread:
|
|
pressButtonThread.onRun(pressButton);
|
|
pressButtonThread.setInterval(buttonCheck); // Interval for checking button pressing
|
|
|
|
display.off(); // Display Off
|
|
displayOn = false; // Display Off
|
|
sendStr("setup_done");
|
|
}
|
|
|
|
void loop() {
|
|
// Threads init:
|
|
if (pressButtonThread.shouldRun())
|
|
pressButtonThread.run();
|
|
}
|
|
|
|
// Check button pressing thread
|
|
void pressButton() {
|
|
// Display off after buttonInterval ms slack
|
|
unsigned long currentMillis = millis();
|
|
if (currentMillis - previousButtonMillis > buttonInterval) {
|
|
previousButtonMillis = currentMillis;
|
|
if (displayOn == true) {
|
|
display.off();
|
|
displayOn = false;
|
|
sendStr("disp_off");
|
|
}
|
|
}
|
|
|
|
if (digitalRead(buttonPin) == HIGH) {
|
|
sendStr("press_butt");
|
|
buttonPressed = buttonPressed + 200;
|
|
if (buttonPressed > buttonLongPress) {
|
|
buttonPressed = 0;
|
|
longPress();
|
|
}
|
|
previousButtonMillis = currentMillis;
|
|
display.on();
|
|
displayOn = true;
|
|
if (buttonView == 0) {
|
|
display.printTime(hour(), minute(), true);
|
|
sendStr("show_time");
|
|
} else if (buttonView == 1) {
|
|
display.print("----");
|
|
sendStr("show_----");
|
|
}
|
|
if (buttonView < 1) buttonView += 1;
|
|
else buttonView = 0;
|
|
} else {
|
|
buttonPressed = 0;
|
|
}
|
|
}
|
|
|
|
// Send string to serial monitor with millis() counter and date/time
|
|
void sendStr(String serString) {
|
|
long millisTime = millis();
|
|
String delimiter = "|";
|
|
String currentTime = "";
|
|
currentTime = currentTime + year() + "-" + month() + "-" + day() + "-" + hour() + "-" + minute() + "-" + second();
|
|
String stringToPrint = millisTime + delimiter + currentTime + delimiter + serString;
|
|
Serial.println(stringToPrint);
|
|
file_log = SD.open("log.txt", FILE_WRITE);
|
|
if (file_log) {
|
|
file_log.println(stringToPrint);
|
|
file_log.close();
|
|
}
|
|
}
|
|
|
|
void longPress() {
|
|
String longPressStr = "pressed_ms|";
|
|
sendStr(longPressStr + buttonLongPress);
|
|
display.print("butt_long");
|
|
}
|