Hey guys, got a question.
I have a function that fires at a button press but it seems to be resetting the OLED, any ideas why?
Setup:
1x 180A AttoPilot Sensor
1x DS18B20 Temp Sensor
1x MQ-17 Gas Sensor (Think its an MQ-17)
1x OLED Display
2x Buttons
AttoPilot = A0
MQ-17 = A1
DS18B20 = D8
OLED = SCL/SDA at top of Uno digital pins
Button A = D2
Button B = D3
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
#define ONE_WIRE_BUS 8
const int AOUTpin = 1;
int value;
OneWire tempSensor(ONE_WIRE_BUS);
DallasTemperature sensors(&tempSensor);
int convertTemp;
int FinalTemp;
int VRaw;
float VFinal;
const int voltageButton = 2;
const int tempButton = 3;
int voltageState = 0;
int tempState = 0;
//const int buzzer = 13;
int hydrocount;
void setup() {
Serial.begin(9600);
sensors.begin();
pinMode(voltageButton, INPUT_PULLUP);
pinMode(tempButton, INPUT_PULLUP);
//pinMode(buzzer, OUTPUT);
delay(1000);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
delay(200);
hydrocount = 0;
}
void getTemp() {
bool soundPlayed;
soundPlayed = false;
sensors.requestTemperatures();
convertTemp = sensors.getTempCByIndex(0) * 1.8;
convertTemp = convertTemp + 32;
FinalTemp = convertTemp;
FinalTemp = FinalTemp - 1;
Serial.print("Temperature is: ");
Serial.print(FinalTemp);
Serial.println();
display.clearDisplay();
display.setCursor(0,0);
display.setTextSize(1);
display.setTextColor(WHITE);
display.print("Cell: 1");
display.setCursor(0,10);
display.setTextSize(1);
display.setTextColor(WHITE);
display.print("Temp: ");
display.setCursor(35,10);
display.print(FinalTemp);
display.display();
if (FinalTemp >= 120) {
//playAlertSound();
soundPlayed = true;
}
if (FinalTemp <= 32) {
//playAlertSound();
soundPlayed = true;
}
if (soundPlayed == false) {
if (FinalTemp <= 120) {
//playRecordingSound();
}
}
}
void getVoltage() {
//bool soundPlayed;
//soundPlayed = false;
float currentData;
currentData = 0.00;
float readings[301];
for (int counter = 1; counter <= 300; counter++) {
VRaw = analogRead(A0);
VFinal = VRaw/8.00;
readings[counter] = VFinal;
delay(3);
}
float lowest;
lowest = 50.00;
int count;
count = 0;
for (int counter = 1; counter <= 300; counter++) {
if (readings[counter] < lowest) {
count = counter;
}
}
readings[count] = 0.00;
lowest = 0.00;
count = 0;
for (int counter = 1; counter <= 300; counter++) {
if (readings[counter] > lowest) {
count = counter;
}
}
readings[count] = 0.00;
float addStrings;
float totalVolts;
addStrings = 0.00;
totalVolts = 0.00;
for (int counter = 1; counter <= 300; counter++) {
totalVolts += readings[counter];
}
Serial.println(totalVolts);
Serial.println();
addStrings = totalVolts / 298.00;
currentData = addStrings;
Serial.print("Voltage: ");
Serial.print(currentData);
Serial.println();
display.clearDisplay();
display.setCursor(0,10);
display.setTextSize(1);
display.setTextColor(WHITE);
display.print("Volt: ");
display.setCursor(35,10);
display.print(currentData);
display.display();
}
void loop() {
value = analogRead(AOUTpin);
if (hydrocount == 20) {
Serial.print("H2: ");
Serial.print(value);
Serial.println();
hydrocount = 0;
}
voltageState = digitalRead(voltageButton);
tempState = digitalRead(tempButton);
if (voltageState == HIGH) {
hydrocount = 0;
getVoltage();
}
if (tempState == HIGH) {
hydrocount = 0;
getTemp();
}
hydrocount += 1;
delay(100);
}
So when I press the D3 button (Temp) I get the screen displayed just as it should.
But when I press the D2 button (Volt) my OLED resets. I also cannot go into Serial monitor and see the results of the button press, its as if the function stops.
But if I remove the OLED from the setup the function works just fine.
Any ideas?
Thanks!