Error in Void Setup() - Sensor Reading

Hi All,

I am currently working on reading the output from an accelometer and converted the output into degrees. However, I keep running into issues on the code and I do not understand why that is. The point of the program is to read the output from the sensor in bits, which is converted to volts by multiplying the value by (5/1024). The voltage is then subtracted by a bias and divided by the sensor sensitivity to obtain a degree. However, the program keeps giving me 0 angle and 0 bias. The voltage at level should be 2.51.

The code is as follows:
(now with Code Tags - please use 'em! Thanks. Moderator)

//This Accelometer code runs on a 10-Bit system i.e. Ardiuno UNO R3
//Accelometer: ADXL203EB is a 5 V sensor | Manual Tested Avg. Sensitivity: 0.0159 (V/Degrees) 
//Conversion factor from bits to volage is (V sensor/system bits) | (5V/1024 bits)
//Degrees is obtained from the equation: [Degree = (Vout - Vbias)/Sensitivity]

#include <Wire.h> //define wire library

//Define Accelometer Variables
const int AccX = A5; //accelometer X-AXIS connected to A5
const int AccY = A1; //accelometer Y-AXIS connected to A1 
int Accel_X = 0; //sets accelometer X-AXIS read variable @ A5 >> enables reading 
int Accel_Y = 0; //sets accelometer Y-AXIS read variable @ A1 >> enables reading
int Angle = 0; //sets Angle variable to 0 >> enables redefining later on

//Define Sampling Variables for Voltage Bias @ Ground Level
int sampleNum = 500; //500 samples will be conducted to determine a value
int Vb = 0; //Set Voltage Bias variable to start at 0 V
int sampleTime = 10; //Sets sampleTime to 10 (milli-seconds in this case)
unsigned long time; //Sets the variable time to be multiple decimal places, non-negative

//Setup the Serial & Begin one time setup processes
void setup() {
  Serial.begin(115200); //Set Baudrate to 115200, fast refresh
  Serial.println("*Setup Begin*"); //print to indicate start of program

  //Accelometer Setup
  Serial.println(); //Space between text
  Serial.println ("Accelometer Setup Start"); //Indicate the setup of the Accelometer

  //Determine the voltage bias @ current ground level  
  Serial.println();//Space between text
  Serial.println ("Sample Voltage Bias"); //Indicate the Sampling of Vb (Voltage Bias)

  for(int n=0;n<sampleNum;n++){ //Take a total of 500 samples increasing by 1 increment
    Accel_X = analogRead(AccX); //Read values from X-AXIS port i.e. A5 --> Redfine Accel_X to value obtained
    Vb += (int)Accel_X; //Add the readings from variable Accel_X to Vb (Voltage Bias) for each sample conducted
  }
   Serial.println();//Space between text
  Serial.println ("Vb Sampling Completed"); //Indicate end of Vb Sampling

  Vb = Vb/sampleNum; //Redefine the Voltage Bias (bits) according to sampling. (Total Voltage Bias / Sample Number) = Redefined Voltage Bias
  Vb = Vb*0.0048828125; //Convert the Voltage Bias from BITS to VOLTAGE using the factor (5/1024)

  Serial.println();//Space between text
  Serial.print("[Voltage Bias]:"); //Print "Voltage Bias:" on Serial Montior 
  Serial.print(Vb); // Print Vb value next to "Vb:"
  Serial.println();//Space between text
  Serial.println();//Space between text
  Serial.println ("*End of Setup Process*"); //Print this after setup has completed
  Serial.println();//Space between text
  Serial.println("*Accelometer Readings*"); //Print out Accelometer Readings in text

  delay(500); //delay the system for 500 milli-seconds -> allows time for user to read Vb
  }

  //Begin Looping Process for Controller
  void loop() { //Start Loop Sequence
  
    //Accelometer Reading
    if (millis() - time > sampleTime) //Take a smaple every 10 ms --> Total Time - Elasped Time > 10 ms --> Take Sample
    {
      time = millis(); //Time elasped since last sample 
      Accel_X = analogRead(AccX)*(5/1024); //Read values from A0, convert from bits->volts using conversion factor (5/1024), define them to accel_x
      Accel_X = Accel_X - Vb; //Subtract the voltage bias to voltage reading of sensor
      Angle = (Accel_X/0.0159); //Use the degree equation to determine the degree given the corrected voltage output & sensitivity
       Serial.println(); //Space between text
       Serial.print("Angle:"); //Print "Angle:" in the Serial Monitor
      Serial.print(Angle); //Print the Angle variable value next to "Angle:" in Serial Monitor
    }

    delay (500); //Delay the system 500 ms before the next sample reading

    }

Vb is an int. How can it be 2.51?

Yes, looks like it should be a float?

 Vb = Vb/sampleNum; //Redefine the Voltage Bias (bits) according to sampling. (Total Voltage Bias / Sample Number) = Redefined Voltage Bias
  Vb = Vb*0.0048828125; //Convert the Voltage Bias from BITS to VOLTAGE using the factor (5/1024)

Hi,

I am relatively new to Arduino programming, but when I place a float in front of all of the Vb's in the program and delete int vb = 0 as the defined variable. I am left with a series of errors including in the portion where "+=" is summing up the total of the sampling in the for loop.

As listed the errors are:

Accelometer.ino: In function 'void setup()': Accelometer:40: error: expected initializer before '+=' token Accelometer:46: error: redeclaration of 'float Vb' Accelometer:45: error: 'float Vb' previously declared here Accelometer.ino: In function 'void loop()': Accelometer:68: error: 'Vb' was not

And as listed, the code looks like. . . ?

You only need to make this line have float:

int float Vb = 0; //Set Voltage Bias variable to start at 0 V

So I set the float variable as you suggested, however the errors in the code as still evident and the program will not run.

Code:
(Code tags please!)

//LAST REVISED: 03/29/2015

//This Accelometer code runs on a 10-Bit system i.e. Ardiuno UNO R3
//Accelometer: ADXL203EB is a 5 V sensor | Manual Tested Avg. Sensitivity: 0.0159 (V/Degrees) 
//Conversion factor from bits to volage is (V sensor/system bits) | (5V/1024 bits)
//Degrees is obtained from the equation: [Degree = (Vout - Vbias)/Sensitivity]

#include <Wire.h> //define wire library

//Define Accelometer Variables
const int xpin = A5; //accelometer X-AXIS connected to A5
const int ypin = A1; //accelometer Y-AXIS connected to A1 
int Accel_X; //sets accelometer X-AXIS read variable @ A5 >> enables reading 
int Accel_Y; //sets accelometer Y-AXIS read variable @ A1 >> enables reading
int Angle; //sets Angle variable to 0 >> enables redefining later on
int prev_angle = 0;

//Define Sampling Variables for Voltage Bias @ Ground Level
int sampleNum = 500; //500 samples will be conducted to determine a value
float vb = 0; //Set Voltage Bias variable to start at 0 V
int sampleTime = 10; //Sets sampleTime to 10 (milli-seconds in this case)
unsigned long time; //Sets the variable time to be multiple decimal places, non-negative

//Setup the Serial & Begin one time setup processes
void setup() {
  Serial.begin(115200); //Set Baudrate to 115200, fast refresh
  Serial.println("*Setup Begin*"); //print to indicate start of program

  //Accelometer Setup
  Serial.println(); //Space between text
  Serial.println ("Accelometer Setup Start"); //Indicate the setup of the Accelometer

  //Determine the voltage bias @ current ground level  
  Serial.println();//Space between text
  Serial.println ("Sample Voltage Bias"); //Indicate the Sampling of Vb (Voltage Bias)

  for(int n=0;n<sampleNum;n++){ //Take a total of 500 samples increasing by 1 increment
    Accel_X = analogRead(xpin); //Read values from X-AXIS port i.e. A5 --> Redfine Accel_X to value obtained
    Vb += Accel_X; //Add the readings from variable Accel_X to Vb (Voltage Bias) for each sample conducted
  }
   Serial.println();//Space between text
  Serial.println ("Vb Sampling Completed"); //Indicate end of Vb Sampling

   Vb = Vb/sampleNum; //Redefine the Voltage Bias (bits) according to sampling. (Total Voltage Bias / Sample Number) = Redefined Voltage Bias
   Vb = Vb*0.0048828125; //Convert the Voltage Bias from BITS to VOLTAGE using the factor (5/1024)

  Serial.println();//Space between text
  Serial.print("[Voltage Bias]:"); //Print "Voltage Bias:" on Serial Montior 
  Serial.print(Vb); // Print Vb value next to "Vb:"
  Serial.println();//Space between text
  Serial.println();//Space between text
  Serial.println ("*End of Setup Process*"); //Print this after setup has completed
  Serial.println();//Space between text
  Serial.println("*Accelometer Readings*"); //Print out Accelometer Readings in text

  delay(500); //delay the system for 500 milli-seconds -> allows time for user to read Vb
  }

  //Begin Looping Process for Controller
  void loop() { //Start Loop Sequence
  
    //Accelometer Reading
    if (millis() - time > sampleTime) //Take a smaple every 10 ms --> Total Time - Elasped Time > 10 ms --> Take Sample
    {
      time = millis(); //Time elasped since last sample 
      Accel_X = analogRead(xpin)*0.0048828125; //Read values from A0, convert from bits->volts using conversion factor (5/1024), define them to accel_x
      Accel_X = Accel_X - Vb; //Subtract the voltage bias to voltage reading of sensor
      Angle = (Accel_X/0.0159); //Use the degree equation to determine the degree given the corrected voltage output & sensitivity
       Serial.println(); //Space between text
       Serial.print("Angle:"); //Print "Angle:" in the Serial Monitor
      Serial.print(Angle); //Print the Angle variable value next to "Angle:" in Serial Monitor
      prev_angle = Angle; //Remember the current angle for the next looping
    }

    delay (500); //Delay the system 500 ms before the next sample reading

    }

Errors:

Accelometer.ino: In function ‘void setup()’:
Accelometer:40: error: ‘Vb’ was not declared in this scope
Accelometer:45: error: ‘Vb’ was not declared in this scope
Accelometer.ino: In function ‘void loop()’:
Accelometer:68: error: ‘Vb’ was not declared in this scope

Vb != vb

Please use code tags

float vb != Vb Case sensitive.

Ah! I see what I did wrong now! Thank you so much! I will definitely try this code out again to see if my sensor will be reading out in the correct voltage and ultimately degrees.

Hi Guys,

I managed to get the accelometer reading, however there appears to be a deviate within the readings I receive. There is an oscillation between roughly .026 to -.015 in degrees. Is there something wrong with my code or is there any way that these readings can be smoothed? The accelometer is at 0 degrees and steadily increases in error as the angle gets larger. The code is noted below:

//LAST REVISED: 03/29/2015

//This Accelometer code runs on a 10-Bit system i.e. Ardiuno UNO R3
//Accelometer: ADXL203EB is a 5 V sensor | Manual Tested Avg. Sensitivity: 0.0159 (V/Degrees) 
//Conversion factor from bits to volage is (V sensor/system bits) | (5V/1024 bits)
//Degrees is obtained from the equation: [Degree = (Vout - Vbias)/Sensitivity]

#include <Wire.h> //define wire library

//Define Accelometer Variables
const int xpin = A5; //accelometer X-AXIS connected to A5
const int ypin = A1; //accelometer Y-AXIS connected to A1 
float Accel_X; //sets accelometer X-AXIS read variable @ A5 >> enables reading 
float Accel_Y; //sets accelometer Y-AXIS read variable @ A1 >> enables reading
float Angle; //sets Angle variable to 0 >> enables redefining later on
float sum_angle = 0; //set prev_angle variable to 0 >> enables redefining later on
float final_angle =0; //set final_angle variable to 0 >> enables redefining later on

//Define Sampling Variables for Voltage Bias @ Ground Level
int sampleNum = 500; //500 samples will be conducted to determine a value
int sampleNumb = 2; //2 samples will be conducted to determine a value
float Vb = 0; //Set Voltage Bias variable to start at 0 V
int sampleTime = 10; //Sets sampleTime to 10 (milli-seconds in this case)
unsigned long time; //Sets the variable time to be multiple decimal places, non-negative

//Setup the Serial & Begin one time setup processes
void setup() {
  Serial.begin(115200); //Set Baudrate to 115200, fast refresh
  Serial.println("*Setup Begin*"); //print to indicate start of program

  //Accelometer Setup
  Serial.println(); //Space between text
  Serial.println ("Accelometer Setup Start"); //Indicate the setup of the Accelometer

  //Determine the voltage bias @ current ground level  
  Serial.println();//Space between text
  Serial.println ("Sample Voltage Bias"); //Indicate the Sampling of Vb (Voltage Bias)

  for(int n=0;n<sampleNum;n++){ //Take a total of 500 samples increasing by 1 increment
    Accel_X = analogRead(xpin); //Read values from X-AXIS port i.e. A5 --> Redfine Accel_X to value obtained
    Vb += Accel_X; //Add the readings from variable Accel_X to Vb (Voltage Bias) for each sample conducted
  }
   Serial.println();//Space between text
  Serial.println ("Vb Sampling Completed"); //Indicate end of Vb Sampling

   Vb = Vb/sampleNum; //Redefine the Voltage Bias (bits) according to sampling. (Total Voltage Bias / Sample Number) = Redefined Voltage Bias
   Vb = Vb*0.0048828125; //Convert the Voltage Bias from BITS to VOLTAGE using the factor (5/1024)

  Serial.println();//Space between text
  Serial.print("[Voltage Bias]:"); //Print "Voltage Bias:" on Serial Montior 
  Serial.print(Vb); // Print Vb value next to "Vb:"
  Serial.println();//Space between text
  Serial.println();//Space between text
  Serial.println ("*End of Setup Process*"); //Print this after setup has completed
  Serial.println();//Space between text
  Serial.println("*Accelometer Readings*"); //Print out Accelometer Readings in text

  delay(500); //delay the system for 500 milli-seconds -> allows time for user to read Vb
  }

  //Begin Looping Process for Controller
  void loop() { //Start Loop Sequence
  
    //Accelometer Reading
  for(int n=0;n<sampleNumb;n++){ //Take a total of 500 samples increasing by 1 increment
    if (millis() - time > sampleTime) //Take a smaple every 10 ms --> Total Time - Elasped Time > 10 ms --> Take Sample
      { 
  time = millis(); //Time elasped since last sample 
      Accel_X = analogRead(xpin); //Read values from A0, convert from bits->volts using conversion factor (5/1024), define them to accel_x
      Accel_X = Accel_X*0.0048828125; //Convert the readings from accelometer from bits into voltage
      Accel_X = Accel_X - Vb; //Subtract the voltage bias to voltage reading of sensor
      Angle = (Accel_X/0.0159); //Use the degree equation to determine the degree given the corrected voltage output & sensitivity
      sum_angle += Angle; //Remember the current angle for the next looping
      }
  }
  
      final_angle = sum_angle / sampleNumb; //Average the angles from the 2 samples taken in the for-loop
      Serial.println(); //Space between text
      Serial.print("Angle:"); //Print "Angle:" in the Serial Monitor
      Serial.print(final_angle); //Print the Angle variable value next to "Angle:" in Serial Monitor
 
     sum_angle = 0; //Redfine sum_angle back to 0 for next iteration of loop
 
    delay (100); //Delay the system 100 ms before the next sample reading

    }