PLEAS ANY ONE CAN HELPE ME . I have tow code i want to compine them together.ats9 is for generator auto start stop and the second ( ETHERNET_WEB_SERVER)for web server switch on off

i want to compine this ats9.ino (4.3 KB)
with this ;;
ETHERNET_WEB_SERVER.ino (3.8 KB)

Why not do it the way told, posting the code here? Most helpers don’t want to fill their computers with a lot of crap.

How do you need the two sketches to interact?

i want to insert this code

int IGN = 6; //Ignition Pin
int SW = 7; //Starter Pin
int GEN = 5; //generator RPM pin
int LINE= 8; 

uint32_t Start_Time = 3000; //10 seconds to allow starter to run per attempt
uint32_t Wait_Time = 3000; //30 seconds to wait between start attempts to allow starter to cool
const unsigned long sampleTime = 1000;
int long Gen_Shutdown_Time = 0;

int Current_Time = 0;
int Time_Left = 0;
int IGN_Attempt = 0;

int Genstate = 0;
int Linestate = 0;
 //this is to indicate if generator manual started or automated.

void setup() {
  Serial.begin(9600); //enable serial connection for debug. Switch any oled.print to serial.print to send to serial
  
  pinMode(GEN,INPUT); 
  pinMode(LINE,INPUT);
    digitalWrite(GEN,HIGH); 
    digitalWrite(LINE,HIGH); 

  pinMode(IGN,OUTPUT);
  pinMode(SW,OUTPUT);
  
  }
 void linestate() {

   int Linestate= digitalRead(LINE); //current pin state

if(digitalRead(LINE)==LOW) { 
Linestate=HIGH;

  Serial.println("GRID ON");
}
else {
  
if(digitalRead(LINE)==HIGH) { 
  Linestate=LOW;
  Serial.print("GRID OFF");

  } 
 }
 }  
 
void genstate() { //generator loop to look at current generator state
   int Genstate=digitalRead(GEN);

  if(digitalRead(GEN)==LOW) { //Check the RPMs of the generator and 500 or higher is running
  Serial.println("operation");
Genstate=HIGH;
  }
  else {
  if(digitalRead(GEN)==HIGH) { //Check the RPMs of the generator and 500 or higher is running
    Genstate =LOW; //Set GenState to low indicating generator is not running
  } 
}
}

void genstart() { //Generator start sequence
  if(IGN_Attempt<3&&digitalRead(GEN)==HIGH) { //Try this for 3 attempts with Generator at low and less than as starts with attempt 0
    IGN_Attempt++; //add one count to start attempts

    Serial.print(IGN_Attempt);
    Serial.println(" of 3 attempts");
    Serial.println("Gen not running");
    
    Serial.println("Ignition on");
    digitalWrite(IGN,HIGH); //Ignition On
    delay(2000); //wait 2 second before starter attempt
    Serial.println("Starter on");
    for( uint32_t timer = millis(); (millis()-timer) < Start_Time; ) { //start a timer in milliseconds called timer and compare to time set against Start_Time
      digitalWrite(SW,HIGH); //Starter On
      genstate();
      if(digitalRead(GEN)==LOW) { //generator running
        Serial.println("Starter off");
        digitalWrite(SW,LOW); //Starter off
        Serial.println("Generator running");
        break;
      }
      delay(1);
    }
    Serial.println("Starter off");
    digitalWrite(SW,LOW); //Starter Off
    genstate();
    //if Start loop complete and genrator not running wait 30 sec before next attempt but if 3rd attempt no reason to delay since wont try start attempt again
    if(digitalRead(GEN)==HIGH &&IGN_Attempt < 3) {
      Serial.println("Tried for 10 seconds");
      Serial.println("Failed to start");
      Serial.println("Wait 30 seconds");
      for( uint32_t timer2 = millis(); (millis()-timer2) < Wait_Time; ) {
      }
    }      
  }
  else {
    if(digitalRead(GEN)==LOW) { //change to RPM to indicate generator running
      Serial.println("Generator running");
          digitalWrite(IGN,HIGH); //Ignition On

      IGN_Attempt=0;
    }
    else { //Generator did not start so shutdown everything
      genshutdown();
      Serial.println("Failed to start!");
      Serial.println("Please check");
      Serial.println("generator!");
     
    }
  }
}

void genshutdown() { //Generator shutdown sequence
  Serial.print("Generator");
  Serial.println("shutdown");
  digitalWrite(IGN,LOW);
  digitalWrite(SW,LOW);
  delay(1);
}

void loop() {
 
 linestate();  //get ac pole voltage info
  genstate(); //get generator run status
   int Genstate= digitalRead(GEN);
    int Linestate = digitalRead(LINE); 
  if(digitalRead(LINE) == HIGH ) { //This indicates the AC voltage sensor on pole side of power has been lost or brown out and 10 helps with initial start
    Serial.println("Power lost");
   genstart();
  }     
  else { //if AC power from the pole is available
    
    if( digitalRead(LINE) ==LOW) { //if generator is running 
    Serial.println("Generator OFF");
      genshutdown();
delay(1);
        }
    delay(800);
    IGN_Attempt=0; //reset ignition attempts
  }
  }

to this code

/*
  Web Server

  A simple web server that shows the value of the analog input pins.
  using an Arduino Wiznet Ethernet shield.

  Circuit:
   Ethernet shield attached to pins 10, 11, 12, 13
   Analog inputs attached to pins A0 through A5 (optional)

  created 18 Dec 2009
  by David A. Mellis
  modified 9 Apr 2012
  by Tom Igoe

*/

#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
String RECIEVER;
String RECIEVER2;
   
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
byte gateway[] = { 172, 17, 91, 1 };
IPAddress ip(172, 17, 91, 177);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
void setup() {
   
 Serial.begin(9600);
      pinMode(8,INPUT);
digitalWrite(8,HIGH);
    pinMode(9,OUTPUT);

  while (!Serial) {
    ; 
  }
 

  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}

void loop() {
int S =(digitalRead(8));
 
     EthernetClient client = server.available();
    
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        RECIEVER += c;
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (RECIEVER.length() < 50) {
          RECIEVER2 += c;
        }

        if (RECIEVER2.indexOf("pin9=off") > 0 ) {
          digitalWrite(9, HIGH);
        }
        else if (RECIEVER2.indexOf("pin9=on") > 0 ) {
          digitalWrite(9, LOW);
        }
          
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.println("</br>");
          client.println  (" <center>");
          client.println("<font size='60%' color='black'> ");
          client.println("<table border='4'   <td align='center '> GENERATOR POWER CONTROL   </td> </table>");
          client.println(" </center>");

          client.println("</font>");
          client.println("<CENTER>");
          client.println(" <h2> SWICH1 <a href=\"/?pin9=on\"\"> <button >  ON </button> </a> ");
          client.println(" <a href=\"/?pin8=OFF\"\"> <button >  OFF  </button> </a> ");
          client.println("</CENTER>");
        
          client.println("</br>");
          client.println(" <h2>GRID ");
          client.println("<CENTER>");

          if (S==LOW)
            client.print("  ON ");
          else

            client.print("OFF ");
       
          client.println("</h2>");
         
          //client.println(RECIEVER2);
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
        currentLineIsBlank = false;
        }
      }
    }
           
    delay(1);
    RECIEVER = " ";
    RECIEVER2 = " ";
    
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
}                         
}

Well I get that.

Is the generator status or control to be reported/performed via the web server? Or does the generator control just sort of happen in the background with no interaction with the server logic?

If it’s just that – no interaction, no reporting etc – what’s stopping you from integrating them yourself?

1 Like

WHEN i compind them together i have an proplem in this part

linestate();  //get ac pole voltage info
 
void loop() {
 genstate(); //get generator run status
  int Genstate = digitalRead(GEN);
  int Linestate = digitalRead(LINE);
  if (digitalRead(LINE) == HIGH ) { //This indicates the AC voltage sensor on pole side of power has been lost or brown out and 10 helps with initial start
    Serial.println("Power lost");
    genstart();
  }
  else { //if AC power from the pole is available

    if ( digitalRead(LINE) == LOW) { //if generator is running
      Serial.println("Generator OFF");
      genshutdown();

    }

  }

when the tow input pin 5 and 8 high the IGN_Attempt after 3 times dont stop

In the web server code you’re using pins 8 (input) and 9 (output) though there’s no indication or comment clarifying what they do.

In the generator code pin 8 indicate mains/line status. Is that the function served in your web server code?

In the generator code pin 9 is the ignition for the generator. In that code it’s controlled as part of the start sequence.

In your web server code:

                if (RECIEVER2.indexOf("pin9=off") > 0 ) 
                    digitalWrite(9, HIGH);
                else if (RECIEVER2.indexOf("pin9=on") > 0 ) 
                    digitalWrite(9, LOW);

What are you trying to accomplish here?

I AM trying to accomplish SWITCH ON /OFF.

IM SORRY MY SON IHAVE MODIFID THE CODE THERE WAS AN ERROR IN PIN NUMBER 9

*/

#include <SPI.h>
#include <Ethernet.h>
String RECIEVER;
String RECIEVER2;
int IGN = 6; //Ignition Pin
int SW = 7; //Starter Pin
int GEN = 5; //generator RPM  INPUT pin
int LINE = 8; // MAINS POWER LINE   INPUT PIN


uint32_t Start_Time = 3000; //10 seconds to allow starter to run per attempt
uint32_t Wait_Time = 3000; //30 seconds to wait between start attempts to allow starter to cool
const unsigned long sampleTime = 1000;
int long Gen_Shutdown_Time = 0;

int Current_Time = 0;
int Time_Left = 0;
int IGN_Attempt = 0;

int Genstate = 0;
int Linestate = 0;

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
byte gateway[] = { 172, 17, 91, 1 };
IPAddress ip(172, 17, 91, 177);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
void setup() {

  Serial.begin(9600);

  pinMode(9, OUTPUT);
  pinMode(GEN, INPUT);
  pinMode(LINE, INPUT);
  digitalWrite(GEN, HIGH);
  digitalWrite(LINE, HIGH);


  pinMode(IGN, OUTPUT);
  pinMode(SW, OUTPUT);
  while (!Serial) {
    ;
  }


  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}



void linestate() {


  int Linestate = digitalRead(LINE); //current pin state

  if (digitalRead(LINE) == LOW) {
    Linestate = HIGH;

    Serial.println("GRID ON");
  }
  else {

    if (digitalRead(LINE) == HIGH) {
      Linestate = LOW;
      Serial.print("GRID OFF");

    }

  }

}

void genstate() { //generator loop to look at current generator state
  int Genstate = digitalRead(GEN);

  if (digitalRead(GEN) == LOW) { //Check the RPMs of the generator and 500 or higher is running
    Serial.println("operation");
    Genstate = HIGH;
  }
  else {
    if (digitalRead(GEN) == HIGH) { //Check the RPMs of the generator and 500 or higher is running
      Genstate = LOW; //Set GenState to low indicating generator is not running
    }
  }
}


void genstart() { //Generator start sequence
  if (IGN_Attempt < 3 && digitalRead(GEN) == HIGH) { //Try this for 3 attempts with Generator at low and less than as starts with attempt 0
    IGN_Attempt++; //add one count to start attempts

    Serial.print(IGN_Attempt);
    Serial.println(" of 3 attempts");
    Serial.println("Gen not running");

    Serial.println("Ignition on");
    digitalWrite(IGN, HIGH); //Ignition On
    delay(2000); //wait 2 second before starter attempt
    Serial.println("Starter on");
    for ( uint32_t timer = millis(); (millis() - timer) < Start_Time; ) { //start a timer in milliseconds called timer and compare to time set against Start_Time
      digitalWrite(SW, HIGH); //Starter On
      genstate();
      if (digitalRead(GEN) == LOW) { //generator running
        Serial.println("Starter off");
        digitalWrite(SW, LOW); //Starter off
        Serial.println("Generator running");
        break;
      }
      delay(1);
    }
    Serial.println("Starter off");
    digitalWrite(SW, LOW); //Starter Off
    genstate();
    //if Start loop complete and genrator not running wait 30 sec before next attempt but if 3rd attempt no reason to delay since wont try start attempt again
    if (digitalRead(GEN) == HIGH && IGN_Attempt < 3) {
      Serial.println("Tried for 10 seconds");
      Serial.println("Failed to start");
      Serial.println("Wait 30 seconds");
      for ( uint32_t timer2 = millis(); (millis() - timer2) < Wait_Time; ) {
      }
    }
  }
  else {
    if (digitalRead(GEN) == LOW) { //change to RPM to indicate generator running
      Serial.println("Generator running");
      digitalWrite(IGN, HIGH); //Ignition On

      IGN_Attempt = 0;
    }
    else { //Generator did not start so shutdown everything
      genshutdown();
      Serial.println("Failed to start!");
      Serial.println("Please check");
      Serial.println("generator!");

    }
  }
}

void genshutdown() { //Generator shutdown sequence
  Serial.print("Generator");
  Serial.println("shutdown");
  digitalWrite(IGN, LOW);
  digitalWrite(SW, LOW);
  delay(1);
}


void loop() {
  linestate();  //get ac pole voltage info
  genstate(); //get generator run status
  int Genstate = digitalRead(GEN);
  int Linestate = digitalRead(LINE);
  if (digitalRead(LINE) == HIGH ) { //This indicates the AC voltage sensor on pole side of power has been lost or brown out and 10 helps with initial start
    Serial.println("Power lost");
    genstart();
  }
  else { //if AC power from the pole is available

    if ( digitalRead(LINE) == LOW) { //if generator is running
      Serial.println("Generator OFF");
      genshutdown();

    }

  }
  EthernetClient client = server.available();

  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        RECIEVER += c;
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (RECIEVER.length() < 50) {
          RECIEVER2 += c;
        }

        if (RECIEVER2.indexOf("pin9=off") > 0 ) {
          digitalWrite(9, HIGH);
        }
        else if (RECIEVER2.indexOf("pin9=on") > 0 ) {
          digitalWrite(9, LOW);
        }


        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.println("</br>");
          client.println  (" <center>");
          client.println("<font size='60%' color='black'> ");
          client.println("<table border='4'   <td align='center '> GENERATOR POWER CONTROL   </td> </table>");
          client.println(" </center>");

          client.println("</font>");
          client.println("<CENTER>");
          client.println(" <h2> SWICH1 <a href=\"/?pin9=on\"\"> <button >  ON </button> </a> ");
          client.println(" <a href=\"/?pin8=OFF\"\"> <button >  OFF  </button> </a> ");
          client.println("</CENTER>");


          client.println("</br>");
          client.println(" <h2>GRID ");
          client.println("<CENTER>");
          if (digitalRead(LINE) == LOW )
            client.print("  ON ");
          else
            client.print("OFF ");
          client.println(" <h2>GEN ");

          if (digitalRead(GEN) == LOW)
            client.print("  ON ");
          else

            client.print("OFF ");

          client.println("</h2>");

          //client.println(RECIEVER2);
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }




    delay(1);
    RECIEVER = " ";
    RECIEVER2 = " ";

    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }


}

