-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
285 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
workshops/arduino-programming/emulator-examples/thermistor_datalogger/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.pio | ||
.vscode/.browse.c_cpp.db* | ||
.vscode/c_cpp_properties.json | ||
.vscode/launch.json | ||
.vscode/ipch |
1 change: 1 addition & 0 deletions
1
workshops/arduino-programming/emulator-examples/thermistor_datalogger/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Basic reading and writing to an SD card |
35 changes: 35 additions & 0 deletions
35
workshops/arduino-programming/emulator-examples/thermistor_datalogger/diagram.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"version": 1, | ||
"author": "Uri Shaked", | ||
"editor": "wokwi", | ||
"parts": [ | ||
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 71.32, "left": -100.01, "attrs": {} }, | ||
{ | ||
"type": "wokwi-microsd-card", | ||
"id": "sd1", | ||
"top": -33.33, | ||
"left": 14.67, | ||
"rotate": 90, | ||
"attrs": {} | ||
}, | ||
{ | ||
"type": "wokwi-ntc-temperature-sensor", | ||
"id": "ntc1", | ||
"top": 319.4, | ||
"left": 162.6, | ||
"attrs": {} | ||
} | ||
], | ||
"connections": [ | ||
[ "sd1:SCK", "uno:13", "green", [ "v18", "h-31" ] ], | ||
[ "sd1:GND", "uno:GND.1", "black", [ "v21", "h-48" ] ], | ||
[ "sd1:DO", "uno:12", "green", [ "v24", "h-42" ] ], | ||
[ "sd1:DI", "uno:11", "green", [ "v14", "h6" ] ], | ||
[ "sd1:CS", "uno:10", "green", [ "v7", "h21" ] ], | ||
[ "uno:5V", "sd1:VCC", "red", [ "v11", "h-89", "v-220", "h79" ] ], | ||
[ "ntc1:GND", "uno:GND.3", "black", [ "h0" ] ], | ||
[ "ntc1:OUT", "uno:A0", "green", [ "h38.4", "v-76.9", "h-230.4" ] ], | ||
[ "ntc1:VCC", "uno:VIN", "red", [ "h9.6", "v-38.4", "h-220.8" ] ] | ||
], | ||
"dependencies": {} | ||
} |
39 changes: 39 additions & 0 deletions
39
workshops/arduino-programming/emulator-examples/thermistor_datalogger/include/README
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
|
||
This directory is intended for project header files. | ||
|
||
A header file is a file containing C declarations and macro definitions | ||
to be shared between several project source files. You request the use of a | ||
header file in your project source file (C, C++, etc) located in `src` folder | ||
by including it, with the C preprocessing directive `#include'. | ||
|
||
```src/main.c | ||
|
||
#include "header.h" | ||
|
||
int main (void) | ||
{ | ||
... | ||
} | ||
``` | ||
|
||
Including a header file produces the same results as copying the header file | ||
into each source file that needs it. Such copying would be time-consuming | ||
and error-prone. With a header file, the related declarations appear | ||
in only one place. If they need to be changed, they can be changed in one | ||
place, and programs that include the header file will automatically use the | ||
new version when next recompiled. The header file eliminates the labor of | ||
finding and changing all the copies as well as the risk that a failure to | ||
find one copy will result in inconsistencies within a program. | ||
|
||
In C, the usual convention is to give header files names that end with `.h'. | ||
It is most portable to use only letters, digits, dashes, and underscores in | ||
header file names, and at most one dot. | ||
|
||
Read more about using header files in official GCC documentation: | ||
|
||
* Include Syntax | ||
* Include Operation | ||
* Once-Only Headers | ||
* Computed Includes | ||
|
||
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html |
46 changes: 46 additions & 0 deletions
46
workshops/arduino-programming/emulator-examples/thermistor_datalogger/lib/README
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
This directory is intended for project specific (private) libraries. | ||
PlatformIO will compile them to static libraries and link into executable file. | ||
|
||
The source code of each library should be placed in a an own separate directory | ||
("lib/your_library_name/[here are source files]"). | ||
|
||
For example, see a structure of the following two libraries `Foo` and `Bar`: | ||
|
||
|--lib | ||
| | | ||
| |--Bar | ||
| | |--docs | ||
| | |--examples | ||
| | |--src | ||
| | |- Bar.c | ||
| | |- Bar.h | ||
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html | ||
| | | ||
| |--Foo | ||
| | |- Foo.c | ||
| | |- Foo.h | ||
| | | ||
| |- README --> THIS FILE | ||
| | ||
|- platformio.ini | ||
|--src | ||
|- main.c | ||
|
||
and a contents of `src/main.c`: | ||
``` | ||
#include <Foo.h> | ||
#include <Bar.h> | ||
|
||
int main (void) | ||
{ | ||
... | ||
} | ||
|
||
``` | ||
|
||
PlatformIO Library Dependency Finder will find automatically dependent | ||
libraries scanning project source files. | ||
|
||
More information about PlatformIO Library Dependency Finder | ||
- https://docs.platformio.org/page/librarymanager/ldf.html |
16 changes: 16 additions & 0 deletions
16
workshops/arduino-programming/emulator-examples/thermistor_datalogger/platformio.ini
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
; PlatformIO Project Configuration File | ||
; | ||
; Build options: build flags, source filter | ||
; Upload options: custom upload port, speed and extra flags | ||
; Library options: dependencies, extra library storages | ||
; Advanced options: extra scripting | ||
; | ||
; Please visit documentation for the other options and examples | ||
; https://docs.platformio.org/page/projectconf.html | ||
|
||
[env:uno] | ||
platform = atmelavr | ||
board = uno | ||
framework = arduino | ||
lib_deps = | ||
arduino-libraries/SD@^1.2.4 |
73 changes: 73 additions & 0 deletions
73
workshops/arduino-programming/emulator-examples/thermistor_datalogger/src/main.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#include <Arduino.h> | ||
#include <SD.h> | ||
|
||
#define CS_PIN 10 | ||
|
||
String dataFileName = "tdata.txt"; | ||
|
||
void write_line_to_file(String fileName, String line){ | ||
File file_to_write; | ||
file_to_write = SD.open(fileName, FILE_WRITE); | ||
|
||
if (file_to_write){ | ||
file_to_write .println(line); | ||
file_to_write.close(); | ||
} else{ | ||
Serial.println("error opening file"); | ||
} | ||
} | ||
|
||
void read_file(String fileName){ | ||
File file; | ||
file = SD.open(fileName); | ||
if (file) { | ||
while (file.available()) { // read from the file until there's nothing else in it: | ||
Serial.write(file.read()); | ||
} | ||
// close the file: | ||
file.close(); | ||
} else { // if the file didn't open, print an error: | ||
Serial.println("error opening file"); | ||
} | ||
} | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
|
||
Serial.print("Initializing SD card... "); | ||
|
||
if (!SD.begin(CS_PIN)) { | ||
Serial.println("Card initialization failed!"); | ||
while (true); | ||
} | ||
|
||
Serial.println("initialization done."); | ||
// initialise output file with header | ||
write_line_to_file(dataFileName, "time, analogue_value, temperature"); | ||
|
||
} | ||
|
||
|
||
float timeValue = 0; | ||
float sampleInterval_s = 2; | ||
const float BETA = 3950; // should match the Beta Coefficient of the thermistor | ||
void loop() { | ||
int analogValue = analogRead(A0); | ||
float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15; | ||
timeValue += sampleInterval_s; | ||
String dataToWrite = String (timeValue) + ", "+ String(analogValue) + ", " + String(celsius) ; | ||
write_line_to_file(dataFileName, dataToWrite); | ||
|
||
//read_file("test.txt"); | ||
|
||
// re-open the file for reading: | ||
read_file(dataFileName); | ||
|
||
delay(1000*sampleInterval_s); | ||
|
||
|
||
} | ||
|
||
|
||
|
||
|
11 changes: 11 additions & 0 deletions
11
workshops/arduino-programming/emulator-examples/thermistor_datalogger/test/README
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
This directory is intended for PlatformIO Test Runner and project tests. | ||
|
||
Unit Testing is a software testing method by which individual units of | ||
source code, sets of one or more MCU program modules together with associated | ||
control data, usage procedures, and operating procedures, are tested to | ||
determine whether they are fit for use. Unit testing finds problems early | ||
in the development cycle. | ||
|
||
More information about PlatformIO Unit Testing: | ||
- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html |
4 changes: 4 additions & 0 deletions
4
workshops/arduino-programming/emulator-examples/thermistor_datalogger/wokwi.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[wokwi] | ||
version=1 | ||
firmware='.pio/build/uno/firmware.hex' | ||
elf='.pio/build/uno/firmware.elf' |