Hi all,
I am having trouble getting my head around debouncing for the various sensors I am using. I have the bounce2 library and have used the bounce library before only with buttons.
This time and I am using a range finder, a slider (potentiometer), an accelerometer, a wind sensor, and a button. I have included the code that I have done so far.
Every reading jitters by an integer or 2 and I was wondering if anyone could point me in the right direction to be able to get more stable readings?
Do you debounce say a wind sensor which has a more continous flow of reading in the same way as a button?
Thanks!
Asha
/*
Analog Input
Demonstrates analog input by reading an analog sensor on analog pins 0-5 and
sending out a serial message of the value of the sensor attached to that pin.
The circuit:
* Potentiometers attached to analog inputs 0-5
* center pin of the potentiometer to the analog pin
* one side pin (either one) to ground
* the other side pin to +5V
Adapted from David Cuartielles analog input tutorial in the examples.
modified 30 Aug 2011
By Tom Igoe
This example code is in the public domain.
http://arduino.cc/en/Tutorial/AnalogInput
*/
//int sensorPin = A0; // select the input pins for the potentiometer
//int sensorValue = 0; // variable to store the values coming from the sensors
int sensorPin1 = A1;
int sensorValue1 = 0;
int sensorPin2 = A2;
int sensorValue2 = 0;
int sensorPin3 = A3;
int sensorValue3 = 0;
int sensorPin4 = A4;
int sensorValue4 = 0;
int sensorPin5 = A5;
int sensorValue5 = 0;
//int sensorPin6 = A6;
//int sensorValue6 = 6;
int pushButton = 3;
int buttonState = 0;
#define trigPin 13
#define echoPin 12
void setup() {
Serial.begin(57600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(pushButton, INPUT);
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 250) { // This is where the LED On/Off happens
Serial.print(distance);
Serial.print(" ");
}
;
int buttonState = digitalRead(pushButton);
Serial.print(buttonState);
Serial.print(" ");
// read the value from the sensor:
sensorValue1 = analogRead(1);
// print hello and sensor number to screen
//b for routing this sensors data
//prints value to screen
Serial.print (sensorValue1);
Serial.print(" ");
// stop the program for <sensorValue> milliseconds
;
// read the value from the sensor number 2:
sensorValue2 = analogRead(2);
// print hello and sensor number to screen
Serial.print(sensorValue2);
Serial.print(" ");
// stop the program for <sensorValue> milliseconds
;
// read the value from the sensor number 3:
sensorValue3 = analogRead(3);
// print hello and sensor number to screen
Serial.print (sensorValue3);
Serial.print(" ");
// stop the program for <sensorValue> milliseconds
;
// read the value from the sensor number 4:
sensorValue4 = analogRead(4);
// print hello and sensor number to screen
Serial.print (sensorValue4);
Serial.print(" ");
// stop the program for <sensorValue> milliseconds
;
// read the value from the sensor number 5:
sensorValue5 = analogRead(5);
//print hello and sensor number to screen
Serial.println (sensorValue5);
// stop the program for <sensorValue> milliseconds
// delay(20);
// read the value from the sensor number 6:
//sensorValue5 = analogRead(5);
// print hello and sensor number to screen
//Serial.println (sensorValue5);
// stop the program for <sensorValue> milliseconds
;
delay (20);
}