I cant seem to find the problem in this script that I have written. The script makes the first temperature go all funky.
Code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_ADS1015.h>
#define lcdBacklightPin 10
Adafruit_ADS1115 ads; // Declare an instance of the ADS1115
int16_t rawADCvalue;
int16_t rawADCvalue2; // The is where we store the value we receive from the ADS1115
//int16_t readADC_Differential_2_3
float scalefactor = 0.1875F; // This is the scale factor for the default +/- 6.144 Volt Range we will use
float volts = 0.0; // The result of applying the scale factor to the raw value
float amps = 0.0;
float amps2 = 0.0;
float watts = 0.0;
int tempPin1 = A1; // the output pin of LM35
int tempPin2 = A2; // second sensor
int fan = 11; // the pin where fan is
int led = 8; // led pin
int temp;
int temp1;
int temp2;
int tempMin = 40; // the temperature to start the fan
int tempMax = 70; // the maximum temperature when fan is at 100%
int fanSpeed;
int fanLCD;
LiquidCrystal_I2C lcd(0x27, 16, 2);
float temp_c,temp_f;
int y;
int state = 0;
int right=0,left=0,up=0,down=0;
int sel=0;
unsigned long up_time_1,up_time_2,up_time;
double time_1,time_2,time;
int last_st=0,st=0;
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin();
ads.begin();
pinMode( lcdBacklightPin, INPUT );
digitalWrite(lcdBacklightPin, LOW);
// Print a message to the LCD.
lcd.setCursor(0,0);
lcd.print("Splashscreen");
lcd.setCursor(0,1);
lcd.print("12333");
up_time_1 = millis();
time_1 = millis();
pinMode(fan, OUTPUT);
pinMode(led, OUTPUT);
pinMode(tempPin1, INPUT);
pinMode(tempPin2, INPUT);
}
void loop() {
time_2 = millis();
last_st = state;
read_state();
st = state;
//if(st != last_st){
process_state();
process_display();
//delay(45);
time_1 = millis();
//}
time = time_2 - time_1;
time = time/1000;
if(time >=10){
pinMode( lcdBacklightPin, OUTPUT);
}else{
pinMode( lcdBacklightPin, INPUT );
}
delay(90);
rawADCvalue = ads.readADC_Differential_0_1();
volts = (rawADCvalue * scalefactor)/100.0;
if ( volts < 0.30 )
{ volts = 0; }
rawADCvalue2 = ads.readADC_Differential_2_3();
amps = (rawADCvalue2 * scalefactor )/1000.0;
amps2 = amps / 0.43;
if ( amps2 < 0.001 )
{ amps2 = 0; }
watts = (volts * amps2);
delay(100);
temp1 = readTemp1(); // get the temperature
temp2 = readTemp2();
temp=temp1>temp2?temp1:temp2;
if(temp < tempMin) { // if temp is lower than minimum temp
fanSpeed = 0; // fan is not spinning
digitalWrite(fan, LOW);
}
if((temp >= tempMin) && (temp <= tempMax)) { // if temperature is higher than minimum temp
fanSpeed = map(temp, tempMin, tempMax, 32, 255); // the actual speed of fan
fanLCD = map(temp, tempMin, tempMax, 0, 100); // speed of fan to display on LCD
analogWrite(fan, fanSpeed); // spin the fan at the fanSpeed speed
}
}
void process_display(){
switch (right) {
case 1:
lcd.clear();
lcd.setCursor(0,0);
lcd.print("VOLTAGE=");
lcd.println(volts,6);
lcd.setCursor(0,1);
lcd.print("AMPERAGE=");
lcd.println(amps2,6);
break;
case 2:
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" WATTS=");
lcd.print(watts,2);
break;
case 3:
lcd.clear();
lcd.print("TEMP: ");
lcd.print(temp1); // display the temperature
lcd.print("C ");
lcd.print(temp2); // display the temperature
lcd.print("C ");
lcd.setCursor(0,1); // move cursor to next line
lcd.print("FANS: ");
lcd.print(fanLCD); // display the fan speed
lcd.print("%");
//delay(200);
//lcd.clear();
break;
case 4:
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Analog 3");
lcd.setCursor(0,1);
lcd.print("Value");
break;
}
}
void process_state(){
switch (state) {
case 1://right
right = right + 1;
right=right%5;
break;
case 2://up
up = up + 1;
break;
case 3://down
up = up - 1;
break;
case 4://left
right = right - 1;
break;
case 5:
break;
}
}
void read_state(){
state = 0;
y = analogRead (0);
//lcd.setCursor(10,1);
if (y < 100) {
//lcd.print ("Right ");
state = 1;
}
else if (y < 200) {
//lcd.print ("Up ");
state = 2;
}
else if (y < 400){
//lcd.print ("Down ");
state = 3;
}
else if (y < 600){
//lcd.print ("Left ");
state = 4;
}
else if (y < 800){
//lcd.print ("Select");
state = 5;
sel = 1;
}
}
int readTemp1() { // get the temperature and convert it to celsius
temp = analogRead(tempPin1);
return temp * 0.48828125;
}
int readTemp2() { // get the temperature and convert it to celsius
temp = analogRead(tempPin2);
return temp * 0.48828125;
}
Watch this video: Temperature - YouTube
Thanks :3