Skip to content

Instantly share code, notes, and snippets.

@JeffersGlass
Created May 25, 2020
Embed
What would you like to do?
const int leds[] = {13, 12, 11, 10, 9};
const int NUMLEDS = 5;
//number of threshholds = number of LEDs
const int threshhold[] = {300, 350, 500, 600, 800};
const int knobPin = A5;
const int button1 = 7;
const int button2 = 8;
long reading = 500;
long timeToAlarm = 5000; //in milliseconds
long timeAlarmStarted = 0;
//time the sensor went below out
long timeStartBeingInFireRange = 0;
bool isSeeingFire = false;
long fireTrigger = 2000;
int MODE_SENSOR = 0;
int MODE_ALARM = 1;
int currentMode = MODE_ALARM;
void setup() {
// put your setup code here, to run once:
for (int i = 0; i < NUMLEDS; i++){
pinMode(leds[i], OUTPUT);
digitalWrite(leds[i], LOW);
}
pinMode(knobPin, INPUT);
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
//read the sensor
if (currentMode == MODE_SENSOR){
reading = getScaledSensorReading();
//if the sensor is lower than the lowest threshhold:
//Turn on the low red light
if (reading > threshhold[4]){
setOutputState(4);
isSeeingFire = false;
}
else if (reading > threshhold[3]){
setOutputState(3);
isSeeingFire = false;
}
else if (reading > threshhold[2]){
setOutputState(2);
isSeeingFire = false;
}
else if (reading > threshhold[1]){
setOutputState(1);
isSeeingFire = false;
}
else if (reading > threshhold[0]){
setOutputState(0);
isSeeingFire = false;
}
else{ //Seeing maximum fire
if (isSeeingFire == false){ //seeing fire for first time
isSeeingFire = true;
timeStartBeingInFireRange = millis();
}
else{
if (millis() > timeStartBeingInFireRange + fireTrigger){
currentMode = MODE_ALARM;
timeAlarmStarted = millis();
}
}
}
}
else if (currentMode == MODE_ALARM){
allOn();
bool isAlarmExpired = (millis() > timeAlarmStarted + timeToAlarm);
bool areBothButtonsPressed = (digitalRead(button1) == LOW && digitalRead(button2) == LOW);
if (isAlarmExpired || areBothButtonsPressed){
currentMode = MODE_SENSOR;
}
}
}
void setOutputState(int ledOn){
allOff();
digitalWrite(leds[ledOn], HIGH);
}
void allOff(){
for (int i = 0; i < NUMLEDS; i++){
digitalWrite(leds[i], LOW);
}
}
void allOn(){
for (int i = 0; i < NUMLEDS; i++){
digitalWrite(leds[i], HIGH);
}
}
int getScaledSensorReading(){
int scaledReading = map(readSensor(), 150, 950, 0, 1023);
return constrain(scaledReading, 0, 1023);
}
//Return a value between 0 and 1023 to reflect
//The current sensor reading
int readSensor(){
return analogRead(A0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment