diff --git a/sketch_may16b/sketch_may16b.ino b/sketch_may16b/sketch_may16b.ino index 2340a4c..b9e1288 100644 --- a/sketch_may16b/sketch_may16b.ino +++ b/sketch_may16b/sketch_may16b.ino @@ -1,6 +1,6 @@ #include -#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