#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C LCD(0x27, 16, 2);
#define passo 12
#define sentido 14
#define pot 34
#define NUM_PASSOS 200
void setup() {
Serial.begin(115200);
LCD.init();
LCD.backlight();
pinMode(pot, INPUT);
pinMode(passo, OUTPUT);
pinMode(sentido, OUTPUT);
LCD.setCursor(0, 0);
LCD.print("Iniciando...");
delay(1000);
LCD.clear();
}
void loop() {
int valorAD = analogRead(pot);
int valorPassos = map(valorAD, 0, 4095, -NUM_PASSOS, NUM_PASSOS);
int valorGraus = map(valorAD, 0, 4095, 0, 360);
int valorPot = map(valorAD, 0, 4095, 0, 100);
LCD.setCursor(0, 0);
LCD.print("GRAUS: ");
LCD.print(valorGraus);
LCD.setCursor(0, 1);
LCD.print("POTENC: ");
LCD.print(valorPot);
moverMotor(valorPassos);
delay(50);
}
void moverMotor(int passos) {
digitalWrite(sentido, passos >= 0 ? HIGH : LOW);
for (int i = 0; i < abs(passos); i++) {
digitalWrite(passo, HIGH);
delayMicroseconds(1000);
digitalWrite(passo, LOW);
delayMicroseconds(1000);
}
}