#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
// Configuração do teclado 4x4
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'.','0','#','D'}
};
byte rowPINS[ROWS] = {11,10,9,8};
byte colPINS[COLS] = {7,6,5,4};
Keypad kpd = Keypad(makeKeymap(keys),rowPINS,colPINS, ROWS, COLS);
LiquidCrystal_I2C lcd(0x27,20,4);
// Configuração do motor de passo
const int stp = 2;
const int dir = 3;
const int StepsPerRotation = 400;
const int TableRatio = 45;
const int Multiplier = (StepsPerRotation * TableRatio)/360;
// --- NOVO: potenciômetro ---
const int potPin = A0; // pino do potenciômetro
int stepdelay = 500; // delay inicial em microssegundos
// Variáveis globais
float Degrees = 0;
float ToMove = 0;
float bob = 0;
int cho = 0;
void setup()
{
lcd.init();
pinMode(stp, OUTPUT);
pinMode(dir, OUTPUT);
pinMode(potPin, INPUT);
lcd.backlight();
lcd.print("**Divisor Digital**");
lcd.setCursor(3,3);
lcd.print("Wagner Koller");
delay(2000);
lcd.init();
cho = 0;
char key = kpd.getKey();
lcd.print(" MENU");
lcd.setCursor(0,1);
lcd.print("Graus ------ Tecla A");
lcd.setCursor(0,2);
lcd.print("Divisao ---- Tecla B");
lcd.setCursor(0,3);
lcd.print("Livre ------ Tecla C");
while(cho == 0)
{
key = kpd.getKey();
switch (key)
{
case NO_KEY:
break;
case 'A':
Degrees=getdegrees();
lcd.clear();
cho = 1;
break;
case 'B':
Degrees=getdivisions();
cho=2;
break;
case 'C':
Degrees=getjog();
lcd.clear();
cho=3;
break;
}
}
}
void loop()
{
lcd.clear();
char key = kpd.getKey();
bob = 0;
lcd.setCursor(7,0);lcd.print("Total: ");lcd.print(bob,2); // total steps
lcd.setCursor(0,3);lcd.print("Vai=A Volta=B sair=C");
while(key != 'C')
{
lcd.setCursor(0,0);lcd.print(abs(Degrees),2);lcd.print((char)223);
// --- NOVO: mostra velocidade atual ---
int potValue = analogRead(potPin);
stepdelay = map(potValue, 0, 1023, 2000, 200);
lcd.setCursor(0,1); lcd.print("Delay: "); lcd.print(stepdelay); lcd.print("us ");
key = kpd.getKey();
if(key == 'A') // dir +
{
bob = bob + Degrees;
ToMove = (Degrees*Multiplier);
digitalWrite(dir, LOW);
printadvance();
}
if(key=='B') // dir-
{
bob = bob - Degrees;
ToMove = (Degrees*Multiplier);
digitalWrite(dir, HIGH);
printadvance();
}
}
lcd.init();
setup();
}
float getjog()
{
float Degrees = 0;
float num = 0.00;
char key = kpd.getKey();
lcd.clear();
lcd.setCursor(6,0);lcd.print("Livre");
lcd.setCursor(0,1);lcd.print("A=1 B=10 C=100 Pasos");
lcd.setCursor(0,2);lcd.print("Entre Graus:");lcd.setCursor(0,3);lcd.print("OK = # ");lcd.print((char)60);lcd.print((char)45);lcd.print(" D");
while(key != '#')
{
switch (key)
{
case NO_KEY:
break;
case 'A':
Degrees = 1;
lcd.setCursor(14,2);lcd.print(Degrees);
break;
case 'B':
Degrees = 10;
lcd.setCursor(14,2);lcd.print(Degrees);
break;
case 'C':
Degrees = 100;
lcd.setCursor(14,2);lcd.print(Degrees);
break;
case 'D':
num=0.00;
lcd.setCursor(14,2);lcd.print(" ");
lcd.setCursor(14,2);
break;
}
key = kpd.getKey();
}
return Degrees;
}
float getdivisions()
{
float Degrees = 0;
float num = 0.00;
char key = kpd.getKey();
lcd.clear();
lcd.setCursor(0,1);lcd.print("Entre Divisao:");lcd.setCursor(0,3);lcd.print("OK = # ");lcd.print((char)60);lcd.print((char)45);lcd.print(" D");
lcd.setCursor(16,1);
while(key != '#')
{
switch (key)
{
case NO_KEY:
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
num = num * 10 + (key - '0');
lcd.print(key);
break;
case 'D':
num=0.00;
lcd.setCursor(16,1);lcd.print(" ");
lcd.setCursor(16,1);
break;
}
Degrees = 360/num;
key = kpd.getKey();
}
return Degrees;
}
float getdegrees()
{
float num = 0.00;
float decimal = 0.00;
float decnum = 0.00;
int counter = 0;
lcd.clear();
char key = kpd.getKey();
lcd.setCursor(0,1);lcd.print("Entre Graus:");lcd.setCursor(0,3);lcd.print("OK = # ");lcd.print((char)60);lcd.print((char)45);lcd.print(" D");
lcd.setCursor(15,1);
bool decOffset = false;
while(key != '#')
{
switch (key)
{
case NO_KEY:
break;
case '.':
if(!decOffset)
{
decOffset = true;
}
lcd.print(key);
break;
case 'D':
num=0.00;
lcd.setCursor(15,1);lcd.print(" ");
lcd.setCursor(15,1);
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
if(!decOffset)
{
num = num * 10 + (key - '0');
lcd.print(key);
}
else if((decOffset) && (counter <= 1))
{
num = num * 10 + (key - '0');
lcd.print(key);
counter++;
}
break;
}
decnum = num / pow(10, counter);
key = kpd.getKey();
}
return decnum;
}
void printadvance()
{
lcd.setCursor(6,1);lcd.print("Movendo ");
lcd.setCursor(4,2);lcd.print("pasos ");lcd.print(ToMove,0);
lcd.setCursor(13,0);lcd.print(bob,2);
rotation(ToMove,0);
lcd.setCursor(6,1);lcd.print(" ");
}
void rotation(float tm, int d)
{
long steps = round(tm);
for(long i = 0; i < steps; i++)
{
// --- NOVO: lê potenciômetro e ajusta delay ---
int potValue = analogRead(potPin);
stepdelay = map(potValue, 0, 1023, 2000, 200);
digitalWrite(stp, HIGH);
delayMicroseconds(stepdelay);
digitalWrite(stp, LOW);
delayMicroseconds(stepdelay);
}
}
void software_Reset()
{
asm volatile (" jmp 0");
}