add SD card module for logs
This commit is contained in:
		
							parent
							
								
									9f3a7fe9a2
								
							
						
					
					
						commit
						5848719822
					
				| 
						 | 
				
			
			@ -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
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
							
								
								
									
										
											BIN
										
									
								
								mctestboard.fzz
								
								
								
								
							
							
						
						
									
										
											BIN
										
									
								
								mctestboard.fzz
								
								
								
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										105
									
								
								mctestboard.ino
								
								
								
								
							
							
						
						
									
										105
									
								
								mctestboard.ino
								
								
								
								
							| 
						 | 
				
			
			@ -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
 | 
			
		||||
// License: MIT License
 | 
			
		||||
 | 
			
		||||
#include <Arduino.h>
 | 
			
		||||
#include <Time.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_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
 | 
			
		||||
const byte PIN_SDCS = 10;              // MicroSDcardSetting // Define SD card SPI CS PIN
 | 
			
		||||
File file_log;                         // MicroSDcardSetting // File object for store log
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
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 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...");
 | 
			
		||||
  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");
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  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");
 | 
			
		||||
  display.off();                       // Display Off
 | 
			
		||||
  displayOn = false;                   // Display Off
 | 
			
		||||
  sendStr("setup_done");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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) {
 | 
			
		||||
| 
						 | 
				
			
			@ -74,12 +64,12 @@ void pressButton() {
 | 
			
		|||
    if (displayOn == true) {
 | 
			
		||||
      display.off();
 | 
			
		||||
      displayOn = false;
 | 
			
		||||
      serStr("display off after slack");
 | 
			
		||||
      sendStr("disp_off");
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (digitalRead(buttonPin) == HIGH) {
 | 
			
		||||
    serStr("button pressed");
 | 
			
		||||
    sendStr("press_butt");
 | 
			
		||||
    buttonPressed = buttonPressed + 200;
 | 
			
		||||
    if (buttonPressed > buttonLongPress) {
 | 
			
		||||
      buttonPressed = 0;
 | 
			
		||||
| 
						 | 
				
			
			@ -90,10 +80,10 @@ void pressButton() {
 | 
			
		|||
    displayOn = true;
 | 
			
		||||
    if (buttonView == 0) {
 | 
			
		||||
      display.printTime(hour(), minute(), true);
 | 
			
		||||
      serStr("time showed");
 | 
			
		||||
      sendStr("show_time");
 | 
			
		||||
    } else if (buttonView == 1) {
 | 
			
		||||
      display.print("----");
 | 
			
		||||
      serStr("something was showed");
 | 
			
		||||
      sendStr("show_----");
 | 
			
		||||
    }
 | 
			
		||||
    if (buttonView < 1) buttonView += 1;
 | 
			
		||||
    else buttonView = 0;
 | 
			
		||||
| 
						 | 
				
			
			@ -102,16 +92,23 @@ void pressButton() {
 | 
			
		|||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Send string to serial monitor with millis() counter
 | 
			
		||||
void serStr(const char* serString) {
 | 
			
		||||
  long currentTime = millis();
 | 
			
		||||
  String space = " ";
 | 
			
		||||
  String stringToPrint = currentTime + space + serString;
 | 
			
		||||
// 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();
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Send string to serial monitor with millis() counter
 | 
			
		||||
void longPress() {
 | 
			
		||||
  serStr("long button pressed");
 | 
			
		||||
  display.print("button was pressed long time");
 | 
			
		||||
}
 | 
			
		||||
  String longPressStr = "pressed_ms|";
 | 
			
		||||
  sendStr(longPressStr + buttonLongPress);
 | 
			
		||||
  display.print("butt_long");
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
										
											Binary file not shown.
										
									
								
							| 
		 Before Width: | Height: | Size: 49 KiB After Width: | Height: | Size: 107 KiB  | 
		Loading…
	
		Reference in New Issue