New and unlearned Arduino user

Hi,

i though that my first arduino project could be a simple fridge that had a 20x4 screen, 2 buttons to change my target temperature, and a tempsensor. but i'm running in to a problem with how to change the target temp.

the target temp won't change if i press one of the buttons, but i can get a readout on the serial from the main loop that the button is pushed, and i don't get any readouts from the "void target" but i don't quite understand how interrupt command works

Could someone help me a little bit here?

btw, i'm using pull down on the button signal

I have attached the whole code as it is right now.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define R1 2 // Peltier 1
#define R2 3 // Peltier 2
#define R3 4 // Fan + Radiator
#define uptmpb 5 // Button increase temp
#define dwntmpb 6 // Button decrease temp
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

int ftmp; //Temp inside
int maxtmp; //max temp
int settmp; //Target temp
int mintmp; //min temp

void setup() {  // put your setup code here, to run once:
  
  ftmp = 4;
  settmp = 5;
  mintmp = settmp - 1;
  maxtmp = settmp + 1;
  Serial.begin(9600);
  lcd.begin(20,4);
  lcd.setCursor(0,0);
  lcd.print("Fridge Cooler V1");
  lcd.setCursor(0,2);
  delay(1000);
  lcd.print("initializing");
  pinMode(R1, OUTPUT);
  pinMode(R2, OUTPUT);
  pinMode(R3, OUTPUT);
  pinMode(uptmpb, INPUT);
  pinMode(dwntmpb, INPUT);

  digitalWrite(R1, LOW);
  digitalWrite(R2, LOW);
  digitalWrite(R3, LOW);
  attachInterrupt(digitalPinToInterrupt(uptmpb || dwntmpb), Target, CHANGE);
  delay(1000);
}

void Target() {
  if (digitalRead (uptmpb) == true){
    settmp + 1;
    maxtmp + 1;
    mintmp + 1;
    Serial.println("increase");
  }
  if (digitalRead (dwntmpb) == true){
    settmp - 1;
    maxtmp - 1;
    mintmp - 1;
    Serial.println("decrease");
  }
}
void loop() {
// put your main code here, to run repeatedly:
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Beer Fridge");
  lcd.setCursor(0,1);
  lcd.print("Cooling down ");
    if (ftmp >= maxtmp){
     digitalWrite(R1, HIGH);
     digitalWrite(R2, HIGH);
     digitalWrite(R3, HIGH);
    }
   if (ftmp <= mintmp){
    digitalWrite(R1, LOW);
    digitalWrite(R2, LOW);
    digitalWrite(R3, LOW);
   }
   if (R1 || R2 || R3 == HIGH){
    lcd.setCursor(15,1);
    lcd.print("true");
   }
  lcd.setCursor(0,2);
  lcd.print("Temp ");
  lcd.setCursor(15,2);
  lcd.print(ftmp);
  lcd.print((char)223);
  lcd.print("C");
  lcd.setCursor(0,3);
  lcd.print("Target ");
  lcd.setCursor(15,3);
  lcd.print(settmp);
  delay(1000);
  Serial.println(mintmp,"Min");
  Serial.println(settmp,"Stilt");
  Serial.println(maxtmp,"Maks");
  Serial.println(digitalRead (uptmpb));
  Serial.println(digitalRead (dwntmpb));
  delay(1000);

}

You can't use OR to try to condense the two interrupt attach lines into one. There should be a line for each one. The result of that or operation is True. Do you want to attach an interrupt on the True pin? That would be pin 1 which doesn't support interrupts.

But really, don't use interrupt for a button press. Button presses are really slow from the processors point of view. Interrupts are for things that happen so fast you could miss them in a millisecond or two. For a button you should just poll it from loop with digitalRead.

You also shouldn't try to do Serial stuff in an interrupt. Serial is driven by interrupts that are turned off during your ISR.

something like this?

void loop() {
// put your main code here, to run repeatedly:
if (digitalRead (uptmpb) == true){
    settmp + 1;
    maxtmp + 1;
    mintmp + 1;
  }
  if (digitalRead (dwntmpb) == true){
    settmp - 1;
    maxtmp - 1;
    mintmp - 1;
  }

Almost. You have to realize how many times that loop is going to be repeated between the time you first press the button and when you release it. Go look at the "State Change Example".

if (R1 || R2 || R3 == HIGH){

You also need to realize the OR is for Boolean math, not for inventing new syntax. Secondly, do you want to compare the pin numbers to HIGH? Or the pin states which would involve a digitalRead.

Then you're not using the interrupt at all... And doing it that will will give you trouble because the Arduino will run the loop a whole lot faster then you can quickly press the button :wink: But not using an interrrupt for it is a good thing. They are more designed for the fast stuff.

Have a look at the state change example :slight_smile: Or, if you want easy, grab a library like Bounce2 to do all the work.

And couple of general tips:

  • Drop the short variable names! Might seen nice now because it's less work to type. But it makes them harder to remember and not clear what the code does. That makes it harder to just read the code for others but also for future you :wink: So just temperatureDownPin (or if it's the only down button simply downPin). And R1 says even less. What has a R to do with a peltier? Probably from Relay but make it easy on yourself and call it what it really is. peltierLeftPin for example

  • Drop the macro's (the #defines) and replace them for pin numbers for "const byte", so for example:
    const byte PeltierLeftPin = 2;
    Or because you have two kind of the same things, use an array:
    const byte PertierPins[] = {2, 3};

  • Don't clear the whole lcd if you just want to update a part. Just empty that part.

Thanks allot to you 2 guys!

I think my brain got locked in a box and couldn't see a solution that was so much simpler for me to understand.

and i see that i have much to learn about arduino :slight_smile: