Skip to content

Instantly share code, notes, and snippets.

@JoshLmao
Created April 17, 2019 18:32
Show Gist options
  • Save JoshLmao/d28e3a944c49894b1428fd136464775a to your computer and use it in GitHub Desktop.
Save JoshLmao/d28e3a944c49894b1428fd136464775a to your computer and use it in GitHub Desktop.
Microphone Temperature Controller & LED Display
/* Josh Shepherd - 1700471
* Microphone Temperature Controller & LED Display
*
* Purpose:
* Monitors the current temperature and opens a window connected
* to a servo if it gets too hot. Can also display warm or cold color
* on LED.
* Microphone monitors for sound and changes to red LED and closes the window
* if the volume hits maximum (User shouts to close window).
* Will remain closed for 30 minutes and opens if temperture is too hot
*
* Fritzing diagrams: https://imgur.com/a/Eub0EVd
*/
#include <Servo.h>
// Analog pin where the Microphone is
const int m_micPin = A3;
// Analog pin where the Temperature Sensor is
const int m_tempPin = A5;
// Digital pin leading to the Red value of the RGB LED
const int m_rPin = 6;
// Digital pin leading to the Green value of the RGB LED
const int m_gPin = 5;
// Digital pin leading to the Blue value of the RGB LED
const int m_bPin = 7;
// Digital pin leading to the Red value of the RGB LED
const int m_servoPin = 0;
// Temperature to keep the room at or below
const int m_targetTemperature = 25;
// Value (between 0-100) the microphone needs to be to close for 30 mins
const int m_micVolTarget = 100;
// Time in ms for updating the LED
const int m_ledUpdateMs = 500;
// Time in ms for checking temperture and updating the servo position
const int m_tempertureUpdateMs = 10000;
// Servo to control the window
Servo m_windowArm;
// Store the previous update time and temperature
int m_lastUpdateMs = 0;
int m_lastUpdateTemp = 0;
// Gets the value of the microphone
// Returned value between 0 and 100
int getMic()
{
int micValue = analogRead(m_micPin);
return map(micValue, 0, 1023, 0, 100);
}
// Gets the temperature of the Temperature Sensor
// Returned in Celcius
int getTemp()
{
int temperature = analogRead(m_tempPin);
return temperature / 110 * 1023;
}
// Sets the LED to a certain color
void setLedColor(int red, int green, int blue)
{
digitalWrite(m_rPin, red);
digitalWrite(m_gPin, green);
digitalWrite(m_bPin, blue);
}
// Update RGB LED color to temperture appropriate color
void updateLED(int currentTemp)
{
// Orange: 255, 107, 0
// Blue: 0, 107, 255
// Rescale temperature to between RGB values
int tempRescaled = map(currentTemp, 0, 40, 0, 255);
// Set RGB colors for the current temp
int r = tempRescaled;
int g = 107;
int b = 255 - tempRescaled;
setLedColor(r, g, b);
}
// Updates the window arm to the correct position depending on current temperature
void updateServo(int currentTemp)
{
int servoPos = m_windowArm.read();
// Set servo to be open if above target temperature or closed
if(currentTemp > m_targetTemperature)
{
m_windowArm.write(180);
}
else if(servoPos > 0)
{
m_windowArm.write(0);
}
}
void setup()
{
// Configure RGB pins to be outputs
pinMode(m_rPin, OUTPUT);
pinMode(m_gPin, OUTPUT);
pinMode(m_bPin, OUTPUT);
// Use internal reference for analog readings
analogReference(INTERNAL);
// Config Servo class to servo pin
m_windowArm.attach(m_servoPin);
}
void loop()
{
int totalMs = millis();
// Run update if enough time has passed between previous update
if(totalMs - m_lastUpdateMs > m_tempertureUpdateMs)
{
int temp = getTemp();
if(m_lastUpdateTemp > 0)
{
// Delay next update by 30 mins if micVolume is above target
// (User wants window to be shut)
int micValue = getMic();
if (micValue >= m_micVolTarget)
{
temp = 0;
// Add 30 mins to lastUpdateMs and store temp
m_lastUpdateMs = totalMs + 30000;
m_lastUpdateTemp = temp;
}
// Update Servo and LED with appropriate values
updateServo(temp);
updateLED(temp);
m_lastUpdateMs = totalMs;
m_lastUpdateTemp = temp;
}
else
{
m_lastUpdateMs = totalMs;
m_lastUpdateTemp = temp;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment