So, I did my best to scour the forumns for a solution to this problem and there are a few solutions that helped reduce fluctuations but not eliminate them:
- Space pins apart, e.g. 1,4,6
- Call the the pin, then pause and read
- Use external power source to provide more consistent voltage. Arduino was putting out 4.6V but now is putting out 5.03V consistently.
As a result of these adjustments, fluctuations decreased from +/- 14 to +/- 2.5 Any ideas why the code is doing this? I am using the SCA100T sensor and since it is a leveling sensor, it needs to be consistent. See spec sheet for more info if you want:
http://www.muratamems.fi/sites/default/files/documents/sca100t_inclinometer_datasheet_8261800a.pdf
Code:
const int XIN = 0;//assigns pin to X AXIS
const int YIN = 1;//assigns pin to X AXIS
int xAxis = 0; int yAxis = 0;//assigns initial value to variable 'xAxis' and 'yAxis'
void loop() {
analogRead(XIN); //Calls pins attention to arduino
delay(25); //Waits for pin, reduces noise
xAxis = analogRead(XIN); //reads X AXIS for input
analogRead(YIN); //Calls pins attention to arduino
delay(25); //Waits for pin, reduces noise
yAxis = analogRead(YIN) ;//reads X and Y AXIS for input
Serial.print("X AXIS: ");Serial.print(xAxis);Serial.print('\n');
Serial.print("Y AXIS: ");Serial.print(yAxis);Serial.print('\n');
delay(1000);
}