Script seems to be fine... Whats wrong

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

Can you post a schematic diagram?

For example, are your temperature signals from thermistor circuits with an impedance higher than 10kΩ (the limit for the AVR specified in section 24.6.1 of the ATmega328P datasheet)?

BigBobby:
Can you post a schematic diagram?

For example, are your temperature signals from thermistor circuits with an impedance higher than 10kΩ (the limit for the AVR specified in section 24.6.1 of the ATmega328P datasheet)?

Im using the 2x lm35, and for some reason the fan script without the menu and voltmeter works perfectly without any problems… However when I add the fan script to the project it stops working properly… Very odd… I beliebe that there is some interference between the rest of the scripts but I cant find the problem anywhere…?

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
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;
 
void setup() {
  pinMode(fan, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(tempPin1, INPUT);
  pinMode(tempPin2, INPUT);
  lcd.begin();
}
 
void loop() {
  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
  }
 
  if (temp > tempMax) {       // if temp is higher than tempMax
    digitalWrite(led, HIGH);  // turn on led
  } else {                    // else turn of led
    digitalWrite(led, LOW);
  }
 
  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();
}
 
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;
}

Hmm...so the code you just posted works? It's something in the difference between your code in post #1 and post #3?

BigBobby:
Hmm...so the code you just posted works? It's something in the difference between your code in post #1 and post #3?

yes, the code in post #1 doesnt work but the code on #3 works.... Its highly possible that it interfears but im not 100% how and where... If someone where to point out at least where is the mistake that would be great thanks :slight_smile:

What is actually wrong? What happens or doesn’t happen that you didn’t expect? Edit - many other people and I can’t be bothered to go watch a YouTube video. A quick verbal description would be more helpful.

I don't see any places where you'd be corrupting RAM. What's your % RAM usage in your sketch that doesn't run?

It could still be electrical too. Have you tried just leaving your BackLight on all of the time? Also try commenting out just your reads from ads.

aarg:
What is actually wrong? What happens or doesn’t happen that you didn’t expect? Edit - many other people and I can’t be bothered to go watch a YouTube video. A quick verbal description would be more helpful.

Basically the first sensor shows a funky temperature…

Within one second the temperature rises to 40c and goes back to 20c… It’s like it was generating random numbers between 20 and 40 and displaying them rapidly

BigBobby:
I don't see any places where you'd be corrupting RAM. What's your % RAM usage in your sketch that doesn't run?

It could still be electrical too. Have you tried just leaving your BackLight on all of the time? Also try commenting out just your reads from ads.

I'm a noob. Could you tell me how do I check the ram usage in my sketch... I will try leaving the backlight out as soon as I get home...

szymonm2:
I’m a noob. Could you tell me how do I check the ram usage in my sketch… I will try leaving the backlight out as soon as I get home…

It should tell you after you build. Something like this:

Sketch uses 1,848 bytes (5%) of program storage space. Maximum is 32,256 bytes.
Global variables use 182 bytes (8%) of dynamic memory, leaving 1,866 bytes for local variables. Maximum is 2,048 bytes.

If you start to get close to using 100% of your dynamic memory, you get a message like this:

Low memory available, stability problems may occur.

You need to have a newer version of the Arduino IDE to get these messages, however. I remember not getting them when I was using v1.0.4, but I do now that I’m using v1.6.6.

BigBobby:
It should tell you after you build. Something like this:
If you start to get close to using 100% of your dynamic memory, you get a message like this:
You need to have a newer version of the Arduino IDE to get these messages, however. I remember not getting them when I was using v1.0.4, but I do now that I'm using v1.6.6.

I dont think I have any memory issues

Sketch uses 8,336 bytes (25%) of program storage space. Maximum is 32,256 bytes.
Global variables use 442 bytes (21%) of dynamic memory, leaving 1,606 bytes for local variables. Maximum is 2,048 bytes.

szymonm2:
I dont think I have any memory issues

