Go Down

Topic: 'getDecimal' was not declared in this scope (Read 648 times) previous topic - next topic

Benya12

I have some problems with code.

#include <Wire.h>
#include "DFRobot_BNO055.h"
#include <SoftwareSerial.h>

SoftwareSerial mySerial(0, 1); //RX, TX

DFRobot_BNO055 mpu;
//char Byte;

//String charangle[50];
//String ang;
//String ang1;
//String ang2;

//float h = mpu.EulerAngles.x;

void setup()
{
  Serial.begin(9600);

  float X = mpu.EulerAngles.x;
  float Y = mpu.EulerAngles.y;
  float Z = mpu.EulerAngles.z;

  String stringVal = "";   
  String stringVal2 = ""; 
  String stringVal3 = ""; 

  stringVal+=String(int(X))+ "."+String(getDecimal(X)); //combining both whole and decimal part in string with a fullstop between them
  Serial.print("stringVal: ");Serial.println(stringVal);              //display string value
  stringVal2+=String(int(Y))+ "."+String(getDecimal2(Y)); //combining both whole and decimal part in string with a fullstop between them
  Serial.print("stringVal2: ");Serial.println(stringVal2);              //display string value
  stringVal3+=String(int(Z))+ "."+String(getDecimal3(Z)); //combining both whole and decimal part in string with a fullstop between them
  Serial.print("stringVal3: ");Serial.println(stringVal3);             //display string value
 
  char charVal[stringVal.length()+1];                      //initialise character array to store the values
  stringVal.toCharArray(charVal,stringVal.length()+1);     //passing the value of the string to the character array
  char charVal2[stringVal2.length()+1];                      //initialise character array to store the values
  stringVal2.toCharArray(charVal2,stringVal2.length()+1);     //passing the value of the string to the character array
  char charVal3[stringVal3.length()+1];                      //initialise character array to store the values
  stringVal3.toCharArray(charVal3,stringVal3.length()+1);     //passing the value of the string to the character array

  Serial.write("charVal: "); 
  for(uint8_t i=0; i<sizeof(charVal);i++)
  Serial.write(charVal); //display character array
  Serial.write("charVal2: "); 
  for(uint8_t i=0; i<sizeof(charVal2);i++)
  Serial.write(charVal2); //display character array
  Serial.write("charVal3: "); 
  for(uint8_t i=0; i<sizeof(charVal3);i++)
  Serial.write(charVal3); //display character array*
}


/*  while (!mpu.init())
  {
    Serial.println("ERROR! Unable to initialize the chip!");
    delay(50);
  }
  //mpu.setMode(mpu.eNORMAL_POWER_MODE, mpu.eFASTEST_MODE);
  delay(100);
  Serial.println("Read euler angles...");
  }*/

void loop() {
  //ถ้ามีค่าส่งเข้ามา Serial.available จะมีค่ามากกว่า 1
  // if(Serial.available() > 0) { //เมื่อมีข้อมูลส่งมาทาง serial ให้ทำ...
  // ทำการอ่านค่า มาเก็บไว้ในตัวแปร incomingByte
  //while (Serial.available() > 0) {
  //  Byte = Serial.read();

  //i++;
  mpu.readEuler();  /* read euler angle */
  //Serial.write( '#' );
  /*Serial.write("charVal: "); 
  for(uint8_t i=0; i<sizeof(charVal);i++)
  Serial.write(charVal); //display character array
  /*Serial.write("  ");
  Serial.write("charVal2: "); 
  for(uint8_t i=0; i<sizeof(charVal2);i++)
  Serial.write(charVal2); //display character array
  Serial.write("  ");
  Serial.write("charVal3: "); 
  for(uint8_t i=0; i<sizeof(charVal3);i++)
  Serial.write(charVal3); //display character array
  Serial.write( '&' );
  Serial.write( '\n' );
 
  /*Serial.write( '#' );
  Serial.write( flt.xBytes, sizeof( float ) );
  Serial.write("  ");
  Serial.write( flt.yBytes, sizeof( float ) );
  Serial.write("  ");
  Serial.write( flt.zBytes, sizeof( float ) );
  Serial.write( '&' );
  Serial.write( '\n' );*/

 
  /*//Serial.print ("term> ");
    //Serial.print (i);
    //Serial.print("  ");
    Serial.print ('#');
    //Serial.print("yaw: ");
    Serial.write(a);
    //Serial.print(a);
    Serial.print("  ");

    //Serial.print("pitch:");
    Serial.write(b);
    //Serial.print(b);
    Serial.print("  ");

    //Serial.print("roll: ");
    Serial.write(c);
    //Serial.print(c);
    Serial.print('&');
    Serial.println("  ");*/
  /*Serial.write ('#');
    ptr = (long *)(&ypr[2]);
    Serial.write(*ptr);
    Serial.write(*ptr>>8);
    Serial.write(*ptr>>16);
    Serial.write(*ptr>>24);
    ptr = (long *)(&ypr[1]);
    Serial.write(*ptr);
    Serial.write(*ptr>>8);
    Serial.write(*ptr>>16);
    Serial.write(*ptr>>24);
    ptr = (long *)(&ypr[0]);
    Serial.write(*ptr);
    Serial.write(*ptr>>8);
    Serial.write(*ptr>>16);
    Serial.write(*ptr>>24);
    Serial.write('&');*/
  /*Serial.write ('#');
    //Serial.print("yaw: ");
    Serial.write(ang);
    //Serial.print(a);
    Serial.write("  ");

    //Serial.print("pitch:");
    Serial.write(ang1);
    //Serial.print(b);
    Serial.write("  ");

    //Serial.print("roll: ");
    Serial.write(ang2);
    //Serial.print(c);
    Serial.write('&');
    Serial.write("  ");

    Serial.print("\n");
  */

  delay (3000);
}
long getDecimal(float val)
long getDecimal2(float val2)
long getDecimal3(float val3)
{
  int intPart = int(val);
  int intPart2 = int(val2);
  int intPart3 = int(val3);
 
  long decPart = 1000*(val-intPart); //I am multiplying by 1000 assuming that the foat values will have a maximum of 3 decimal places.
                                    //Change to match the number of decimal places you need
  long decPart2 = 1000*(val2-intPart2);
  long decPart3 = 1000*(val3-intPart3);
                             
  if(decPart>0)return(decPart);           //return the decimal part of float number if it is available
  else if(decPart<0)return((-1)*decPart); //if negative, multiply by -1
  else if(decPart=0)return(00);           //return 0 if decimal part of float number is not available
  else if(decPart2>0)return(decPart2);           //return the decimal part of float number if it is available
  else if(decPart2<0)return((-1)*decPart2); //if negative, multiply by -1
  else if(decPart2=0)return(00);           //return 0 if decimal part of float number is not available
  else if(decPart3>0)return(decPart3);           //return the decimal part of float number if it is available
  else if(decPart3<0)return((-1)*decPart3); //if negative, multiply by -1
  else if(decPart3=0)return(00);        //return 0 if decimal part of float number is not available
}

pert

Please use code tags when you post code or warning/error messages. To do this, click the </> button on the forum toolbar, then paste the text you want to be in the code tags. Finally, move the cursor out of the code tags before adding any additional text you don't want to be in the code tags. If your browser doesn't show the posting toolbar, then you can manually add the code tags like this:
[code]// your code is here[/code]

The reason for doing this is that, without code tags, the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier for us to read your code and to copy it to the IDE or editor.

Using code tags and other important information is explained in the "How to use this forum" post. Please read it.


Please don't post screenshots of text. That is very unhelpful. When you encounter an error you'll see a button on the right side of the orange bar in the Arduino IDE "Copy error messages" (or the icon that looks like two pieces of paper at the top right corner of the black console window in the Arduino Web Editor). Click that button.  In a message here, click the code tags button (</>  on the forum toolbar) and then paste the error.

If the text exceeds the forum's 9000 character limit, save it to a .txt  file and post it as an attachment. If you click the "Reply" button here, you will see an "Attachments and other settings" link.

Code: [Select]
SoftwareSerial mySerial(0, 1); //RX, TX
Pins 0 and 1 are hardware serial pins on the Uno. It makes absolutely no sense to use the inferior software serial library on hardware serial pins. Be aware that pins 0 and 1 on the Uno are used for communicating with the computer. Using these pins for any other purpose will interfere with your ability to communicate with Serial Monitor and may also cause you to no longer be able to upload to your Uno. For this reason, it's recommended to avoid using pins 0 and 1. That should be easy since mySerial is not even used anywhere in your code. You really need to take the time to understand what every line in your code does.

Code: [Select]
long getDecimal(float val)
long getDecimal2(float val2)
long getDecimal3(float val3)
{

This is invalid syntax. I have no clue what you're attempting to accomplish here, but it's wrong. Please take some time to learn how to use functions by writing simple test programs before attempting a more advanced program like the one you posted.


Go Up