Created
October 30, 2016 05:06
-
-
Save pReya/5b5c5e9374119439c04358bdbf06e683 to your computer and use it in GitHub Desktop.
Arduino Temperature Alarm and bar LED script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <math.h> | |
float tempRaw, tempAvg; | |
uint8_t tempPin = 7; | |
uint8_t speakerPin = 2; | |
uint8_t barLedPins[] = { 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; | |
uint8_t barLedPinsLength = sizeof(barLedPins) / sizeof(barLedPins[0]); | |
uint8_t lowTempLimit = 24; | |
uint8_t highTempLimit = 36; | |
uint8_t tempDisplayRange = highTempLimit - lowTempLimit; | |
float tempDiffPerLed = tempDisplayRange / barLedPinsLength; | |
float tempArray[10]; | |
uint8_t tempArrayLength = sizeof(tempArray) / sizeof(tempArray[0]); | |
uint8_t noOfLedsToActivate; | |
uint8_t wPos = 0; | |
void setup() | |
{ | |
analogReference(INTERNAL); | |
printSerialDebug(); | |
// Initialize tempreature average array with nil values | |
for (uint8_t i =0; i < tempArrayLength; i++) | |
{ | |
tempArray[i] = -300.0; | |
} | |
// Set Output mode for LEDs | |
for (uint8_t i = 0; i < barLedPinsLength; i++) | |
{ | |
pinMode(barLedPins[i], OUTPUT); | |
} | |
} | |
void loop() | |
{ | |
// Get new measurement | |
tempRaw = analogRead(tempPin) / 9.31; | |
tempAvg = pushNewValueAndGetAverage(tempArray, tempRaw, tempArrayLength); | |
noOfLedsToActivate = (uint8_t) (roundf(tempAvg - lowTempLimit) / tempDiffPerLed); | |
Serial.print("Raw: "); | |
Serial.print(tempRaw); | |
Serial.print(" -- Avg: "); | |
Serial.print(tempAvg); | |
Serial.print(" -- LEDs: "); | |
Serial.print(noOfLedsToActivate); | |
Serial.print("\n"); | |
// Activate bar LEDs | |
digitalWrite(barLedPins[noOfLedsToActivate], HIGH); | |
if (tempAvg > highTempLimit) | |
{ | |
analogWrite(speakerPin,500); | |
delay(1000); | |
analogWrite(speakerPin,0); | |
} | |
delay(100); | |
digitalWrite(barLedPins[noOfLedsToActivate], LOW); | |
} | |
void printSerialDebug() | |
{ | |
Serial.begin(9600); | |
Serial.print("High Temp: "); | |
Serial.print(highTempLimit); | |
Serial.print(" -- Low Temp: "); | |
Serial.print(lowTempLimit); | |
Serial.print(" -- Diff: "); | |
Serial.print(tempDisplayRange); | |
Serial.print(" -- Diff per LED: "); | |
Serial.print(tempDiffPerLed); | |
Serial.print("\n"); | |
} | |
float pushNewValueAndGetAverage(float *array, float newVal, uint8_t arrSize) | |
{ | |
// Insert new value | |
array[wPos] = newVal; | |
// Increment next writing position with wraparound | |
wPos = (wPos+1) % arrSize; | |
float sum = 0.0; | |
uint8_t validValues = 0; | |
// Calculate average value | |
for (uint8_t i = 0; i < arrSize; i++) | |
{ | |
if (array[i] != -300.0) | |
{ | |
validValues++; | |
sum = sum + array[i]; | |
} | |
} | |
return sum/validValues; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment