|
| 1 | +// SPDX-FileCopyrightText: 2019 Limor Fried/ladyada for Adafruit Industries |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +// Adalogger FeatherWing data logger, log data on pin A0 |
| 6 | + |
| 7 | +#include <SPI.h> |
| 8 | +#include <SD.h> |
| 9 | + |
| 10 | +// Set the pins used, varies depending on the Feather |
| 11 | +#if defined(ESP8266) |
| 12 | + #define LED_RED 0 |
| 13 | + #define SD_CS 15 |
| 14 | +#elif defined(ARDUINO_ADAFRUIT_FEATHER_ESP32C6) |
| 15 | + #define LED_RED LED_BUILTIN |
| 16 | + #define SD_CS 8 |
| 17 | +#elif defined(ESP32) |
| 18 | + #define LED_RED 13 |
| 19 | + #define SD_CS 33 |
| 20 | +#elif defined(ARDUINO_STM32F2_FEATHER) |
| 21 | + #define LED_RED PB4 |
| 22 | + #define SD_CS PB5 |
| 23 | +#elif defined(TEENSYDUINO) |
| 24 | + #define LED_RED 13 |
| 25 | + #define SD_CS 10 |
| 26 | +#elif defined(ARDUINO_FEATHER52832) |
| 27 | + #define LED_RED 17 |
| 28 | + #define SD_CS 11 |
| 29 | +#else // 32u4, M0 or 328 |
| 30 | + #define LED_RED LED_BUILTIN |
| 31 | + #define SD_CS 4 |
| 32 | +#endif |
| 33 | + |
| 34 | +File logfile; |
| 35 | + |
| 36 | +// blink out an error code |
| 37 | +void error(uint8_t errnum) { |
| 38 | + while(1) { |
| 39 | + uint8_t i; |
| 40 | + for (i=0; i<errnum; i++) { |
| 41 | + digitalWrite(LED_RED, HIGH); |
| 42 | + delay(100); |
| 43 | + digitalWrite(LED_RED, LOW); |
| 44 | + delay(100); |
| 45 | + yield(); |
| 46 | + } |
| 47 | + for (i=errnum; i<10; i++) { |
| 48 | + delay(200); |
| 49 | + yield(); |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +void setup() { |
| 55 | + Serial.begin(115200); |
| 56 | + |
| 57 | + Serial.println("\r\nAnalog logger test"); |
| 58 | + pinMode(LED_RED, OUTPUT); |
| 59 | + |
| 60 | + // see if the card is present and can be initialized: |
| 61 | + if (!SD.begin(SD_CS)) { |
| 62 | + Serial.println("Card init. failed!"); |
| 63 | + error(2); |
| 64 | + } |
| 65 | + |
| 66 | + Serial.println("SD card OK"); |
| 67 | + File root = SD.open("/"); |
| 68 | + printDirectory(root, 0); |
| 69 | + |
| 70 | + char filename[15]; |
| 71 | + strcpy(filename, "/ANALOG00.TXT"); |
| 72 | + for (uint8_t i = 0; i < 100; i++) { |
| 73 | + filename[7] = '0' + i/10; |
| 74 | + filename[8] = '0' + i%10; |
| 75 | + // create if does not exist, do not open existing, write, sync after write |
| 76 | + if (! SD.exists(filename)) { |
| 77 | + break; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + logfile = SD.open(filename, FILE_WRITE); |
| 82 | + if( ! logfile ) { |
| 83 | + Serial.print("Couldnt create "); |
| 84 | + Serial.println(filename); |
| 85 | + error(3); |
| 86 | + } |
| 87 | + Serial.print("Writing to "); |
| 88 | + Serial.println(filename); |
| 89 | + Serial.println("Ready!"); |
| 90 | +} |
| 91 | + |
| 92 | +void loop() { |
| 93 | + digitalWrite(LED_RED, HIGH); // LED onn during logging |
| 94 | + // Read the data on pin A0 and log it to SD card and to serial |
| 95 | + logfile.print("A0 = "); logfile.println(analogRead(0)); |
| 96 | + Serial.print("A0 = "); Serial.println(analogRead(0)); |
| 97 | + digitalWrite(LED_RED, LOW); // LED off when not actively logging |
| 98 | + |
| 99 | + // save the output! |
| 100 | + logfile.flush(); |
| 101 | + |
| 102 | + delay(100); // Change the delay to space out readings |
| 103 | +} |
| 104 | + |
| 105 | +void printDirectory(File dir, int numTabs) { |
| 106 | + while(true) { |
| 107 | + |
| 108 | + File entry = dir.openNextFile(); |
| 109 | + if (! entry) { |
| 110 | + // no more files |
| 111 | + break; |
| 112 | + } |
| 113 | + for (uint8_t i=0; i<numTabs; i++) { |
| 114 | + Serial.print('\t'); |
| 115 | + } |
| 116 | + Serial.print(entry.name()); |
| 117 | + if (entry.isDirectory()) { |
| 118 | + Serial.println("/"); |
| 119 | + printDirectory(entry, numTabs+1); |
| 120 | + } else { |
| 121 | + // files have sizes, directories do not |
| 122 | + Serial.print("\t\t"); |
| 123 | + Serial.println(entry.size(), DEC); |
| 124 | + } |
| 125 | + entry.close(); |
| 126 | + } |
| 127 | + |
| 128 | +} |
0 commit comments