Compare commits

...

2 Commits

Author SHA1 Message Date
rio.loann 9cf6baf5b4 new timer methode 2023-05-21 13:42:00 +02:00
rio.loann c0313dd825 idk 2023-05-21 13:38:48 +02:00
3 changed files with 160 additions and 3 deletions

76
Lab3SAP/Lab3SAP.ino Normal file
View File

@ -0,0 +1,76 @@
#include <Servo.h>
Servo Scale;
int outputPinServoMotor = 10;
int const sampleSize = 50;
double sampleArray[sampleSize];
int const inputPinSensor = 0;
void setup() {
Serial.begin(115200);
Scale.attach(outputPinServoMotor);
/*TCCR2A = 0;
TCCR2B = 0;
TCNT2 = 0;
OCR2A = 250;
TCCR2A |= (1 << WGM21);
TCCR2B |= (1 << CS21) | (1 << CS20);
TIMSK2 |= (1 << OCIE2A);
sei();*/
}
void loop() {
// put your main code here, to run repeatedly:
delay(10);
int val = analogRead(inputPinSensor);
addval(val);
int servoVal = sensorValToServoVal(getAVG());
Serial.println(servoVal);
Scale.write(servoVal);
}
/*ISR(TIMER2_COMPA_vect){
TCNT1 = 0;
int val = analogRead(inputPinSensor);
addval(val);
//Serial.println(val);
}*/
void addval(int val)
{
// we need to add the val at the start of the array
for (int i=0; i<sampleSize-1; i++)
{
sampleArray[i+1] = sampleArray[i];
}
sampleArray[0] = val;
}
int sensorValToServoVal(double sensorVal)
{
//float y = -272.6*log(sensorVal)+1103.9;
//float x = 851.99*exp(-0.054*sensorVal);
//Serial.println(x);
return map(sensorVal, 0, 1023, 0, 180);
}
double getAVG()
{
double avg = 0;
for (int val: sampleArray)
{
avg += (double) val/(double)sampleSize;
}
return avg;
}

View File

@ -0,0 +1,22 @@
int const inputPinSensor = 0;
int nbSample = 1000;
double result = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
for (int i=0; i<nbSample;i++)
{
delay(10);
double val = analogRead(inputPinSensor);
result += (double)val/(double)nbSample;
}
Serial.println(result);
result = 0;
}

View File

@ -1,9 +1,68 @@
#include <Servo.h>
Servo Scale;
int const sampleSize = 50;
int sampleArray[sampleSize];
int const outputPinServoMotor = 10;
int const inputPinSensor = 0;
//int timer;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Scale.attach(outputPinServoMotor);
// conf timer:
// thanks to lucas marais for his help
// we use timer 2 because timer 1 is used by servo
TCCR2A = 0;
TCCR2B = 0;
TCNT2 = 0;
OCR2A = 250;
TCCR2A |= (1 << WGM21);
TCCR2B |= (1 << CS21) | (1 << CS20);
TIMSK2 |= (1 << OCIE2A);
sei();
}
void loop() {
// put your main code here, to run repeatedly:
int servoVal = sensorValToServoVal(getAVG());
Serial.println(servoVal);
Scale.write(servoVal);
}
int sensorValToServoVal(float sensorVal)
{
double correctionfactor = 1; // correction to get kg
return map(sensorVal*correctionfactor, 0, 1023, 0, 180);
}
ISR(TIMER2_COMPA_vect){
TCNT1 = 0;
int val = analogRead(inputPinSensor);
addval(val);
//Serial.println(val);
}
void addval(int val)
{
// we need to add the val at the start of the array
for (int i=0; i<sampleSize-1; i++)
{
sampleArray[i+1] = sampleArray[i];
}
sampleArray[0] = val;
}
float getAVG()
{
float avg = 0;
for (int val: sampleArray)
{
avg += val/sampleSize;
}
return avg;
}