Guest User

Untitled

a guest
Dec 11th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. //the scale factor of TMP36 (temperature sensor) is 10 mV/°C with a 500 mV offset to allow for negative temperatures
  2. #include <Wire.h>
  3. #include <math.h>
  4.  
  5. // the analog pin number connected to the TMP36
  6. #define slaveAddr 0x08
  7.  
  8. // defining global flags as actual registers
  9. #define RD_REG_TEMP_INT_CEL 0x00
  10. #define RD_REG_TEMP_FRA_CEL 0x01
  11. #define RD_REG_TEMP_INT_FAR 0x02
  12. #define RD_REG_TEMP_FRA_FAR 0x03
  13.  
  14. //define the analog port that is used on Arduino Dock 2
  15. int sensorPin = A0;
  16.  
  17. //Byte that is being receiving
  18. byte recvData;
  19.  
  20. //Global variables of the temperature to be accessible at any time
  21. int celsiusInt;
  22. int celsiusFrac;
  23. int fahrenheitInt;
  24. int fahrenheitFrac;
  25.  
  26. void setup()
  27. {
  28. //received data from the Master
  29. recvData = 0xff;
  30.  
  31. //initializing serial communication with the Omega2 for sending sensor data
  32. Serial.begin(9600);
  33.  
  34. // initializing i2c interface
  35. Wire.begin(slaveAddr);
  36. Wire.onReceive(receiveEvent);
  37. Wire.onRequest(requestEvent);
  38. }
  39.  
  40. void receiveEvent(int bytesReceived)
  41. {
  42. // loop through all received bytes
  43. for (int i = 0; i < bytesReceived; i++)
  44. {
  45. if (i == 0)
  46. {
  47. // only store the very first received byte
  48. recvData = Wire.read();
  49. }
  50. else
  51. {
  52. // make sure to read the rest of the bytes to clear the i2c buffer
  53. Wire.read();
  54. }
  55. }
  56. }
  57.  
  58. //Function definision for requesting the values from the slave (ATmega) and transfer it over to the master (Omega).
  59. //We can only send one value at a time
  60. void requestEvent()
  61. {
  62. if (recvData == RD_REG_TEMP_INT_CEL)
  63. {
  64. Wire.write((byte)celsiusInt);
  65. }
  66. else if (recvData == RD_REG_TEMP_FRA_CEL)
  67. {
  68. Wire.write((byte)celsiusFrac);
  69. }
  70. else if (recvData == RD_REG_TEMP_INT_FAR)
  71. {
  72. Wire.write((byte)fahrenheitInt);
  73. }
  74. else if (recvData == RD_REG_TEMP_FRA_FAR)
  75. {
  76. Wire.write((byte)fahrenheitFrac);
  77. }
  78. else
  79. {
  80. Wire.write(0xff);
  81. }
  82. }
  83.  
  84. float ReadSensor()
  85. {
  86. // getting the voltage reading from the temperature sensor
  87. int reading = analogRead(sensorPin);
  88. // convert the analog reading (0 to 1023) to voltage (0 - 5V)
  89. float voltage = (float)reading * 5.0;
  90. voltage /= 1024.0;
  91. return (voltage);
  92. }
  93.  
  94. float convertVoltTocelsius(float voltage)
  95. {
  96. float temperatureC = (voltage - 0.5) * 100;
  97. return (temperatureC);
  98. }
  99.  
  100. float convertCelcToFahrenheit (float temperatureC)
  101. {
  102. float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
  103. return (temperatureF);
  104. }
  105.  
  106. //A function definition to get an Integer part of the reading
  107. //e.g. you get a value of 26.73. This function returns 26 as you can only send one byte at a time
  108. int GetInteger (float inputFloat)
  109. {
  110. int intPart=(int)inputFloat;
  111. return intPart;
  112. }
  113.  
  114. //A function definition to get a Fractional part of the reading
  115. //e.g. you get a value of 26.73. This function returns 73 as you can only send one byte at a time
  116. int GetFractional(float inputFloat)
  117. {
  118. float frac = (inputFloat-GetInteger(inputFloat))+0.05; // +0.05 is added to either round up or down the end result
  119. frac = frac*100;
  120. return (int)frac;
  121. }
  122.  
  123. void loop()
  124. {
  125. float voltage_reading = ReadSensor();
  126. float celsius = convertVoltTocelsius(voltage_reading);
  127. float fahrenheit = convertCelcToFahrenheit(celsius);
  128. //functions to get Integer and Fractional part of celsius and Fahrenheit values
  129. celsiusInt = GetInteger(celsius);
  130. celsiusFrac = GetFractional(celsius);
  131.  
  132. fahrenheitInt = GetInteger(fahrenheit);
  133. fahrenheitFrac = GetFractional(fahrenheit);
  134. /* This is optional feature to see the values that are correspondin to Celsius and Fahrenheit
  135. Serial.print("Celcius Integer: "); Serial.println(celsiusInt);
  136. Serial.print("Celcius Fractional: "); Serial.println(celsiusFrac);
  137. Serial.print("Fahrenheit Integer: "); Serial.println(fahrenheitInt);
  138. Serial.print("Fahrenheit Fractional: "); Serial.println(fahrenheitFrac);
  139. */
  140. delay(1000); // delay between sensor reads
  141. }
Add Comment
Please, Sign In to add comment