Electricity meter using function

I use this code

https://github.com/DaveBerkeley/elster_meter/blob/master/elec_meter.ino

of this project:

http://www.rotwang.co.uk/projects/meter.html

and I change it by adding

String  incomingByte;   // for incoming serial data

and changing the loop code as below:

void loop()
{
    incomingByte = Serial.readString();
    if (incomingByte == "PRINT")
    {
        int byte_data = decode_bit_stream();
        if (byte_data == -1)
           return;
        meter.on_data(byte_data);
    }
}

so that when I write PRINT on the serial to show the measurement. But it does not work. Can you help me?

Why did you give a String the name "incomingByte"?

It is just a name for the variable. What is wrong with that?

360modena_cs: It is just a name for the variable. What is wrong with that?

Nothing, unless you need help and you want other people to understand it. It's not a byte, so... ?

Of course I need help...! :) Can you see anything other that makes the code not working?

You are doing a comparison. When they don't work it is often a good idea to print the values being compared. Have you tried that ?

As to the name of the variable, why not call it wibble999 ? It would make make just as much sense.

Ok, I printed the variables compared. But, again the code is not working.

Can you see anything other that makes the code not working?

Yes. Why do you attempt to read serial data without checking to see if there is any available() first?

Why do you expect the Arduino to guess when the string is complete? readString() is the lamest method in the Stream class. It is used only by people that don't know what they are doing.

Mandatory reading BEFORE you post again. http://forum.arduino.cc/index.php?topic=396450.0 Nary a String in site on that page.

360modena_cs: Ok, I printed the variables compared. But, again the code is not working.

That is the lamest possible thing you could have posted.

The code does something. Only YOU can see what it does.

You expect the code to do something. Only YOU know what you expect.

If you can't provide useful details, don't bother whining posting here.

360modena_cs: Ok, I printed the variables compared. But, again the code is not working.

Printing them will not make it work but seeing what is printed might provide clues. What did you print and what did you see ?

I printed the incomingByte on Serial monitor to see if it reads (the feedback) what I press through the Serial. And it reads it correctly. But for some reason it does not do what it has to do, meaning to print the power of the meter.

The changed code:

void loop()
{
    incomingByte = Serial.readString();
    if (incomingByte == "PRINT")
    { 
        Serial.println(incomingByte); // for testing
        int byte_data = decode_bit_stream();
        if (byte_data == -1)
           return;
        meter.on_data(byte_data);
    }
}
void loop()
{
    incomingByte = Serial.readString();
    if (incomingByte == "PRINT")
    {
        Serial.println(incomingByte); // for testing
        int byte_data = decode_bit_stream();
        if (byte_data == -1)
           return;
        meter.on_data(byte_data);
    }
}

Which of those 12 lines is supposed to send ANYTHING to the serial port?

Printing the incoming string after you've decided that it does contain "PRINT" didn't tell you a thing.

I think it is this part of the code:

void meter_reading(unsigned long reading)
{
  Serial.print(reading);
  Serial.print("\r\n");

  static int state = 0;
  digitalWrite(ledPin, state = !state);
}

from lines 352-359 here:

https://github.com/DaveBerkeley/elster_meter/blob/master/elec_meter.ino

I think it is this part of the code:

Some proof that that code is called would be useful.

That function's name is stupid. It does NOT read from a meter.

360modena_cs, multiple times you have posted small snippets of your code. Don't do that. Post the entire sketch. That lets us see how you declared variables. How you call various functions.

Once again,

I took the code from here:

https://github.com/DaveBerkeley/elster_meter/blob/master/elec_meter.ino

that works.

And I changed the

Serial.begin(9600); to Serial.begin(38400);

in void setup()

I changed the void loop(). to this:

void loop()
{   
    if (incomingByte == "PRINT")
    { 
        Serial.println(incomingByte); // for testing
        int byte_data = decode_bit_stream();
        if (byte_data == -1)
           return;
        meter.on_data(byte_data);
    }
}

Also I put String  incomingByte; at the begining of the code. And after these changes the code does not work!

I tried to upload the whole code here, but it is too large to be accepted.

You were supposed to print incomingByte before you tested it. When you do, print “>” immediately before it and “<” immediately after it so that you can see whether any stray characters have crept in. It is also a good idea to print a text label before it so that you know what you are seeing in the serial monitor.

Printing a text message when the test succeeds, or when it fails, is also a good idea so that you know what the program is doing.

Thanx! I put print labels. And when I run it, it only prints 1, not 2, so something wrong happens between there.

The changed code is this:

void loop()
{
    incomingByte = Serial.readString();
    if (incomingByte == "PRINT")
    {
        Serial.println("1");
        int byte_data = decode_bit_stream();
        if (byte_data == -1)
           return;
        meter.on_data(byte_data);
        Serial.println("2");
    }
}

*In the previous post I forgot to add incomingByte = Serial.readString();

It looks like decode_bit_stream() is returning -1

More debugging prints required. If you posted the whole program here it would be easier to provide help.

I changed it to

void loop()
{
    incomingByte = Serial.readString();
    if (incomingByte == "PRINT")
    {
        Serial.println("1");
        int byte_data = decode_bit_stream();
        Serial.println("2");
        if (byte_data == -1)
        {
           Serial.println("3"); 
           return;
        }
        Serial.println("4");
        meter.on_data(byte_data);
        Serial.println("5");
    }
}

Now it prints 1,2,3

Please see #15 for the changes I have made.