Increse/Decrese a value with a button

  • Hello MySensors friends,

    I am working on making a thermostat for my shed, in general it already works.
    I use two set points, one as frost protection, 5 degrees C, and a second one if I want to work in the shed.

    To change the set points I use two push buttons, and I can also operate it via the controller so that the heating can be set higher without having to go to the shed.
    This all works, it is still in parts / breadboards on my desk for testing.

    Now I would like to make an extra function where I can manually increase or decrease the 2nd setpoint (high setpoint).

    I have already tried many things and I have the problem that the setpoint goes sky-high until I release the button, so not just one degree up or down .....

    What have I tried:
    input both with digitalread and with a debouncer, both have the same effect.

    -if (SetHigher == HIGH) {
    Setpoint2 ++;
    -if (SetHigher == HIGH) {
    Setpoint2 + = 1;
    -if (SetHigher! = SetHigherOld) {
    Setpoint2 ++;
    (SetHigherOld = SetHigher);
    -if SetHigher == HIGH && SetHigherOld == 0) {
    SetHigherOld = 1;
    Setpoint2 ++;
    wait (1)
    SetHigherOld = 0;

    and many other possibilities.
    Some times it resulted in no increase or decrease at all, but mostly in the gigantic increase or decrease of the set point.

    To be honest, I am about to give up now, I have tried all options on different forums, but without result.

    It is not a MySensors problem, that part just works, so do the other functions, just raising or lowering the setpoint a few degrees does not work ...

    The entire sketch is more than 800 lines long (I also connected an LCD 1602), so probably some overkill to post it here.

    I use an Arduino Uno for testing.

    Who knows maybe a tip, or perhaps the solution for this challenge?

    Thanks in advance!

    !! EDITED at request of @dbemowsk !!

  • Mod

    @dzjr you probably need to add debouncing. There are many ways to add debouncing and which is best depends on what else your sketch is doing.

    See the button example for one way to do it.

  • @mfalkvidd

    i have used the debouncer example from the button exemple, for changing the setpoint from frost protection to working setpoint the debouncers are working.

  • @dzjr Can you edit your post to put your code in a code block. When you edit the post, highlight your code section and then click on the </> icon. Also, your code seems a little odd to me. You have for example

    if (SetHigher == HIGH) {
    (Setpoint2 ++);}

    I would write that like this

    if (SetHigher == HIGH) {
        Setpoint2 ++;

    No need to put parenthesis around your "Setpoint2 ++".

    @mfalkvidd He did say "input both with digitalread and with a debouncer, both have the same effect.", though I'm not sure what kind of debouncer he tried.

  • @dbemowsk i did edited the original post as you asked.

  • @dzjr Thank you. Many here will appreciate that as it makes it much easier to read.

  • You now increase as long as the button is pressed, you should only increase on pressing the button. So you should check if the button has changed, and if it does increase the thermostat value

    So something like

    button = digitalRead (pin);
    if (button != OldButton)
      OldButton = button;

  • @electrik

    I even tried that, and it's still sky high

    the code i made is :

    int SetHigher == digitalRead(Higher_PIN);
    int SetHigherOld ;
    if (SetHigher != SetHigherOld)
    Heating_set_High ++;
    SetHigherOld = SetHigher;

    Also tried boolean for digitaRead.

    !!! EDIT, changed the code !!!

  • @dzjr Delay incrementing via a while loop with 50-500ms sleep/wait, to check the button has changed state before proceeding. Modify delay value to suit.

  • @dzjr

    you are reading the pin into SetHigher first, which is good.
    Then you are setting SetHigherOld to a value which is negative, so always different from SetHigher. So that is always true. I also don't see how a negative value can fit into a boolean.

    I now see I made a mistake in my previous post. I'll update it.

  • @electrik
    Thank you all for the help!

    It's working!!!'

    I have done the following;

    at the top:

    float Heater_set_High = 17;
    float Change_Degree = 0.5;
    unsinged long SetHigherOld = 0;
    unsinged long SetLowerOld = 0;

    i used 0,5 becouse than the change of de setpoint is changed by 1 when you press the button.

    The reading from te button i do with the debouncer (SetHigher & SetLower)

    The sketch:

    if (SetHigher !=SetHigherOld){
    Heater_set_High += ChangeDegree;
    SetHigherOld = SetHigher;
    if (SetLower !=SetLowerOld){
    Heater_set_High -= ChangeDegree;
    SetLowerOld = SetLower;
    // To protect te setpoint going to high i made this:
    if (Heater_set_High >= 25){
    Heater_set_High = 25;
    else if (Heater_set_High <=7){
    Heater_set_High = 7;
    //to reset the High setpoint after High temperature time or switching it off:
    if (Heater_High == LOW){
    Heater_set_High = 17;

    I also can change it in the controller by sending a temperature message to the node.

    When i finished the project i will make a post of this project.

  • It is now still activated twice per button press, as this code will be executed on both pressing and releasing the button.
    You could check if it is high so you can change your setpoint by 0.5 degrees

  • @electrik I already dit set it at 0,5 so it is changing 1 degree.

    I now have made it even 0.05 so i can change it by 0,1 degree.

    Thanks for all the help!

Log in to reply

Suggested Topics

  • 3
  • 15
  • 1
  • 4
  • 2
  • 3