Question about code1

can help pleas , thise my code for generator auto start stop it work fine .

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
  }
  }

/////////////////////////////////////
i want compine this code with this code (ethernet web server switch on off)

/*
  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");
}                         
}

how i can do this please

@128asdfghi

TOPIC SPLIT
PLEASE DO NOT HIJACK / NECRO POST !

Could you take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

What do you need help with if it works fine ?

i want to compine it with this ETHERNET_WEB_SERVER.ino (3.8 KB) pleas help

pleas tell me how i can that ?? to work fine in the second code

im sorry: PerryBebbington

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.