Guest User

Instrumented Surfboard Arduino Code

a guest
Apr 2nd, 2012
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.33 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <SD.h>
  3. #include <TimerThree.h>
  4. #include <RTClib.h>
  5. #include <Wire.h>
  6.  
  7. RTC_DS1307 RTC;
  8.  
  9.  
  10. const int trimPot1SSP = 53;
  11. const int trimPot2SSP = 49;
  12. const int trimPot3SSP = 47;
  13.  
  14. const int mux1EnablePin = 26;
  15. const int mux2EnablePin = 27;
  16. const int SDCardSSP = 45;
  17.  
  18. const int a1Pin = 24;
  19. const int b1Pin = 22;
  20. const int c1Pin = 36;
  21. const int a2Pin = 37;
  22. const int b2Pin = 39;
  23. const int c2Pin = 35;
  24.  
  25. const int debug1 = 25;
  26.  
  27. int sensorValues[13];
  28.  
  29. int channelLevels[13];
  30.  
  31. int muxCounter = 1;
  32. int muxNu = 1;
  33.  
  34. int iteration = 1;
  35.  
  36. int channelMuxRead[13];
  37.  
  38. String DataStream = "";
  39. File dataFile;
  40. File headerDataFile;
  41.  
  42. String buffer1 = "";
  43. String buffer2 = "";
  44. int numberSaved = 0;
  45. boolean buffer1Full = false;
  46. boolean buffer2Full = false;
  47. int numberDone = 0;
  48. int timerCounter = 0;
  49. int numberToDo = 105;
  50. int totalNumberToDo = 0;
  51.  
  52. int dataArray1[14*105];
  53. int dataArray2[14*105];
  54. int zeroArray[14*105];
  55. int startDay = 0;
  56. int startMonth = 0;
  57. int startHour = 0;
  58. int startMinute = 0;
  59.  
  60. char fileName[] = "00000000.txt";
  61. char headerFileName[] = "00000000.txt";
  62.  
  63. void setup() {
  64. pinMode(trimPot1SSP, OUTPUT);
  65. pinMode(trimPot2SSP, OUTPUT);
  66. pinMode(trimPot3SSP, OUTPUT);
  67. pinMode(SDCardSSP, OUTPUT);
  68. pinMode(debug1, OUTPUT);
  69. pinMode(23, OUTPUT);
  70. pinMode(31, OUTPUT);
  71. pinMode(33, OUTPUT);
  72. pinMode(35, OUTPUT);
  73.  
  74. SPI.begin();
  75. Wire.begin();
  76.  
  77. RTC.begin();
  78. if (! RTC.isrunning()) {
  79.  
  80. RTC.adjust(DateTime(__DATE__, __TIME__));
  81. }
  82. //Serial.begin(9600);
  83.  
  84. if(!SD.begin(SDCardSSP))
  85. {
  86. digitalWrite(31,HIGH);
  87.  
  88. return;
  89. }
  90.  
  91. makeHeaderFileName(headerFileName);
  92. Serial.println(headerFileName);
  93. if(headerDataFile = SD.open(headerFileName,O_WRITE | O_CREAT))
  94. {
  95. }
  96. else
  97. {
  98. Serial.println("FILE NOT MADE");
  99. }
  100.  
  101. balance();
  102. shuntCal();
  103. digitalWrite(35, LOW);
  104.  
  105. makeFileName(fileName);
  106. Serial.println(fileName);
  107. if(dataFile = SD.open(fileName,O_WRITE | O_CREAT))
  108. {
  109. }
  110. else
  111. {
  112. Serial.println("DATA FILE NOT MADE");
  113. }
  114. Timer3.initialize(9091);
  115. Timer3.attachInterrupt(timerIsr);
  116. }
  117.  
  118. void loop()
  119. {
  120. if(numberDone == numberToDo)
  121. {
  122. numberDone = 0;
  123. if(buffer1Full)
  124. {
  125. //Timer3.detachInterrupt();
  126. digitalWrite(23, HIGH);
  127. digitalWrite(31, HIGH);
  128. //dataFile.println(buffer1);
  129. for(int i = 0; i < 14*numberToDo; i++)
  130. {
  131. if(i %14 == 0)
  132. {
  133. dataFile.println();
  134. }
  135. dataFile.print(dataArray1[i]);
  136. dataArray1[i]=0;
  137. dataFile.print(",");
  138. }
  139.  
  140. dataFile.flush();
  141. //buffer1 = "";
  142. digitalWrite(23, LOW);
  143. digitalWrite(31, LOW);
  144. //Timer3.attachInterrupt(timerIsr);
  145. }
  146. else if(buffer2Full)
  147. {
  148. //Timer3.detachInterrupt();
  149. digitalWrite(23, HIGH);
  150. digitalWrite(33, HIGH);
  151. buffer2Full = false;
  152. //dataFile.println(buffer2);
  153. for(int i = 0; i < 14*numberToDo; i++)
  154. {
  155. if(i %14 == 0)
  156. {
  157. dataFile.println();
  158. }
  159. dataFile.print(dataArray2[i]);
  160. dataArray2[i]=0;
  161. dataFile.print(",");
  162. }
  163.  
  164. dataFile.flush();
  165. //buffer2 = "";
  166. digitalWrite(23, LOW);
  167. digitalWrite(33, LOW);
  168. //Timer3.attachInterrupt(timerIsr);
  169. }
  170. }
  171. }
  172.  
  173. void timerIsr()
  174. {
  175. digitalWrite(debug1, HIGH);
  176. if(iteration == 32767)
  177. {
  178. iteration = 0;
  179. }
  180. if(!buffer1Full)
  181. {
  182. dataArray1[numberDone*14] = iteration;
  183. //buffer1 += iteration;
  184. //buffer1 += ",";
  185. }
  186. else
  187. {
  188. dataArray2[numberDone*14] = iteration;
  189. //buffer2 += iteration;
  190. //buffer2 += ",";
  191. }
  192. for(int i = 0; i < 13; i++)
  193. {
  194. if(!buffer1Full)
  195. {
  196. dataArray1[numberDone*14+i+1] = analogRead(i);
  197. //buffer1 +=analogRead(i);
  198. //buffer1 += ",";
  199. }
  200. else
  201. {
  202. dataArray2[numberDone*14+i+1] = analogRead(i);
  203. //buffer2 += analogRead(i);
  204. //buffer2 += ",";
  205. }
  206. }
  207. numberSaved++;
  208.  
  209. if(numberSaved == numberToDo)
  210. {
  211. if(!buffer1Full)
  212. {
  213. buffer1Full = true;
  214. buffer2Full = false;
  215. numberSaved = 0;
  216. }
  217. else
  218. {
  219. buffer1Full = false;
  220. buffer2Full = true;
  221. numberSaved = 0;
  222. }
  223. }
  224. numberDone++;
  225. iteration++;
  226. digitalWrite(debug1, LOW);
  227. }
  228.  
  229. void balance()
  230. {
  231. digitalWrite(35, HIGH);
  232. digitalWrite(37, HIGH);
  233. int numberOfChannelOn = 0;
  234.  
  235. for(int i = 0; i < 13; i++)
  236. {
  237. while(sensorValues[i] >=650 || sensorValues[i] <=450)
  238. {
  239. if(channelLevels[i] == 256)
  240. {
  241. channelLevels[i] = 0;
  242. }
  243. sensorValues[i] = analogRead(i);
  244. Serial.print(i,DEC);
  245. Serial.print(" ");
  246. Serial.print(sensorValues[i], DEC);
  247. Serial.print(" ");
  248. Serial.println(channelLevels[i], DEC);
  249.  
  250. if(sensorValues[i] >= 650 || sensorValues[i] <= 450)
  251. {
  252. if(i == 12)
  253. {
  254. channelLevels[i] = channelLevels[i] + 1;
  255. digitalPotWrite(numberOfChannelOn, channelLevels[i], trimPot3SSP);
  256. delay(10);
  257. }
  258. else if (i > 5)
  259. {
  260. channelLevels[i] = channelLevels[i] + 1;
  261. digitalPotWrite(numberOfChannelOn, channelLevels[i], trimPot2SSP);
  262. numberOfChannelOn++;
  263. delay (10);
  264. }
  265. else
  266. {
  267. channelLevels[i] = channelLevels[i] + 1;
  268. digitalPotWrite(numberOfChannelOn, channelLevels[i], trimPot1SSP);
  269. numberOfChannelOn++;
  270. delay(10);
  271. }
  272. }
  273. }
  274. if(i == 5 || i == 11)
  275. {
  276. numberOfChannelOn = 0;
  277. }
  278. }
  279. digitalWrite(35, LOW);
  280. digitalWrite(37, LOW);
  281. }
  282.  
  283. void shuntCal()
  284. {
  285. digitalWrite(31, HIGH);
  286. digitalWrite(33, HIGH);
  287.  
  288. for(int i = 0; i < 13; i++)
  289. {
  290. if( i > 6 && muxNu == 1)
  291. {
  292. muxNu++;
  293. }
  294. muxWrite(muxNu, i);
  295. delay(1000);
  296. channelMuxRead[i] = analogRead(i);
  297. DataStream += sensorValues[i];
  298. DataStream += ",";
  299. DataStream += channelMuxRead[i];
  300.  
  301. headerDataFile.println(DataStream);
  302. headerDataFile.flush();
  303. DataStream ="";
  304.  
  305. Serial.print (i, DEC);
  306. Serial.print(" ");
  307. Serial.print(sensorValues[i], DEC);
  308. Serial.print(" ");
  309. Serial.println(channelMuxRead[i],DEC);
  310.  
  311. if(muxNu == 1)
  312. {
  313. digitalWrite(mux1EnablePin, HIGH);
  314. digitalWrite(a1Pin, LOW);
  315. digitalWrite(b1Pin, LOW);
  316. digitalWrite(c1Pin, LOW);
  317. }
  318. else if(muxNu ==2)
  319. {
  320. digitalWrite(mux2EnablePin, HIGH);
  321. digitalWrite(a2Pin, LOW);
  322. digitalWrite(b2Pin, LOW);
  323. digitalWrite(c2Pin, LOW);
  324. }
  325. }
  326. digitalWrite(31, LOW);
  327. digitalWrite(33, LOW);
  328. }
  329.  
  330. int digitalPotWrite(int address, int value, int slaveSelectPin)
  331. {
  332. digitalWrite(slaveSelectPin,LOW);
  333. SPI.transfer(address);
  334. SPI.transfer(value);
  335. digitalWrite(slaveSelectPin,HIGH);
  336. }
  337.  
  338. int muxWrite(int muxNumber, int channelNumber)
  339. {
  340. if(muxNumber == 1)
  341. {
  342. digitalWrite(mux1EnablePin, LOW);
  343. if(channelNumber == 0)
  344. {
  345. digitalWrite(a1Pin, HIGH);
  346. }
  347. else if(channelNumber == 1)
  348. {
  349. digitalWrite(b1Pin, HIGH);
  350. }
  351. else if(channelNumber == 2)
  352. {
  353. digitalWrite(a1Pin, HIGH);
  354. digitalWrite(b1Pin, HIGH);
  355. }
  356. else if(channelNumber == 3)
  357. {
  358. digitalWrite(c1Pin, HIGH);
  359. }
  360. else if(channelNumber == 4)
  361. {
  362. digitalWrite(a1Pin, HIGH);
  363. digitalWrite(c1Pin, HIGH);
  364. }
  365. else if(channelNumber == 5)
  366. {
  367. digitalWrite(b1Pin, HIGH);
  368. digitalWrite(c1Pin, HIGH);
  369. }
  370. }
  371. else
  372. {
  373. digitalWrite(mux2EnablePin, LOW);
  374. if(channelNumber == 6)
  375. {
  376. }
  377. else if(channelNumber == 7)
  378. {
  379. digitalWrite(a2Pin, HIGH);
  380. }
  381. else if(channelNumber ==8)
  382. {
  383. digitalWrite(b2Pin, HIGH);
  384. }
  385. else if(channelNumber == 9)
  386. {
  387. digitalWrite(a2Pin, HIGH);
  388. digitalWrite(b2Pin, HIGH);
  389. }
  390. else if(channelNumber == 10)
  391. {
  392. digitalWrite(c2Pin, HIGH);
  393. }
  394. else if(channelNumber == 11)
  395. {
  396. digitalWrite(a2Pin, HIGH);
  397. digitalWrite(c2Pin, HIGH);
  398. }
  399. else if(channelNumber == 12)
  400. {
  401. digitalWrite(b2Pin, HIGH);
  402. digitalWrite(c2Pin, HIGH);
  403. }
  404. }
  405. }
  406.  
  407. void makeFileName(char *fileName)
  408. {
  409. fileName[0]=startMonth/10+'0';
  410. fileName[1]=startMonth%10+'0';
  411. fileName[2]=startDay/10+'0';
  412. fileName[3]=startDay%10+'0';
  413. fileName[4]=startHour/10+'0';
  414. fileName[5]=startHour%10+'0';
  415. fileName[6]=startMinute/10+'0';
  416. fileName[7]=startMinute%10+'0';
  417. fileName[8]='.';
  418. fileName[9]='t';
  419. fileName[10]='x';
  420. fileName[11]='t';
  421. return;
  422. }
  423.  
  424. void makeHeaderFileName(char *headerFileName)
  425. {
  426. DateTime startTime = RTC.now();
  427.  
  428. startDay = startTime.day();
  429. startMonth = startTime.month();
  430. startHour = startTime.hour();
  431. startMinute = startTime.minute();
  432.  
  433. headerFileName[0]=startMonth%10+'0';
  434. headerFileName[1]=startDay/10+'0';
  435. headerFileName[2]=startDay%10+'0';
  436. headerFileName[3]=startHour/10+'0';
  437. headerFileName[4]=startHour%10+'0';
  438. headerFileName[5]=startMinute/10+'0';
  439. headerFileName[6]=startMinute%10+'0';
  440. headerFileName[7]='h';
  441. headerFileName[8]='.';
  442. headerFileName[9]='t';
  443. headerFileName[10]='x';
  444. headerFileName[11]='t';
  445. return;
  446. }
Add Comment
Please, Sign In to add comment