pleasedontcode

Temperature Indicator rev_01

Jul 25th, 2025
633
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Temperature Indicator
  13.     - Source Code NOT compiled for: Arduino Nano
  14.     - Source Code created on: 2025-07-25 23:45:21
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Integrate the DS18B20 library for temperature */
  21.     /* sensing and ensure the code reads temperature data */
  22.     /* from the connected sensor. Use PWM outputs to */
  23.     /* control the RGB LED color fading based on */
  24.     /* temperature thresholds at 30°C (blue) and 60°C */
  25.     /* (orange). */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /* START CODE */
  29. /****** DEFINITION OF LIBRARIES *****/
  30. // Include the DallasTemperature library for DS18B20
  31. #include <OneWire.h>
  32. #include <DallasTemperature.h>
  33.  
  34. // Define pins
  35. const int oneWireBusPin = 2; // Pin where DS18B20 data line is connected
  36. const int redLEDPin = 9;     // PWM pin for Red LED
  37. const int greenLEDPin = 10;  // PWM pin for Green LED
  38. const int blueLEDPin = 11;   // PWM pin for Blue LED
  39.  
  40. // Create instances for OneWire and DallasTemperature
  41. OneWire oneWire(oneWireBusPin);
  42. DallasTemperature sensors(&oneWire);
  43.  
  44. // Variables to store temperature
  45. float temperatureC = 0.0;
  46.  
  47. // Function prototypes
  48. void setup(void);
  49. void loop(void);
  50.  
  51. /****** FUNCTION IMPLEMENTATION *****/
  52.  
  53. void setup(void)
  54. {
  55.   // Initialize serial communication for debugging
  56.   Serial.begin(9600);
  57.  
  58.   // Initialize the temperature sensor
  59.   sensors.begin();
  60.  
  61.   // Set LED pins as outputs
  62.   pinMode(redLEDPin, OUTPUT);
  63.   pinMode(greenLEDPin, OUTPUT);
  64.   pinMode(blueLEDPin, OUTPUT);
  65. }
  66.  
  67. void loop(void)
  68. {
  69.   // Request temperature measurement
  70.   sensors.requestTemperatures();
  71.   // Read temperature in Celsius
  72.   temperatureC = sensors.getTempCByIndex(0);
  73.  
  74.   // Debug: print temperature
  75.   Serial.print("Temperature: ");
  76.   Serial.print(temperatureC);
  77.   Serial.println(" °C");
  78.  
  79.   // Map temperature to RGB color
  80.   // Below 30°C: Blue
  81.   // Between 30°C and 60°C: Fade from Blue to Orange
  82.   // Above 60°C: Orange
  83.  
  84.   if (temperatureC <= 30)
  85.   {
  86.     // Fully blue
  87.     analogWrite(redLEDPin, 0);
  88.     analogWrite(greenLEDPin, 0);
  89.     analogWrite(blueLEDPin, 255);
  90.   }
  91.   else if (temperatureC >= 60)
  92.   {
  93.     // Fully orange (Red + Green)
  94.     analogWrite(redLEDPin, 255);
  95.     analogWrite(greenLEDPin, 165); // Approximate orange
  96.     analogWrite(blueLEDPin, 0);
  97.   }
  98.   else
  99.   {
  100.     // Fade between blue and orange
  101.     // Calculate the interpolation factor
  102.     float factor = (temperatureC - 30) / (60 - 30);
  103.     // Blue decreases from 255 to 0
  104.     int blueValue = (int)(255 * (1 - factor));
  105.     // Red increases from 0 to 255
  106.     int redValue = (int)(255 * factor);
  107.     // Green increases from 0 to 165
  108.     int greenValue = (int)(165 * factor);
  109.    
  110.     analogWrite(redLEDPin, redValue);
  111.     analogWrite(greenLEDPin, greenValue);
  112.     analogWrite(blueLEDPin, blueValue);
  113.   }
  114.  
  115.   delay(1000); // Wait for 1 second before next reading
  116. }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment