Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- float offset;
- float distance;
- float distance_raw;
- float filter_coef = 0.025;
- float measureToDistance = 1;
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(9600);
- offset=0;
- /*
- for (int i = 0; i < 1000; i++) {
- offset += measure();
- }
- offset /= 1000;
- */
- }
- void loop() {
- // put your main code here, to run repeatedly:
- distance_raw = ((float)capMeasure() - offset) * measureToDistance;
- distance = distance * (1 - filter_coef) + distance_raw * filter_coef;
- Serial.print(distance_raw);
- Serial.print(',');
- Serial.print(distance);
- Serial.println();
- delay(10);
- }
- unsigned int capMeasure() {
- //charge share capacitance measure on A1 pin. A0 is used to charge S/H cap
- pinMode(A1, OUTPUT);
- pinMode (A0, INPUT_PULLUP);
- analogRead(A0);
- pinMode(A1, INPUT);
- unsigned long value = analogRead(A1);
- pinMode(A1, OUTPUT);
- return value;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement