updating loop function
This commit is contained in:
parent
fe0c6570b7
commit
790724bac0
|
|
@ -1,6 +1,6 @@
|
||||||
#include <Servo.h>
|
#include <Servo.h>
|
||||||
|
|
||||||
#define SAMPLESIZE 100;
|
#define SAMPLESIZE 100
|
||||||
|
|
||||||
int Sensor_1;
|
int Sensor_1;
|
||||||
int samples[SAMPLESIZE];
|
int samples[SAMPLESIZE];
|
||||||
|
|
@ -9,7 +9,7 @@ int oldTimer;
|
||||||
int delayBetweenOutput = 100;
|
int delayBetweenOutput = 100;
|
||||||
int servoPin = 3;
|
int servoPin = 3;
|
||||||
|
|
||||||
Servo balanceScale
|
Servo balanceScale;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
// put your setup code here, to run once:
|
// put your setup code here, to run once:
|
||||||
|
|
@ -29,10 +29,34 @@ void setup() {
|
||||||
// enable timer compare interrupt
|
// enable timer compare interrupt
|
||||||
TIMSK2 |= (1 << OCIE2A);
|
TIMSK2 |= (1 << OCIE2A);
|
||||||
sei(); // Enable interrupts
|
sei(); // Enable interrupts
|
||||||
timer = millis()
|
timer = millis();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
// put your main code here, to run repeatedly:
|
// 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);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue