Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Electricity meter using function on Arduino

Status
Not open for further replies.

adwnis123

Full Member level 4
Joined
Jun 19, 2014
Messages
214
Helped
0
Reputation
0
Reaction score
1
Trophy points
18
Activity points
1,737
I use this code

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

of this project:

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

and I change it by adding

Code:
 String  incomingByte;   // for incoming serial data


and changing the setup and loop code as below:


Code:
void setup() 
{
  buff_init(& bits);
  buff_init(& bytes);

  pinMode(ledPin, OUTPUT);
  pinMode(intPin, INPUT);

  attachInterrupt(0, on_change, RISING);

  Serial.begin(38400);
}

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?
 
Last edited by a moderator:

Hi,

Your loop migt be too fast...and you don't wait for the transmissiin of "PRINT" is complete.
So it might be possible that the loop runs three times for example and the loop sees the following incoming data:
* "P"
* "RI"
* "NT"
But never the complete instruction.
Therefore you need to define a protocol.
I recommend to add a byte to detect the end of the instruction. Maybe [CR]
Then the complete instruction is "PRINT[CR]"
The input needs to be like this: incomingString = incomingString + Seriea.readString
Then run the loop as long as the string includes the [CR]..
Then process the string.

Klaus
 

Hi,

Your loop migt be too fast...and you don't wait for the transmissiin of "PRINT" is complete.
So it might be possible that the loop runs three times for example and the loop sees the following incoming data:
* "P"
* "RI"
* "NT"
But never the complete instruction.
Therefore you need to define a protocol.
I recommend to add a byte to detect the end of the instruction. Maybe [CR]
Then the complete instruction is "PRINT[CR]"
The input needs to be like this: incomingString = incomingString + Seriea.readString
Then run the loop as long as the string includes the [CR]..
Then process the string.

Klaus

Something like this?

Code:
void loop()
{   
    if (Serial.available() > 0)  
    {
        incomingByte = incomingByte + Serial.readString();
        if (incomingByte == "PRINT[CR]")
        { 
            Serial.println(incomingByte); // for testing
            int byte_data = decode_bit_stream();
            if (byte_data == -1)
               return;
            meter.on_data(byte_data);
        }
    }
}
 

Hi,

this is a simple solution, but it will hang on every single error.
And for sure it needs to clear "incomingBuffer"

Assume folowing buffer contents:
* "[garbage]PRINT[CR]" --> your "IF" won´t detect is as true, and it will never becoume true anymore. It hangs.

Independent of error: A LOW instead of a HIGH, a HIGH instead of a LOW, missing character or an added character... All will make that the application hangs.


Klaus
 

Hi,

The problem is the "=" operator.
There are other operators like: stringA contains stringB.
And i recomment to include a timeout: If for a dedicated time is no data transfer, then clear the incomingBuffer.

Klaus
 

I made some modifications to the Serial monitor and also I changed the code to

Code:
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.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top