Buonasera a tutti,
sto tentando di fare un'automazione a una macchina.
I componenti:
Nema 17
Easy Driver
ESP 32
Display
tastiera 4x4
pulsante 1 collegato anche un micro switch (finecorsa)
pulsante 2 collegato anche un micro switch (finecorsa)
l'ho modificato per fare questo, inserisci la quantità di movimento che deve fare il motore (espresso in mm) e poi la salvo con il tasto #,
ogni volta che premo il pulsante 1 si muove per i millimetri che ho salvato (DX)
ogni volta che premo il pulsante 2 si muove per i millimetri che ho salvato (SX)
Funziona tutto ma ho notato un problema, il finecorsa quando viene eccitato fa fare il movimento ma se rimango fermo in questa posizione dopo un po muove ancora e delle volte quando il fine corsa viene diseccitato fa un altro movimento, come faccio a dire allo sketch che se si chiude il contatto (finecorsa eccitato) fa un movimento (solo uno) fino che non viene aperto e richiuso un altra volta, spero di essermi spiegato
Grazie Infinite
allego lo sketch
#include <Wire.h>
#include <EEPROM.h>
#include <Adafruit_GFX.h> // Adafruit core graphics library
#include <Adafruit_ST7789.h> // Adafruit hardware-specific library for ST7789
#include "AccelStepper.h" // AccelStepper Library
#include <Keypad.h> // Keypad Library
#define TFT_CS 27 // define chip select pin ST7789 TFT module
#define TFT_DC 15 // define data/command pin ST7789 TFT module
#define TFT_RST 13 // define reset pin, or set to -1 and connect to Arduino RESET pin
#define TFT_MOSI 23
#define TFT_SCLK 18
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
long tempoDiAttesa = 4000;
int button_1 = 32; //Pulsante 1 sul pin 32
int button_2 = 33; //Pulsante 2 sul pin 33
int enablePin = 26; //Pin di abilitazione motore
/*
int motorPin1 = 8; //IN1 sul pin 8
int motorPin2 = 9; //IN2 sul pin 9
int motorPin3 = 10; //IN3 sul pin 10
int motorPin4 = 11; //IN4 sul pin 11
int val=0;
const int StepsPerRevolution = 200;
const int myStep = 200;
*/
int firstnumber=99; // used to tell how many numbers were entered on keypad
int secondnumber=99;
int thirdnumber=99;
int keyfullnumber=0; // used to store the final calculated distance value
String currentposition = ""; // Used for display on Nokia LCD
int val = 0;
int val2 = 0;
// Keypad Setup
const byte ROWS = 4; // Four Rows
const byte COLS = 4; // Four Columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {19, 21, 5, 17}; // Arduino pins connected to the row pins of the keypad
byte colPins[COLS] = {16, 12, 14, 2}; // Arduino pins connected to the column pins of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); // Keypad Library definition
// AccelStepper Setup
AccelStepper stepper(4, 0, 3); // 1 = Easy Driver interface
// Arduino A0 connected to STEP pin of Easy Driver
// Arduino A1 connected to DIR pin of Easy Driver
char space1[] = " ";
char ver2[] = "Analyzer software";
char warn1[] = "Digitare la";
char warn2[] = "misura che si";
char warn3[] = "vuole muovere";
char warn4[] = "la carta";
char warn5[] = "STEPPER";
char warn6[] = "MOTOR";
void setup() {
// put your setup code here, to run once:
pinMode(button_1, INPUT);
pinMode(button_2, INPUT);
pinMode(enablePin, OUTPUT);
// AccelStepper speed and acceleration setup
stepper.setMaxSpeed(50000); // Not to fast or you will have missed steps
stepper.setAcceleration(50000); // Same here
stepper.setCurrentPosition(0);
tft.init(240, 320);
tft.fillScreen(0x0000);
tft.setRotation(2);
tft.setTextWrap(false); // disattivare l'opzione di ritorno a capo del testo
tft.setTextSize(3);
tft.setCursor(20,60);
tft.println(warn1);
tft.setCursor(5,90);
tft.println(warn2);
tft.setCursor(0,120);
tft.println(warn3);
tft.setCursor(40,150);
tft.print(warn4);
tft.setTextColor(0xF800, 0x0000);
tft.setCursor(60,220);
tft.println(warn5);
tft.setCursor(80,270);
tft.print(warn6);
delay(5000);
tft.fillScreen(0x0000);
tft.drawRect(0,0,240,110, 0x3C5F);//rettangolo blu
tft.drawRect(1,1,238,108, 0x3C5F);//rettangolo blu
tft.setTextColor(0x3C5F, 0x0000);
tft.setTextSize(2);
tft.setCursor(25,20);
tft.print("INSERISCI MISURA");
tft.setTextColor(0x379F, 0x0000);
tft.setTextSize(2);
tft.setCursor(25,60);
tft.print("MM");
tft.setTextColor(0xfffc41, 0x0000);
tft.setTextColor(0xFFFF);
tft.drawRect(0,206,240,38, 0xFFFF);//linea bianca attorno al rettangolo
tft.fillRect(1,207,238,36, 0x56BF);// blocco azzurro dove vengono scritte le situazioni
tft.setCursor(45,215);
tft.print("Misura Salvata");
tft.setTextColor(0x3C5F, 0x0000);
tft.setTextSize(2);
tft.setCursor(25,120);
tft.print("Legenda:");
tft.setTextColor(0x3C5F, 0x0000);
tft.setTextSize(2);
tft.setCursor(25,140);
tft.print("# salva");
tft.setTextColor(0x3C5F, 0x0000);
tft.setTextSize(2);
tft.setCursor(25,160);
tft.print("* cancella");
tft.setTextColor(0x3C5F, 0x0000);
tft.setTextSize(2);
tft.setCursor(25,180);
tft.print("D resetta");
}
int ver = 1;
int ver4 = 1;
int ver3 = 0;
void loop() {
if(ver3 ==0){
ver3=1;
digitalWrite(enablePin, HIGH);
}
char keypressed = keypad.getKey();
if(keypressed != NO_KEY){
saveKey(keypressed);
}
val = digitalRead(button_1);
if(val == 1 && ver == 1){
movestepper();
ver = 0;
}else if(val == 0){
ver = 1;
}
val2 = digitalRead(button_2);
if(val2 == 1 && ver4 ==1){
reversestepper();
ver4 = 0;
}else if(val2 == 0){
ver4 = 1;
}
}
void movestepper() { // Move the stepper
digitalWrite(enablePin, LOW); //Abilitazione del motore, in modo che si sposti quando richiesto
int calculatedmove=((keyfullnumber*1600)/80); // Calculate number of steps needed in mm
stepper.runToNewPosition(calculatedmove);
stepper.setCurrentPosition(0);
digitalWrite(enablePin, HIGH); //Disabilita il motore, in modo che possa riposare fino a quando non viene richiamato
delay(tempoDiAttesa);
}
void reversestepper(){
digitalWrite(enablePin, LOW); //Abilitazione del motore, in modo che si sposti quando richiesto
int calculatedmove=((keyfullnumber*1600)/80);// Calculate number of steps needed in mm
int reverse = (-1)*(calculatedmove);
stepper.runToNewPosition(reverse);
stepper.setCurrentPosition(0);
digitalWrite(enablePin, HIGH); //Disabilita il motore, in modo che possa riposare fino a quando non viene richiamato
val2=0;
delay(tempoDiAttesa);
}
void saveKey(char keys){
switch (keys) {
case '1':
checknumber(1);
break;
case '2':
checknumber(2);
break;
case '3':
checknumber(3);
break;
case '4':
checknumber(4);
break;
case '5':
checknumber(5);
break;
case '6':
checknumber(6);
break;
case '7':
checknumber(7);
break;
case '8':
checknumber(8);
break;
case '9':
checknumber(9);
break;
case '0':
checknumber(0);
break;
case '*':
deletenumber();
break;
case '#':
calculatedistance();
break;
case 'D':
resetnumbers();
break;
}
}
void checknumber(int x){
if (firstnumber == 99) { // Check if this is the first number entered
firstnumber=x;
String displayvalue = String(firstnumber); // Transform int to a string for display
drawnokiascreen(displayvalue); // Redraw Nokia lcd
} else {
if (secondnumber == 99) { // Check if it's the second number entered
secondnumber=x;
String displayvalue = (String(firstnumber) + String(secondnumber));
drawnokiascreen(displayvalue);
} else { // It must be the 3rd number entered
thirdnumber=x;
String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber));
drawnokiascreen(displayvalue);
}
}
}
void deletenumber() { // Used to backspace entered numbers
if (thirdnumber !=99) {
String displayvalue = (String(firstnumber) + String(secondnumber));
drawnokiascreen(displayvalue);
thirdnumber=99;
}
else {
if (secondnumber !=99) {
String displayvalue = String(firstnumber);
drawnokiascreen(displayvalue);
secondnumber=99;
}
else {
if (firstnumber !=99) {
String displayvalue = "";
drawnokiascreen(displayvalue);
firstnumber=99;
}
}
}
reloadScreen();
}
void calculatedistance() { // Used to create a full number from entered numbers
if (thirdnumber == 99 && secondnumber == 99 && firstnumber != 99) {
keyfullnumber=firstnumber;
}
if (secondnumber != 99 && thirdnumber == 99) {
keyfullnumber=(firstnumber*10)+secondnumber;
}
if (thirdnumber != 99) {
keyfullnumber=(firstnumber*100)+(secondnumber*10)+thirdnumber;
}
currentposition = String(keyfullnumber);
reloadScreen();
firstnumber=99;
secondnumber=99;
thirdnumber=99;
}
void reloadScreen(){
currentposition = String(keyfullnumber);
rewrite();
}
void rewrite(){
tft.setTextColor(0xfffc41, 0x0000);
tft.setTextSize(6);
tft.setCursor(70,260);
tft.print(" ");
tft.setCursor(70,260);
tft.print(currentposition);
}
void drawnokiascreen(String y) {
rewrite();
tft.setTextColor(0x379F, 0x0000);
tft.setTextSize(2);
tft.setCursor(70,60);
tft.print(" ");
tft.setCursor(70,60);
tft.print(y);
}
void resetnumbers() { // Reset numbers for next entry
firstnumber=99;
secondnumber=99;
thirdnumber=99;
keyfullnumber=0;
currentposition="";
reloadScreen();
}