Here’s something you can try. It’s based on your original but not a copy of it. Compiles as far as I can tell but not tested:

/*
  Web Server

  A simple web server that shows the value of the analog input pins.
  using an Arduino Wiznet Ethernet shield.

  Circuit:
   Ethernet shield attached to pins 10, 11, 12, 13
   Analog inputs attached to pins A0 through A5 (optional)

  created 18 Dec 2009
  by David A. Mellis
  modified 9 Apr 2012
  by Tom Igoe

*/

#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>

//generator-related constants
#define K_TIME_IGN_TO_CRANK         2000ul      //mS    time from ignition on to starter
#define K_TIME_CRANK_DURATION       10000ul     //mS    maximum engine cranking time
#define K_TIME_CRANK_COOLDN         30000ul     //mS    time to allow starter to cool between attempts
#define K_TIME_TURNOFF_DELAY        15000ul     //mS    time after running before returning to top of state machine
#define K_MAX_START_ATTEMPTS        3           //#     maximum start attempts before giving up on generator

//generator state machine state names
enum eGenStates 
{
    ST_GEN_OFF=0,
    ST_GEN_IGNITION_ON,
    ST_GEN_CRANK,
    ST_GEN_MONITOR_START,
    ST_GEN_RUNNING,
    ST_GEN_TIMEDELAY,
    ST_GEN_ERROR
    
};

String RECIEVER;
String RECIEVER2;
   
byte mac[] = 
{
    0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};

byte gateway[] = { 172, 17, 91, 1 };

IPAddress ip(172, 17, 91, 177);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

//generator-specific
const uint8_t pinIgnition = 9;          //generator ignition output (high == turn on ignition)
const uint8_t pinStarter = 7;           //generator starter output (high == engage starter)
const uint8_t pinGeneratorStatus = 5;   //generator running input (low == generator running)
const uint8_t pinLineStatus = 8;        //AC mains status (low == mains available)
bool
    bWebSrvr_GenEnable;

void setup() 
{   
    Serial.begin(9600);    
    while (!Serial);        //wait for serial monitor
    
    Ethernet.begin(mac, ip);
    server.begin();
    Serial.print("server is at ");
    Serial.println(Ethernet.localIP());

    //gen specific
    bWebSrvr_GenEnable = false;
    pinMode( pinGeneratorStatus, INPUT_PULLUP ); 
    pinMode( pinLineStatus, INPUT_PULLUP );        
    pinMode( pinIgnition, OUTPUT );
    pinMode( pinStarter, OUTPUT );
        
}//setup

void loop() 
{
    GeneratorStateMachine();
    DoWebClient();
        
}//loop

void DoWebClient( void )
{
    EthernetClient client = server.available();
    int S = digitalRead( pinLineStatus );
    
    if (client) 
    {
        Serial.println("new client");
        
        // an http request ends with a blank line
        boolean currentLineIsBlank = true;
        while (client.connected()) 
        {
            if (client.available()) 
            {
                char c = client.read();
                RECIEVER += c;
                // if you've gotten to the end of the line (received a newline
                // character) and the line is blank, the http request has ended,
                // so you can send a reply
                if( RECIEVER.length() < 50 ) 
                    RECIEVER2 += c;
    
                if( RECIEVER2.indexOf( "gen=on" ) > 0 ) 
                    bWebSrvr_GenEnable = true;
                else if( RECIEVER2.indexOf( "gen=off" ) > 0 ) 
                    bWebSrvr_GenEnable = false;
    
                if( c == '\n' && currentLineIsBlank) 
                {
                    // send a standard http response header
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println("Connection: close");    // the connection will be closed after completion of the response
                    client.println("Refresh: 5");           // refresh the page automatically every 5 sec
                    client.println();
                    client.println("<!DOCTYPE HTML>");
                    client.println("<html>");
                    client.println("</br>");
                    client.println  (" <center>");
                    client.println("<font size='60%' color='black'> ");
                    client.println("<table border='4'   <td align='center '> GENERATOR POWER CONTROL   </td> </table>");
                    client.println(" </center>");
    
                    client.println("</font>");
                    client.println("<CENTER>");
                    client.println(" <h2> SWITCH1 <a href=\"/?pin9=on\"\"> <button >  ON </button> </a> ");
                    client.println(" <a href=\"/?pin8=OFF\"\"> <button >  OFF  </button> </a> ");
                    client.println("</CENTER>");
        
                    client.println("</br>");
                    client.println(" <h2>GRID ");
                    client.println("<CENTER>");
    
                    if (S==LOW)
                        client.print("  ON ");
                    else                    
                        client.print("OFF ");    
    
                    client.println("</h2>");
    
                    //client.println(RECIEVER2);
                    client.println("</html>");
                    break;
                    
                }//if
                
                if (c == '\n') 
                {
                    // you're starting a new line
                    currentLineIsBlank = true;
                    
                }//if
                else if (c != '\r') 
                {
                    // you've gotten a character on the current line
                    currentLineIsBlank = false;
                    
                }//else
                
            }//if
            
        }//while
    
        delay(1);
        RECIEVER = " ";
        RECIEVER2 = " ";
        
        // close the connection:
        client.stop();
        Serial.println("client disconnected");
        
    }//if                      
    
}//DoWebClient


void GeneratorStateMachine( void )
{
    uint32_t
        timeNow = millis();
    static uint32_t
        timeGen,
        timeGenDelay;
    static uint8_t
        startAttempts,
        nextState,
        stateGen = ST_GEN_OFF;

    switch( stateGen )
    {
        case    ST_GEN_OFF:
            if( bWebSrvr_GenEnable == true || digitalRead( pinLineStatus ) == HIGH )
            {
                if( digitalRead( pinGeneratorStatus ) == LOW )
                {
                    //if generator is already running, go to that state
                    stateGen = ST_GEN_RUNNING;
                }
                    
                else
                {    
                    //proceed to start the generator
                    stateGen = ST_GEN_IGNITION_ON;
                    startAttempts = 0;
                    
                }//else
                
            }//if
            
        break;

        case    ST_GEN_IGNITION_ON:   
            startAttempts++;
                     
            //turn on the ignition and wait the ign-to-crank delay
            digitalWrite( pinIgnition, HIGH );
            nextState = ST_GEN_CRANK;
            timeGen = timeNow;
            timeGenDelay = K_TIME_IGN_TO_CRANK;
            stateGen = ST_GEN_TIMEDELAY;           
            
        break;

        case    ST_GEN_CRANK:
            //activate the starter motor
            digitalWrite( pinStarter, HIGH );
            timeGen = timeNow;
            timeGenDelay = K_TIME_CRANK_DURATION;   //set max starter run time
            stateGen = ST_GEN_MONITOR_START;            
            
        break;

        case    ST_GEN_MONITOR_START:        
            //has crank time exceeded the limit?
            if( (timeNow - timeGen) >= timeGenDelay )
            {
                //crank duration exceeded; msg for operator
                Serial.print( "Generator start unsuccessful on attempt # " ); Serial.println( startAttempts );                
                //shut off the starter and ignition                
                digitalWrite( pinStarter, LOW );
                digitalWrite( pinIgnition, LOW );
                
                if( startAttempts >= K_MAX_START_ATTEMPTS )
                {
                    //generator has failed to start after max # of tries
                    Serial.println( "**ERROR**: Generator failed to start!" );
                    Serial.println( "Please check generator!" );
                    bWebSrvr_GenEnable = false;
                    
                    stateGen = ST_GEN_ERROR;
                    
                }//if
                else
                {
                    //allow the starter to cool and then go back for another attempt
                    nextState = ST_GEN_IGNITION_ON;     //when delay is finished, go to this state
                    timeGen = timeNow;
                    timeGenDelay = K_TIME_CRANK_COOLDN; //cool down time delay
                    stateGen = ST_GEN_TIMEDELAY;           
                    
                }//else
                
            }//if
            else
            {
                //not timed out yet; is generator status showing running?
                if( digitalRead( pinGeneratorStatus ) == LOW )
                {
                    //generator has started; disable the starter
                    Serial.println( "Generator running" );
                    Serial.println( "Starter off" );
                    digitalWrite( pinStarter, LOW );

                    stateGen = ST_GEN_RUNNING;
                    
                }//if

            }//else
            
        break;

        case    ST_GEN_RUNNING:
            //look for:
            //  - generator not running (e.g. out of fuel), OR 
            //  - a server command to turn it off, OR
            //  - mains power is available
            //
            // If any case true, turn off the generator
            if( digitalRead( pinGeneratorStatus ) == HIGH || bWebSrvr_GenEnable == false || digitalRead( pinLineStatus ) == LOW )
            {
                //generator needs to be shut-down
                //turn off the ignition    
                bWebSrvr_GenEnable = false;     //in case generator was off due to some other reason
                digitalWrite( pinIgnition, LOW );
                nextState = ST_GEN_OFF;         //when delay is finished, go to this state
                timeGen = timeNow;
                timeGenDelay = K_TIME_TURNOFF_DELAY; //shut-down delay
                stateGen = ST_GEN_TIMEDELAY;           
                                    
            }//if
            
        break;

        case    ST_GEN_TIMEDELAY:
            if( (timeNow - timeGen) >= timeGenDelay )
                stateGen = nextState;
        break;

        case    ST_GEN_ERROR:
            //do nothing
            //we're here if the generator failed to start after three tries
            //only way out is to reset the Arduino
            //if web srvr requests another start, send msg to serial monitor and clear flag
            if( bWebSrvr_GenEnable == true )
            {
                Serial.println( "**ERROR**: Generator failed to start!" );
                Serial.println( "Please check generator!" );
                
                bWebSrvr_GenEnable = false;
                
            }//if
            
        break;
        
    }//switch
    
}//GeneratorStateMachine
1 Like

Note that the message to the web client is “gen=on” and “gen=off”…

EXPLAINING THE CODE : TOW INPUT PIN
1-int GEN = 5; //generator RPM INPUT pin
2-int LINE = 8; // MAINS POWER LINE INPUT pin

