PageRenderTime 23ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/dannys_temperatureRGB_setupFINISHEDMODULAR.pde

https://github.com/DannyGITHUB/Danny-Huynh-3292443
Processing | 232 lines | 148 code | 43 blank | 41 comment | 6 complexity | 2635db5d1ddbc48ed3ecfdf22406e04c MD5 | raw file
  1. // Danny Huynh's Temperature RGB LED setup
  2. // Based on code taken from Circuit 10 in Sparksfun Circuit to read and print temperature output.
  3. // http://ardx.org/CODE10
  4. // Copyright 2011 : Danny Huynh, z3292443, UNSW
  5. #include <Firmata.h>
  6. int totalPins = 16;
  7. //TMP36 Pin Variables
  8. int temperaturePin = 0; //the analog pin the TMP36's Vout (sense) pin is connected to
  9. //the resolution is 10 mV / degree centigrade
  10. //(500 mV offset) to make negative temperatures an option
  11. int temperaturePinTwo = 1; //the second analog pin TMP36's Vout (sense) pin is connected to
  12. //the resolution is 10mV / degree centigrade
  13. //(500 mV offset) to make negative temperatures an option
  14. int temperaturePinThree = 2; //the third analog pin TMP36's Vout (sense) pin is connected to
  15. //the resolution is 10mV / degree centigrade
  16. //(500 mV offset) to make negative temperatures an option
  17. /*
  18. * setup() - this function runs once when you turn your Arduino on
  19. * We initialize the serial connection with the computer
  20. */
  21. // LED leads connected to PWM pins
  22. const int RED_LED_PIN = 9; // Sets LED pin 9 to colour red
  23. const int GREEN_LED_PIN = 10; // Sets LED pin 10 to colour green
  24. const int BLUE_LED_PIN = 11; // Sets LED pin 11 to colour blue
  25. const int RED_LED_PIN2 = 5; // Sets second LED pin 5 to colour red
  26. const int GREEN_LED_PIN2 = 6; // Sets second LED pin 6 to colour green
  27. const int BLUE_LED_PIN2 = 7; // Sets second LED pin 7 to colour blue
  28. const int RED_LED_PIN3 = 2; // Sets third LED pin 2 to colour red
  29. const int GREEN_LED_PIN3 = 3; // Sets third LED pin 3 to colour green
  30. const int BLUE_LED_PIN3 = 4; // Sets third LED pin 4 to colour blue
  31. int MaxTemp = 40; // The max temperature range limit the sensor will peak at
  32. int MinTemp = 10; // The minimum temperature range limit the sensor will peak
  33. int sensorValue = 0; // The value given by the first temperature sensor analog pin
  34. int sensorValue2 = 1; // The value given by the second temperature sensor analog pin
  35. int sensorValue3 = 2; // The value given by the third temperature sensor analog pin
  36. int smoothing = 4; // An average value smoothing for the temperature sensors
  37. void setup()
  38. {
  39. Serial.begin(9600); //Start the serial connection with the copmuter
  40. Firmata.setFirmwareVersion(0, 1); //Firmata code example from public domain
  41. Firmata.begin(57600);
  42. }
  43. void loop() // run over and over again
  44. {
  45. // Firmata function
  46. delay(500);
  47. for (int i=0;i<totalPins;i++){
  48. Firmata.sendAnalog(i,i);
  49. float temperature = getVoltage(temperaturePin); //getting the voltage reading from the temperature sensor
  50. temperature = (temperature - .5) * 100; //converting from 10 mv per degree wit 500 mV offset
  51. //to degrees ((volatge - 500mV) times 100)
  52. float temperature2 = getVoltage(temperaturePinTwo); //getting the voltage reading from the temperature sensor
  53. temperature2 = (temperature2 - .5) * 100; //converting from 10 mv per degree wit 500 mV offset
  54. //to degrees ((volatge - 500mV) times 100)
  55. float temperature3 = getVoltage(temperaturePinThree); //getting the voltage reading from the temperature sensor
  56. temperature3 = (temperature3 - .5) * 100; //converting from 10 mv per degree wit 500 mV offset
  57. //to degrees ((volatge - 500mV) times 100)
  58. // Smoothing function added in to help fade RGB light colours
  59. // Source taken from : http://www.arduino.cc/en/Tutorial/Smoothing
  60. // This is used to prevent colour changing to spike so inconsistently.
  61. // Equation demonstrates a gradual process of gain rather dramatic shifting values.
  62. sensorValue = ((sensorValue * smoothing) + temperature)/(smoothing+1); // Works out the RGB output of the first temp sensor
  63. temperature = sensorValue;
  64. sensorValue2 = ((sensorValue2 * smoothing) + temperature2)/(smoothing+1); // // Works out the RGB output of the second temp sensor
  65. temperature2 = sensorValue2;
  66. sensorValue3 = ((sensorValue3 * smoothing) + temperature3)/(smoothing+1); // // Works out the RGB output of the third temp sensor
  67. temperature3 = sensorValue3;
  68. // At this point we have temperature in C
  69. // we map it to 0 to 360 hue
  70. float saturation = 1; // Between 0 and 1 (0 = gray, 1 = full color)
  71. float brightness = 1; // Between 0 and 1 (0 = dark, 1 is full brightness)
  72. float hue = map(temperature, MinTemp, MaxTemp, 200, 360);
  73. float hue2 = map(temperature2, MinTemp, MaxTemp, 200, 360);
  74. float hue3 = map(temperature3, MinTemp, MaxTemp, 200, 360);
  75. hue = constrain(hue, 0, 360);
  76. hue2 = constrain(hue2, 0, 360);
  77. hue3 = constrain(hue3, 0, 360);
  78. //float hue = (colorNumber / float(numColors)) * 360; // Number between 0 and 360
  79. long color = HSBtoRGB(hue, saturation, brightness);
  80. long color2 = HSBtoRGB(hue2, saturation, brightness);
  81. long color3 = HSBtoRGB(hue3, saturation, brightness);
  82. // Printing Function output to serial monitor
  83. Serial.print(temperature);
  84. Serial.print(",");
  85. Serial.print(hue);
  86. Serial.print(",");
  87. Serial.print(temperature2);
  88. Serial.print(",");
  89. Serial.print(hue2);
  90. Serial.print(",");
  91. Serial.print(temperature3);
  92. Serial.print(",");
  93. Serial.println(hue3);
  94. // Get the red, blue and green parts from generated color
  95. int red = color >> 16 & 255;
  96. int green = color >> 8 & 255;
  97. int blue = color & 255;
  98. int red2 = color2 >> 16 & 255;
  99. int green2 = color2 >> 8 & 255;
  100. int blue2 = color2 & 255;
  101. int red3 = color3 >> 16 & 255;
  102. int green3 = color3 >> 8 & 255;
  103. int blue3 = color3 & 255;
  104. // Assigning a colour to each PIN of the RGB light component.
  105. analogWrite(GREEN_LED_PIN, green);
  106. analogWrite(RED_LED_PIN, red);
  107. analogWrite(BLUE_LED_PIN, blue);
  108. // Assigning a colour to each PIN of the second RGB light component.
  109. analogWrite(GREEN_LED_PIN2, green2);
  110. analogWrite(RED_LED_PIN2, red2);
  111. analogWrite(BLUE_LED_PIN2, blue2);
  112. // Assigning a colour to each PIN of the third RGB light component.
  113. analogWrite(GREEN_LED_PIN3, green3);
  114. analogWrite(RED_LED_PIN3, red3);
  115. analogWrite(BLUE_LED_PIN3, blue3);
  116. delay(200); //waiting a second
  117. }
  118. /*
  119. * getVoltage() - returns the voltage on the analog input defined by
  120. * pin
  121. */
  122. float getVoltage(int pin){
  123. return (analogRead(pin) * .004882814); //converting from a 0 to 1024 digital range
  124. // to 0 to 5 volts (each 1 reading equals ~ 5 millivolts
  125. }
  126. // Following code used was sourced from Action Script;
  127. // http://www.actionscript.org/forums/showthread.php3?t=15155
  128. // Code functions as a converter for changing a hue, saturation and brightness
  129. long HSBtoRGB(float _hue, float _sat, float _brightness) {
  130. float red = 0.0;
  131. float green = 0.0;
  132. float blue = 0.0;
  133. if (_sat == 0.0) {
  134. red = _brightness;
  135. green = _brightness;
  136. blue = _brightness;
  137. } else {
  138. if (_hue == 360.0) {
  139. _hue = 0;
  140. }
  141. int slice = _hue / 60.0;
  142. float hue_frac = (_hue / 60.0) - slice;
  143. float aa = _brightness * (1.0 - _sat);
  144. float bb = _brightness * (1.0 - _sat * hue_frac);
  145. float cc = _brightness * (1.0 - _sat * (1.0 - hue_frac));
  146. switch(slice) {
  147. case 0:
  148. red = _brightness;
  149. green = cc;
  150. blue = aa;
  151. break;
  152. case 1:
  153. red = bb;
  154. green = _brightness;
  155. blue = aa;
  156. break;
  157. case 2:
  158. red = aa;
  159. green = _brightness;
  160. blue = cc;
  161. break;
  162. case 3:
  163. red = aa;
  164. green = bb;
  165. blue = _brightness;
  166. break;
  167. case 4:
  168. red = cc;
  169. green = aa;
  170. blue = _brightness;
  171. break;
  172. case 5:
  173. red = _brightness;
  174. green = aa;
  175. blue = bb;
  176. break;
  177. default:
  178. red = 0.0;
  179. green = 0.0;
  180. blue = 0.0;
  181. break;
  182. }
  183. }
  184. long ired = red * 255.0;
  185. long igreen = green * 255.0;
  186. long iblue = blue * 255.0;
  187. return long((ired << 16) | (igreen << 8) | (iblue));
  188. }
  189. // End of HSBtoRGB converter code segment sourced from Action Script