I need the code to convert an analog voltage between 0 and 3.3 volts to a g value of -3 to 3.
Anyone able to help out?!
If 0-3.3volts represents -3g to 3g then you have 6 g's represented by 3.3v so each g is .55 volts. Since I assume 1.65v is zero g. You have g = (V / .55) - 3, but if you are reading this 0-3.3 from an Analog Arduino port that ranges from 0-1024 you can skip the voltage conversion. The 0-1024 range also represents -3 to 3 g's. So if a 512 analog reading is 0 g's then g = (Analog Reading * 6 / 1024) - 3.
To check: (0 * 6 / 1024) - 3 = -3 , (512 * 6 / 1024) - 3 = 0 , (1024 * 6 / 1024) - 3 = 3.
Is this what you were looking for?
thank you for the reply!
very helpful and easy to understand.
i'm curious..is the range 0-1024 or 0-1023?
What do you think of my code converting x axis voltage to g, for the accelerometer?
//3.3v connected to AREF
int xpin = A0;
int sv1 = 0;
int ov1 = 0;
float xg;
int led1 = 12;
void setup() {
Serial.begin(9600);
}
void loop() {
analogReference(EXTERNAL);
//the analog in value:
sv1 = analogRead(xpin);
// mapping it to the range of the analog out:
ov1 = map(sv1, 0, 1023, 0, 255);
delay(2);
Serial.print("An-to-Dig-Conversion range = " );
Serial.print(sv1);
Serial.print("PMW analog output range = ");
Serial.println(ov1);
delay(3000);
//for acceleration
xg = (analogRead(0)*6 / 1024)-3;
Serial.println("MEASURING ACCELERATION... AXIS X");
Serial.println(xg);
delay(1000);
if (xg==12 || xg==120); {
digitalWrite( led1, HIGH);
}
Is the if statement correctly written seeing as I want the led to turn on when the lowest or highest value of g is reached
Thank you!
float xg;
if (xg==12 || xg==120); {
One must be very careful using equality with floats. 12.0001 is not equal to 12.0000.
These pages explain better than I can:
http://www.cygnus-software.com/papers/comparingfloats/Comparing%20floating%20point%20numbers.htm
if (xg==12 || xg==120);
Noting the earlier comments about testing floats for equality, but also, Oops.
Thanks guys!
Okay, aside the float, is the code okay? I'm using only the x-axis of my accelerometer. I've got the reading for g as -3, from the serial monitor.
is the code okay?
See reply #4
Please remember to use code tags when posting code
I'm only using the x-axis for now. the serial monitor reads only -3
//3.3v connected to AREF
int xpin = A0;
int sv1 = 0;
int ov1 = 0;
int led1 = 12;
void setup() {
Serial.begin(9600);
}
void loop() {
analogReference(EXTERNAL);
//the analog in value:
sv1 = analogRead(xpin);
// mapping it to the range of the analog out:
ov1 = map(sv1, 0, 1023, 0, 255);
delay(2);
Serial.print("An-to-Dig-Conversion range = " );
Serial.print(sv1);
Serial.print("PMW analog output range = ");
Serial.println(ov1);
delay(3000);
//for acceleration
xg = (ov1 * 6 / 1024) - 3;
Serial.println("MEASURING ACCELERATION... AXIS X");
Serial.println(xg);
What do your serial prints produce?
The code cannot possibly compile, so no, it isn't correct
what do you suggest I do?
I need help regarding this and help in form of written code please. That would be easier to understand for the than explanations without code.
thanks!
what do you suggest I do?
Post your code.
All of it.
That is all my code!
This is what I plan to do for my project;
convert the accelerometer volts to g.
then use the highest and lowest values of g to trigger on an led.
I'm relatively new to coding so I know i'm making alot of mistakes with my code.
Please look at your reply #7.
You state
I'm only using the x-axis for now. the serial monitor reads only -3
The code in that post cannot compile, so cannot print anything to the serial monitor.
So, where is the code that prints only -3?
The code that prints -3, here it is!
xg = (ov1 * 6 / 1024) - 3;
Serial.println("MEASURING ACCELERATION... AXIS X");
Serial.println(xg);
Is the mapping right?
I dont deleting everything and starting afresh if it is all wrong. I just want to get the LED turn on when the accelerometer g values are either extremes
Remember BODMAS and integer arithmetic.
(The code in reply #7 is still incomplete)
The code is compiling and uploading just fine
//3.3v connected to AREF
int xpin = A0;
int sv1 = 0;
int ov1 = 0;
float xg;
int led1 = 12;
void setup() {
Serial.begin(9600);
}
void loop() {
analogReference(EXTERNAL);
//the analog in value:
sv1 = analogRead(xpin);
// mapping it to the range of the analog out:
ov1 = map(sv1, 0, 1023, 0, 255);
delay(2);
Serial.print("An-to-Dig-Conversion range = " );
Serial.print(sv1);
Serial.print("PMW analog output range = ");
Serial.println(ov1);
delay(3000);
//for acceleration
xg = (ov1 * 6 / 1024) - 3;
// from the calculation
//(0 * 6 / 1024) - 3 = -3 ,
//(512 * 6 / 1024) - 3 = 0 ,
//(1024 * 6 / 1024) - 3 = 3.
Serial.println("MEASURING ACCELERATION... AXIS X");
Serial.println(xg);
delay(1000);
if (xg == 0 || xg == 255) {
digitalWrite( led1, HIGH);
}
else if (xg != 0 || xg != 255)
{
digitalWrite( led1, LOW);
}
}
QuinNYU:
The code is compiling and uploading just fine
But still has the same problem with basic arithmetic and operator precedence?
That's why I came on here for help..
How do i resolve these things?!
Floating point arithmetic?