// include the library code:
#include <LiquidCrystal.h>
#include <Stepper.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define STEP 7
#define DIR 6
void setup()
{
Serial.begin(9600);
// set up the LCD's number of columns and rows:
}
void loop()
{
//VARIABLES
//POTENCIOMETRO
int angulo;
int anguloMaximo;
int pasoMaximo;
//LEDS
const int LED=13;
int ledPinred = 13;
int ledPingreen = 10;
//SWITCH
const int BOTON=9;
int val;
//SALIDAS
pinMode(ledPinred, OUTPUT);
pinMode(ledPingreen, OUTPUT);
pinMode(STEP, OUTPUT );
pinMode(DIR, OUTPUT );
//ENTRADAS
pinMode(BOTON,INPUT);
val=digitalRead(BOTON);//LECTURA DEL ESTADO DEL SWITCH
digitalWrite(DIR, LOW); //SENTIDO DE GIRO DEL MOTOR PASO A PASO LOW/HIGH
lcd.begin(16, 2); //TAMAÑO DE LCD
if (val==HIGH) //CONDICION PULSO DE SWITCH
{
for(int n=1;n<=3;n++) //N: CANTIDAD DE CICLOS
{
int pasoMaximo = 0;
int anguloMaximo = 0;
lcd.setCursor(6, 0);
lcd.print(" - Ciclo ");
lcd.print(n);
for(int i=1;i<=200;i++)// I: CANTIDAD DE PASOS DEL MOTOR
{
int lectura = analogRead(A0); //LECTURA ANALOGICA DE A0
angulo = (360.0 * lectura) / 1023; //CONVERSION DE TENSION A ANGULO
if (angulo > anguloMaximo) //CONDICION PARA ANGULO MAXIMO
{
anguloMaximo = angulo; //REEMPLAZO DE VALOR MAXIMO EN VARIABLE "ANGULO"
pasoMaximo = i; //GUARDA EN "I" EL PASO DEL ANGULO MAXIMO
}
//IMPRESION DE DATOS A MOSTRAR
Serial.print("paso=");
Serial.print(i);
Serial.print(" angulo=");
Serial.print(angulo);
Serial.print(" anguloMax=");
Serial.print(anguloMaximo);
Serial.print(" pasoMax=");
Serial.println(pasoMaximo);
lcd.setCursor(0, 0);
lcd.print(i);
lcd.print("/");
lcd.print(angulo);
//SECUENCIA DE MOTOR PASO A PASO
digitalWrite(STEP, LOW);
delay(5);
digitalWrite(STEP, HIGH);
delay(5);
while (val==HIGH) {};
while (val==LOW) {break;};
}
lcd.setCursor(0, 1);
lcd.print("AM:");
lcd.print(anguloMaximo);
lcd.print(" /PM:");
lcd.print(pasoMaximo);
// delay a bit
delay(50);
for(int i=1;i<=pasoMaximo;i++) // HACE GIRAR AL MOTOR HASTA EL PASO MAXIMO
{
digitalWrite(STEP, LOW);
delay(20);
digitalWrite(STEP, HIGH);
delay(20);
}
digitalWrite(ledPinred, HIGH); // Enciende el LED
delay(1000); // Espera un segundo
digitalWrite(ledPinred, LOW); // Apaga el LED
delay(1000);
}
digitalWrite(ledPingreen, HIGH); // Enciende el LED
delay(1000); // Espera un segundo
digitalWrite(ledPingreen, LOW); // Apaga el LED
delay(1000);
lcd.clear();
lcd.print("Fin de Ciclo");
delay (3000);
}
if (val==LOW) //CONDICION PARA ESTADO DE REPOSO DEL BOTON
{
digitalWrite(ledPinred, HIGH); // Enciende el LED
delay(1000); // Espera un segundo
digitalWrite(ledPinred, LOW); // Apaga el LED
delay(1000);
lcd.print("NO HAY EJES");
delay(3000);
}
}