user61533: The first one reads from sensors and displays it, the second code receives an incoming byte to control LEDs. I tried to combine it but i don't know why it doesn't work. Please help me out. I need it for my project to run both of them. 
In my java netbeans i have two GUIs. For monitoring(first code) and controlling(second). I want it both to run with only one combined code in arduino.
I tried to use if else statement for the arduino to know what to do. Not working also. I think i just missed something
const int model = 0;  

void setup() {
Serial.begin(9600);

}

float sensitivity[] ={
      0.185,// for ACS712ELCTR-05B-T
      0.100,// for ACS712ELCTR-20A-T
      0.066// for ACS712ELCTR-30A-T

     } ;

void loop() {
static unsigned long count=0;
static float Vsum=0;
float Vave=0;
int volt = analogRead(A1);

float voltage= map(volt,0,1023, 0, 2500)/100.0;
Vsum=Vsum+voltage;

static float Csum=0;
float Cave=0;
 float milli = pow(10, -3);
 float current = (voltage / sensitivity[model]);
 float current1 = current*milli;

Csum=Csum+current1;
count++;
Vave=Vsum/count;
Cave=Csum/count;
float watts = Vave*Cave;

long milisec = millis();
long time=milisec/1000; 

 float energy = (watts*time)/3600;
Serial.print(energy,6);
Serial.println("Wh");
delay(1000);

}

And this is the second code. This receives byte from java to control LEDs
int incomingByte;

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(0, OUTPUT);


}

void loop() {
 // if (Serial.available()>0) {
    incomingByte = Serial.read();

    if(incomingByte == 0x01){
      digitalWrite(13, HIGH);
     Serial.println("ON");

    }else if(incomingByte == 0x00){
      digitalWrite(13, LOW);
       Serial.println("OFF");
    }

    if(incomingByte == 0x02){
      digitalWrite(12, HIGH);
    }else if(incomingByte == 0x03){
      digitalWrite(12, LOW);
    }

    if(incomingByte == 0x04){
      digitalWrite(11, HIGH);
    }else if(incomingByte == 0x05){
      digitalWrite(11, LOW);
    }

    if(incomingByte == 0x06){
      digitalWrite(10, HIGH);
    }else if(incomingByte == 0x07){
      digitalWrite(10, LOW);
    }

    if(incomingByte == 0x08){
      digitalWrite(9, HIGH);
    }else if(incomingByte == 0x09){
      digitalWrite(9, LOW);
    }

    if(incomingByte == 0x10){
      digitalWrite(8, HIGH);
    }else if(incomingByte == 0x11){
      digitalWrite(8, LOW);
    }

    if(incomingByte == 0x12){
      digitalWrite(7, HIGH);
    }else if(incomingByte == 0x13){
      digitalWrite(7, LOW);
    }

    if(incomingByte == 0x14){
      digitalWrite(6, HIGH);
    }else if(incomingByte == 0x15){
      digitalWrite(6, LOW);
    }

    if(incomingByte == 0x16){
      digitalWrite(5, HIGH);
   }else if(incomingByte == 0x17){
      digitalWrite(5, LOW);
    }

    if(incomingByte == 0x18){
      digitalWrite(4, HIGH);
    }else if(incomingByte == 0x19){
      digitalWrite(4, LOW);
    }

    if(incomingByte == 0x20){
      digitalWrite(3, HIGH);
    }else if(incomingByte == 0x21){
      digitalWrite(3, LOW);
    }

    if(incomingByte == 0x22){
      digitalWrite(2, HIGH);
    }else if(incomingByte == 0x23){
      digitalWrite(2, LOW);
    }

    if(incomingByte == 0x24){
      digitalWrite(1, HIGH);
    }else if(incomingByte == 0x25){
      digitalWrite(1, LOW);
    }

    if(incomingByte == 0x026){
      digitalWrite(0, HIGH);
    }else if(incomingByte == 0x027){
      digitalWrite(0, LOW);
    }

  delay(100);
}

This is my sample code from javafx application
    try {
        if (tbLED12.isSelected()) {
            if (arduinoPort != null) {
                arduinoPort.writeByte((byte) 0x02);
                System.out.println("LED 12 ON");
            } else {
                System.out.println("arduinoPort not connected!");
            }
        } else {
            if (arduinoPort != null) {
                arduinoPort.writeByte((byte) 0x03);
                System.out.println("LED 12 OFF");
            } else {
                System.out.println("arduinoPort not connected!");
            }
        }
    } catch (SerialPortException ex) {
        Logger.getLogger(JavaFXApplication11.class.getName())
                .log(Level.SEVERE, null, ex);
    }


user10285: Combining this code is all about synchronizing the reading of the sensors with the control sequences that are sent from the Java program. 
The second program reads the Serial port continuously in the loop method. A better way would be to use a serial interrupt for this. Let me give you an example. 
My code is written without an IDE so it is just an untested example. But hopefully I did not include errors ;-).
I use the first program as a base:
const int model = 0;  
float sensitivity[] = {
  0.185,// for ACS712ELCTR-05B-T
  0.100,// for ACS712ELCTR-20A-T
  0.066// for ACS712ELCTR-30A-T
} ;

unsigned long count = 0;
float         Vsum  = 0;
float         Csum  = 0;

long startTime = 0;

void setup() 
{
  Serial.begin(9600);
  // setting the LED pins to output
  // I use a loop
  for ( int i = 2; i < 14; i++ )
  {
    pinMode( i, OUTPUT );
  }
}


void loop() 
{
  float Vave = 0;
  float Cave = 0;
  // pow is to expensive for the loop
  // I use a constant
  float milli = 0.001;

  if ( startTime = 0 ) startTime = millis();

  float voltage = map( analogRead( A1 ), 0, 1023, 0, 2500 ) / 100.0;
  Vsum = Vsum + voltage;

  float current  = ( voltage / sensitivity[ model ] );
  float current1 = current * milli;

  Csum = Csum + current1;

  count++;

  Vave = Vsum / count;
  Cave = Csum / count;
  float watts = Vave * Cave;

  // you did not use a time delta here,
  // but energiy it work over a time period    
  long timeDelta = ( millis() - startTime ) / 1000; 

  float energy = ( watts * timeDelta ) / 3600;
  Serial.print( energy, 6 );
  Serial.println( "Wh" );

  serialHandler();

  delay( 1000 );
}

void serialHandler() 
{
   delay( 200 );
   while ( Serial.available() ) 
   {
     // get the new byte:
     int inByte = Serial.read();

     if(inByte == 0x01) 
     {
       digitalWrite(13, HIGH);
       //Serial.println("ON");
     }
     else if(inByte == 0x00)
     {
       digitalWrite(13, LOW);
       //Serial.println("OFF");
     }
     else if(inByte == 0x02) digitalWrite(12, HIGH);
     else if(inByte == 0x03) digitalWrite(12, LOW);
     else if(inByte == 0x04) digitalWrite(11, HIGH);
     else if(inByte == 0x05) digitalWrite(11, LOW);
     else if(inByte == 0x06) digitalWrite(10, HIGH);
     else if(inByte == 0x07) digitalWrite(10, LOW);
     else if(inByte == 0x08) digitalWrite(9,  HIGH);
     else if(inByte == 0x09) digitalWrite(9,  LOW);
     else if(inByte == 0x10) digitalWrite(8,  HIGH);
     else if(inByte == 0x11) digitalWrite(8,  LOW);
     else if(inByte == 0x12) digitalWrite(7,  HIGH);
     else if(inByte == 0x13) digitalWrite(7,  LOW);
     else if(inByte == 0x14) digitalWrite(6,  HIGH);
     else if(inByte == 0x15) digitalWrite(6,  LOW);
     else if(inByte == 0x16) digitalWrite(5,  HIGH);
     else if(inByte == 0x17) digitalWrite(5,  LOW);
     else if(inByte == 0x18) digitalWrite(4,  HIGH);
     else if(inByte == 0x19) digitalWrite(4,  LOW);
     else if(inByte == 0x20) digitalWrite(3,  HIGH);
     else if(inByte == 0x21) digitalWrite(3,  LOW);
     else if(inByte == 0x22) digitalWrite(2,  HIGH);
     else if(inByte == 0x23) digitalWrite(2,  LOW);
     //else if(inByte == 0x24) digitalWrite(1,  HIGH);
     //else if(inByte == 0x25) digitalWrite(1,  LOW);
     //else if(inByte == 0x26) digitalWrite(0,  HIGH);
     //else if(inByte == 0x27) digitalWrite(0,  LOW);
   }
}