Sketch uses 8,336 bytes (25%) of program storage space. Maximum is 32,256 bytes.

Global variables use 442 bytes (21%) of dynamic memory, leaving 1,606 bytes for local variables. Maximum is 2,048 bytes.

Yeah, you’re fine there. Your sketch didn’t look like it used huge amounts of memory anywhere, but I don’t have the libraries you’re using so it was a thing to check.

Well, there’s not that many lines of code that you eliminated between post #1 and post #3. One of them must be what’s making your temperatures go nutty. I inspected it for statements that could be corrupting memory, but it’s not like you’re using a bunch of pointers or arrays or anything.

For that reason I’d next look at if your other statements using hardware (such as your backlight) are somehow affecting the analog readings from your temperature sensors. Starting with your code in #1, start commenting out the other stuff that uses hardware and see if any of them stop making your temperatures go wacky.

Edited to Add: Another thing you could try is temporarily making readTemp1() and readTemp2() return constant temperature values. If your LCD is displaying the correct constant values then you know that some of your other code must be affecting your analog readings. If your LCD is still displaying wacky things, however, then you know that your temperatures are somehow getting corrupted as you write to the LCD.

BigBobby:
Yeah, you're fine there. Your sketch didn't look like it used huge amounts of memory anywhere, but I don't have the libraries you're using so it was a thing to check.

Well, there's not that many lines of code that you eliminated between post #1 and post #3. One of them must be what's making your temperatures go nutty. I inspected it for statements that could be corrupting memory, but it's not like you're using a bunch of pointers or arrays or anything.

For that reason I'd next look at if your other statements using hardware (such as your backlight) are somehow affecting the analog readings from your temperature sensors. Starting with your code in #1, start commenting out the other stuff that uses hardware and see if any of them stop making your temperatures go wacky.

Edited to Add: Another thing you could try is temporarily making readTemp1() and readTemp2() return constant temperature values. If your LCD is displaying the correct constant values then you know that some of your other code must be affecting your analog readings. If your LCD is still displaying wacky things, however, then you know that your temperatures are somehow getting corrupted as you write to the LCD.

Sorry for the late reply. After investigation I discovered that pin A0 messes up the temperature. When I disconnect the button the temperature starts working 100%. How do I fix this? I was thinking of replacing the button and putting it in on one of the digital pins but I do not know how do I change the code
I found example code for putting the button on digital pin 12.

// Declare the pins for the Button and the LED
int buttonPin = 12;
int LED = 13;

void setup() {
   // Define pin #12 as input and activate the internal pull-up resistor
   pinMode(buttonPin, INPUT_PULLUP);
   // Define pin #13 as output, for the LED
   pinMode(LED, OUTPUT);
}

void loop(){
   // Read the value of the input. It can either be 1 or 0
   int buttonValue = digitalRead(buttonPin);
   if (buttonValue == LOW){
      // If button pushed, turn LED on
      digitalWrite(LED,HIGH);
   } else {
      // Otherwise, turn the LED off
      digitalWrite(LED, LOW);
   }
}

szymonm2:
I was thinking of replacing the button and putting it in on one of the digital pins but I do not know how do I change the code

You're not going to be able to do that because the switch is not just a simple on/off, it requires an analog pin because different voltage levels sent by the switch indicate the states.

Please use Tools > Auto Format before posting code on the forum. You have some screwy indentation in loop() which makes it hard to read.

szymonm2:
Sorry for the late reply. After investigation I discovered that pin A0 messes up the temperature. When I disconnect the button the temperature starts working 100%. How do I fix this? I was thinking of replacing the button and putting it in on one of the digital pins but I do not know how do I change the code
I found example code for putting the button on digital pin 12.

Was there a button on A0 in your original code? It doesn't seem like you removed button stuff between post #1 (which didn't work) and post #3 (which did)?

It sounds like posting a schematic would be the best way to debug your problem.