Pages: [1]   Go Down
Author Topic: Display to LCD from Serial CSV out? Help...  (Read 395 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi...i'm planning to display CSV serial out to LCD....but it only display 0 watt and 0 kwh...no change at all...please do reply....

Here is the code:

Quote
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(9,8,7,6,5,4);

const int lcdLed=13;
const int pulseLed=12;

char incomingByte;

int i;

const int rxPin=2;
const int txPin=3;
const int byteSize=53;

char * range;
char * watts;
char * va;
char * var;
char * pf;
char * volts;
char * amps;

char buffer[53];

bool startDataReceived=false;
bool dataAvailable=false;

SoftwareSerial mySerial(rxPin,txPin);

float wattHour,currentConsumption,currentwattHour;
float wattSecond=0;
float lastConsumption=0;
float KWHInterval=1000;

void setup(){
  pinMode(lcdLed,OUTPUT);
  digitalWrite(lcdLed,HIGH);
  pinMode(pulseLed,OUTPUT);
  digitalWrite(pulseLed,LOW);
  lcd.begin(16,2);
  Serial.begin(9600);
  mySerial.begin(9600);}
 
void extractData(){
  if(mySerial.available()>0){
    incomingByte=mySerial.read();
    if(incomingByte=='I'){
      startDataReceived=true;
      buffer[0]=incomingByte;}}
  if((startDataReceived==true)&&(mySerial.available()>0)){
    for(i=1;i<byteSize;i++){
      while(mySerial.available()==0){}
      buffer=mySerial.read();}
    startDataReceived=false;
    dataAvailable=true;}
  if(dataAvailable==true){
    range=strtok(buffer, ",");
    watts=strtok(NULL, ",");
    va=strtok(NULL, ",");
    var=strtok(NULL, ",");
    pf=strtok(NULL, ",");
    volts=strtok(NULL, ",");
    amps=strtok(NULL, ",");

    Serial.print("Watts: "); Serial.println(watts);
    Serial.print("Va: "); Serial.println(va);
    Serial.print("Var: "); Serial.println(var);
    Serial.print("Pf: "); Serial.println(pf);
    Serial.print("Volts: "); Serial.println(volts);
    Serial.print("Amps: "); Serial.println(amps);
   
    memset(buffer,NULL,byteSize);
    dataAvailable=false;}}
   
void pulseOn(){
  digitalWrite(pulseLed,HIGH);
  delay(5000);
  digitalWrite(pulseLed,LOW);}
 
void displayData(){
 lcd.setCursor(0,0);
 lcd.print(wattSecond,0);
 lcd.print("w ");
 lcd.setCursor(0,1);
 lcd.print(wattHour/1000, 4);
 lcd.print("kwh ");}

 
void loop(){
  extractData();
  wattSecond+=atof(watts);
  currentwattHour=wattSecond/3600;
  wattHour=wattSecond/3600;
  displayData();
  currentConsumption=currentwattHour;
  if((currentConsumption-lastConsumption)>(KWHInterval)){
    lastConsumption+=KWHInterval;
    pulseOn();}}
   
   
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Please post your code inside [ code ] [ /code ] tags not as a quote.

What is your CSV input that you are sending over the serial link?

What is the output that you receive back over the serial link?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 630
Posts: 50009
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Alsogothroughthatcodeandaddspaces.Thatcodeisnearlyimpossibletoreadjammedotgehterlikethat.

There are differing opinions on whether { goes on the line with the statement, or on the next line. There are no differences of opinion on whether the { should have a space in front of it, if it is on the same line as the statement. It should.

There are no differences of opinion regarding the placement of the }. They go one a new line. Period. ONE per line.

Code:
      buffer=mySerial.read();
Time to hit the documentation. The Serial.read() function returns ONE character.

If the problem is that the forum mangled your code, that happened because you failed to post it correctly. Use the code button (#) not the quote button.
Logged

Pages: [1]   Go Up
Jump to: