Go Down

Topic: Error in Void Setup() - Sensor Reading (Read 73 times) previous topic - next topic

Mar 30, 2015, 08:07 pm Last Edit: Mar 30, 2015, 08:22 pm by CrossRoads
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)
Code: [Select]

//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

    }

KeithRB


CrossRoads

Yes, looks like it should be a float?
Code: [Select]

 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)
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#3
Mar 30, 2015, 08:42 pm Last Edit: Mar 30, 2015, 08:43 pm by SegwayRobo
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

AWOL

And as listed, the code looks like. . . ?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

CrossRoads

You only need to make this line have float:

int float Vb = 0; //Set Voltage Bias variable to start at 0 V
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#6
Mar 30, 2015, 08:58 pm Last Edit: Mar 30, 2015, 08:59 pm by CrossRoads
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!)
Code: [Select]

//AUTHOR: ALLAN R. WONG - UML 2014-2018
//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

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

CrossRoads

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

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.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy