#include <FastLED.h> // FastLED library
#include <OneWire.h> // OneWire library
#include <DallasTemperature.h> // DallasTemperature library
#define ONE_WIRE_BUS 2 // Data wire is plugged into pin 2 on the Arduino
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature sensor
float temperatureC = 0; // Temperature in Celsius
int tempC = 0; // Temperature in Celsius
int tempMin = 50; // Minimum temperature
int tempMax = 92; // Maximum temperature
#define LED_PIN1 6 // LED pin
#define NUM_LEDS1 15 // Number of LEDs
CRGB leds1[NUM_LEDS1]; // LED array
int leds1_color_index = 0; // LED color index
int leds1_light_index = 0; // LED light index
int leds1_brightness = 50; // LED brightness
int buzzPin = 9; // Buzzer pin
void setup()
{
Serial.begin(9600); // Initialize serial port
sensors.begin(); // Initialize temperature sensor
pinMode(buzzPin, OUTPUT); // Initialize buzzer pin
FastLED.addLeds<WS2812B, LED_PIN1, GRB>(leds1, NUM_LEDS1); // Initialize LED strip
FastLED.setBrightness(leds1_brightness); // Set LED brightness
FastLED.clear(); // Clear LED strip
FastLED.show(); // Update LED strip
}
void loop()
{
temperatureC = getTemperature(); // Get temperature
tempC = int(temperatureC); // Convert temperature to integer
Serial.print("Temperature: "); // Print temperature
Serial.print(tempC);
Serial.println(" *C");
tempC = constrain(tempC, tempMin, tempMax); // Constrain temperature to min and max
leds1_color_index = map(tempC, tempMin, tempMax, 180, 255); // Map temperature to color
leds1_light_index = map(tempC, tempMin, tempMax, 0, NUM_LEDS1); // Map temperature to light index
leds1_brightness = map(tempC, tempMin, tempMax, 50, 255); // Map temperature to brightness
// Set LED strip color, brightness, and light index
fill_palette(leds1, leds1_light_index, leds1_color_index, 0,RainbowColors_p,leds1_brightness, LINEARBLEND);
fill_palette(leds1 + leds1_light_index, NUM_LEDS1 - leds1_light_index, leds1_color_index, 0, RainbowColors_p, 0, LINEARBLEND);
FastLED.show(); // Update LED strip
delay(100); // Wait 100ms
// If temperature is greater than 90 degrees, turn on buzzer
if (tempC > 90)
{
analogWrite(buzzPin, 200);
}
else
{
analogWrite(buzzPin, 0);
}
}
// get temperature value
float getTemperature()
{
sensors.requestTemperatures();
float temp = sensors.getTempCByIndex(0);
return temp;
}