|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
-
- #include <SdFat.h>
-
-
- const uint8_t chipSelect = SS;
-
-
- SdFat sd;
-
-
- ArduinoOutStream cout(Serial);
-
- void writeTestFile() {
-
- ofstream sdout("AVG_TEST.TXT");
-
-
- for (int16_t i = -1001; i < 2000; i += 13) {
- sdout << 0.1 * i << endl;
- }
- if (!sdout) sd.errorHalt("sdout failed");
-
- sdout.close();
- }
-
- void calcAverage() {
- uint16_t n = 0;
- double num;
- double sum = 0;
-
-
- ifstream sdin("AVG_TEST.TXT");
-
-
- if (!sdin) sd.errorHalt("sdin failed");
-
-
- while (sdin >> num) {
- n++;
- sum += num;
- }
-
-
- cout << "sum of " << n << " numbers = " << sum << endl;
- cout << "average = " << sum/n << endl;
- }
-
- void setup() {
- Serial.begin(9600);
- while (!Serial) {}
-
-
- cout << pstr("Type any character to start\n");
- while (Serial.read() <= 0) {}
- delay(400);
-
-
-
- if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();
-
-
- writeTestFile();
-
-
- calcAverage();
- }
-
- void loop() {}
|