updating loop function

This commit is contained in:
Gabri6 2023-05-16 20:12:19 +02:00
parent fe0c6570b7
commit 790724bac0
1 changed files with 28 additions and 4 deletions

View File

@ -1,6 +1,6 @@
#include <Servo.h>
#define SAMPLESIZE 100;
#define SAMPLESIZE 100
int Sensor_1;
int samples[SAMPLESIZE];
@ -9,7 +9,7 @@ int oldTimer;
int delayBetweenOutput = 100;
int servoPin = 3;
Servo balanceScale
Servo balanceScale;
void setup() {
// put your setup code here, to run once:
@ -29,10 +29,34 @@ void setup() {
// enable timer compare interrupt
TIMSK2 |= (1 << OCIE2A);
sei(); // Enable interrupts
timer = millis()
timer = millis();
}
void loop() {
// put your main code here, to run repeatedly:
balanceScale.write(map(moyenne(),0,1023,180,0));
}
ISR(TIMER2_COMPA_vect){
TCNT1 = 0;
Sensor_1 = analogRead(0);
rollSamples(Sensor_1);
}
void rollSamples(int newData){
// décaler les données existantes vers la gauche
for (int i = 0; i < SAMPLESIZE - 1; i++) {
samples[i] = samples[i+1];
}
// ajouter la nouvelle donnée à la fin du tableau
samples[SAMPLESIZE - 1] = newData;
}
double moyenne(){
long all = 0;
for(int i = 0; i<SAMPLESIZE-1;i++){
all += samples[i];
}
return double(all)/double(SAMPLESIZE);
}