Good morning I have a problem with a code that I tried to move two servomotres my problem is that for example in the first 3 boxes I keep the servo1 degrees and in the other 3 boxes I keep the values of the servo 2
but at the time of calling them instead of qeu run the valroes of servo 1 and after the two both are executed at the same time¡¡¡ how could ahcer to correctly retrieve the values qeu keep ???
#include<Servo.h>
#include <EEPROM.h>
//Creamos los objetos servo
Servo servo;
Servo servo2;
unsigned char c; //Letra del Servo
int posicion; //Posicion del servo
int valor[5]; //valor para almacenar lo del servo1
int valor2[5]; //valor2 para el servo2
int j[20]; // array para la EEPROM
void setup()
{
//Inicializamos los Servos
servo.attach(12);
servo2.attach(11);
//Inicializamos la comunicacion por Serial
Serial.begin(9600);
Serial.print(" \n");
Serial.println("xxxxxxxxxBienvenido Usuarioxxxxxxxxxxx");
Serial.println(" AUTOMATA Codigo1 by: Daniel Juarez");
}
void loop() {
if (Serial.available())
{
c = Serial.read();
posicion = Serial.parseInt();
// movemos el servo 1 o el dos y automaticamente el valor que se le dio se guarda
// se introduce la letra del servo + los grados para moverlo
if (c == 'a')
{
Serial.print("Motor1: ");
Serial.println(posicion);
servo.write(valor[0]);
delay(10);
valor[0] = posicion;
EEPROM.write(j[0], valor[0]);
Serial.print("Guardo en la posicion ");
Serial.print(j[0]);
Serial.print("\t");
Serial.println(valor[0]);
j[0]++;
valor[0]++;
}
else if (c == 'b')
{
Serial.print("Motor2: ");
Serial.println(posicion);
servo2.write(posicion);
delay(10);
valor2[0] = posicion;
EEPROM.write(j[0], valor2[0]);
Serial.print("Guardo en la posicion ");
Serial.print(j[0]);
Serial.print("\t");
Serial.println(valor2[0]);
j[0]++;
valor2[0]++;
}
/*
else if (c == 'm')
{
graba();
} */
else if (c == 'z')
{
automata(); //lee los datos guardados y hace mover los servos
}
else if (c == 'x')
{
borrar(); //boorra los datos de la EEPROM
}
}
}
/*
void graba() {
EEPROM.write(j[0], valor);
Serial.print("Guardo en la posicion ");
Serial.print(j[0]);
Serial.print("\t");
Serial.println(valor);
j[0]++;
EEPROM.write(j[0], valor2);
//Serial.print("Guardo en la posicion ");
//Serial.print(j[0]);
//Serial.print("\t");
Serial.println(valor2);
j[0]++;
}
*/
void automata() {
Serial.println("EJECUTANDO MEMORIA¡¡¡¡¡");
delay(10);
int i = 0;
for (i = 0; i < 10; i++)
{
valor[0] = EEPROM.read(i);
valor2[0] = EEPROM.read(i);
Serial.println( EEPROM.read(i));
servo.write(valor[0]);
delay(500);
servo2.write(valor2[0]);
delay(500);
}
}
void borrar() {
pinMode(13, OUTPUT);
Serial.println("Borrando Memoria¡¡¡¡¡");
for (int i = 0 ; i <100; i++) {
EEPROM.write(i, 0);
/*EEPROM.length()*/
}
digitalWrite(13, HIGH);
Serial.println("Memoria Borrada¡¡¡¡¡");
}