The project: I want to have a small 5v window fan controlled by temperature to bring cool air into my bedroom in the winter. I also want it to display the actual temp and target temp, and have the target temp controlled by momentary switches. I've made it, and it seemed to work perfectly on the breadboard, but when I soldered it up, the displays go funky (random characters) and the serial monitor shows that the actual temp is -127c (lowest value/null voltage). I'm also getting random button presses.
I thought that I had probably wired the flyback diode wrong across the fan terminals, but when I disconnect the fan, I'm still getting the same problem.
Any advice or tips to improve my code/wiring would be appreciated.
Cheers,
Scotty
EDIT: I have the wrong resistor values in the diagram. should be 9.7k on the temp reader and 820 on the switches.
#include <OneWire.h>
#include <DallasTemperature.h>
#include <TM1637Display.h> //has to be the library from Robojax as it supports multiple display outputs
#define ONE_WIRE_BUS 13
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float Celcius = 0;
const int buttonPin1 = 9, buttonPin2 = 8; // the number of the pushbutton pins
// variables will change:
int button1_State = 0, button2_State = 0; // variable for reading the pushbuttons status
int count_value = 20; //sets initial target temp on startup
int prestate = 0;
/// Module 1 connection pins (Digital Pins)
#define CLK1 3
#define DIO1 2
// Module 2 connection pins (Digital Pins)
#define CLK2 5
#define DIO2 4
TM1637Display display1(CLK1, DIO1);// define display 1 object
TM1637Display display2(CLK2, DIO2);// define display 2 object
uint8_t data[] = { 0x0, 0x0, 0x0, 0x0 }; // all segments clear
void setup(void)
{
display1.setBrightness(0); //sets brightness to lowest
display2.setBrightness(0);
pinMode(10, OUTPUT); //goes to mosfet
Serial.begin(9600); //i still don't really get why it's 9600...
sensors.begin();
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}
void loop(void)
{
sensors.requestTemperatures(); //checks temp
Celcius = sensors.getTempCByIndex(0); //defines temp as celcius
Serial.print("Actual Temp: ");
Serial.print(Celcius);
Serial.print(" C ");
Serial.print("Target Temp: ");
Serial.print(count_value);
Serial.println(" C "); //records actual and target temp on one line
display1.showNumberDecEx(Celcius, false); //display one shows actual temp in degrees c
display2.showNumberDecEx(count_value, false); //display 2 shows target temp in degrees c
if (Celcius > count_value) digitalWrite(10, HIGH); //tells mosfet to turn 5v fan on when actual temp is above target temp
if (Celcius <= count_value) digitalWrite(10, LOW); //turns fan off when actual temp isat or below target temp
// everything from here to the end is cut and paste from a sample button counter code i found online.
button1_State = digitalRead(buttonPin1);
button2_State = digitalRead(buttonPin2);
// counter increment if the pushbutton 1 is pressed.
if (button1_State == HIGH && prestate == 0) {
count_value++;
prestate = 1;
}
// counter decrement if the pushbutton 2 is pressed.
else if (button2_State == HIGH && prestate == 0) {
count_value--;
prestate = 1;
}
else if (button1_State == LOW && button2_State == LOW) {
prestate = 0;
}
delay(50);
}