Arduino Code

User avatar
Graeme
Cosmic Lounger
Posts: 1192
Joined: 11 Feb 2010, 12:23
Location: Medway, Kent, UK

Arduino Code

Post by Graeme »

Another Arduino question if I may. My astrophotography software (NINA) uses a Safety Monitor which is a simple Boolean open or closed switch status. I'm planning on installing a rain sensor which will close a switch to ground when rain is detected which will change the status of my Arduino data pin which was held at 5V through a pull up resistor. A third party ASCOM executable then sets the NINA Safety Monitor to unsafe. You can use this to close the observatory roof but I'll probably just send a notification and shut the roof myself!

The Arduino code, based on C+, below is short and simple but has a couple of lines I don't understand:

Code: Select all


//pin 2 is pulled high by pullup resistor
//printing "safe#" to the serial port indicates safe
//printing "notsafe#" to the serial port indicates not safe

bool pinState = LOW;

//-----------------------------------------------------------------------------------------
//--------------------------------------Set safeState--------------------------------------
//-----------------------------------------------------------------------------------------

bool safeState = HIGH; //set desired safeState of the monitor HIGH or LOW

//-----------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------

void setup() {

  pinMode(2, INPUT);

  Serial.begin(9600);         // initialize serial
  Serial.flush();             // flush the port
  Serial.print("notsafe#");   // send notsafe# as first state while monitor and client initialize

}

void loop() {

  String cmd;

  if (Serial.available() > 0) {
    cmd = Serial.readStringUntil('#');
    if (cmd == "S") {
      pinState = digitalRead(2);
      if (pinState == safeState) {
        Serial.print("safe#");
      }
      if (pinState != safeState) {
        Serial.print("notsafe#");
      }

    }

  }
}


I take it "String cmd;" is just setting up a constant with a not very helpful name and has nothing to do with Windows cmd.

What is the # character for in "cmd = Serial.readStringUntil('#');"?

Where does the S come from in "if (cmd == "S") {". I can see it's from .readStringUntil but why might it be or not be an S?

I've been looking for a list of Serial. functions in the hope I can work things out but I can't find one.

Hope this makes sense.

Regards

Graeme
_______________________________________

http://www.averywayobservatory.co.uk/

User avatar
Graeme
Cosmic Lounger
Posts: 1192
Joined: 11 Feb 2010, 12:23
Location: Medway, Kent, UK

Re: Arduino Code

Post by Graeme »

I think I got to the bottom of this dilemma on the Arduino forum. The ASCOM driver, the software interface between my astrophotography software and all connected peripherals, in this instance, continuously outputs an S to inquire if the rain sensor switch is open or closed. Then it sends a safe or not safe message to the NINA Safety Monitor depending on the switch status. All messages are terminated with the # character.

I didn't get to the bottom of the cmd naming thing but I'm over it now!

So I have a functioning rain sensor that can park my telescope and close the observatory roof. All I need now is to finish building my observatory!

Graeme
_______________________________________

http://www.averywayobservatory.co.uk/

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15499
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Arduino Code

Post by ChrisGreaves »

Graeme wrote:
14 Mar 2023, 07:29
So I have a functioning rain sensor that can park my telescope and close the observatory roof. All I need now is to finish building my observatory!
Congratulations Graeme! :clapping: :clapping:
Not only that, but you've learned a bit about C++ too!

I do think that this is going a bit over the edge, but.

No astrologer I, yet not ten minutes ago, while a mug of yesterday's tea heated up in the microwave, I trotted outside in my holey bedroom sleepers slippers to read the electricity meter and, noting that my feet remained dry and seeing what I think of (but only to myself) as "a full-ish moon" hanging low above Hubert and Linda's house, I said to myself in an appreciative tone, "Not raining then!", and returned indoors
Cheers, Chris
Last edited by ChrisGreaves on 14 Mar 2023, 14:45, edited 2 times in total.
An expensive day out: Wallet and Grimace

User avatar
Graeme
Cosmic Lounger
Posts: 1192
Joined: 11 Feb 2010, 12:23
Location: Medway, Kent, UK

Re: Arduino Code

Post by Graeme »

ChrisGreaves wrote:
14 Mar 2023, 08:50
Not only that, but you've learned a bit about C++ too!
Yes, the Arduino forum thread was quite long and detailed. I have a much improved understanding of C (+, ++ or #, not sure exactly which one Arduino sketches are based on.)

No astrologer I, yet not ten minutes ago, while a mug of yesterday's tea heated up in the microwave, I trotted outside in my holey bedroom sleepers to read the electricity meter and, noting that my feet remained dry and seeing what I think of (but only to myself) as "a full-ish moon" hanging low above the Hubert and Linda's house, I said to myself in an appreciative tone, "Not raining then!", and returned indoors
Very astute! The Moon though, if you want to be specific, was a 21 day old, 60% lit, waning gibbous.
_______________________________________

http://www.averywayobservatory.co.uk/

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15499
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Arduino Code

Post by ChrisGreaves »

Graeme wrote:
14 Mar 2023, 14:01
...The Moon though, if you want to be specific, was a 21 day old, 60% lit, waning gibbous.
Thank you Graeme. I have corrected both errors in my post. (I wandered outside in my slippers, doh! I was not fully-awake at the time, more of a gibbous state you ask me)
Ever curious: Suppose then that I did NOT want to be specific; how would I express " was a 21 day old, 60% lit, waning gibbous"
And at the other extreme, how would I express " was a 21 day old, 60% lit, waning gibbous" in VBA or, for that matter, C, C+, C++, C# or C-gibbous?
Cheers, Chris.

P.S. You are honoured to receive this post, the first of what I hope will be many with my PC almost reset to Factory.
C
An expensive day out: Wallet and Grimace

User avatar
Graeme
Cosmic Lounger
Posts: 1192
Joined: 11 Feb 2010, 12:23
Location: Medway, Kent, UK

Re: Arduino Code

Post by Graeme »

ChrisGreaves wrote:
14 Mar 2023, 14:50
Ever curious: Suppose then that I did NOT want to be specific; how would I express " was a 21 day old, 60% lit, waning gibbous"
Last quarter.

And at the other extreme, how would I express " was a 21 day old, 60% lit, waning gibbous" in VBA or, for that matter, C, C+, C++, C# or C-gibbous?
Dunno! I'm the astronomer, you're the programmer. :grin:

P.S. You are honoured to receive this post, the first of what I hope will be many with my PC almost reset to Factory.
Thankyou.
_______________________________________

http://www.averywayobservatory.co.uk/