add SD card module for logs

This commit is contained in:
ivan 2018-06-02 17:11:34 +03:00
parent 9f3a7fe9a2
commit 5848719822
4 changed files with 52 additions and 55 deletions

View File

@ -1,3 +1,3 @@
Arduino Sketch for MicroController Test Board with 4-Digit-Display and one button Arduino Sketch for MicroController Test Board with 4-Digit-Display, one button and SD card module
![mctestboard](https://raw.githubusercontent.com/faustech/mctestboard/master/mctestboard_bb.jpg) ![mctestboard](https://raw.githubusercontent.com/faustech/mctestboard/master/mctestboard_bb.jpg)

Binary file not shown.

View File

@ -1,72 +1,62 @@
// MicroController Test Board with 4-Digit-Display and one button // MicroController Test Board with 4-Digit-Display, one button and SD card module
// v0.3
// https://github.com/faustech/mctestboard // https://github.com/faustech/mctestboard
// License: MIT License // License: MIT License
#include <Arduino.h> #include <Arduino.h>
#include <Time.h> #include <Time.h>
#include <Thread.h> #include <Thread.h>
#include <SevenSegmentExtended.h> // Extended TM1637 library https://github.com/bremme/arduino-tm1637 #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_CLK = 2; // Define CLK pin (for 4-Digit Display) const byte PIN_SDCS = 10; // MicroSDcardSetting // Define SD card SPI CS PIN
const byte PIN_DIO = 3; // Define DIO pin (for 4-Digit Display) File file_log; // MicroSDcardSetting // File object for store log
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 const byte buttonPin = 4; // ButtonSetting // Define button pin (a 10K resistor is necessary)
long buttonLongPress = 2500; // Interval for long press button action long buttonPressed = 0; // ButtonSetting // Button ms pressed counter
long buttonCheck = 200; // Interval for checking button state long previousButtonMillis = 0; // ButtonSetting // Button previous press counter
long ledPulse = 1000; // Interval for LED blinking pulse byte buttonView = 0; // ButtonSetting // Button viewing state
bool displayOn = false; // Boolian for store display power status 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: // Threads:
Thread ledThread = Thread(); // Create thread for LED pulse indication
Thread pressButtonThread = Thread(); // Create thread for button state checking Thread pressButtonThread = Thread(); // Create thread for button state checking
SevenSegmentExtended display(PIN_CLK, PIN_DIO);
void setup() { void setup() {
Serial.begin(9600); // initializes the Serial connection @ 9600 baud for debug Serial.begin(9600); // Initializes the Serial connection @ 9600 baud for debug
serStr("starting setup..."); 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");
}
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: // Button state cheking thread:
pressButtonThread.onRun(pressButton); pressButtonThread.onRun(pressButton);
pressButtonThread.setInterval(buttonCheck); // Interval for checking button pressing pressButtonThread.setInterval(buttonCheck); // Interval for checking button pressing
display.off(); display.off(); // Display Off
displayOn = false; displayOn = false; // Display Off
serStr("...setup finished"); sendStr("setup_done");
} }
void loop() { void loop() {
// Threads init: // Threads init:
if (ledThread.shouldRun())
ledThread.run();
if (pressButtonThread.shouldRun()) if (pressButtonThread.shouldRun())
pressButtonThread.run(); 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 // Check button pressing thread
void pressButton() { void pressButton() {
// Display off after buttonInterval ms slack // Display off after buttonInterval ms slack
unsigned long currentMillis = millis(); unsigned long currentMillis = millis();
if (currentMillis - previousButtonMillis > buttonInterval) { if (currentMillis - previousButtonMillis > buttonInterval) {
@ -74,12 +64,12 @@ void pressButton() {
if (displayOn == true) { if (displayOn == true) {
display.off(); display.off();
displayOn = false; displayOn = false;
serStr("display off after slack"); sendStr("disp_off");
} }
} }
if (digitalRead(buttonPin) == HIGH) { if (digitalRead(buttonPin) == HIGH) {
serStr("button pressed"); sendStr("press_butt");
buttonPressed = buttonPressed + 200; buttonPressed = buttonPressed + 200;
if (buttonPressed > buttonLongPress) { if (buttonPressed > buttonLongPress) {
buttonPressed = 0; buttonPressed = 0;
@ -90,10 +80,10 @@ void pressButton() {
displayOn = true; displayOn = true;
if (buttonView == 0) { if (buttonView == 0) {
display.printTime(hour(), minute(), true); display.printTime(hour(), minute(), true);
serStr("time showed"); sendStr("show_time");
} else if (buttonView == 1) { } else if (buttonView == 1) {
display.print("----"); display.print("----");
serStr("something was showed"); sendStr("show_----");
} }
if (buttonView < 1) buttonView += 1; if (buttonView < 1) buttonView += 1;
else buttonView = 0; else buttonView = 0;
@ -102,16 +92,23 @@ void pressButton() {
} }
} }
// Send string to serial monitor with millis() counter // Send string to serial monitor with millis() counter and date/time
void serStr(const char* serString) { void sendStr(String serString) {
long currentTime = millis(); long millisTime = millis();
String space = " "; String delimiter = "|";
String stringToPrint = currentTime + space + serString; String currentTime = "";
currentTime = currentTime + year() + "-" + month() + "-" + day() + "-" + hour() + "-" + minute() + "-" + second();
String stringToPrint = millisTime + delimiter + currentTime + delimiter + serString;
Serial.println(stringToPrint); Serial.println(stringToPrint);
file_log = SD.open("log.txt", FILE_WRITE);
if (file_log) {
file_log.println(stringToPrint);
file_log.close();
}
} }
// Send string to serial monitor with millis() counter
void longPress() { void longPress() {
serStr("long button pressed"); String longPressStr = "pressed_ms|";
display.print("button was pressed long time"); sendStr(longPressStr + buttonLongPress);
display.print("butt_long");
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 49 KiB

After

Width:  |  Height:  |  Size: 107 KiB