Advertisement
Guest User

Serial yuckiness

a guest
Oct 24th, 2022
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.38 KB | None | 0 0
  1. #include <RingBuf.h>
  2. #include <SdFatConfig.h>
  3. #include <sdios.h>
  4. #include <BufferedPrint.h>
  5. #include <FreeStack.h>
  6. #include <MinimumSerial.h>
  7. #include <SdFat.h>
  8.  
  9. #include <Adafruit_TCS34725.h>
  10. #include <NMEAGPS.h>
  11. #include <GPSport.h>
  12. #include <Streamers.h>
  13. #include <DFRobot_OxygenSensor.h>
  14. #include <SPI.h>
  15. #include <SoftwareSerial.h>
  16. #include <Adafruit_BNO055.h>
  17.  
  18.  
  19. ////------------------------------------------TO-DO-------------------------------
  20. //GPS SDCARD 9DOF TEMP
  21. //
  22. //
  23. //
  24.  
  25. //
  26. //
  27. //
  28. ///////////////////////////////////////////////////constants//////////////////////////////////////////
  29. #define RXpin 17
  30. #define TXpin 16
  31. #define FILE_BASE_NAME "Data"                            //sd
  32. const int chipSelect = 53;
  33.  
  34.  
  35. //SD card header count and header fields
  36. const uint8_t ANALOG_COUNT = 18;
  37. String dataPoints[18] = {"lat", "long", "Altitude", "OrientX", "OrientY", "OrientZ", "AccelX", "AccelY", "AccelZ", "GravityX", "GravityY", "GravityZ", "Red", "Green", "Blue", "IR", "Temperature", "UV"};
  38. //Sd storage array
  39. String data[ANALOG_COUNT];
  40.  
  41. #define Oxygen_IICAddress ADDRESS_3
  42. #define COLLECT_NUMBER  10             // collect number, the collection range is 1-100.
  43.  
  44. #define GPSSerial Serial1
  45.  
  46. //SD card
  47. const uint32_t SAMPLE_INTERVAL_MS = 1000;
  48.  
  49. /* Set the delay between fresh samples BNO055 */
  50. uint16_t BNO055_SAMPLERATE_DELAY_MS = 1000;
  51. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  52. //////////////////////////////////////////////////Objects///////////////////////////////////////////////////
  53. ////Create instance Software Serial
  54. SoftwareSerial mySerial(RXpin, TXpin);
  55.  
  56. Adafruit_BNO055 bno = Adafruit_BNO055(55, 0x28);//Instantiate BNO-055
  57.  
  58.  
  59. Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);
  60.  
  61. ////------------------------------------------------------------------------------
  62. // File system object.
  63. SdFat sd;
  64.  
  65. // Log file.
  66. SdFile file;
  67.  
  68. // Time in micros for next data record.
  69. uint32_t logTime;
  70.  
  71. ////==============================================================================
  72.  
  73. long msElapsed = millis();
  74.  
  75.  
  76. void setup() {
  77.   //  // Open serial communications and wait for port to open:
  78.   Serial.begin(115200);
  79.   //mySerial.begin(9600);
  80.   delay(1000);
  81.   // wait for hardware serial to appear
  82.  
  83.  
  84.   // 9600 baud is the default rate for the Ultimate GPS
  85.   //GPSSerial.begin(9600);
  86.  
  87.  
  88.   //--------------------------
  89.   //  //INITIALIZE BNO 055 ACCELEROMETER
  90.   //  if (!bno.begin())
  91.   //  {
  92.   //    /* There was a problem detecting the BNO055 ... check your connections */
  93.   //    Serial.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!");
  94.   //    // while (1);
  95.   //  }
  96.   //  delay(1000);
  97.   //  // Define pin modes for TX and RX
  98.   //  pinMode(RXpin, INPUT);
  99.   //
  100.   //  pinMode(TXpin, OUTPUT);
  101.  
  102.  
  103.  
  104.  
  105. }
  106.  
  107. void loop() {
  108.     Serial.println("beginning of loop");
  109.   //  Serial.println("ms Passed: ");
  110.   //  Serial.println(millis());
  111.   //      File dataFile = SD.open("holymoley.txt", FILE_WRITE);//Instantiate SD card ONLY 1 open at a time!!!!!
  112.   //      Serial.println(dataFile);
  113.   //      Serial.println("^^^^^dataFile");
  114.   //      // if the file is available, write to it:
  115.   //      if (dataFile) {
  116.   //        dataFile.println("fartso, fatso");
  117.   //        dataFile.close();
  118.   //        // print to the serial port too:
  119.   //        Serial.println("closed card");//////////////////////////////////////////
  120.   //      }
  121.   //      // if the file isn't open, pop up an error:
  122.   //      else {
  123.   //        Serial.println("ERROR opening holeymoley.txt");
  124.   //      }
  125.  
  126.  
  127.  
  128.  
  129.   // uint8_t system, gyro, accel, mag = 0;
  130.   // bno.getCalibration(&system, &gyro, &accel, &mag);
  131.   // Serial.println();
  132.   // Serial.print("Calibration: Sys=");
  133.   // Serial.print(system);
  134.   // Serial.print(" Gyro=");
  135.   // Serial.print(gyro);
  136.   // Serial.print(" Accel=");
  137.   // Serial.print(accel);
  138.  
  139.  
  140.   // Serial.println("--");
  141.   measureColor();
  142.  
  143.   //  if (Serial.available()) {
  144.   //    char c = Serial.read();
  145.   //    GPSSerial.println(c);
  146.   //  }
  147.   //  if (GPSSerial.available()) {
  148.   //    char c = GPSSerial.read();
  149.   //    Serial.print(c);
  150.   //  }
  151. }
  152.  
  153.  
  154. ////////////////////////////////////////////Functions///////////////////////////////////
  155. ////--------------------------
  156. void measureColor() {
  157.   Serial.println("line 157/");
  158.   float red, green, blue;
  159.   tcs.setInterrupt(false);  // turn on LED
  160.  
  161.   delay(60);  // takes 50ms to read
  162.  
  163.   tcs.getRGB(&red, &green, &blue);
  164.  
  165.   Serial.print("R:\t"); Serial.print(int(red));
  166.   Serial.print("\tG:\t"); Serial.print(int(green));
  167.   Serial.print("\tB:\t"); Serial.print(int(blue));
  168.   Serial.println();
  169. }
  170. //--------------------------
  171. void printEvent(sensors_event_t* event) {
  172.   double x = -1000000, y = -1000000 , z = -1000000; //dumb values, easy to spot problem
  173.   if (event->type == SENSOR_TYPE_ACCELEROMETER) {
  174.     Serial.print("Accl:");
  175.     x = event->acceleration.x;
  176.     y = event->acceleration.y;
  177.     z = event->acceleration.z;
  178.   }
  179.   else if (event->type == SENSOR_TYPE_ORIENTATION) {
  180.     Serial.print("Orient:");
  181.     x = event->orientation.x;
  182.  
  183.     y = event->orientation.y;
  184.     z = event->orientation.z;
  185.     data[3] = x;
  186.     data[4] = y;
  187.     data[5] = z;
  188.   }
  189.   else if (event->type == SENSOR_TYPE_GYROSCOPE) {
  190.     Serial.print("Gyro:");
  191.     x = event->gyro.x;
  192.     y = event->gyro.y;
  193.     z = event->gyro.z;
  194.   }
  195.   else if (event->type == SENSOR_TYPE_ROTATION_VECTOR) {
  196.     Serial.print("Rot:");
  197.     x = event->gyro.x;
  198.     y = event->gyro.y;
  199.     z = event->gyro.z;
  200.   }
  201.   else if (event->type == SENSOR_TYPE_LINEAR_ACCELERATION) {
  202.     Serial.print("Linear:");
  203.     x = event->acceleration.x;
  204.     y = event->acceleration.y;
  205.     z = event->acceleration.z;
  206.   }
  207.   else if (event->type == SENSOR_TYPE_GRAVITY) {
  208.     Serial.print("Gravity:");
  209.     x = event->acceleration.x;
  210.     y = event->acceleration.y;
  211.     z = event->acceleration.z;
  212.   }
  213.   else {
  214.     Serial.print("Unk:");
  215.   }
  216.  
  217.   Serial.print("\tx= ");
  218.   Serial.print(x);
  219.   Serial.print(" |\ty= ");
  220.   Serial.print(y);
  221.   Serial.print(" |\tz= ");
  222.   Serial.println(z);
  223.   //Print elapsed time
  224.   Serial.println("ms Passed: ");
  225.   Serial.println(millis());
  226.   Serial.println(msElapsed);
  227. }
  228. //------------------------------------------------------------------------------
  229. // Write data header.
  230. void writeHeader() {
  231.   file.print(F("micros"));
  232.   file.write(",");
  233.   for (uint8_t i = 0; i < ANALOG_COUNT; i++) {
  234.     file.print(dataPoints[i]);
  235.     file.write(",");
  236.   }
  237.   file.println();
  238. }
  239. //-----------------------------------------------------------
  240.  
  241.  
  242. //------------------------------------------------------------------------------
  243. // Log a data record.
  244. void logData() {
  245.   int column;
  246.   //data[0] = logTime;
  247.   data[1] = "Lat";
  248.   data[2] = "Long";
  249.  
  250.   data[6] = "";
  251.   data[7] = "";
  252.   data[8] = "";
  253.   data[9] = "";
  254.   data[10] = "";
  255.   data[11] = "";
  256.   data[12] = "";
  257.   data[13] = "";
  258.   data[14] = "";
  259.   data[15] = "";
  260.   data[16] = "";
  261.   data[17] = "";
  262.   // Read all channels to avoid SD write latency between readings.
  263.   for (uint8_t i = 0; i < ANALOG_COUNT; i++) {
  264.     data[i] = analogRead(i);
  265.   }
  266.   // Write data to file.  Start with log time in micros.
  267.   file.print(logTime);
  268.  
  269.   // Write ADC data to CSV record.
  270.   for (uint8_t i = 0; i < ANALOG_COUNT; i++) {
  271.     file.write(',');
  272.     file.print(data[i]);
  273.   }
  274.   file.println();
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement