Compare commits
2 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
9cf6baf5b4 | |
|
|
c0313dd825 |
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue