working on drift

This commit is contained in:
Loic Delattre 2023-05-16 16:14:16 +02:00
parent 6e82040afd
commit 4482ec2548
1 changed files with 23 additions and 6 deletions

View File

@ -1,7 +1,12 @@
#include <Servo.h>
int const Fs = 200;
int const timePeriod = 1000/Fs; //for delay
float timeStart = 0;
int const myScale = A0;
int unsigned avgValue = 0;
long unsigned avgValue = 0;
long unsigned lastVal = 0;
int const movAvgVal = 20;
Servo myservo;
@ -16,15 +21,27 @@ void setup() {
void loop() {
// put your main code here, to run repeatedly:
int localValue = 0;
long unsigned localValue = 0;
for (int i = 0; i < movAvgVal; i++){
timeStart = micros();
localValue = localValue + analogRead(myScale);
int readTime = timeStart - micros();
delay(timePeriod-readTime/1000);
}
avgValue = localValue/movAvgVal;
Serial.println(avgValue);
int compare = avgValue - lastVal;
val = map(avgValue, 80, 900, 0, 180);
myservo.write(val);
delay(15);
if (compare > 1 || compare < -1){
val = map(avgValue, 345, 870, 0, 180);
myservo.write(val);
Serial.print("current VAL: ");
Serial.println(avgValue);
Serial.print("compare VAL: ");
Serial.println(compare);
delay(15);
lastVal = avgValue;
}
}