No announcement yet.

need help with my code, description and code attached

  • Filter
  • Time
  • Show
Clear All
new posts

  • need help with my code, description and code attached

    so, i have this project that controls a heater, water pump and LED strand for an aquarium. i am using an arduino mega 2560 to help with this as well as a 3.2 inch touch pad from 4d. one of the only problems i have left i am very confused on what the issue actually is. i'm not sure if code is in the wrong place or what is happening and any help would be amazing in understanding what is happening.

    the problem section is actually displaying my temperature on the screen for both the led digits as well as the thermometer. they display properly, but not before i actually either use the button to turn on the lights or select a temperature i want set for the system. both of these functions are in the event handler function, so that leads me to believe that i should display them outside of that. i have tried to move the code that writes to the screen the temperature, but when i do i get this weird interaction where the objects on the screen will toggle on and off or flash.

    so either they flash on screen or don't initially show until i do something else in the event handler function.

    on top of this, i am thinking that the comparison between the desired temperature (rotary switch) and the actual temperature from temp() is not working right. i say this because when i compare a value less than the actual temp the relay should turn off. sometimes it does and sometimes it doesn't. short hand, the relay doesn't turn on or off the right way like it should. i'm not sure if it is where the code is located in the program, or something is written wrong. i feel like the logic is right, so i can't wrap my head around what the real issue is.

    you'll see a lot of commented code and things that i tried to move around, but usually either didn't fix the problem, or created a different one.

    i can further explain code or anything if necessary.
    thanks for any advice ahead of time.

    Attached Files

  • #2
    Hello Wack

    Looking over your code and reading what you have written, here is my interpretation of what you think isnt working and the reasons.

    The myGenieEventHandler() function will only be run once you interact with the screen and get it to generate an event (Reported Message) by doing something with one of the objects, OR if you do a genie.ReadObject() and request the value of an object. Until that time, the myGenieEventHandler() function sits idle.

    So the reason your relay is not working right, is due to you only changing the state of the relay in the Rotary Switch section in the myGenieEventHandler() function. This code is only going to be run when you change the rotary switch. It will therefore only check the temperature and switch the relay based on this, when you switch the actual Rotary Switch. This doesnt make any sense to me, and this section should be brought into your main loop and checked periodically.

    You are also only checking your temperature at the top of the myGenieEventHandler() function, which means your system is not being updated as to what the actual temperature is, until you do something with the screen and cause an Event. Again, this doesnt make much sense, this should also be in your main loop and checked periodically. (note, periodically doesnt mean every single loop... Refer to 'blink without delay' demos in the Arduino IDE).

    Hopefully with the above information you will be able to figure out what is wrong with the rest of your code. You need to think about when the myGenieEventHandler() is actually called (only on an Event from the display), and when you need the information updated. The above 2 are the main things I have found which will cause the Temperature to not be updated until you do something on the screen, and it will cause your relays not to switch appropriately.

    Post back if you need more help, but try the above first and report back.



    • #3
      Thank you James.

      Your advice helped a lot. I was able to figure out how to properly display the current temperature and compare it with the desired temperature with only one problem left.

      I attached my updated code. Now I compare the two temperatures in the main loop so it can update as needed. The only problem left at this point is the now global variable set_temp is initialized to 0. This assures that it will be less than the temperature_read coming from temp(). The relay however toggles on and off. It is as if the code in the main function for if (set_temp > (temperature_read + 0.7)) is ran even though it shouldn't be.

      I actually have a feeling that this section is not being ran because logically it shouldn't so long as set_temp is less than that value. I add .7 and subtract .3 to add a sense of hysteresis, so that when the temperature is on the bubble it doesn't toggle uncontrollably.

      When the set_temp value is greater than the temperature_read value the relay turns on with no problems. I've tried tweaking the code and changing things around slightly but nothing has fixed this so far.

      This is the very last bug in my project and I feel the solution is simple, but is alluding me.

      Thanks again for any advice.
      Attached Files


      • #4
        Hi Wack

        To start with, get rid of the delay you have in the main loop. Delays are bad. They are blocking functions and nothing else can be done while they are in use. What you have is only small, but if you use them then it stops the Genie Library from working right if they get too big, as it cant process the data coming in on the Serial Port, and you risk overflowing the buffer.

        Refer to 'Blink Without Delay' which I mentioned in my last post. It allows you to do things periodically without having to delay the whole processor until its ok to run again.
        You are reading in the temperature every single loop, which is excessive. You dont need to update the temperature hundreds if not thousands of times a second, do you... Probably wont run that fast anyway, but you need to put this into a non blocking delay so it does it every say 100ms or even 1s or more, depending what you need.

        Its a bit hard for me to see what is wrong as I dont have your setup here.
        Try setting the relay to a known state in your setup function, such as on or off, and in your main loop only change the relay if the temperature has changed and if the state of the relay needs to be different to what it already is.

        Sorry I cant be much more of a help than that. This is more a project problem, and isn't really anything to do with the display any more.

        If you are still really stuck, post again and ill try and help further.



        • #5
          I took your advice James and it worked. I'm not sure why I forgot to look at that the first time. Thank you so much for your help and advice. 10/10 would recommend.