Lab 20/ HW 20
Lab 20 (SD Reader w/ Temp Data):
/Write to SD
#include <SD.h>
#include <Wire.h>
int temp_address = 72;
long previousMillis = 0;
int interval = 1000;
volatile long int timemax = 60000;
const int button1 = 2;
const int button2 = 3;
//Set by default for the SD Card Library
//MOSI = pin 11
//MISO = pin 12
//SCLK = pin 13
//We always need to set the CS Pin
const int CS_PIN = 10;
void setup()
{
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(button1), increase, RISING);
attachInterrupt(digitalPinToInterrupt(button2), decrease, RISING);
Wire.begin();
Serial.println("Initializing Card");
//CS pin is an output
pinMode(CS_PIN, OUTPUT);
if (!SD.begin(CS_PIN))
{
Serial.println("Card Failure");
return;
}
Serial.println("Card Ready");
}
void increase()
{
timemax = timemax + 1000;
Serial.print("New Time Interval: ");
Serial.println(timemax);
}
void decrease()
{
timemax = timemax - 1000;
Serial.print("New Time Interval: ");
Serial.println(timemax);
}
void loop()
{
if(millis() < timemax)
{
unsigned long timeStamp = millis();
if(timeStamp - previousMillis >= interval)
{
previousMillis = timeStamp;
Wire.beginTransmission(temp_address);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(temp_address, 1);
int c = Wire.read();
//Open a file and write to it.
File dataFile = SD.open("temp.csv", FILE_WRITE);
if (dataFile)
{
dataFile.print(timeStamp);
dataFile.print(",");
dataFile.print(c);
dataFile.println(" C");
dataFile.close(); //Data isn't actually written until we close the connection!
//Print to serial monitor for debugging
Serial.print(timeStamp);
Serial.print(",");
Serial.print(c);
Serial.println(" C");
}
else
{
Serial.println("Couldn't open log file");
}
}
}
//Serial.println("Done Recording");
}
/Write to SD
#include <SD.h>
#include <Wire.h>
int temp_address = 72;
long previousMillis = 0;
int interval = 1000;
volatile long int timemax = 60000;
const int button1 = 2;
const int button2 = 3;
//Set by default for the SD Card Library
//MOSI = pin 11
//MISO = pin 12
//SCLK = pin 13
//We always need to set the CS Pin
const int CS_PIN = 10;
void setup()
{
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(button1), increase, RISING);
attachInterrupt(digitalPinToInterrupt(button2), decrease, RISING);
Wire.begin();
Serial.println("Initializing Card");
//CS pin is an output
pinMode(CS_PIN, OUTPUT);
if (!SD.begin(CS_PIN))
{
Serial.println("Card Failure");
return;
}
Serial.println("Card Ready");
}
void increase()
{
timemax = timemax + 1000;
Serial.print("New Time Interval: ");
Serial.println(timemax);
}
void decrease()
{
timemax = timemax - 1000;
Serial.print("New Time Interval: ");
Serial.println(timemax);
}
void loop()
{
if(millis() < timemax)
{
unsigned long timeStamp = millis();
if(timeStamp - previousMillis >= interval)
{
previousMillis = timeStamp;
Wire.beginTransmission(temp_address);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(temp_address, 1);
int c = Wire.read();
//Open a file and write to it.
File dataFile = SD.open("temp.csv", FILE_WRITE);
if (dataFile)
{
dataFile.print(timeStamp);
dataFile.print(",");
dataFile.print(c);
dataFile.println(" C");
dataFile.close(); //Data isn't actually written until we close the connection!
//Print to serial monitor for debugging
Serial.print(timeStamp);
Serial.print(",");
Serial.print(c);
Serial.println(" C");
}
else
{
Serial.println("Couldn't open log file");
}
}
}
//Serial.println("Done Recording");
}
Comments
Post a Comment