Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <SD.h>
- #include <TimerThree.h>
- #include <RTClib.h>
- #include <Wire.h>
- RTC_DS1307 RTC;
- const int trimPot1SSP = 53;
- const int trimPot2SSP = 49;
- const int trimPot3SSP = 47;
- const int mux1EnablePin = 26;
- const int mux2EnablePin = 27;
- const int SDCardSSP = 45;
- const int a1Pin = 24;
- const int b1Pin = 22;
- const int c1Pin = 36;
- const int a2Pin = 37;
- const int b2Pin = 39;
- const int c2Pin = 35;
- const int debug1 = 25;
- int sensorValues[13];
- int channelLevels[13];
- int muxCounter = 1;
- int muxNu = 1;
- int iteration = 1;
- int channelMuxRead[13];
- String DataStream = "";
- File dataFile;
- File headerDataFile;
- String buffer1 = "";
- String buffer2 = "";
- int numberSaved = 0;
- boolean buffer1Full = false;
- boolean buffer2Full = false;
- int numberDone = 0;
- int timerCounter = 0;
- int numberToDo = 105;
- int totalNumberToDo = 0;
- int dataArray1[14*105];
- int dataArray2[14*105];
- int zeroArray[14*105];
- int startDay = 0;
- int startMonth = 0;
- int startHour = 0;
- int startMinute = 0;
- char fileName[] = "00000000.txt";
- char headerFileName[] = "00000000.txt";
- void setup() {
- pinMode(trimPot1SSP, OUTPUT);
- pinMode(trimPot2SSP, OUTPUT);
- pinMode(trimPot3SSP, OUTPUT);
- pinMode(SDCardSSP, OUTPUT);
- pinMode(debug1, OUTPUT);
- pinMode(23, OUTPUT);
- pinMode(31, OUTPUT);
- pinMode(33, OUTPUT);
- pinMode(35, OUTPUT);
- SPI.begin();
- Wire.begin();
- RTC.begin();
- if (! RTC.isrunning()) {
- RTC.adjust(DateTime(__DATE__, __TIME__));
- }
- //Serial.begin(9600);
- if(!SD.begin(SDCardSSP))
- {
- digitalWrite(31,HIGH);
- return;
- }
- makeHeaderFileName(headerFileName);
- Serial.println(headerFileName);
- if(headerDataFile = SD.open(headerFileName,O_WRITE | O_CREAT))
- {
- }
- else
- {
- Serial.println("FILE NOT MADE");
- }
- balance();
- shuntCal();
- digitalWrite(35, LOW);
- makeFileName(fileName);
- Serial.println(fileName);
- if(dataFile = SD.open(fileName,O_WRITE | O_CREAT))
- {
- }
- else
- {
- Serial.println("DATA FILE NOT MADE");
- }
- Timer3.initialize(9091);
- Timer3.attachInterrupt(timerIsr);
- }
- void loop()
- {
- if(numberDone == numberToDo)
- {
- numberDone = 0;
- if(buffer1Full)
- {
- //Timer3.detachInterrupt();
- digitalWrite(23, HIGH);
- digitalWrite(31, HIGH);
- //dataFile.println(buffer1);
- for(int i = 0; i < 14*numberToDo; i++)
- {
- if(i %14 == 0)
- {
- dataFile.println();
- }
- dataFile.print(dataArray1[i]);
- dataArray1[i]=0;
- dataFile.print(",");
- }
- dataFile.flush();
- //buffer1 = "";
- digitalWrite(23, LOW);
- digitalWrite(31, LOW);
- //Timer3.attachInterrupt(timerIsr);
- }
- else if(buffer2Full)
- {
- //Timer3.detachInterrupt();
- digitalWrite(23, HIGH);
- digitalWrite(33, HIGH);
- buffer2Full = false;
- //dataFile.println(buffer2);
- for(int i = 0; i < 14*numberToDo; i++)
- {
- if(i %14 == 0)
- {
- dataFile.println();
- }
- dataFile.print(dataArray2[i]);
- dataArray2[i]=0;
- dataFile.print(",");
- }
- dataFile.flush();
- //buffer2 = "";
- digitalWrite(23, LOW);
- digitalWrite(33, LOW);
- //Timer3.attachInterrupt(timerIsr);
- }
- }
- }
- void timerIsr()
- {
- digitalWrite(debug1, HIGH);
- if(iteration == 32767)
- {
- iteration = 0;
- }
- if(!buffer1Full)
- {
- dataArray1[numberDone*14] = iteration;
- //buffer1 += iteration;
- //buffer1 += ",";
- }
- else
- {
- dataArray2[numberDone*14] = iteration;
- //buffer2 += iteration;
- //buffer2 += ",";
- }
- for(int i = 0; i < 13; i++)
- {
- if(!buffer1Full)
- {
- dataArray1[numberDone*14+i+1] = analogRead(i);
- //buffer1 +=analogRead(i);
- //buffer1 += ",";
- }
- else
- {
- dataArray2[numberDone*14+i+1] = analogRead(i);
- //buffer2 += analogRead(i);
- //buffer2 += ",";
- }
- }
- numberSaved++;
- if(numberSaved == numberToDo)
- {
- if(!buffer1Full)
- {
- buffer1Full = true;
- buffer2Full = false;
- numberSaved = 0;
- }
- else
- {
- buffer1Full = false;
- buffer2Full = true;
- numberSaved = 0;
- }
- }
- numberDone++;
- iteration++;
- digitalWrite(debug1, LOW);
- }
- void balance()
- {
- digitalWrite(35, HIGH);
- digitalWrite(37, HIGH);
- int numberOfChannelOn = 0;
- for(int i = 0; i < 13; i++)
- {
- while(sensorValues[i] >=650 || sensorValues[i] <=450)
- {
- if(channelLevels[i] == 256)
- {
- channelLevels[i] = 0;
- }
- sensorValues[i] = analogRead(i);
- Serial.print(i,DEC);
- Serial.print(" ");
- Serial.print(sensorValues[i], DEC);
- Serial.print(" ");
- Serial.println(channelLevels[i], DEC);
- if(sensorValues[i] >= 650 || sensorValues[i] <= 450)
- {
- if(i == 12)
- {
- channelLevels[i] = channelLevels[i] + 1;
- digitalPotWrite(numberOfChannelOn, channelLevels[i], trimPot3SSP);
- delay(10);
- }
- else if (i > 5)
- {
- channelLevels[i] = channelLevels[i] + 1;
- digitalPotWrite(numberOfChannelOn, channelLevels[i], trimPot2SSP);
- numberOfChannelOn++;
- delay (10);
- }
- else
- {
- channelLevels[i] = channelLevels[i] + 1;
- digitalPotWrite(numberOfChannelOn, channelLevels[i], trimPot1SSP);
- numberOfChannelOn++;
- delay(10);
- }
- }
- }
- if(i == 5 || i == 11)
- {
- numberOfChannelOn = 0;
- }
- }
- digitalWrite(35, LOW);
- digitalWrite(37, LOW);
- }
- void shuntCal()
- {
- digitalWrite(31, HIGH);
- digitalWrite(33, HIGH);
- for(int i = 0; i < 13; i++)
- {
- if( i > 6 && muxNu == 1)
- {
- muxNu++;
- }
- muxWrite(muxNu, i);
- delay(1000);
- channelMuxRead[i] = analogRead(i);
- DataStream += sensorValues[i];
- DataStream += ",";
- DataStream += channelMuxRead[i];
- headerDataFile.println(DataStream);
- headerDataFile.flush();
- DataStream ="";
- Serial.print (i, DEC);
- Serial.print(" ");
- Serial.print(sensorValues[i], DEC);
- Serial.print(" ");
- Serial.println(channelMuxRead[i],DEC);
- if(muxNu == 1)
- {
- digitalWrite(mux1EnablePin, HIGH);
- digitalWrite(a1Pin, LOW);
- digitalWrite(b1Pin, LOW);
- digitalWrite(c1Pin, LOW);
- }
- else if(muxNu ==2)
- {
- digitalWrite(mux2EnablePin, HIGH);
- digitalWrite(a2Pin, LOW);
- digitalWrite(b2Pin, LOW);
- digitalWrite(c2Pin, LOW);
- }
- }
- digitalWrite(31, LOW);
- digitalWrite(33, LOW);
- }
- int digitalPotWrite(int address, int value, int slaveSelectPin)
- {
- digitalWrite(slaveSelectPin,LOW);
- SPI.transfer(address);
- SPI.transfer(value);
- digitalWrite(slaveSelectPin,HIGH);
- }
- int muxWrite(int muxNumber, int channelNumber)
- {
- if(muxNumber == 1)
- {
- digitalWrite(mux1EnablePin, LOW);
- if(channelNumber == 0)
- {
- digitalWrite(a1Pin, HIGH);
- }
- else if(channelNumber == 1)
- {
- digitalWrite(b1Pin, HIGH);
- }
- else if(channelNumber == 2)
- {
- digitalWrite(a1Pin, HIGH);
- digitalWrite(b1Pin, HIGH);
- }
- else if(channelNumber == 3)
- {
- digitalWrite(c1Pin, HIGH);
- }
- else if(channelNumber == 4)
- {
- digitalWrite(a1Pin, HIGH);
- digitalWrite(c1Pin, HIGH);
- }
- else if(channelNumber == 5)
- {
- digitalWrite(b1Pin, HIGH);
- digitalWrite(c1Pin, HIGH);
- }
- }
- else
- {
- digitalWrite(mux2EnablePin, LOW);
- if(channelNumber == 6)
- {
- }
- else if(channelNumber == 7)
- {
- digitalWrite(a2Pin, HIGH);
- }
- else if(channelNumber ==8)
- {
- digitalWrite(b2Pin, HIGH);
- }
- else if(channelNumber == 9)
- {
- digitalWrite(a2Pin, HIGH);
- digitalWrite(b2Pin, HIGH);
- }
- else if(channelNumber == 10)
- {
- digitalWrite(c2Pin, HIGH);
- }
- else if(channelNumber == 11)
- {
- digitalWrite(a2Pin, HIGH);
- digitalWrite(c2Pin, HIGH);
- }
- else if(channelNumber == 12)
- {
- digitalWrite(b2Pin, HIGH);
- digitalWrite(c2Pin, HIGH);
- }
- }
- }
- void makeFileName(char *fileName)
- {
- fileName[0]=startMonth/10+'0';
- fileName[1]=startMonth%10+'0';
- fileName[2]=startDay/10+'0';
- fileName[3]=startDay%10+'0';
- fileName[4]=startHour/10+'0';
- fileName[5]=startHour%10+'0';
- fileName[6]=startMinute/10+'0';
- fileName[7]=startMinute%10+'0';
- fileName[8]='.';
- fileName[9]='t';
- fileName[10]='x';
- fileName[11]='t';
- return;
- }
- void makeHeaderFileName(char *headerFileName)
- {
- DateTime startTime = RTC.now();
- startDay = startTime.day();
- startMonth = startTime.month();
- startHour = startTime.hour();
- startMinute = startTime.minute();
- headerFileName[0]=startMonth%10+'0';
- headerFileName[1]=startDay/10+'0';
- headerFileName[2]=startDay%10+'0';
- headerFileName[3]=startHour/10+'0';
- headerFileName[4]=startHour%10+'0';
- headerFileName[5]=startMinute/10+'0';
- headerFileName[6]=startMinute%10+'0';
- headerFileName[7]='h';
- headerFileName[8]='.';
- headerFileName[9]='t';
- headerFileName[10]='x';
- headerFileName[11]='t';
- return;
- }
Add Comment
Please, Sign In to add comment