Bonjour à tout et à toutes,
Je suis en cours de réalisation d'un comparateur du diamètre du filament pour l'impression 3D. Je me suis donc mis à faire ce projet à ce jour le résultat est concluant, mais un problème persiste lors de sa mise en œuvre les valeur affichée ne sont pas correct par exemple sur un de mes test pour un filament de diamètre 1.75 mm j'ai obtenue 3 mm
J'ai déjà essayé des solutions trouvé sur internet mais rien n'y fait si quelque aurait une piste je suis à votre disposition !
#define PIN A1
#define VARIATION 0.05
#define FILAMENT_1_75 1.75
#define FILAMENT_2_85 2.85
#define COURSE 12
char input;
int choix_diametre;
int min_potentiometre = 1023;
int max_potentiometre = 0;
void menu();
void menu_selection_diametre_filament();
float mapfloat(long x,long in_min, long in_max, long out_min, long out_max);
void read_value(int diameter);
class Average {
public:
void setStrength(int numSamples) {
//should be called rarely, like once in setup and maybe once if the configuration changes
//call begin() after this to re-populate the array and reset the current sample
//numSamples should be a number like 10 or 100
if (numSamples <= 0) return;
_value = (int*)realloc(_value, numSamples * sizeof(int));
_numSamples = numSamples;
_numSamples_2 = numSamples / 2;
}
void begin(int firstValue) {
if (_value == NULL) setStrength(1); //If you forgot to set the strength, default strength = no averaging
_currentTotal = 0;
for (int i = 0; i < _numSamples; i++) {
_value[i] = firstValue;
_currentTotal += firstValue;
}
_currentSample = 0;
}
void update(int sample) {
if (_numSamples < 1) return;
if (_currentSample >= _numSamples) _currentSample = 0;
_currentTotal += sample - _value[_currentSample];
_value[_currentSample] = sample;
_currentSample++;
return;
}
int value() {
return (_currentTotal + _numSamples_2) / _numSamples;
}
Average& operator=(int sample) {
//allows us to use the assignment filterInstance=x; to update the filter
update(sample);
return *this;
}
operator int() {
//allows us to return a default value, for the construction x=filterInstance;
return value();
}
private:
int _numSamples = 0;
int _numSamples_2 = 0;
int* _value = NULL;
int _currentSample = 0;
int _currentTotal = 0;
int _rawValue = 0;
};
Average myAvg;
const int inputPin = A0;
void setup() {
myAvg.setStrength(30); //ne pas dépasser la valeur 32 use a larger number for "stronger" averaging. Watch out you don't use all your memory if you are on a smaller Arduino like an UNO
myAvg.begin(analogRead(inputPin));
Serial.begin(9600);
pinMode(PIN, INPUT_PULLUP);
menu();
while (!Serial && millis() < 5000) {
//on native USB Arduinos, wait for a USB serial connection
}
Serial.println("Beginning averager");
}
void menu()
{
Serial.println("*************** MENU ***************");
Serial.println("*\th : Menu\t\t\t*");
Serial.println("*\t1 : Filament 1.75\t\t*");
Serial.println("*\t2 : Filament 2.85\t\t*");
Serial.println("*\t3 : Choix diametre filament\t*");
Serial.println("*****************************************");
}
void menu_selection_diametre_filament ()
{
Serial.println("** MENU SELECTION DIAMETRE FILAMENT **");
Serial.println("* Entrer un valeur de filament...\t*");
Serial.println("******************************************");
}
float mapfloat(long x, long in_min, long in_max, long out_min, long out_max) // définition de la fonction correspondant à tout ce qui ce trouve dans les acolades
{
return (float)(x - in_min) * (out_max - out_min) / (float)(in_max - in_min) + out_min; //valeur de la fonction
}
void read_value(int diameter)
{
if (diameter == 1 || diameter == 2)
{
int value = analogRead(PIN);
if (myAvg < min_potentiometre)
{
min_potentiometre = myAvg;
}
if (myAvg > max_potentiometre)
{
max_potentiometre = myAvg;
}
float d = mapfloat(myAvg, min_potentiometre, max_potentiometre, 0.0, COURSE);
float e;
if (diameter == 1)
{
e = FILAMENT_1_75;
}
else
{
e = FILAMENT_2_85;
}
if (d > e + VARIATION)
{
Serial.print("Filament trop large");
}
else if (d < e - VARIATION)
{
Serial.print("Filament trop fin");
}
else
{
Serial.print("Filament correct");
}
Serial.print(" : ");
Serial.print(value);
Serial.print(" - ");
Serial.print(d);
Serial.println("mm");
}
}
void loop() {
const unsigned long inputDataPeriod = 5; //milliseconds; only take a sample from the input this often
static unsigned long lastDataInput = millis();
const unsigned long outputDataPeriod = 50; //milliseconds; only print to the output this often
static unsigned long lastDataOutput = millis();
static int inputValue;
if (Serial.available() > 0)
{
input = Serial.read();
}
switch (input)
{
case 'h':
menu ();
choix_diametre = -1;
break;
case '1':
choix_diametre = 1;
Serial.println("Selection diametre 1.75mm");
delay (2000);
break;
case '2':
choix_diametre = 2;
Serial.println("Selection diametre 2.85mm");
delay (2000);
break;
case '3':
choix_diametre = -1;
menu_selection_diametre_filament();
break;
}
read_value(choix_diametre);
//read the input on the defined frequency and store in the averager
if (millis() - lastDataInput > inputDataPeriod) {
lastDataInput += inputDataPeriod;
inputValue = analogRead(inputPin);
myAvg.update(inputValue); //may also be written as myAvg = inputValue;
}
//output the average and the last instantaneous reading on the defined frequency
if (millis() - lastDataOutput > outputDataPeriod) {
lastDataOutput += outputDataPeriod;
/*Serial.print(inputValue);
Serial.print(" - ");
Serial.print(myAvg);
Serial.println();*/
}
}

