I am new to writing code but have made a few successful projects including code to operate a fuel oil furnace just as the original controller did. I am currently trying to write a code that will start my home generator. The generator is an rv unit so it is very simple with auto choke and only a momentary start and stop switch. I think my problem lies in the if-else statements within other if-else statements. Any help is appreciated as I have been struggling with it for probably 30 hours or more. The problem is as soon as I upload the code with the board wired to lights(instead of relays) the start pin is immediately on with no response to any of the input buttons.
// Arduino looks for start button to be pushed. This will
bring start relay high which will crank over the motor until it is
running and making AC power at which time will close an
AC relay that is used as in input (voltage) to go high. This will signal
for the starter to be disengaged(start Low) and a 30 second warm up
delay. Then the code will close the contactor(contactor High) that allows the
AC power to my main breaker panel. The arduino will monitor
for the kill button to be pressed which will kill
AC contactor(contactor Low) and give a 10 second stabilizing delay before the
kill relay will go high for 7 seconds then go low.
const int start = 13;
const int contactor = 11;
const int kill = 12;
const int voltage = 2;
const int startbut = 1;
const int killbut = 0;
int startState = 0;
int killState = 0;
int voltageState = 0;
void setup() {
// put your setup code here, to run once:
pinMode ( start , OUTPUT );
pinMode ( contactor , OUTPUT );
pinMode ( kill , OUTPUT );
pinMode ( voltage , INPUT );
pinMode ( startbut, INPUT );
pinMode ( killbut, INPUT );
}
void loop() {
startState = digitalRead(startbut);
killState = digitalRead(killbut);
voltageState = digitalRead(voltage);
L2:
//check state of start button
if (startState == HIGH )
delay(10); {
L1:
//if start button pushed engage starter
digitalWrite(start , HIGH );
delay(10);
{
//check if ac voltage relay closed
if (voltageState == HIGH ){
//if ac relay closed generator is running so turn off starter
digitalWrite ( start, LOW );
} else {
{goto L1;}
}
//30 second delay for warm up
delay(30000);
//close contactor to allow ac current to house
digitalWrite (contactor , HIGH );
}
// ________generator should be currently running and power to the house_________
//check is kill button is pressed
if (killState == HIGH ) {
//kill button pressed so turn off ac contactor
digitalWrite( contactor , LOW );
//10 second delay for generator to stabilize
delay(10000);
//engage kill relay
digitalWrite( kill, HIGH );
//7 second delay for generator to stop running
delay(7000);
//turn off kill relay
digitalWrite( kill, LOW );
}
}
}
[code/]
Before even stopping to breathe, get rid of the goto statements. Take it from there, if the relays work but not the lights, you probably have a hardware problem.
The outputs in general are not working how they should. The start pin is high before I press the start button. I'm not sure how to write it without goto but I'm not opposed to learning though I understand it should still work.
L2:
//check state of start button
if (startState == HIGH )
delay(10); {
What do you think this does? It looks at the startState variable and delays for 10 milliseconds if it is HIGH. Then it does whatever follows. I think you've put the opening brace ({) in the wrong position. Use the auto-format feature of the Arduino editor and if it scrambles your layout, you know that you have the braces in the wrong places.
Additionally, this code snippet has another problem. If you have a goto come to label L2, are you expecting it to read the state of the start button? It doesn't. It just uses the state of the button that was read some time ago. If you need to get new information from outside to make a decision in an if() statement, go and get that information directly above the if().
Then you have the problem that you've set up all the output pins in setup() but don't assign them a value. digitalWrite() each pin to a known value as soon as you make it an output.
Thank you PaulMurrayCbr! Your advise worked. I got most of the code working. My problem now is when the code gets to the part after the generator is running, and waiting for the kill button to be pressed, the code just continues to the end as if I had pressed the kill button. Any ideas?
// Arduino looks for start button to be pushed. This will
//bring start relay high which will crank over the motor until it is
//running and making AC power at which time will close an
//AC relay that is used as in input (voltage) to go high. This will signal
//for the starter to be disengaged(start Low) and a 30 second warm up
//delay. Then the code will close the contactor(contactor High) that allows the
//AC power to my main breaker panel. The arduino will monitor
//for the kill button to be pressed which will kill
//AC contactor(contactor Low) and give a 10 second stabilizing delay before the
//kill relay will go high for 7 seconds then go low.
const int start = 13;
const int contactor = 11;
const int kill = 12;
const int voltage = 2;
const int startbut = 1;
const int killbut = 0;
int startState = 0;
int killState = 0;
int voltageState = 0;
void setup() {
// put your setup code here, to run once:
pinMode ( start , OUTPUT );
pinMode ( contactor , OUTPUT );
pinMode ( kill , OUTPUT );
pinMode ( voltage , INPUT );
pinMode ( startbut, INPUT );
pinMode ( killbut, INPUT );
}
void loop() {
startState = digitalRead(startbut);
killState = digitalRead(killbut);
voltageState = digitalRead(voltage);
//check state of start button
if (startState == HIGH )
{
L1:
//if start button pushed engage starter
digitalWrite(start , HIGH );
{
voltageState = digitalRead(voltage);
//check if ac voltage relay closed
if (voltageState == HIGH ) {
//if ac relay closed generator is running so turn off starter
digitalWrite(start , LOW );
//30 second delay for warm up
delay(1000);
//close contactor to allow ac current to house
digitalWrite (contactor , HIGH );
delay(1000);
} else {
{
goto L1;
}
}
}
// ________generator should be currently running and power to the house_________
killState = digitalRead(killbut);
//check if kill button is pressed
if (killState == HIGH ) {
//kill button pressed so turn off ac contactor
digitalWrite( contactor , LOW );
//10 second delay for generator to stabilize
delay(10000);
//engage kill relay
digitalWrite( kill, HIGH );
//7 second delay for generator to stop running
delay(7000);
//turn off kill relay
digitalWrite( kill, LOW );
}
}
}
[code/]
Then replace your little goto loop with a while. Or a do-while will also work here.
As for the kill button behaving as if it's always pressed, is it a floating input? You've given no description or schematic for the circuit. If there's no pulldown resistor, then the input may randomly float up to HIGH.
MorganS- Thanks for the do-while advise. I imagine it will make all of my future code easier to follow. As for my problem, All of the inputs have a 10,000 ohm pulldown resistor on them(inputs 0-2). They go HIGH when pressed. The start button works fine and is wired the same. I already verified to make sure the button works properly. The outputs for now are just turning on and off an led(outputs 11-13). The code runs as if my if(killstate == HIGH) is not even there and just continues on through the code til the end then loops and waits for another start button to be pushed. I feel like I have a small error somewhere or brackets placed incorrectly though even after messing with them there is no change.
ratheadracing:
I feel like I have a small error somewhere or brackets placed incorrectly though even after messing with them there is no change.
Well... look closely at the code below:
L1:
//if start button pushed engage starter
digitalWrite(start , HIGH );
{
voltageState = digitalRead(voltage);
//check if ac voltage relay closed
if (voltageState == HIGH ) {
[...]
} else {
{
goto L1;
}
}
}
// ________generator should be currently running and power to the house_________
It's probably not your problem, but you have several sets of extra brackets here. Try to auto format your code (CTRL + T) and you might find them all.
MorganS
IT WORKS!!!
The serial pins were a problem amongst one other code issue but everything functions as designed now. Any advise on where to begin to control this via bluetooth on my phone? I have the hc-06 and played with it in someone else's code but never from scratch on top of another already working program.