117 lines
3.9 KiB
C++
117 lines
3.9 KiB
C++
// MicroController Test Board with 4-Digit-Display and one button
|
|
// https://github.com/faustech/mctestboard
|
|
// License: MIT License
|
|
|
|
#include <Arduino.h>
|
|
#include <Time.h>
|
|
#include <Thread.h>
|
|
#include <SevenSegmentExtended.h> // Extended TM1637 library https://github.com/bremme/arduino-tm1637
|
|
|
|
const byte PIN_CLK = 2; // Define CLK pin (for 4-Digit Display)
|
|
const byte PIN_DIO = 3; // Define DIO pin (for 4-Digit Display)
|
|
const byte buttonPin = 4; // Analog PIN Button (a 10K resistor is necessary)
|
|
const byte led = 13; // Default onboard LED
|
|
long buttonPressed = 0; // Button (PIN4) ms pressed counter
|
|
int buttonView = 0; // Button (PIN4) viewing state
|
|
long previousButtonMillis = 0; // Button (PIN4) previous press counter
|
|
|
|
long buttonInterval = 10000; // Interval for highlighting 4-Digit Display after pressing button
|
|
long buttonLongPress = 2500; // Interval for long press button action
|
|
long buttonCheck = 200; // Interval for checking button state
|
|
long ledPulse = 1000; // Interval for LED blinking pulse
|
|
bool displayOn = false; // Boolian for store display power status
|
|
|
|
// Threads:
|
|
Thread ledThread = Thread(); // Create thread for LED pulse indication
|
|
Thread pressButtonThread = Thread(); // Create thread for button state checking
|
|
|
|
SevenSegmentExtended display(PIN_CLK, PIN_DIO);
|
|
|
|
void setup() {
|
|
Serial.begin(9600); // initializes the Serial connection @ 9600 baud for debug
|
|
serStr("starting setup...");
|
|
|
|
pinMode(buttonPin, INPUT);
|
|
display.begin(); // Initializes the display
|
|
displayOn = true;
|
|
display.setBacklight(100); // Set the brightness to 100 %
|
|
display.print("INIT-INIT"); // Display INITx2 on the display
|
|
|
|
// LED blinking thread:
|
|
ledThread.onRun(ledBlink);
|
|
ledThread.setInterval(ledPulse); // Interval for LED blinking
|
|
// Button state cheking thread:
|
|
pressButtonThread.onRun(pressButton);
|
|
pressButtonThread.setInterval(buttonCheck); // Interval for checking button pressing
|
|
|
|
display.off();
|
|
displayOn = false;
|
|
serStr("...setup finished");
|
|
}
|
|
|
|
void loop() {
|
|
// Threads init:
|
|
if (ledThread.shouldRun())
|
|
ledThread.run();
|
|
if (pressButtonThread.shouldRun())
|
|
pressButtonThread.run();
|
|
}
|
|
|
|
// LED pulse blinking thread
|
|
void ledBlink() {
|
|
static bool ledStatus = false; // LED status bool
|
|
ledStatus = !ledStatus; // Invert LED state
|
|
digitalWrite(led, ledStatus); // Activate LED state
|
|
}
|
|
|
|
// 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;
|
|
serStr("display off after slack");
|
|
}
|
|
}
|
|
|
|
if (digitalRead(buttonPin) == HIGH) {
|
|
serStr("button pressed");
|
|
buttonPressed = buttonPressed + 200;
|
|
if (buttonPressed > buttonLongPress) {
|
|
buttonPressed = 0;
|
|
longPress();
|
|
}
|
|
previousButtonMillis = currentMillis;
|
|
display.on();
|
|
displayOn = true;
|
|
if (buttonView == 0) {
|
|
display.printTime(hour(), minute(), true);
|
|
serStr("time showed");
|
|
} else if (buttonView == 1) {
|
|
display.print("----");
|
|
serStr("something was showed");
|
|
}
|
|
if (buttonView < 1) buttonView += 1;
|
|
else buttonView = 0;
|
|
} else {
|
|
buttonPressed = 0;
|
|
}
|
|
}
|
|
|
|
// Send string to serial monitor with millis() counter
|
|
void serStr(const char* serString) {
|
|
long currentTime = millis();
|
|
String space = " ";
|
|
String stringToPrint = currentTime + space + serString;
|
|
Serial.println(stringToPrint);
|
|
}
|
|
|
|
// Send string to serial monitor with millis() counter
|
|
void longPress() {
|
|
serStr("long button pressed");
|
|
display.print("button was pressed long time");
|
|
} |