Friday, 16 October 2015

Week 6 FYP 2

List of activity
Study about data logging with arduino

Objective
make database for FYP to capture date and time


Content
In my project database i need a database to record date and time every time user switch on or off electrical appliances. For Fyp 1 i decide to use VB.net with microsoft access to make a database. But after make some simple research i had decide to change the method to use microsoft excel direct code from Arduino. This because i want make my project stand alone and not depend to computer to save the data into database. The communication between the microcontroller and the SD card uses SPI, which takes place on digital pins 50, 51, and 52 (Arduino Mega). Additionally, another pin must be used to select the SD card. This can be the hardware SS pin-pin 53 (on the Mega) - or another pin specified in the call to SD.begin(). Note that even if you don't use the hardware SS pin, it must be left as an output or the SD library won't work. Different boards use different pins for this functionality, so be sure you’ve selected the correct pin in SD.begin(). Before use user must user need a SD reader and computer to format your card. The library supports the FAT16 and FAT32 filesystems, but i suggest to use FAT16 when possible. Formatting the card will create a file system that the Arduino can read and write to. Below is a example code. 

#include <SPI.h>
#include <SD.h>


const int chipSelect = 4;



void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }




  Serial.print("Initializing SD card...");



  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
}



void loop() {
  // make a string for assembling the data to log:
  String dataString = "";



  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 2) {
      dataString += ",";
    }
  }



  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.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");
  }
}


i got the code from https://www.arduino.cc/en/Tutorial/Datalogger and i have try it. It works.

No comments:

Post a Comment