Electron reading digital pin state help request (novice)

Good day all:
We need to publish when our building line power drops out, and publish again when it comes back. The plan is to power a relay that takes a digital pin High with power failure, Low with good power. Simple project, but no luck with the following code. Any suggestions would be terrific!

int buttonPin = D5 ; 
void setup() 
{
pinmode(buttonpinD5, Input) ;

void loop() {

Startup:
delay (10000) ;  	//10 sec
If (digitalRead (buttonPinD5) ==High ) {goto OffWait ;

}
else {
goto Startup
}

OffWait:
delay (120000) ;		// 2 min
If (digitalRead (buttonPinD5) ==High ) {goto PowerOff ;
}
else {
goto Startup
}

PowerOff:
delay (10000) ;		// 10 sec
Particle.publish("Building Power","Power Is Shutdown",60,PRIVATE) ;
goto Timer ;
}

Timer:
delay (600000) ;		//10 min
If (digitalRead (buttonPinD5) ==Low ) {goto PowerON;
}
else {
goto Timer
}

PowerOn
delay (10000) ;		// 10 sec
Particle.publish("Building Power","Power Is Running",60,PRIVATE);
goto Startup;
}

We do like properly formatted and indented code :wink:
Have a look at

Also your curly braces don't match up (which would be easier to see with proper indentaion).

And please don't over-use goto. This is C++ not Basic.

2 Likes

4 posts were split to a new topic: Some discussion about formatting code blocks in posts

Hello:
Thanks for the response. Is the code (beside indenting) totally faulty? Compiling is not happy!

take a look here:

When I rework the indentation (and remove superfluous or “confusing” blanks) of your code it would look like this (to stress the pairing of curly braces I place each of them on a seperate line)

int buttonPin = D5;
 
void setup() 
{
  pinmode(buttonpinD5, Input);
  // <-- missing closing brace!!

void loop() 
{
Startup:
  delay(10000);  	//10 sec
  If(digitalRead(buttonPinD5) == High) // <-- C/C++ is case sensitive so it needs to be if and HIGH  
  {
    goto OffWait;
  }
  else 
  {
    goto Startup // <-- missing semicolon ;
  }

OffWait:
  delay(120000);		// 2 min
  If(digitalRead(buttonPinD5) == High) // <-- case sensitive if and HIGH 
  {
    goto PowerOff;
  }
  else 
  {
    goto Startup // <-- missing semicolon ;
  }

PowerOff:
  delay (10000);		// 10 sec
  Particle.publish("Building Power", "Power Is Shutdown", 60, PRIVATE) ;
  goto Timer;
} // <-- wrong closing brace 

// due to premature closing brace the rest of the rest of the code is not allowed here
Timer:
  delay(600000);		//10 min
  If(digitalRead (buttonPinD5) == Low) // <-- if and LOW
  {
    goto PowerON;
  }
  else 
  {
    goto Timer // <-- missing semicolon ;
  }

PowerOn // <-- missing colon : for this label
  delay(10000);		// 10 sec
  Particle.publish("Building Power", "Power Is Running", 60, PRIVATE);
  goto Startup;
}

But as said, since this is not Basic you need to rid yourself from the Basic (or machinge code :wink: ) programming paradigm (e.g. goto) and adopt a more C (procedural) and C++ (object oriented) way of thinking.

2 Likes

Hi:
I want to thank you for your kind help today. I cleaned things up per your instructions and with a few mods, it seems to work! I’m pretty disabled with this stuff and have a lot to learn as I’m certain you know!

Thanks again

2 Likes