// For the SD Shield Plus const int chipSelect = 4; //Selects the Slave (Ethernet Shield) port that connects to the master Uno // This is to talk to the real time clock #include "Wire.h" #include "SD.h" #include "SPI.h" #define DS1307_I2C_ADDRESS 0x68 // This is the I2C address // Global Variables long previousMillis = 0; // will store last time Temp was updated byte zero; // This is to talk to the real time clock #include "Wire.h" #include "SD.h" #include "SPI.h" #define DS1307_I2C_ADDRESS 0x68 // This is the I2C address void adder(int *, int); int bits[] = {8,7,6,5}; //Pins on the Arduino that are set int i; char *Day[] = {"","Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; char *Mon[] = {"","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}; byte decToBcd(byte value) { return ( (value/10*16) + (value%10) ); } // Convert binary coded decimal to normal decimal numbers byte bcdToDec(byte value) { return ( (value/16*10) + (value%16) ); } // 1) Sets the date and time on the ds1307 // 2) Starts the clock // 3) Sets hour mode to 24 hour clock // Assumes you're passing in valid numbers, Probably need to put in checks for valid numbers. void setDateDs1307(byte second, // 0-59 byte minute, // 0-59 byte hour, // 1-23 byte dayOfWeek, // 1-7 byte dayOfMonth, // 1-28/29/30/31 byte month, // 1-12 byte year) // 0-99 { Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.write(0); Wire.write(decToBcd(second)); // 0 to bit 7 starts the clock Wire.write(decToBcd(minute)); Wire.write(decToBcd(hour)); // If you want 12 hour am/pm you need to set // bit 6 (also need to change readDateDs1307) Wire.write(decToBcd(dayOfWeek)); Wire.write(decToBcd(dayOfMonth)); Wire.write(decToBcd(month)); Wire.write(decToBcd(year)); Wire.endTransmission(); } // Gets the date and time from the ds1307 and prints result void getDateDs1307(byte *second, byte *minute, byte *hour, byte *dayOfWeek, byte *dayOfMonth, byte *month, byte *year) { // Reset the register pointer Wire.beginTransmission(DS1307_I2C_ADDRESS); Wire.write(0); Wire.endTransmission(); Wire.requestFrom(DS1307_I2C_ADDRESS, 7); // A few of these need masks because certain bits are control bits *second = bcdToDec(Wire.read() & 0x7f); *minute = bcdToDec(Wire.read()); *hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm *dayOfWeek = bcdToDec(Wire.read()); *dayOfMonth = bcdToDec(Wire.read()); *month = bcdToDec(Wire.read()); *year = bcdToDec(Wire.read()); } void setup() { byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; //pinMode(A0, OUTPUT); //Testing Arduino Atmega328 clock rate //SPI.setClockDivider(SPI_CLOCK_DIV8); //analogWrite(A0, clocker); //Clock Rate is set at 16 MHz UBRR0H=0x00; //Upper 8 bits UBRR0L=0x33; //Lower 8 bits Wire.begin(); Serial.begin(19200); //Baud rate should be held at 250K bps to account for at least 16bits*(1 per 600 ms)*12 signals Serial.print("Initializing SD card...\n"); // make sure that the default chip select pin is set to // output, even if you don't use it: pinMode(chipSelect, OUTPUT); Serial.print("chipSelect set to output\n"); // see if the card is present and can be initialized: if (!SD.begin(chipSelect)) { Serial.println("Card failed, or not present\n"); // don't do anything more: return; } Serial.println("card initialized.\n"); // DO NOT TOUCH CODE ABOVE THIS COMMENT // JUST CHANGE THIS PART WHEN SETTING TIME/DATE OF WHEN YOU WANT TO START RECORDING DATA second = 0; minute = 34; hour = 12; dayOfWeek = 4; dayOfMonth = 20; month = 12; year = 12; // DO NOT TOUCH CODE BEYOND THIS POINT setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year); int pin; for(pin=5; pin<9; pin++){ pinMode(pin, OUTPUT); } } void loop() { byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year); // make a string for assembling the data to log: String data=""; if (hour < 10) data+=String("0"); data+=String(hour, DEC); data+=String(":"); if (minute < 10) data+=String("0"); data+=String(minute, DEC); data+=String(":"); if (second < 10) data+=String("0"); data+=String(second, DEC); data+=String(" "); data+=String(Day[dayOfWeek]); data+=String(", "); data+=String(dayOfMonth, DEC); data+=String(" "); data+=String(Mon[month]); data+=String(" 20"); if (year < 10) data+=String("0"); data+=String(year, DEC); data+=String(", sensor voltage:"); Serial.println(data); // label input with the pin data and append to the string: int analogPin =2;//Read from pin 2 float sensor = analogRead(analogPin); // Serial.println("Sensor debug\n"); Serial.println(sensor*(0.0049*2)); sensor*=(0.0049*2); // 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.csv", FILE_WRITE); // if the file is available, write to it: if (datafile) { datafile.println(data); // print the date/time stamp datafile.println(sensor); // Print the sensor voltage to the data file datafile.close(); // print to the serial port too: // Serial.println(data); //SPI requires that there's a SD card serial port to interface with Uno } // if the file isn't open, pop up an error: else { Serial.println("error opening datalog.csv"); } i= 0; for(i; i < 12; i++){ //Can omment out and enter a specific signal to toggle from instead of i adder(bits, i);; delayMicroseconds(167); //provides sampling rate of 500 samples of signal/channel per sec } delayMicroseconds(167); // Delay between the MUX counter and the Arduino receiving the data } void adder(int * bits, int i){ int sum = i; int j = 0; for(j; j<4; j++){ if((sum - power(3-j)) >= 0){ digitalWrite(bits[j], HIGH); sum = sum - power(3-j); } else digitalWrite(bits[j], LOW); }//end for } int power(int powervalue){ int i = 0; int pinnum = 1; if(powervalue != 0){ for(i;i < powervalue; i++) pinnum = 2*pinnum; } return pinnum; }