Browse Source

Replace speaker with buzzer

master
Burathar 4 years ago
parent
commit
f532432d8b
  1. 136
      smoker.ino

136
smoker.ino

@ -48,17 +48,20 @@ Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
Queue tempQueue; Queue tempQueue;
void setup() { void setup() {
pinMode(SPEAKER_PIN, OUTPUT);
digitalWrite(SPEAKER_PIN, LOW);
//Serial.begin(9600); //Serial.begin(9600);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
//Serial.println(F("SSD1306 allocation failed")); //Serial.println(F("SSD1306 allocation failed"));
for(;;); for (;;);
} }
display.setRotation(2); display.setRotation(2);
display.clearDisplay(); display.clearDisplay();
display.setTextColor(WHITE); display.setTextColor(WHITE);
airTempSensor.preload(); airTempSensor.preload();
} }
void loop() { void loop() {
updateAirTemp(); updateAirTemp();
byte bUp = buttonUp.getState(); byte bUp = buttonUp.getState();
@ -66,12 +69,12 @@ void loop() {
byte bLeft = buttonLeft.getState(); byte bLeft = buttonLeft.getState();
byte bRight = buttonRight.getState(); byte bRight = buttonRight.getState();
if (bRight >= 1){ if (bRight >= 1) {
if(state >= MAX_STATE) state = 0; if (state >= MAX_STATE) state = 0;
else state += 1; else state += 1;
} }
if (bLeft >= 1){ if (bLeft >= 1) {
if(state <= 0) state = MAX_STATE; if (state <= 0) state = MAX_STATE;
else state -= 1; else state -= 1;
} }
@ -98,57 +101,60 @@ void loop() {
display.display(); display.display();
} }
void updateAirTemp(){ void updateAirTemp() {
airTempSensor.update(); airTempSensor.update();
if(millis() - lastTempQueue > TEMP_PLOT_INTERVAL){ if (millis() - lastTempQueue > TEMP_PLOT_INTERVAL) {
tempQueue.push(airTempSensor.getTemperature()); tempQueue.push(airTempSensor.getTemperature());
lastTempQueue = millis(); lastTempQueue = millis();
} }
} }
void updateRelay(){ void updateRelay() {
float temperature = airTempSensor.getTemperature(); float temperature = airTempSensor.getTemperature();
if (temperature - targetTemp >= TEMP_DELTA){ //Temp too high if (temperature - targetTemp >= TEMP_DELTA) { //Temp too high
relay.off(); relay.off();
} else if (targetTemp - temperature >= TEMP_DELTA) { //Temp too low } else if (targetTemp - temperature >= TEMP_DELTA) { //Temp too low
relay.on(); relay.on();
} }
if(temperature - targetTemp >= ALARM_TEMP_DELTA){ if (temperature - targetTemp >= ALARM_TEMP_DELTA) {
int second = millis() % 1000; int second = millis() % 1000;
if(alarmState == 0 and second < 100){ if (alarmState == 0 and second < 100) {
alarmState = 1; alarmState = 1;
tone(SPEAKER_PIN, 440, 400); digitalWrite(SPEAKER_PIN, HIGH);
} } else if (alarmState == 1 and second > 500 and second < 600) {
if(alarmState == 1 and second > 500 and second < 600){
alarmState = 0; alarmState = 0;
tone(SPEAKER_PIN, 554, 400); digitalWrite(SPEAKER_PIN, LOW);
} else if (alarmState == -1) {
digitalWrite(SPEAKER_PIN, LOW);
} }
} else {
digitalWrite(SPEAKER_PIN, LOW);
} }
} }
void targetTempState(byte bUp, byte bDown){ void targetTempState(byte bUp, byte bDown) {
updateRelay(); updateRelay();
switch(bUp){ switch (bUp) {
case 1: case 1:
targetTemp +=1; targetTemp += 1;
break; break;
case 2: case 2:
targetTemp +=1; targetTemp += 1;
break; break;
case 3: case 3:
targetTemp +=3; targetTemp += 3;
break; break;
} }
switch(bDown){ switch (bDown) {
case 1: case 1:
targetTemp -=1; targetTemp -= 1;
break; break;
case 2: case 2:
targetTemp -=1; targetTemp -= 1;
break; break;
case 3: case 3:
targetTemp -=3; targetTemp -= 3;
break; break;
} }
@ -156,10 +162,10 @@ void targetTempState(byte bUp, byte bDown){
// Target Temp // Target Temp
display.setTextSize(1); display.setTextSize(1);
display.setCursor(0,40); display.setCursor(0, 40);
display.print("Target: "); display.print("Target: ");
display.setTextSize(2); display.setTextSize(2);
display.setCursor(0,50); display.setCursor(0, 50);
display.print(targetTemp); display.print(targetTemp);
display.print(" "); display.print(" ");
display.setTextSize(1); display.setTextSize(1);
@ -182,81 +188,81 @@ void manualState(byte bUp, byte bDown) {
displayCurrentTemp(); displayCurrentTemp();
display.setTextSize(1); display.setTextSize(1);
display.setCursor(0,40); display.setCursor(0, 40);
display.print("Manual Mode"); display.print("Manual Mode");
display.setTextSize(2); display.setTextSize(2);
display.setCursor(0,50); display.setCursor(0, 50);
if (relay.getState()){ if (relay.getState()) {
display.print("Heater ON"); display.print("Heater ON");
} else { } else {
display.print("Heater OFF"); display.print("Heater OFF");
} }
} }
void uptimeState(byte bUp, byte bDown){ void uptimeState(byte bUp, byte bDown) {
updateRelay(); updateRelay();
unsigned int seconds = millis()/1000; //convect milliseconds to seconds unsigned int seconds = millis() / 1000; //convect milliseconds to seconds
unsigned int minutes=seconds/60; //convert seconds to minutes unsigned int minutes = seconds / 60; //convert seconds to minutes
unsigned int hours=minutes/60; //convert minutes to hours unsigned int hours = minutes / 60; //convert minutes to hours
seconds=seconds-(minutes*60); //subtract the coverted seconds to minutes in order to display 59 secs max seconds = seconds - (minutes * 60); //subtract the coverted seconds to minutes in order to display 59 secs max
minutes=minutes-(hours*60); //subtract the coverted minutes to hours in order to display 59 minutes max minutes = minutes - (hours * 60); //subtract the coverted minutes to hours in order to display 59 minutes max
displayCurrentTemp(); displayCurrentTemp();
display.setTextSize(1); display.setTextSize(1);
display.setCursor(0,40); display.setCursor(0, 40);
display.print("Uptime:"); display.print("Uptime:");
display.setTextSize(2); display.setTextSize(2);
display.setCursor(0,50); display.setCursor(0, 50);
if(hours < 10) display.print(0); if (hours < 10) display.print(0);
display.print(hours); display.print(hours);
display.print(":"); display.print(":");
if(minutes < 10) display.print(0); if (minutes < 10) display.print(0);
display.print(minutes); display.print(minutes);
display.print(":"); display.print(":");
if(seconds < 10) display.print(0); if (seconds < 10) display.print(0);
display.print(seconds); display.print(seconds);
} }
void displayMenuBar(){ void displayMenuBar() {
byte space = display.width() / (MAX_STATE + 1); byte space = display.width() / (MAX_STATE + 1);
for (byte i = 0; i < MAX_STATE + 1; i++){ for (byte i = 0; i < MAX_STATE + 1; i++) {
if (i == state) display.fillCircle(space * i + space / 2, 3, 3, WHITE); if (i == state) display.fillCircle(space * i + space / 2, 3, 3, WHITE);
else display.drawCircle(space * i + space / 2, 3, 3, WHITE); else display.drawCircle(space * i + space / 2, 3, 3, WHITE);
} }
} }
void tempPlotState(byte bUp, byte bDown){ void tempPlotState(byte bUp, byte bDown) {
updateRelay(); updateRelay();
byte maxValue = tempQueue.getMax(); byte maxValue = tempQueue.getMax();
byte minValue = tempQueue.getMin(); byte minValue = tempQueue.getMin();
if(minValue > 99) minValue = 99; if (minValue > 99) minValue = 99;
display.setTextSize(1); display.setTextSize(1);
if(maxValue > 99)display.setCursor(110,8); if (maxValue > 99)display.setCursor(110, 8);
else display.setCursor(116,8); else display.setCursor(116, 8);
display.print(maxValue); display.print(maxValue);
display.setCursor(116,57); display.setCursor(116, 57);
display.print(minValue); display.print(minValue);
if(maxValue - minValue < 10){ if (maxValue - minValue < 10) {
minValue -= 1; minValue -= 1;
maxValue += 1; maxValue += 1;
} }
for (int i=0; i<TEMP_QUEUE_LENGTH; i++){ for (int i = 0; i < TEMP_QUEUE_LENGTH; i++) {
byte barHeight = tempQueue.lookup(i); byte barHeight = tempQueue.lookup(i);
if(barHeight < minValue) barHeight = minValue; if (barHeight < minValue) barHeight = minValue;
barHeight = map(barHeight, minValue, maxValue, 2, 58); barHeight = map(barHeight, minValue, maxValue, 2, 58);
display.fillRect(i * 2, SCREEN_HEIGHT - barHeight, 1, barHeight, WHITE); display.fillRect(i * 2, SCREEN_HEIGHT - barHeight, 1, barHeight, WHITE);
} }
} }
void alarmSettingState(byte bUp, byte bDown){ void alarmSettingState(byte bUp, byte bDown) {
updateRelay(); updateRelay();
if (bUp == 1) { if (bUp == 1) {
alarmState = 0; alarmState = 0;
} }
@ -267,7 +273,7 @@ void alarmSettingState(byte bUp, byte bDown){
displayCurrentTemp(); displayCurrentTemp();
display.setTextSize(1); display.setTextSize(1);
display.setCursor(0,40); display.setCursor(0, 40);
display.print("Alarm temp: "); display.print("Alarm temp: ");
display.print(targetTemp + ALARM_TEMP_DELTA); display.print(targetTemp + ALARM_TEMP_DELTA);
display.print(" "); display.print(" ");
@ -275,22 +281,22 @@ void alarmSettingState(byte bUp, byte bDown){
display.write(167); display.write(167);
display.setTextSize(1); display.setTextSize(1);
display.print("C"); display.print("C");
display.setTextSize(2); display.setTextSize(2);
display.setCursor(0,50); display.setCursor(0, 50);
if (alarmState == -1){ if (alarmState == -1) {
display.print("Alarm OFF"); display.print("Alarm OFF");
} else { } else {
display.print("Alarm ON"); display.print("Alarm ON");
} }
} }
void displayCurrentTemp(){ void displayCurrentTemp() {
display.setTextSize(1); display.setTextSize(1);
display.setCursor(0,10); display.setCursor(0, 10);
display.print("Current: "); display.print("Current: ");
display.setTextSize(2); display.setTextSize(2);
display.setCursor(0,20); display.setCursor(0, 20);
display.print(airTempSensor.getTemperature()); display.print(airTempSensor.getTemperature());
display.print(" "); display.print(" ");
display.setTextSize(1); display.setTextSize(1);

Loading…
Cancel
Save