2

I've been doing a little thermometer project to learn Arduino and there is an annoying thing that I have no idea how to resolve.

I have two push buttons to set Min and Max temperature and when I push the buttons it's supposed to set the Min and Max temperature on display. The problem is that sometimes (50% of times) when I push the buttons during the reading of the temperature sensor, the buttons don't work. I press it but the Min/Max temperature are not set because Arduino is stuck in reading the temperature sensor.

Is there any trick to solve this kind of problem? If I had a keyboard for typing some number for example I imagine I would have the same problem and It's not "user-friendly".

Here is an example of part of the code I'm using:

#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>

//variables declaration...

void setup() {
  sensors.begin();
  sensors.getAddress(sensor1, 0);
  pinMode(buzzer, OUTPUT);
  pinMode(btBuzzer, INPUT);
  pinMode(btMin, INPUT);
  pinMode(btMax, INPUT);
}

void loop() {
  readButtons();

  playBuzzer();

  readTemperature();

  printDisplay();

  delay(150);
}

void readButtons(){
  if(digitalRead(btBuzzer)){
    buzzerOn = !buzzerOn;
  }

  if(digitalRead(btMin)){
    if(tempMin == 69)
      tempMin = 59;
    else
      tempMin++;
  }

  if(digitalRead(btMax)){
    if(tempMax == 75)
      tempMax = 63;
    else
      tempMax++;
  }
}

void readTemperature(){
  sensors.requestTemperatures();
  temperature = sensors.getTempC(sensor1);
}

//lots of other methods
Raiiy
  • 320
  • 2
  • 12
  • Maybe your button presses are during the delay? Try removing/reducing the delay for improvement. Or remove the delay completely doing something like this https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay – Sush Jan 23 '16 at 04:17

2 Answers2

3

As others have pointed out here, the button press may not happen at the same time as you query the pin with digitalRead(btBuzzer). This type of problem is what so called "interrupts" were invented for, which allow you to respond to events that may occur while you are not monitoring the pin of interest.

For example, the Arduino UNO R3 allow for interrupts on pin 2 and 3. You should look up the reference for attachInterrupt(). The processor will execute a callback function in the event (the "interrupt") that you register for (e.g. the voltage on pin 2 changing from low to high). This means that you will no longer have to call a readButtons() function from your main loop.

s.bandara
  • 5,636
  • 1
  • 21
  • 36
  • This is exactally what I was looking for. Interruptions! Thank you so much, helped me a lot! – Raiiy Jan 23 '16 at 14:28
-1

Some of the best ways to learn coding exist in how to answer this question. What I'd like to suggest doing is to try timing your code. Remember that loop() is creating a repeating structure. So we can say things like how long does the computer take to run each loop. When we have an interrupt like a button press how does that effect the iteration through the loop and is it conditional about how to rest the processor (the delay).

The delay is required so as to not do what's called "spin" the processor (have the processor as quickly as it can do a lot of work to accomplish absolutely nothing). However, notice how the code doesn't account for work done changing how long we delay? Now let's imagine the processor actually can go through that loop more than one time very quickly. Remember delay of only 150 milliseconds isn't a lot of time. So maybe one button press will be enough to set tempMin from 59 to 69 in rapid succession and loop several times over rather than just increasing one number at a time. What you have here is a chance to learn debugging. First trick is to determine is the loop running too fast or too slow; are you getting desired functionality or not and finally if you can reask the question after you know if it's happening too fast or slow. For now, I'd recommend taking a look at global variables and finite state machines (i.e. if you're in a state of button press, don't accept any further button presses until you're done with your state and only transition in known ways).

  • 1
    This is really more of a very long comment, than it is an _answer_ to the question. Note how the other answer provides _specific_ advice to address the _specific_ question. There are some useful bits of advice in this answer, but nothing that actually would directly lead the OP to an actual solution to his problem. Note also that even if it were an actual answer to the question, it would be better if you would spend a little more time proof-reading to avoid spelling and grammar errors, and avoid the long, run-on sentences and paragraphs. – Peter Duniho Jan 23 '16 at 05:02