Go Down

Topic: substring error (Read 4612 times) previous topic - next topic

backbone

Jun 29, 2013, 12:30 pm Last Edit: Jun 29, 2013, 05:22 pm by backbone Reason: 1
Ik heb al gezocht met google op de zelfde error maar kom er niet uit wat ze bedoelen met deze foutmelding (zie screenshot).
Een duwtje in de juiste richting zou fijn zijn.

Een gedeelte van de code staat hier onder.

Code: [Select]

  double volt = Result * vref /16/6912000; //data coming from this line is .....      "0.123456" for example
  long microvolt = (volt*1000000); // microvolts are easier to break down in blocks of 3 then floats so we multiply with 1000000.
  //long microvolt = (volt*1000); // microvolts are easier to break down in blocks of 3 then floats so we multiply with 1000000.
  //split the microvolt value in blocks of three digits so it can be parsed to the serial output by Serial.print as integer value as it is lower as 1024.
  //sample value of volt  =  "123456" so we brake it down in 123 and 456 <> volt.
  //In our case it will never go below 0 or above 999999
  float (load.substring (0,2)); // we need the three most right side digits
  float (load.substring (3,5)); //we need the three most left side digits
  //at the PC side VB6 should detect the "letter - comma - value" and parse it in a textbox
  // how things work with Processing I have no idea yet :-(.
 
  float load = ((loadB - loadA)/(valueB - valueA)) * ( microvolt - valueA) + loadA; //the final value of the real load on the sensor

  Serial.print ("A,");
  Serial.println (volt,6); //raw sensor value in volts
  Serial.print ("B,");
  Serial.println (microvoltB);
  Serial.print ("C,");
  Serial.println (extradata1);
  Serial.print ("D,");
  Serial.println (extradata2);
  Serial.print ("E,");
  Serial.println (microvolt); //raw sensor value in microvolts
  Serial.print ("F,");
  Serial.println (load,0); // value in grams
 
Never to old to learn and I learn every day

Jantje

volgens mij heb je geen substring fout maar heb je een fout gemaakt in de samenstelling van verschillende commando's in een.
wat je schrijft is
Code: [Select]
float (load.substring (0,2));
en een beetje later
Code: [Select]
float load = ((loadB - loadA)/(valueB - valueA)) * ( microvolt - valueA) + loadA; //the final value of the real load on the sensor

je gebruikt load dus voor je het definieerd als een float.
float heeft geen methode substring dus dat slaat (volgens de c++ compiler) nergens op.

de code die je wilt is
Code: [Select]
float float1 = (load.substring (0,2));
Maar je kan een string niet zomaar omvormen in een float. ook dat slaat nergens op volgens de c++ compiler.

In het kort: volgens mij zijn je problemen veroorzaakt door iets anders dan substring.
Met vriendelijke groet
Jantje

Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

backbone

Hallo Jantje,

Sorry het attachment hoorde niet bij dit probleem. :-(
heb ik verwijderd.
Maar het gaat wel om die twee regels met substring.
Als ik ze uit zet compiled hij goed.
request of member 'substring' in 'load', which is in non class type float is de fout melding.

Nu maar de hele code er bij geplakt.
Load wordt wel gedeclareerd als float.
Maar zoals je zei kan ik dus geen string als float declareren.
Hoe werk jo zo iets heen?
Ik moet de float breken en in stukken hakken om het over te fluiten naar het VB gedeelte wat al klaar is.

Paco

Code: [Select]

//TM7709 24bit ADC module arduino sketch
//coldtears electronics
//code reworked to read in grams

//default connection arduino UNO
//DRDY = pin 11 >MOSI
//ADIO = pin 12 >MISO
//SCLK = pin 13 >SCK

//default connection arduino MEGA
//DRDY = pin 21 >MOSI
//ADIO = pin 22 >MISO
//SCLK = pin 28 >SCK


#define TM7710_DRDY        3         
#define TM7710_ADIO        4         
#define TM7710_ADIO_OUT()  DDRB|=1<<4     
#define TM7710_ADIO_IN()   DDRB&=~(1<<4) 
#define Set_TM7710_SCLK()  PORTB|=1<<5
#define Set_TM7710_ADIO()  PORTB|=1<<4
#define Clr_TM7710_SCLK()  PORTB&=~(1<<5)
#define Clr_TM7710_ADIO()  PORTB&=~(1<<4)

unsigned char x[3];
long Result;
float vref=4.89;
double microvolt = 0;
long microvoltA = 123;
long microvoltB = 456;
int extradata1 = 777;
int extradata2 = 888;
float loadA = 0; //this is the first weight (we use no load at the sensor) our sensor already has as a small plate on it we use a preload.
float loadB = 1229; //this is the second calibrated weight in grams
float valueA = 804; //value of the serialmonitor reading with the first known weigth at the sensor (we use no load at the sensor).
float valueB = 22581; //value of the serialmonitor reading with the second known load at the sensor (we 1590 grams at the sensor).
float load;
long finalValue;

void setup()
{
DDRB|=1<<5 ;
delay(1000);
Serial.begin(57600);// set baudrate for PC or serialmonitor the same!
TM7710_Init();
}
   
void loop()

   while((PINB&(1<<TM7710_DRDY))==(1<<TM7710_DRDY));
   TM7710_start();
   TM7710_write(0x7F);       
   TM7710_ADIO_IN();         
   for(unsigned char j=0;j<3;j++)
   {
   x[j]=TM7710_read();
   }
   
   TM7710_ADIO_OUT();         
   TM7710_stop();
   Result=x[0];
   Result = Result * 256;
   Result = Result + x[1];
   Result = Result * 256;
   Result = Result + x[2];
   Result = Result - 6912000;
   
   double volt = Result * vref /16/6912000; //data coming from this line is .....      "0.123456" for example
   long microvolt = (volt*1000000); // microvolts are easier to break down in blocks of 3 then floats so we multiply with 1000000.
   //long microvolt = (volt*1000); // microvolts are easier to break down in blocks of 3 then floats so we multiply with 1000000.
   //split the microvolt value in blocks of three digits so it can be parsed to the serial output by Serial.print as integer value as it is lower as 1024.
   //sample value of volt  =  "123456" so we brake it down in 123 and 456 <> volt.
   //In our case it will never go below 0 or above 999999
   float (load.substring (0,2)); // we need the three most right side digits
   float (load.substring (3,5)); //we need the three most left side digits
   //at the PC side VB6 should detect the "letter - comma - value" and parse it in a textbox
   // how things work with Processing I have no idea yet :-(.
   
   float load = ((loadB - loadA)/(valueB - valueA)) * ( microvolt - valueA) + loadA; //the final value of the real load on the sensor

   Serial.print ("A,");
   Serial.println (volt,6); //raw sensor value in volts
   Serial.print ("B,");
   Serial.println (microvoltB);
   Serial.print ("C,");
   Serial.println (extradata1);
   Serial.print ("D,");
   Serial.println (extradata2);
   Serial.print ("E,");
   Serial.println (microvolt); //raw sensor value in microvolts
   Serial.print ("F,");
   Serial.println (load,0); // value in grams
   
}

void TM7710_Init()
{
   TM7710_ADIO_OUT();
   delay(100);
   TM7710_stop();
   TM7710_start();
   TM7710_write(0xBF);       
   TM7710_write(0x20);   //Gain=128
   //TM7710_write(0x00); //Gain=16
   TM7710_stop();     
}

void TM7710_start(void)   
{
    Clr_TM7710_ADIO();
    delayMicroseconds(1);
    Clr_TM7710_SCLK();
    delayMicroseconds(1);
}

void TM7710_stop(void)   
{
    Clr_TM7710_ADIO();
    delayMicroseconds(1);
    Set_TM7710_SCLK();
    delayMicroseconds(1);
    Set_TM7710_ADIO();
    delayMicroseconds(1);
}

void TM7710_write(unsigned char dd)
{
    unsigned char i;

    for(i=8;i>0;i--)
    {
        if(dd&0x80)
            Set_TM7710_ADIO();   
        else
            Clr_TM7710_ADIO();   
       
        delayMicroseconds(1);
        Set_TM7710_SCLK();       
        delayMicroseconds(1);
        Clr_TM7710_SCLK();     
        dd<<=1;                 
    }
}

unsigned char TM7710_read(void)
{
    unsigned char data=0,i;

    for(i=0;i<8;i++)
    {
        Set_TM7710_SCLK();                   
       
        data=data<<1;                                 
        if((PINB&(1<<TM7710_ADIO))==(1<<TM7710_ADIO)) 
        {
          data=data+1;
        }
        delayMicroseconds(1);
        Clr_TM7710_SCLK();
         delayMicroseconds(1);
    }
    return data;
}

void printFloat(float value, int places) {
// this is used to cast digits
int digit;
float tens = 0.1;
int tenscount = 0;
int i;
float tempfloat = value;

// if value is negative, set tempfloat to the abs value
// calculate rounding term d:   0.5/pow(10,places)
float d = 0.5;
if (value < 0)
   d *= -1.0;
// divide by ten for each decimal place
for (i = 0; i < places; i++)
   d/= 10.0;
// this small addition, combined with truncation will round out

tempfloat +=  d;

if (value < 0)
   tempfloat *= -1.0;
while ((tens * 10.0) <= tempfloat)
{
   tens *= 10.0;
   tenscount += 1;
}

// write out the negative if needed
if (value < 0)
   //Serial.print('-');

if (tenscount == 0)
   //Serial.print(0, DEC);

for (i=0; i< tenscount; i++)
{
   digit = (int) (tempfloat/tens);
   //Serial.print(digit, DEC);
   tempfloat = tempfloat - ((float)digit * tens);
   tens /= 10.0;
}

// if no places after decimal, stop now and return
if (places <= 0)
   return;

// otherwise, write the point and continue on
//Serial.print('.');

for (i = 0; i < places; i++)
{
   tempfloat *= 10.0;
   digit = (int) tempfloat;
   //Serial.print(digit,DEC);
   // once written, subtract off that digit
   tempfloat = tempfloat - (float) digit;
}

}


Never to old to learn and I learn every day

nicoverduin

Als jij de float wil omzetten de text versie in bytes wil doorsturen kun je toch gewoon dtostrf gebruiken

Code: [Select]

char str[10];
float var = 0.123456;
dtostrf(var, 8, 6, str);

En je hebt een string met de tekst "0.123456" en een '\0' er achteraan.
Die string kan je daarna gewoon kopiƫren in stukken met strncpy(source, destination, number of bytes)
Met vriendelijke groet / kindest regards
Nico Verduin
www.verelec.nl

backbone

Thanks, Nico

Een en ander veranderd aan zowel Arduino maar ook aan PC kant.
Alles werkt nu.
Never to old to learn and I learn every day

Go Up