EDIT:
I changed the code a little bit according to Juraj comments. I also 
commented the `Serial.println' s that had reported the LED 13 state.  

61533: <p>The first one reads from sensors and displays it, the second code receives an incoming byte to control LEDs. I tried to combine it but i don't know why it doesn't work. Please help me out. I need it for my project to run both of them. </p>

<p>In my java netbeans i have two GUIs. For monitoring(first code) and controlling(second). I want it both to run with only one combined code in arduino.</p>

<p>I tried to use if else statement for the arduino to know what to do. Not working also. I think i just missed something</p>

<pre><code>const int model = 0;  

void setup() {
Serial.begin(9600);

}

float sensitivity[] ={
      0.185,// for ACS712ELCTR-05B-T
      0.100,// for ACS712ELCTR-20A-T
      0.066// for ACS712ELCTR-30A-T

     } ;

void loop() {
static unsigned long count=0;
static float Vsum=0;
float Vave=0;
int volt = analogRead(A1);

float voltage= map(volt,0,1023, 0, 2500)/100.0;
Vsum=Vsum+voltage;

static float Csum=0;
float Cave=0;
 float milli = pow(10, -3);
 float current = (voltage / sensitivity[model]);
 float current1 = current*milli;

Csum=Csum+current1;
count++;
Vave=Vsum/count;
Cave=Csum/count;
float watts = Vave*Cave;

long milisec = millis();
long time=milisec/1000; 

 float energy = (watts*time)/3600;
Serial.print(energy,6);
Serial.println("Wh");
delay(1000);

}
</code></pre>

<p>And this is the second code. This receives byte from java to control LEDs</p>

<pre><code>int incomingByte;

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(0, OUTPUT);


}

void loop() {
 // if (Serial.available()&gt;0) {
    incomingByte = Serial.read();

    if(incomingByte == 0x01){
      digitalWrite(13, HIGH);
     Serial.println("ON");

    }else if(incomingByte == 0x00){
      digitalWrite(13, LOW);
       Serial.println("OFF");
    }

    if(incomingByte == 0x02){
      digitalWrite(12, HIGH);
    }else if(incomingByte == 0x03){
      digitalWrite(12, LOW);
    }

    if(incomingByte == 0x04){
      digitalWrite(11, HIGH);
    }else if(incomingByte == 0x05){
      digitalWrite(11, LOW);
    }

    if(incomingByte == 0x06){
      digitalWrite(10, HIGH);
    }else if(incomingByte == 0x07){
      digitalWrite(10, LOW);
    }

    if(incomingByte == 0x08){
      digitalWrite(9, HIGH);
    }else if(incomingByte == 0x09){
      digitalWrite(9, LOW);
    }

    if(incomingByte == 0x10){
      digitalWrite(8, HIGH);
    }else if(incomingByte == 0x11){
      digitalWrite(8, LOW);
    }

    if(incomingByte == 0x12){
      digitalWrite(7, HIGH);
    }else if(incomingByte == 0x13){
      digitalWrite(7, LOW);
    }

    if(incomingByte == 0x14){
      digitalWrite(6, HIGH);
    }else if(incomingByte == 0x15){
      digitalWrite(6, LOW);
    }

    if(incomingByte == 0x16){
      digitalWrite(5, HIGH);
   }else if(incomingByte == 0x17){
      digitalWrite(5, LOW);
    }

    if(incomingByte == 0x18){
      digitalWrite(4, HIGH);
    }else if(incomingByte == 0x19){
      digitalWrite(4, LOW);
    }

    if(incomingByte == 0x20){
      digitalWrite(3, HIGH);
    }else if(incomingByte == 0x21){
      digitalWrite(3, LOW);
    }

    if(incomingByte == 0x22){
      digitalWrite(2, HIGH);
    }else if(incomingByte == 0x23){
      digitalWrite(2, LOW);
    }

    if(incomingByte == 0x24){
      digitalWrite(1, HIGH);
    }else if(incomingByte == 0x25){
      digitalWrite(1, LOW);
    }

    if(incomingByte == 0x026){
      digitalWrite(0, HIGH);
    }else if(incomingByte == 0x027){
      digitalWrite(0, LOW);
    }

  delay(100);
}
</code></pre>

<p>This is my sample code from javafx application</p>

<pre><code>    try {
        if (tbLED12.isSelected()) {
            if (arduinoPort != null) {
                arduinoPort.writeByte((byte) 0x02);
                System.out.println("LED 12 ON");
            } else {
                System.out.println("arduinoPort not connected!");
            }
        } else {
            if (arduinoPort != null) {
                arduinoPort.writeByte((byte) 0x03);
                System.out.println("LED 12 OFF");
            } else {
                System.out.println("arduinoPort not connected!");
            }
        }
    } catch (SerialPortException ex) {
        Logger.getLogger(JavaFXApplication11.class.getName())
                .log(Level.SEVERE, null, ex);
    }
</code></pre>
 10285: <p>Combining this code is all about synchronizing the reading of the sensors with the control sequences that are sent from the Java program. </p>

<p>The second program reads the Serial port continuously in the loop method. A better way would be to use a serial interrupt for this. Let me give you an example. </p>

<p>My code is written without an IDE so it is just an untested example. But hopefully I did not include errors ;-).</p>

<p>I use the first program as a base:</p>

<pre><code>const int model = 0;  
float sensitivity[] = {
  0.185,// for ACS712ELCTR-05B-T
  0.100,// for ACS712ELCTR-20A-T
  0.066// for ACS712ELCTR-30A-T
} ;

unsigned long count = 0;
float         Vsum  = 0;
float         Csum  = 0;

long startTime = 0;

void setup() 
{
  Serial.begin(9600);
  // setting the LED pins to output
  // I use a loop
  for ( int i = 2; i &lt; 14; i++ )
  {
    pinMode( i, OUTPUT );
  }
}


void loop() 
{
  float Vave = 0;
  float Cave = 0;
  // pow is to expensive for the loop
  // I use a constant
  float milli = 0.001;

  if ( startTime = 0 ) startTime = millis();

  float voltage = map( analogRead( A1 ), 0, 1023, 0, 2500 ) / 100.0;
  Vsum = Vsum + voltage;

  float current  = ( voltage / sensitivity[ model ] );
  float current1 = current * milli;

  Csum = Csum + current1;

  count++;

  Vave = Vsum / count;
  Cave = Csum / count;
  float watts = Vave * Cave;

  // you did not use a time delta here,
  // but energiy it work over a time period    
  long timeDelta = ( millis() - startTime ) / 1000; 

  float energy = ( watts * timeDelta ) / 3600;
  Serial.print( energy, 6 );
  Serial.println( "Wh" );

  serialHandler();

  delay( 1000 );
}

void serialHandler() 
{
   delay( 200 );
   while ( Serial.available() ) 
   {
     // get the new byte:
     int inByte = Serial.read();

     if(inByte == 0x01) 
     {
       digitalWrite(13, HIGH);
       //Serial.println("ON");
     }
     else if(inByte == 0x00)
     {
       digitalWrite(13, LOW);
       //Serial.println("OFF");
     }
     else if(inByte == 0x02) digitalWrite(12, HIGH);
     else if(inByte == 0x03) digitalWrite(12, LOW);
     else if(inByte == 0x04) digitalWrite(11, HIGH);
     else if(inByte == 0x05) digitalWrite(11, LOW);
     else if(inByte == 0x06) digitalWrite(10, HIGH);
     else if(inByte == 0x07) digitalWrite(10, LOW);
     else if(inByte == 0x08) digitalWrite(9,  HIGH);
     else if(inByte == 0x09) digitalWrite(9,  LOW);
     else if(inByte == 0x10) digitalWrite(8,  HIGH);
     else if(inByte == 0x11) digitalWrite(8,  LOW);
     else if(inByte == 0x12) digitalWrite(7,  HIGH);
     else if(inByte == 0x13) digitalWrite(7,  LOW);
     else if(inByte == 0x14) digitalWrite(6,  HIGH);
     else if(inByte == 0x15) digitalWrite(6,  LOW);
     else if(inByte == 0x16) digitalWrite(5,  HIGH);
     else if(inByte == 0x17) digitalWrite(5,  LOW);
     else if(inByte == 0x18) digitalWrite(4,  HIGH);
     else if(inByte == 0x19) digitalWrite(4,  LOW);
     else if(inByte == 0x20) digitalWrite(3,  HIGH);
     else if(inByte == 0x21) digitalWrite(3,  LOW);
     else if(inByte == 0x22) digitalWrite(2,  HIGH);
     else if(inByte == 0x23) digitalWrite(2,  LOW);
     //else if(inByte == 0x24) digitalWrite(1,  HIGH);
     //else if(inByte == 0x25) digitalWrite(1,  LOW);
     //else if(inByte == 0x26) digitalWrite(0,  HIGH);
     //else if(inByte == 0x27) digitalWrite(0,  LOW);
   }
}
</code></pre>

<p><strong>EDIT:</strong></p>

<p>I changed the code a little bit according to Juraj comments. I also 
commented the `Serial.println' s that had reported the LED 13 state.  </p>
