Elster A100C electricity meter using a function

Hello,
How can this code https://github.com/DaveBerkeley/elster_meter/blob/master/elec_meter.ino make it work giving the command “PRINT”. I changed the void loop() to this:

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"); 
        }
        Serial.println("5");
        meter.on_data(byte_data);
        Serial.println("6");
    }
}

I put the numbers for testing reason to see if it works, but it prints up to 3. It does not work.Can you help me?

That is not surprising. It will stop after printing “3”, because the return statement causes the function loop( ) to end early. It will never print “4”. But if byte_data == -1 is not true, then it will go on to print “5”.

I think you need to read up on the C language.

Reference Textbooks
My bible for C is of course “Kernigan & Ritchie” http://www.amazon.co.uk/C-Programming-Language-2nd/dp/0131103628/ref=sr_1_3?s=books&ie=UTF8&qid=1292502807&sr=1-3. This is the standard text book. The normal place I point people at who want to move up to C++ is http://www.relisoft.com/book/index.htm and that assumes you know C. Because the Arduino environment normally uses a very small subset of the language, neither are the best place for a beginner to start, nor are many of the other on-line tutorials. I’d still suggest you have both of those, and maybe Bruce Eckel’s “Thinking in C++” http://www.planetpdf.com/developer/article.asp?ContentID=6634 available for reference.

However, there is this: Home | Mechanical Engineering | College of Science and Engineering which does look to be a good starting place for a beginner. It does not go as far as classes and methods that are used here, though. For that, you need the Relisoft course.

Thank you for the reply! It looks like the function is returning -1 to indicate a failure in the buff_get() function. Can anyone see why is that happening? I think it has something to do with Arduino rather than C language.

More likely a recent change to the Arduino IDE. There have been some changes within the last year or so - I’m not fully informed about what exactly was changed nor the implications of that, but I think it would be worth investigating.

I don’t have the necessary hardware to be able to investigate, Dave Berkeley used to appear on the old forums, but I can’t say that I’ve noticed his presence recently.