and tow output pins:
int IGN = 6; //Ignition out Pin
int SW = 7; //Starter out Pin

and the third output { pin9}
for greating switch (button) in the page of web server for on off A lAMP.

HOW THE CODE WORKS:
WHEN the main power lost (pin 8) the generator start with 3 attempts (if (IGN_Attempt < 3 && digitalRead(GEN) == HIGH) { //Try this for 3 attempts with Generator at low and less than as starts with attempt 0
) . if the generator started the SW = 7; //Starter Pin will becom low .

when the power line ON the generator will stop.

but the proplem is when the generator didnt started and the power line off the the loop of generator start dont stoping .

if (digitalRead(LINE) == HIGH ) { //This indicates the AC voltage sensor on pole side of power has been lost or brown out and 10 helps with initial start
Serial.println(“Power lost”);
genstart();
}
else { //if AC power from the pole is available


if ( digitalRead(LINE) == LOW) { //if generator is running
  Serial.println("Generator OFF");
  genshutdown();

}
}

i want the starter pin stop after the 3 attempts .

think think think so much very match my son :slight_smile: iwill trying it…

THIS MESSGE DONT APPEAR IN WEB PAGE?

I mean that I modified what originally was:

    if (RECIEVER2.indexOf("pin9=off") > 0 ) {
      digitalWrite(9, HIGH);
    }
    else if (RECIEVER2.indexOf("pin9=on") > 0 ) {
      digitalWrite(9, LOW);
    }

to

                if( RECIEVER2.indexOf( "gen=on" ) > 0 ) 
                    bWebSrvr_GenEnable = true;
                else if( RECIEVER2.indexOf( "gen=off" ) > 0 ) 
                    bWebSrvr_GenEnable = false;

HELLO
MY SON " Blackfin".

if (RECIEVER2.indexOf(“pin9=off”) > 0 ) {
digitalWrite(9, HIGH);
}
else if (RECIEVER2.indexOf(“pin9=on”) > 0 ) {
digitalWrite(9, LOW);
}
this pin i was used it in that code for greating switch (button) for ON/OFF LAMP.
i did not used in generator code or web server code for ignition output pin for the generator .and the ignition pin in the generator code was pin6 . it was mastik (error)
and i modifid it.sorry Blackfin.

1 Like

my son Blackfin.
i have tested the program and if the power line available the generator start ok and
and the generator stop after three attempts if it did not start also the starter stop if generator start( state==low ) code work fine to here.////////////////
\\\\\\\
but i found problem:
FRIST when the generator running and the power mains available the generator don’t stopping .
also when the the three attempts complete and the generator(pinGeneratorStatus==high) did not star and the (pinLineStatus==high ) so when the power mains available and power off again the generator did not start.did not rest.
i want to modified it WHEN THE GENERATOR START the generator ignition output still high so when the power mains available become low the generator will power off
i want the generator start when the power mains off
i want the generator stop only if the power mains available

i would not stopping the generator with a server command to turn it off i want to delate it
if( RECIEVER2.indexOf( “gen=on” ) > 0 )
bWebSrvr_GenEnable = true;
else if( RECIEVER2.indexOf( “gen=off” ) > 0 )
bWebSrvr_GenEnable = false;

how ican do that pleas so match Blackfin?

my son Blackfin.
i have tested the program and if the power line available the generator start ok and
and the generator stop after three attempts if it did not start also the starter stop if generator start( state==low ) code work fine to here.////////////////
\\\\\\\
but i found problem:
FRIST when the generator running and the power mains available the generator don’t stopping .
also when the the three attempts complete and the generator(pinGeneratorStatus==high) did not star and the (pinLineStatus==high ) so when the power mains available and power off again the generator did not start.did not rest.
i want to modified it WHEN THE GENERATOR START the generator ignition output still high so when the power mains available become low the generator will power off
i want the generator start when the power mains off
i want the generator stop only if the power mains available

i would not stopping the generator with a server command to turn it off i want to delate it
if( RECIEVER2.indexOf( “gen=on” ) > 0 )
bWebSrvr_GenEnable = true;
else if( RECIEVER2.indexOf( “gen=off” ) > 0 )
bWebSrvr_GenEnable = false;

how ican do that pleas so match Blackfin?