[solved]Tiny bug that I can't see is terrific: math is not correct


almost got right, when increasing pwm, output value go negative.  normally, when goes negative, , math correct, because value overflow, don't think here.  realma not huge number. must math. can find tiny bug me plz? thank you


here serial output:
code: [select]
shunt:1049
-------------
pwm:180
-------------
pwm:30
-------------
realma:314
-------------
shunttotal:9121
------------------------------------


shunt:1044
-------------
pwm:155
-------------
pwm:40
-------------
realma:-237
-------------
shunttotal:8884
------------------------------------


shunt:0
-------------
pwm:155
-------------
pwm:40
-------------
realma:-235
-------------
shunttotal:8649
------------------------------------



and here sketch:
code: [select]
#include <wire.h>
#include "clickbutton.h"
#include <liquidcrystal_i2c.h>

liquidcrystal_i2c lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, positive);  // set lcd i2c address/ pins


clickbutton ba(9, low, clickbtn_pullup);
clickbutton bb(8, low, clickbtn_pullup);

int sec=0;
int min=0;
int switch=1;
int pwm=255;
int pwm=0;
int shunt=0;
int mah=0;

int realma=0;

unsigned long shunttotal=0;
unsigned long nowtimer=0;
unsigned long pretimer=0;

unsigned long nptimer=0;
unsigned long pptimer=0;
//=====================================================================================

void setup()
{  
 serial.begin(9600);
 lcd.begin(16,2);               // initialize lcd  
 ba.multiclicktime = 50;
 bb.multiclicktime = 50;    

}

//=====================
void loop()
{
 ba.update();
 bb.update();  

 pwm=constrain(pwm, 0, 255);  // limited pwm 0 255
 pwm=map(pwm, 0,255, 100,0); // map pwm pwm percentage reading. it's connected pnp transisotr, 255=0;

 nowtimer=millis();
 if(nowtimer-pretimer>=1000) {
   sec++;
   shunttotal+=realma;

   serial.print("shunt:");
   serial.println(shunt);
   serial.println("-------------");    
   
   serial.print("pwm:");
   serial.println(pwm);      
   serial.println("-------------");
   
   serial.print("pwm:");
   serial.println(pwm);  
   serial.println("-------------");
   
   serial.print("realma:");
   serial.println(realma);  
   serial.println("-------------");
   
    serial.print("shunttotal:");
   serial.println(shunttotal);
   serial.println("------------------------------------");    
     

   pretimer=nowtimer;    
 }

 if(sec==60){
   min++;
   sec=0;
 }

 char linea[17];
 sprintf(linea, "%01dcc-markii %02d:%02d", switch, min, sec);
 lcd.setcursor (0,0);
 lcd.print (linea);  
 nptimer=millis();


 if(nptimer-pptimer>=200) {
   char lineb[17];
   sprintf(lineb, "%03d %04dma %04dc", pwm, realma, mah);
   lcd.setcursor (0,1);
   lcd.print (lineb);
   
   pptimer=nptimer;
 }



 //================================  

 int shuntvolt= analogread(a0);   // read shunt resistor voltage
 shunt = shuntvolt*5.0/1024.0*1000.0; // math shunt current

 if(shunt>10){                         // if shunt current more 10ma
   realma=shunt*pwm/100.0;             // realma equals (the reading * pwm duty cycle )
 }

 mah=shunttotal/3600;    

 if(switch==1) analogwrite(3, pwm);
 else analogwrite(3, 255);


 if(ba.clicks==1) pwm-=25;
 if(bb.clicks==1) pwm+=25;  

 if(ba.clicks==-1) switch=!switch;
 if(bb.clicks==-1) ;

}


[font=courier new]    realma=shunt*pwm/100.0;
    realma=(1044*40)/100.0;
    realma=(41760)/100.0;[/font]

oops.  exceeded 32767.


Arduino Forum > Using Arduino > Programming Questions > [solved]Tiny bug that I can't see is terrific: math is not correct


arduino

Comments

Popular posts from this blog

Connecting Raspberry Pi 2 to P10(1R)-V706 LED Dot Matrix - Raspberry Pi Forums

TypeError: <unknown> is not a numpy array - Raspberry Pi Forums

datso and removing imagetitle - Joomla! Forum - community, help and support