#include <arduino.h>
#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
const int OLED_RESET = -1;
Adafruit_SSD1306 display(128, 64, &Wire, OLED_RESET);
const int boutonstart = 12;
const int boutonfilament = 13;
const int vitesse_pin = A1;
const int largeur_pin = A0;
const int moteurbobinems1_pin = 7;
const int moteurbobinems2_pin = 6;
const int moteurguidems1_pin = 10;
const int moteurguidems2_pin = 9;
const int moteurbobineen_pin = 8;
const int moteurguideen_pin = 11;
AccelStepper moteurbobine(AccelStepper::FULL2WIRE, 3, 2); // Step, Dir
AccelStepper moteurguide(AccelStepper::FULL2WIRE, 5, 4);
int vitesse = 0;
int largeur = 0;
long balayage = 0;
long distance = 0;
// Côté moteur bobine
// le rapport petite roue (36 dents) grande roue (92 dents) est 92/36=2.556. soit nombre de pas pour un tour de bobine = 6400*92/36=16356
// ce qui est équivalent au diamètre du filament soit 1.75mm . Pour un déplacement d'1mm, il faut 6400*92/36/1.75 pas soit 9346,03
// Côté moteur guide
// 160000 pas donnent un déplacement latéral de 97.5 mm. pour un déplacement d'1 mm, il faut 160000/97.5 pas soit 1641.025
// la vitesse de rotation du guide doit être proportionnelle à la vitesse de la bobine.
// le rapport devrait être : 6400*92/36/1.75/160000*97.5 soit : 5.695238
// Quand la bobine aura fait un tour, le guide sera déplacé de 1.75mm (diamètre du filament)
float pas = 3.9; // 1 tour moteurguide, pas de la vis en mm. 6400/1641.025= 3.9
float rapport = 6400.0 * 92 / 36 / 1.75 / 160000 * 97.5;
int vitesseprecedente = 0;
float moteurbobinevitesse = 500;
float moteurguidevitesse = moteurbobinevitesse / rapport;
bool bobinageencours = false;
bool disable = true;
int startetat = 1;
int filamentetat = 1;
int moy = 15;
int largeurx = 0;
int couches = 0;
int decalage = 6564; // around 4 mm
void affichage();
void setup()
{
Serial.begin(115200);
delay(500);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.display();
pinMode(boutonstart, INPUT_PULLUP);
pinMode(boutonfilament, INPUT_PULLUP);
pinMode(moteurbobinems1_pin, OUTPUT);
pinMode(moteurbobinems2_pin, OUTPUT);
pinMode(moteurguidems1_pin, OUTPUT);
pinMode(moteurguidems2_pin, OUTPUT);
pinMode(moteurbobineen_pin, OUTPUT);
pinMode(moteurguideen_pin, OUTPUT);
moteurbobine.setMaxSpeed(moteurbobinevitesse);
moteurbobine.setAcceleration(500);
moteurbobine.moveTo(-100000000); //
moteurguide.setMaxSpeed(moteurguidevitesse);
moteurguide.setAcceleration(500 / rapport);
moteurguide.moveTo(0);
digitalWrite(moteurbobinems1_pin, LOW);
digitalWrite(moteurbobinems2_pin, LOW);
digitalWrite(moteurguidems1_pin, LOW);
digitalWrite(moteurguidems2_pin, LOW);
digitalWrite(moteurbobineen_pin, disable); // coil motor disabled
digitalWrite(moteurguideen_pin, disable); // guide motor disabled
affichage();
}
void loop()
{
vitesse = map(analogRead(A1), 0, 1000, 1, 10);
startetat = digitalRead(boutonstart);
filamentetat = digitalRead(boutonfilament);
if (bobinageencours == false) // Before starting the motors
{
largeurx = 0;
for (int i = 0; i < moy; i++)
{
largeurx += analogRead(A0);
}
largeur = map(largeurx / moy, 20, 1022, 25, 60);
distance = 6400L / pas * largeur; // number of steps the guide motor must move to cover the coil width
balayage = distance;
affichage();
}
if (filamentetat == 1) // if that switch opens, the motors and the process must stop
{
disable = true;
digitalWrite(moteurbobineen_pin, disable); // coil motor disabled
digitalWrite(moteurguideen_pin, disable); // guide motor disabled
bobinageencours = false;
}
if (startetat == 0) // when this switch close, the process must start
{
couches=0;
disable = false;
moteurguide.moveTo(-decalage);
moteurbobine.setMaxSpeed(moteurbobinevitesse * vitesse);
moteurguide.setMaxSpeed(moteurguidevitesse * vitesse);
digitalWrite(moteurguideen_pin, disable); // guide motor enabled
while (moteurguide.distanceToGo() != 0)
{ // move guide 4mm to the right limit of the coil
moteurguide.run();
}
moteurguide.stop();
moteurguide.setMaxSpeed(moteurguidevitesse * vitesse);
moteurguide.moveTo(balayage);
digitalWrite(moteurbobineen_pin, disable); // coil motor enabled
bobinageencours = true; // Indicate that the process is started
}
if (vitesse != vitesseprecedente) // The speed can be modified during the process
{
vitesseprecedente = vitesse;
moteurbobine.setMaxSpeed(moteurbobinevitesse * vitesse);
moteurguide.setMaxSpeed(moteurguidevitesse * vitesse);
affichage();
}
if (bobinageencours == true) // Following happens only if the process is started
{
if (moteurguide.distanceToGo() == 0)
{
moteurbobine.stop();
if (balayage > 0)
{
moteurguide.stop();
moteurguide.setMaxSpeed(moteurguidevitesse * vitesse);
moteurguide.moveTo(balayage + decalage * 2);
while (moteurguide.distanceToGo() != 0)
{ // move guide 4mm to the left limit of the coil
moteurguide.run();
}
moteurguide.stop();
moteurguide.setMaxSpeed(moteurguidevitesse * vitesse);
balayage = 0L;
}
else
{
moteurguide.stop();
moteurguide.setMaxSpeed(moteurguidevitesse * vitesse);
moteurguide.moveTo(-decalage);
while (moteurguide.distanceToGo() != 0)
{ // move guide 4mm to the right limit of the coil
moteurguide.run();
}
moteurguide.stop();
moteurguide.setMaxSpeed(moteurguidevitesse * vitesse);
balayage = distance;
}
moteurguide.moveTo(balayage);
couches++;
affichage();
moteurbobine.setMaxSpeed(moteurbobinevitesse * vitesse);
moteurbobine.moveTo(-100000000);
}
moteurbobine.run();
moteurguide.run();
}
}
void affichage()
{
display.clearDisplay();
display.setCursor(0, 0);
display.print("Vitesse: ");
display.println(vitesse);
display.setCursor(0, 20);
display.print("largeur: ");
display.print(largeur);
display.setCursor(0, 40);
display.print("Couches : ");
display.print(couches);
display.display();
}