Stap 3: programmeren
De programmering is ook vrij eenvoudig. We maken gebruik van de LStorage bibliotheek voor de functionaliteit van de SD-kaart.
Ook de bibliotheek van de DHT voor de sensor.
Code: #include "DHT.h"#include "LTask.h"#include "LFlash.h"#include "LSD.h"#include "LStorage.h" #define Drv LSD // use SD card #define DHTPIN 2 // what pin we're connected to#define DHTTYPE DHT22 // DHT 22 (AM2302) DHT dht(DHTPIN, DHTTYPE);void setup() { // Open serial communications and wait for port to open: Serial.begin(9600); while(!Serial.available()); // input any thing to start Serial.print("Initializing SD card..."); // make sure that the default chip select pin is set to // output, even if you don't use it: pinMode(10, OUTPUT);// see if the card is present and can be initialized: LTask.begin(); Drv.begin(); Serial.println("card initialized."); dht.begin(); //initialize sensor library} void loop(){ // make a string for assembling the data to log: String dataString = ""; float t = 0.0; float h = 0.0;int temp; if(dht.readHT(&t, &h)) { temp = (int)t;dataString += String(temp); } // open the file. note that only one file can be open at a time, // so you have to close this one before opening another. LFile dataFile = Drv.open("datalog.txt", FILE_WRITE); // if the file is available, write to it: if (dataFile) { dataFile.println(dataString); dataFile.close(); // print to the serial port too: Serial.println(dataString); } // if the file isn't open, pop up an error: else { Serial.println("error opening datalog.txt"); } delay(1000); }