-1

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);
    }
  • 1
    You didn't show us your combined code, so we cannot say, what is wrong with it. There are plenty of question like "Need to combine these two codes" on this site. Have you looked into them? – chrisl Dec 14 '19 at 13:08
  • sorry but i cant add my combined code coz its too long but i just add my first code to the second one and it became faulty, but if i run them separately they work well. – Rica Jacutina Dec 14 '19 at 14:17
  • don't set pinmode for 0 and 1. it is Serial RX and TX. – Juraj Dec 14 '19 at 17:22
  • Even if you simply put both codes in one file, that shouldn't be too long for the question editor. Otherwise you should create a minimal complete example. Asking how to combine 2 codes without the problematic combined code is a very broad and general question, that was asked and answered many times here. Without your combined code and descriptions about how it is faulty, every answer to prone to a long comment discussion to get to the bottom of the problem (as we see with Peters answer below). This site is not good for this; it's not a forum. – chrisl Dec 14 '19 at 20:48
1

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.

| improve this answer | |
  • 1
    serialEvent is not an interrupt. outside the loop() the core tests Serial.available() and if is not 0 it calls serialEvent() . – Juraj Dec 14 '19 at 15:31
  • I cannot control the LEDs. One LED only will light up when i run the program even though i switched them all on. But the monitoring works – Rica Jacutina Dec 14 '19 at 16:08
  • @Juraj And again, i learned something. Meanwhile I'm at home and could try out my program, and it didn't worked as I expected. And you gave the explanation. And now I feel sad and thinking about the ice cream in my refrigerator and if I should eat it. Hmmm yes I'll do that. But before I find a better solution for the problem. Perhaps I renmae the funtion, wait a little bit after sending the values to java and then trigger the former event callback. Then I wait one second and once again. – Peter Paul Kiefer Dec 14 '19 at 16:11
  • @RicaJacutina Which LED does work, 13 perhaps? Thats the built in LED. Did you connect the others correctly. Are you sending the right values? Are you aware that for LED 13, you have to send an odd value (1) to switch it on. The others are switched on by even values ( 2,4,6,8,10,12,). This functionallity should work I tried it a few minutes ago with my own setup. – Peter Paul Kiefer Dec 14 '19 at 16:20
  • @PeterPaulKiefer I tried your code, The monitoring works but when i control LED it does not work properly on my java gui. Yes the 13 switched on, but does not switch off. When i tried to run the program again, no LED worked. :( I am aware that 13 light up to odd value and others on even values. And i am sure that i connected it all the right way. – Rica Jacutina Dec 14 '19 at 16:32
0
void serialEvent();
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;
float         DecPowerRate = 9.8623 ;//kWh
long          startTime = 0;
float         no_voltage = 0.00;
//float           address = 0;
float         no_CURRENT = 0.000;
float energy;
long timeDelta;float consumption;float Kwatts;
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;
  float milli = 0.001;// pow is to expensive for the loop

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


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

  if( voltage >no_voltage){
  Vsum = Vsum + voltage;

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

  Csum = Csum + current1;

  count++;

  Vave = Vsum / count;
  Cave = Csum / count;
  Kwatts = (Vave * Cave)/1000;
  }
    if (Cave >no_CURRENT){
  // you did not use a time delta here,
  // but energiy it work over a time period    
  timeDelta = ( millis() - startTime ) / 1000; 
  energy = ( Kwatts * timeDelta ) / 3600;
  consumption = energy * DecPowerRate;
}


  //Serial.print( energy, 6 );
 // Serial.println( "kWh" );

  Serial.print( "P " );
  Serial.print( consumption, 6 );
  Serial.println( "/kWh" );
  delay( 1000 );



}
// naming convention for a serial interrupt
void serialEvent() 
{
   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);

   }
}

This worked! Thanks for the help! <3

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.