Advertisement
Guest User

HAB Sensor Payload Programming

a guest
Feb 24th, 2015
707
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.21 KB | None | 0 0
  1. /*
  2.  Weather Shield Example Used in this code.
  3.  This code was utilized by Nico for a High Altitutde Balloon Payload.
  4.  Geiger Counter example used in this code.
  5.  */
  6.  
  7. #include <Wire.h> //I2C needed for sensors
  8. #include "MPL3115A2.h" //Pressure sensor
  9. #include "HTU21D.h" //Humidity sensor
  10.  
  11. MPL3115A2 myPressure; //Create an instance of the pressure sensor
  12. HTU21D myHumidity; //Create an instance of the humidity sensor
  13.  
  14. //Hardware pin definitions
  15. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  16. // digital I/O pins
  17. const byte WSPEED = 3;
  18. const byte RAIN = 2;
  19. const byte STAT1 = 7;
  20. const byte STAT2 = 8;
  21.  
  22. // analog I/O pins
  23. const byte REFERENCE_3V3 = A3;
  24. const byte LIGHT = A1;
  25. const byte BATT = A2;
  26. const byte WDIR = A0;
  27. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  28.  
  29. //Global Variables
  30. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  31. long lastSecond; //The millis counter to see when a second rolls by
  32. byte seconds; //When it hits 60, increase the current minute
  33. byte seconds_2m; //Keeps track of the "wind speed/dir avg" over last 2 minutes array of data
  34. byte minutes; //Keeps track of where we are in various arrays of data
  35. byte minutes_10m; //Keeps track of where we are in wind gust/dir over last 10 minutes array of data
  36.  
  37. long lastWindCheck = 0;
  38. volatile long lastWindIRQ = 0;
  39. volatile byte windClicks = 0;
  40.  
  41. int radiation = 0; //CPM value for Geiger counter
  42. //These are all the weather values that wunderground expects:
  43. float humidity = 0; // [%]
  44. float tempf = 0; // [temperature F]
  45. //float baromin = 30.03;// [barom in] - It's hard to calculate baromin locally, do this in the agent
  46. float pressure = 0;
  47. //float dewptf; // [dewpoint F] - It's hard to calculate dewpoint locally, do this in the agent
  48.  
  49. float batt_lvl = 11.8; //[analog value from 0 to 1023]
  50. float light_lvl = 455; //[analog value from 0 to 1023]
  51.  
  52.  
  53.  
  54. int i;
  55. int count;
  56. int old = -1;
  57. int check;
  58. float CPM;
  59. float now;
  60. float time;
  61. int start;
  62. int statusled = 10;
  63. int sketchstatusled = 12;
  64. char OnesString[10];
  65. char DecimalString[10];
  66. char TimerString[10];
  67.  
  68. // volatiles are subject to modification by IRQs
  69.  
  70. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  71.  
  72.  
  73.  
  74. void setup()
  75. {
  76.   Serial.begin(9600); //Serial port for Output to OpenLog
  77.   Serial1.begin(9600);
  78.   Serial.println("MCP HAB Project");
  79.  
  80.   pinMode(STAT1, OUTPUT); //Status LED Blue
  81.   pinMode(STAT2, OUTPUT); //Status LED Green
  82.  
  83.  
  84.   pinMode(REFERENCE_3V3, INPUT);
  85.   pinMode(LIGHT, INPUT);
  86.  
  87.   //Configure the pressure sensor
  88.   myPressure.begin(); // Get sensor online
  89.   myPressure.setModeBarometer(); // Measure pressure in Pascals from 20 to 110 kPa
  90.   myPressure.setOversampleRate(7); // Set Oversample to the recommended 128
  91.   myPressure.enableEventFlags(); // Enable all three pressure and temp event flags
  92.  
  93.   //Configure the humidity sensor
  94.   myHumidity.begin();
  95.  
  96.   seconds = 0;
  97.   lastSecond = millis();
  98.  
  99.  
  100.  
  101.   Serial.println("Weather Shield online.");
  102.  
  103. }
  104.  
  105. void loop()
  106. {
  107.   //Keep track of which minute it is
  108.   if(millis() - lastSecond >= 1000)
  109.   {
  110.     digitalWrite(STAT1, HIGH); //Blink stat LED
  111.    
  112.     lastSecond += 1000;
  113.  
  114.     //Report all readings every second
  115.     printWeather();
  116.  
  117.     digitalWrite(STAT1, LOW); //Turn off stat LED
  118.   }
  119.  
  120.  
  121.  
  122.  
  123.  
  124.   delay(100);
  125. }
  126.  
  127. //Calculates each of the variables that wunderground is expecting
  128. void calcWeather()
  129. {
  130.  
  131.   //Calc humidity
  132.   humidity = myHumidity.readHumidity();
  133.   //float temp_h = myHumidity.readTemperature();
  134.   //Serial.print(" TempH:");
  135.   //Serial.print(temp_h, 2);
  136.  
  137.   //Calc tempf from pressure sensor
  138.   tempf = myPressure.readTempF();
  139.   //Serial.print(" TempP:");
  140.   //Serial.print(tempf, 2);
  141.  
  142.  
  143.   //Calc pressure
  144.   pressure = myPressure.readPressure();
  145.  
  146.   //Calc dewptf
  147.  
  148.   //Calc light level
  149.   light_lvl = get_light_level();
  150.  
  151.   //Calc battery level
  152.   batt_lvl = get_battery_level();
  153.  
  154.   //Calc CPM.
  155.   radiation = GetCPM();
  156. }
  157. int GetCPM()
  158. {
  159.  
  160.   i = 0;
  161.   count = 0;
  162.   start = millis();
  163.   while (i < 30)
  164.   {
  165.     if(Serial1.available() > 0)
  166.     {
  167.       check = Serial1.read();
  168.       if(check > 0)
  169.       {
  170.         count++;
  171.       }
  172.     }
  173.     i = millis();
  174.     i = i - start;
  175.     i = i/1000;
  176.    
  177.   }
  178.  
  179.   CPM = count*2;
  180.   old = count; //resetting the if statement
  181.   int cpm = CPM;
  182.   int temp = CPM*1000;
  183.   int decimal = temp % (cpm * 1000);
  184.   return cpm;
  185. }
  186.  
  187.  
  188.  
  189.  
  190. //Returns the voltage of the light sensor based on the 3.3V rail
  191. //This allows us to ignore what VCC might be (an Arduino plugged into USB has VCC of 4.5 to 5.2V)
  192. float get_light_level()
  193. {
  194.   float operatingVoltage = analogRead(REFERENCE_3V3);
  195.  
  196.   float lightSensor = analogRead(LIGHT);
  197.  
  198.   operatingVoltage = 3.3 / operatingVoltage; //The reference voltage is 3.3V
  199.  
  200.   lightSensor = operatingVoltage * lightSensor;
  201.  
  202.   return(lightSensor);
  203. }
  204.  
  205. //Returns the voltage of the raw pin based on the 3.3V rail
  206. //This allows us to ignore what VCC might be (an Arduino plugged into USB has VCC of 4.5 to 5.2V)
  207. //Battery level is connected to the RAW pin on Arduino and is fed through two 5% resistors:
  208. //3.9K on the high side (R1), and 1K on the low side (R2)
  209. float get_battery_level()
  210. {
  211.   float operatingVoltage = analogRead(REFERENCE_3V3);
  212.  
  213.   float rawVoltage = analogRead(BATT);
  214.  
  215.   operatingVoltage = 3.30 / operatingVoltage; //The reference voltage is 3.3V
  216.  
  217.   rawVoltage = operatingVoltage * rawVoltage; //Convert the 0 to 1023 int to actual voltage on BATT pin
  218.  
  219.   rawVoltage *= 4.90; //(3.9k+1k)/1k - multiple BATT voltage by the voltage divider to get actual system voltage
  220.  
  221.   return(rawVoltage);
  222. }
  223.  
  224.  
  225.  
  226. //Prints the various variables directly to the port
  227. //I don't like the way this function is written but Arduino doesn't support floats under sprintf
  228. void printWeather()
  229. {
  230.   calcWeather(); //Go calc all the various sensors
  231.  
  232.   Serial.println();
  233.   Serial.print(",CPM=");
  234.   Serial.print(CPM, 1);
  235.   Serial.print(",humidity=");
  236.   Serial.print(humidity, 1);
  237.   Serial.print(",tempf=");
  238.   Serial.print(tempf, 1);
  239.   Serial.print(",pressure=");
  240.   Serial.print(pressure, 2);
  241.   Serial.print(",batt_lvl=");
  242.   Serial.print(batt_lvl, 2);
  243.   Serial.print(",light_lvl=");
  244.   Serial.print(light_lvl, 2);
  245.   Serial.print(",");
  246.   Serial.println("#");
  247.  
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement