We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello,
Recently, I bought my first Arduino and ADXL335 and was able to make a basic program that calibrates the accelerometer.
int sample = 1000;
boolean isCalibrated = false;
const int xPin = A0;
const int yPin = A1;
const int zPin = A2;
int dim = 3;
float average[3];
float total[3];
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
analogReference(EXTERNAL);
}
void loop() {
if (!isCalibrated) {
for (int i = 0; i < dim; i++){
for (int j = 0; j < sample; j++){
if (i == 0)
total[i] += analogRead(xPin);
if (i == 1)
total[i] += analogRead(yPin);
if (i == 2)
total[i] += analogRead(zPin);
}
}
for (int i = 0; i < dim; i++){
average[i] = total[i]/sample;
}
isCalibrated = true;
}
Serial.print(analogRead(xPin) - average[0]);
Serial.print("\t");
Serial.print(analogRead(yPin) - average[1]);
Serial.print("\t");
Serial.print(analogRead(zPin) - average[2]);
Serial.println();
delay(10);
}
I want to translate this to Processing to some graphics stuff. Most of the stuff seems pretty easy to translate, but the one thing that has me really scared is making sure I get the equivalent of analogReference(EXTERNAL); in the sketch. On the wiring section of the ADXL, it states It seems if I don't include this, I'll short my Arduino. After searching I can't find any equivalency of it. Does anyone know of the equivalency of what I should do?
Thanks
Answers
You are printing data to Serial port. Processing can read that. Read the Processing Serial tutorial for more info.
I'm a bit confused on what you mean by that. Are you saying since I'm reading data from the serial port I don't have to work about setting the analog reference? I'm assuming you want me to read about the section and example on the analog output.
@Dinoswarleafs
Just to clarify, you want to write a processing program that interact with your Arduino code, right?
That Arduino code should run in your Arduino. In Processing, you should open a serial connection and listen to the port and process bytes as they arrive. You should check previous Arduino posts:
https://forum.processing.org/two/search?Search=arduino
Now
This is very important. Not need to be scared but it is imperative that you understand your board very well. It is definitely your responsibility to make sure you use the board property. In this case, if you set a line into your aforementioned pin, you can burn your board if you don't follow the provided instructions.
For this, I will suggest two things:
I will suggest posting in the Arduino forum for more info. I am sure you can find examples the Arduino website.
By the way, do you know where the AREF pin is located in your board? are you using it at all?
Kf
Your Arduino is already sending information through the serial port (lines 40 - 47). So leave the Arduino code as it is. O the Processing side, connect to the Arduino using the processing.serial library.