tuttelikz

veryverystrange [---]

May 15th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.74 KB | None | 0 0
  1. // General Library
  2. #include <Arduino.h>
  3. #include "binary.h"
  4. // Math operation
  5. #include <math.h>
  6. // I2C & TWI communication
  7. #include <Wire.h>
  8. #include "I2Cdev.h"
  9. // Serial connection
  10. #include <SoftwareSerial.h>
  11. // LiquidCrystal Display
  12. #include <hd44780.h>
  13. #include <hd44780ioClass/hd44780_I2Cexp.h>
  14. // 7 segment
  15. #include "LedControl.h"
  16. // Library for heart rate & oxymeter
  17. #include "MAX30100_new.h"
  18. // Library for RTC
  19. #include <MyRealTimeClock.h>
  20. // Library for accelerometer
  21.  
  22. // Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
  23. // is used in I2Cdev.h
  24. #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  25. #include "Wire.h"
  26. #endif
  27.  
  28. // Definiting constants
  29. #define aref_voltage 3.3
  30.  
  31. // uncomment "OUTPUT_READABLE_QUATERNION" if you want to see the actual
  32. // quaternion components in a [w, x, y, z] format (not best for parsing
  33. // on a remote host such as Processing or something though)
  34. // #define OUTPUT_READABLE_QUATERNION
  35.  
  36. // uncomment "OUTPUT_READABLE_YAWPITCHROLL" if you want to see the yaw/
  37. // pitch/roll angles (in degrees) calculated from the quaternions coming
  38. // from the FIFO. Note this also requires gravity vector calculations.
  39. // Also note that yaw/pitch/roll angles suffer from gimbal lock (for
  40. // more info, see: http://en.wikipedia.org/wiki/Gimbal_lock)
  41.  
  42.  
  43. // uncomment "OUTPUT_TEAPOT" if you want output that matches the
  44. // format used for the InvenSense teapot demo
  45. //#define OUTPUT_TEAPOT
  46.  
  47. // MCU defaults
  48.  
  49. #define LED_PIN 13 // (Arduino is 13, Teensy is 11, Teensy++ is 6)
  50. bool blinkState = false; //Indicate activity state
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57. // Sensor class objects
  58. MAX30100_new* pulseOxymeter;
  59. SoftwareSerial BTserial(8,9); // RX | TX
  60. hd44780_I2Cexp lcd;
  61. LedControl lc=LedControl(12,11,10,1);
  62. MyRealTimeClock myRTC(5, 6, 7); // Assign Digital Pins
  63.  
  64. // Constants
  65. const int LCD_COLS = 16;
  66. const int LCD_ROWS = 2;
  67. const int buttonPin = 3;
  68.  
  69. // Temperature Sensor
  70. int tempSensorPin = A0;
  71.  
  72. // Sound Sensor
  73. int audioVal = 0;
  74. int soundSensorPin = A3;
  75.  
  76. // Pulse Oxymeter
  77. //int heartRate = 0;
  78. float heartRate = 0;
  79. int oxSat = 0;
  80. long avgHeartRate = 0;
  81. long ttlHeartRate = 0;
  82. long cntHeartRate = 0;
  83.  
  84. // Button
  85. int buttonState = 0;
  86.  
  87. // Common variable for temp and stretch
  88. int readIndex = 0; // the index of the current reading
  89. const int numReadings = 50; // averaging parameter for stretch and temp
  90.  
  91. // Averaging temperature parameter details
  92. //const int numReadingsTemp = 10;
  93. int readingsTemp[numReadings]; // the readings from the analog input
  94. int averageTemp = 0; // the average
  95. int totalTemp = 0; // the running total
  96.  
  97. // Averaging stretching sensor values
  98. //const int numReadingsStretch = 10;
  99. int readingsStretch[numReadings]; // the readings from the analog input
  100. int averageStretch = 0; // the average
  101. int totalStretch = 0; // the running total
  102.  
  103.  
  104. // Timer settings for printing each second
  105. int interval = 500;
  106. unsigned long previousMillis = 0;
  107. unsigned long currentMillis;
  108.  
  109. int heartBeatNotDetected = 0;
  110.  
  111. // disp heart boundary
  112. byte sf[8]= {B00000000,B00100100,B01011010,B10000001,B10000001,B01000010,B00100100,B00011000};
  113. // disp heart inside
  114. byte nf[8]={B00000000, B00100100,B01111110,B11111111,B11111111,B01111110,B00111100,B00011000};
  115.  
  116. // ================================================================
  117. // === INTERRUPT DETECTION ROUTINE ===
  118. // ================================================================
  119.  
  120. // ================================================================
  121.  
  122.  
  123.  
  124.  
  125. void setup()
  126. {
  127. // ================================================================
  128.  
  129. // join I2C bus (I2Cdev library doesn't do this automatically)
  130. #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  131. Wire.begin();
  132. Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation difficulties
  133. #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
  134. Fastwire::setup(400, true);
  135. #endif
  136.  
  137.  
  138. // Serial monitor settings
  139. Wire.begin();
  140. Serial.begin(115200); // pulse oxymeter
  141. //Serial.begin(9600); //temperature sensor
  142. BTserial.begin(9600);
  143. //BTserial.begin(115200);
  144. while (!Serial); // wait for Leonardo enumeration, others continue immediately
  145.  
  146.  
  147.  
  148. // wait for ready
  149. Serial.println(F("\nSend any character to begin DMP programming and demo: "));
  150. /* Read for sensor from bluetooth of mobile
  151. while (Serial.available() && Serial.read()); // empty buffer
  152. while (!Serial.available()); // wait for data
  153. while (Serial.available() && Serial.read()); // empty buffer again
  154. */
  155. while (BTserial.available() && BTserial.read()); // empty buffer
  156. while (!BTserial.available()); // wait for data
  157. while (BTserial.available() && BTserial.read()); // empty buffer again
  158. //
  159. // load and configure the DMP
  160.  
  161. // ================================================================
  162.  
  163. // configure LED for output
  164. pinMode(LED_PIN, OUTPUT);
  165.  
  166. // Board settings
  167. analogReference(EXTERNAL);
  168.  
  169. // Necessary printing
  170. Serial.println("Pulse oxymeter initialized!");
  171.  
  172. // Oxymeter settings
  173. pulseOxymeter = new MAX30100_new();
  174.  
  175. // Reset clock to a new time while needed
  176. //myRTC.setDS1302Time(00, 14, 17, 4, 10, 5, 2018);
  177.  
  178. // LCD settings
  179. lc.shutdown(0,false);
  180. lc.setIntensity(0,8);
  181. lc.clearDisplay(0);
  182.  
  183. int status;
  184.  
  185. status = lcd.begin(LCD_COLS, LCD_ROWS);
  186. if(status) // non zero status means it was unsuccesful
  187. {
  188. status = -status; // convert negative status value to positive number
  189.  
  190. // begin() failed so blink error code using the onboard LED if possible
  191. hd44780::fatalError(status); // does not return
  192. }
  193.  
  194. Serial.println("LCD initialized!");
  195. // Temp sensor settings
  196. for (int thisReading = 0; thisReading < numReadings; thisReading++) {
  197. readingsTemp[thisReading] = 0;
  198. readingsStretch[thisReading] = 0;
  199. }
  200.  
  201. Serial.println("Temp Sensor initialized!");
  202. // Button settings
  203. pinMode(buttonPin, INPUT);
  204.  
  205. // setSyncProvider( requestSync); //set function to call when sync required
  206. //Serial.println("Waiting for sync message");
  207.  
  208. //setTime(11,58,00,16,4,2018);
  209. }
  210.  
  211. void drawEmptyHeart(){
  212. lc.setRow(0,0,sf[0]);
  213. lc.setRow(0,1,sf[1]);
  214. lc.setRow(0,2,sf[2]);
  215. lc.setRow(0,3,sf[3]);
  216. lc.setRow(0,4,sf[4]);
  217. lc.setRow(0,5,sf[5]);
  218. lc.setRow(0,6,sf[6]);
  219. lc.setRow(0,7,sf[7]);
  220. }
  221.  
  222. void drawHeart(){
  223. // Display no heart
  224. lc.setRow(0,0,nf[0]);
  225. lc.setRow(0,1,nf[1]);
  226. lc.setRow(0,2,nf[2]);
  227. lc.setRow(0,3,nf[3]);
  228. lc.setRow(0,4,nf[4]);
  229. lc.setRow(0,5,nf[5]);
  230. lc.setRow(0,6,nf[6]);
  231. lc.setRow(0,7,nf[7]);
  232. delay(10);
  233. }
  234.  
  235. void printTerminal(uint8_t hourVal,uint8_t minVal, uint8_t secVal, uint8_t hrVal, uint8_t oxSatVal, uint8_t tempVal, uint8_t stretchVal, uint8_t audVal, long yawVal, long pitVal, long rollVal) {
  236. Serial.print(hourVal); // Element 4
  237. Serial.print(":");
  238. Serial.print(minVal); // Element 5
  239. Serial.print(":");
  240. Serial.println(secVal);
  241. Serial.print("HR: ");
  242. Serial.print(hrVal);
  243. Serial.print("bpm / O2: ");
  244. Serial.print(oxSatVal);
  245. Serial.print("% / T: ");
  246. Serial.print(tempVal);
  247. Serial.print(" / Stretch: ");
  248. Serial.println(stretchVal);
  249. Serial.print("Audio: ");
  250. Serial.print(audVal);
  251. Serial.print(" / Y: ");
  252. Serial.print(yawVal);
  253. Serial.print(" / P: ");
  254. Serial.print(pitVal);
  255. Serial.print(" / R: ");
  256. Serial.println(rollVal);
  257. }
  258.  
  259. void printBT(uint8_t hourVal,uint8_t minVal, uint8_t secVal, uint8_t hrVal, uint8_t oxSatVal, uint8_t tempVal, uint8_t stretchVal, uint8_t audVal, long yawVal, long pitVal, long rollVal) {
  260. BTserial.write(Serial.read());
  261. BTserial.print("Time: /");
  262. BTserial.print(hourVal); // Element 4
  263. BTserial.print(":");
  264. BTserial.print(minVal); // Element 5
  265. BTserial.print(":");
  266. BTserial.print(secVal);
  267. BTserial.print("/\t");
  268. BTserial.print("HR: /");
  269. BTserial.print(hrVal);
  270. BTserial.print("/\tO2: /");
  271. BTserial.print(oxSatVal);
  272. BTserial.print("/% \tT: /");
  273. BTserial.print(tempVal);
  274. BTserial.print("/C\t Y:/");
  275. BTserial.print(yawVal);
  276. BTserial.print("/\t P:/");
  277. BTserial.print(pitVal);
  278. BTserial.print("/\t R:/");
  279. BTserial.print(rollVal);
  280. BTserial.print("/\t Aud:/");
  281. BTserial.print(audVal);
  282. BTserial.print("/\t Str:/");
  283. BTserial.print(stretchVal);
  284. BTserial.println("/");
  285. }
  286.  
  287. void printLCD(uint8_t hourVal, uint8_t minVal, uint8_t tempVal, uint8_t hrVal, uint8_t oxSatVal) {
  288. lcd.clear();
  289. lcd.setCursor(0,0);
  290. lcd.print(hourVal); // Element 4
  291. lcd.print(":");
  292. lcd.print(minVal); // Element 5
  293. lcd.print(" / T: ");
  294. lcd.print(tempVal);
  295. lcd.print("C");
  296. lcd.setCursor(0,1);
  297. lcd.print("HR: ");
  298. lcd.print(hrVal);
  299. lcd.print("b / O2: ");
  300. lcd.print(oxSatVal);
  301. lcd.print("%");
  302. }
  303.  
  304. void loop()
  305. {
  306. // ================================================================
  307.  
  308. // Update time
  309. myRTC.updateTime();
  310.  
  311. // Default behavior
  312. drawEmptyHeart();
  313.  
  314. // Button logic
  315. buttonState = digitalRead(buttonPin);
  316.  
  317. // Sound sensor logic
  318. audioVal = analogRead(soundSensorPin);
  319.  
  320. // Temp sensor reading
  321. int readingTemp = analogRead(tempSensorPin);
  322.  
  323. //Reading Stretch Sensor
  324. int readingStretch = analogRead(A1);
  325. float tempVoltage = readingTemp * aref_voltage;
  326. // voltage /= 1024.0;
  327. // float temperatureC = (voltage - 0.5) * 100 ;
  328.  
  329. // Averaging Temperature Results
  330. totalTemp = totalTemp - readingsTemp[readIndex];
  331. totalStretch = totalStretch - readingsStretch[readIndex];
  332.  
  333. readingsTemp[readIndex] = tempVoltage;
  334. readingsStretch[readIndex] = readingStretch;
  335.  
  336. totalTemp = totalTemp + readingsTemp[readIndex];
  337. totalStretch = totalStretch + readingsStretch[readIndex];
  338.  
  339. readIndex = readIndex + 1;
  340.  
  341. if (readIndex >= numReadings) {
  342. readIndex = 0;
  343. }
  344.  
  345. averageTemp = totalTemp / numReadings;
  346. averageStretch = totalStretch / numReadings;
  347.  
  348. // ================================================================
  349.  
  350.  
  351. // Pulse oxymeter logic
  352. pulseoxymeter_t result = pulseOxymeter->update();
  353.  
  354. if(result.pulseDetected == true) {
  355. drawHeart();
  356.  
  357. // Receving Sensor Values
  358. heartRate = result.heartBPM;
  359. // ttlHeartRate = ttlHeartRate + heartRate;
  360. // cntHeartRate = cntHeartRate + 1;
  361. Serial.print(heartRate); // Element 4
  362. Serial.println("bpm");
  363. }
  364.  
  365.  
  366. // else {
  367. // heartBeatNotDetected = heartBeatNotDetected + 1;
  368. //
  369. // if (heartBeatNotDetected >= 500) {
  370. // heartRate = 0;
  371. // oxSat = 0;
  372. // heartBeatNotDetected = 0;
  373. // }
  374. // }
  375.  
  376.  
  377.  
  378. // currentMillis = millis();
  379. //
  380. // if((unsigned long)(currentMillis - previousMillis) >= interval) {
  381. // long yawAvg = 0;
  382. // long pitchAvg = 0;
  383. // long rollAvg = 0;
  384. // avgHeartRate = ttlHeartRate/cntHeartRate;
  385. //
  386. // printTerminal(myRTC.hours, myRTC.minutes, myRTC.seconds, heartRate, oxSat, averageTemp, averageStretch, audioVal, yawAvg, pitchAvg, rollAvg);
  387. // printBT(myRTC.hours, myRTC.minutes, myRTC.seconds, heartRate, oxSat, averageTemp, averageStretch, audioVal, yawAvg, pitchAvg, rollAvg);
  388. // printLCD(myRTC.hours, myRTC.minutes,averageTemp, heartRate, oxSat);
  389. //
  390. // previousMillis = currentMillis;
  391. //
  392. // cntHeartRate = 0;
  393. // ttlHeartRate = 0;
  394. // }
  395. }
Advertisement
Add Comment
Please, Sign In to add comment