Advertisement
Guest User

Untitled

a guest
Jun 1st, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. //TMP36 Pin Variables
  2. int temperaturePin = 0; //input: the analog pin the TMP36 is connected
  3.  
  4. //RGB LED pins
  5. int ledDigitalOne[] = {9, 10, 11}; //output: the three digital pins of the RGB LED
  6. //9 = redPin, 10 = greenPin, 11 = bluePin
  7.  
  8. const boolean ON = LOW;
  9. const boolean OFF = HIGH;
  10.  
  11. //Predefined Colors
  12. const boolean RED[] = {ON, OFF, OFF};
  13. const boolean GREEN[] = {OFF, ON, OFF};
  14. const boolean BLUE[] = {OFF, OFF, ON};
  15. const boolean YELLOW[] = {ON, ON, OFF};
  16. const boolean CYAN[] = {OFF, ON, ON};
  17. const boolean MAGENTA[] = {ON, OFF, ON};
  18. const boolean WHITE[] = {ON, ON, ON};
  19. const boolean BLACK[] = {OFF, OFF, OFF};
  20.  
  21. void setup()
  22. {
  23.   for (int i = 0; i < 3; i++) {
  24.     pinMode(ledDigitalOne[i], OUTPUT); //Set the RGB LED pins as outputs
  25.   }
  26.   Serial.begin(9600); //Start the serial connection with the computer
  27. }
  28. void loop()
  29. {
  30.   float temperature = getVoltage(temperaturePin); //getting the voltage reading from the temperature sensor
  31.   temperature = (((temperature - .5) * 100) * 1.8) + 32; //converting from 10 mv per degree wit 500 mV offset
  32.   int newTemperature = temperature; //to degrees ((volatge – 500mV) times 100)
  33.   Serial.println(newTemperature); //printing the result
  34.  
  35.   delay(7000); //waiting 7 seconds to get a new result
  36.  
  37.   // Set the three LEDs to any predefined color depending of the temperature in ΒΊF
  38.   if ((newTemperature > 40) && (newTemperature <= 71)) {
  39.     setColor(ledDigitalOne, BLACK);
  40.     Serial.println("Black");
  41.   }
  42.   else if ((newTemperature >= 72) && (newTemperature <= 73)) {
  43.     setColor(ledDigitalOne, WHITE);
  44.     Serial.println("White");
  45.   }
  46.   else if ((newTemperature >= 74) && (newTemperature <= 75)) {
  47.     setColor(ledDigitalOne, GREEN);
  48.     Serial.println("Green");
  49.   }
  50.   else if ((newTemperature >= 76) && (newTemperature <= 77)) {
  51.     setColor(ledDigitalOne, CYAN);
  52.     Serial.println("Cyan");
  53.   }
  54.   else if ((newTemperature >= 78) && (newTemperature <= 79)) {
  55.     setColor(ledDigitalOne, BLUE);
  56.     Serial.println("Blue");
  57.   }
  58.   else if ((newTemperature >= 80) && (newTemperature <= 81)) {
  59.     setColor(ledDigitalOne, YELLOW);
  60.     Serial.println("Yellow");
  61.   }
  62.   else if ((newTemperature >= 82) && (newTemperature <= 83)) {
  63.     setColor(ledDigitalOne, RED);
  64.     Serial.println("Red");
  65.   }
  66.   else {
  67.     setColor(ledDigitalOne, MAGENTA);
  68.     Serial.println("Magenta");
  69.   }
  70. }
  71. float {getVoltage(int pin) {
  72.     return (analogRead(pin) * .004882814); //converting from a 0 to 1024 digital range
  73.     // to 0 to 5 volts (each 1 reading equals ~ 5 millivolts
  74.   }}
  75. // Function to set the color
  76. void {setColor(int* led, boolean * color) {
  77.     for (int i = 0; i < 3; i++) {
  78.       digitalWrite(led[i], color[i]);
  79.     }
  80.   }
  81. }
  82. // A version of setColor that allows for using const boolean colors
  83. void setColor(int* led, const boolean* color) {
  84.   boolean tempColor[] = {color[0], color[1], color[2]};
  85.   setColor(led, tempColor);
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement