Less Optimised Code for Beginners PLEASE?



  • Hi there,

    Thanks to Hek, I've been having a great time with My Sensors for a while. I've just got back into it after about a 12 month break and upgrading various sensors from 1.3 to 1.4.

    I'm struggling with the new 1.4 code as the examples under the Build section use some very nice code which helps optimise the sketch size.
    This optimisation makes it more difficult to clearly understand what is happening for "beginner coders" like myself.

    I use the function incomingMessage from the relay sketch below as an example. It contains a line of code to change the relay status as commented.

     // Change relay state
     digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
    

    Trying to adapt this to work with ones own variables can be very confusing.

    Could someone please expand this out to show such code more clearly for beginners?
    It might make it easier if the expanded version of this line is shown as a comment in the code?

    This basic approach for the Build examples may help others overcome similar issues and increase the adoption of My Sensors in general.

    Regards
    Bluman



  • @bluman
    If it is the ? operator that is the fuzzy one then you can rewrite the statement like:

    if (message.getBool())
      digitalWrite(message.sensor-1+RELAY_1, true);
    else
      digitalWrite(message.sensor-1+RELAY_1, false);
    

    The ? operator act like: expression?truepart:falsepart
    So if expression is true the it executes the truepart else it executes the falsepart


Log in to reply
 

Suggested Topics

  • 4
  • 2
  • 8
  • 9
  • 20
  • 9

60
Online

11.4k
Users

11.1k
Topics

112.7k
Posts