Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Temperature Indicator
- - Source Code NOT compiled for: Arduino Nano
- - Source Code created on: 2025-07-25 23:45:21
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Integrate the DS18B20 library for temperature */
- /* sensing and ensure the code reads temperature data */
- /* from the connected sensor. Use PWM outputs to */
- /* control the RGB LED color fading based on */
- /* temperature thresholds at 30°C (blue) and 60°C */
- /* (orange). */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- // Include the DallasTemperature library for DS18B20
- #include <OneWire.h>
- #include <DallasTemperature.h>
- // Define pins
- const int oneWireBusPin = 2; // Pin where DS18B20 data line is connected
- const int redLEDPin = 9; // PWM pin for Red LED
- const int greenLEDPin = 10; // PWM pin for Green LED
- const int blueLEDPin = 11; // PWM pin for Blue LED
- // Create instances for OneWire and DallasTemperature
- OneWire oneWire(oneWireBusPin);
- DallasTemperature sensors(&oneWire);
- // Variables to store temperature
- float temperatureC = 0.0;
- // Function prototypes
- void setup(void);
- void loop(void);
- /****** FUNCTION IMPLEMENTATION *****/
- void setup(void)
- {
- // Initialize serial communication for debugging
- Serial.begin(9600);
- // Initialize the temperature sensor
- sensors.begin();
- // Set LED pins as outputs
- pinMode(redLEDPin, OUTPUT);
- pinMode(greenLEDPin, OUTPUT);
- pinMode(blueLEDPin, OUTPUT);
- }
- void loop(void)
- {
- // Request temperature measurement
- sensors.requestTemperatures();
- // Read temperature in Celsius
- temperatureC = sensors.getTempCByIndex(0);
- // Debug: print temperature
- Serial.print("Temperature: ");
- Serial.print(temperatureC);
- Serial.println(" °C");
- // Map temperature to RGB color
- // Below 30°C: Blue
- // Between 30°C and 60°C: Fade from Blue to Orange
- // Above 60°C: Orange
- if (temperatureC <= 30)
- {
- // Fully blue
- analogWrite(redLEDPin, 0);
- analogWrite(greenLEDPin, 0);
- analogWrite(blueLEDPin, 255);
- }
- else if (temperatureC >= 60)
- {
- // Fully orange (Red + Green)
- analogWrite(redLEDPin, 255);
- analogWrite(greenLEDPin, 165); // Approximate orange
- analogWrite(blueLEDPin, 0);
- }
- else
- {
- // Fade between blue and orange
- // Calculate the interpolation factor
- float factor = (temperatureC - 30) / (60 - 30);
- // Blue decreases from 255 to 0
- int blueValue = (int)(255 * (1 - factor));
- // Red increases from 0 to 255
- int redValue = (int)(255 * factor);
- // Green increases from 0 to 165
- int greenValue = (int)(165 * factor);
- analogWrite(redLEDPin, redValue);
- analogWrite(greenLEDPin, greenValue);
- analogWrite(blueLEDPin, blueValue);
- }
- delay(1000); // Wait for 1 second before next reading
- }
Advertisement
Add Comment
Please, Sign In to add comment