//
// Block array
//
#include <Servo.h>
//
//---------------------------- crear el control del servo
Servo myservo207;
Servo myservo206;
Servo myservo205;
Servo myservo204;
//
//---------------------------- Posicion de inico de los servos
//
int pos206 = 90;
int pos207 = 85;
int pos205 = 70; // final 90
int pos204 = 52; // final 70
int velocidad = 10;
//------------------------------ Posicion del espadin
int posicion206 = 0; // posicion del desvio
int posicion207 = 0; // 0 = desvio, 1= recto
int posicion205 = 0;
int posicion204 = 0;
int ViadePaso = 0; // Via libre = 0 y ocupada = 1
int Libre = 0; // Cruce libre = 0 y ocupado = 1
int SalidaEstacion = 0; // = 0 entrada Fabrica, = 1 entrada montaña
int SENTIDO = 0; // Sentido dentro del bucle de retorno
int RUTA=2; // switch define las acciones dentro de la ruta
int CTRRUTA = 0;
int SERVO = 0; // switch define los movimientos de los servos
int CTRSERVO = 0;
int x = 0;
//
//-------------------------------- Arrays
int Block[] = {22,24,26,28,30,32,34,36};
int SEN[] = {23,25,27,29,31,33,35,49,50,51,52,53,37.39};
int buttonsen[14];
int ctrsen[14];
int isen; // indice sensores
int iblock;
//---------------------------------------------
void setup() {
myservo207.attach(8); // ENTRADA SEÑAL SERVOS
myservo206.attach(9 );
myservo205.attach(10);
myservo204.attach(11);
pinMode(prueba, OUTPUT);
for (isen = 0; isen < 14; isen++) // inicializar las arrays
{
buttonsen[isen] = 0;
ctrsen[isen] = 0;
pinMode(SEN[isen], INPUT);
digitalWrite(SEN[isen],HIGH); //Activar resistencia Pullup
}
for (iblock = 0; iblock < 8; iblock++) // inicializar los block, se activan con cero
{
Block[iblock] = 1;
pinMode (Block[iblock], OUTPUT);
}
}
void loop(){
//----------------------------------- // INICIO
if (inicio ==0 ){
delay (tiempo);
inicio = 1;
}
//----------------------------------- // lectura de sensores
for (isen = 0; isen < 14; isen++)
{
buttonsen[isen] = digitalRead(SEN[isen]);
if (buttonsen[isen] == LOW)
{
ctrsen[isen] = HIGH;
}
if (isen < 8)
{
if (buttonsen[isen] == LOW && ctrsen[isen] == 1) // Activmmos los reles cuando tenemos algo.
{
Block[isen] = 1;
}
if (buttonsen[isen] == HIGH && ctrsen[isen] == 0) // Desactivamos los reles cuando no hay nada.
{
Block[isen] = 0;
}
}
}
//
//----------------------------------------------- inicializar los block, se activan con cero
//
//
//------------------------------------------- Bucle de retorno
//
if (buttonsen[11] == LOW) // SEN20
{
RUTA = 4;
}
if (buttonsen[10] == LOW) // SEN19
{
RUTA = 5;
}
//
// -------------------------------------------------------------------------
for ( x=0+CTRSERVO; x < 5; x++ ){
SERVO = x;
switch (SERVO){
case 0:
//------------------- no hace nada
break;
case 1:
if (posicion204 == 1) {
if (pos204 != 70){
pos204 += 1;
myservo204.write(pos204);
delay(velocidad);
}
}
if (posicion204 == 0){
if (pos204 != 52){
pos204 -= 1;
myservo204.write(pos204);
delay(velocidad);
}
}
digitalWrite(Block[4],HIGH);
break;
default:
break;
}
}
}
Gracias Jose.
Este sería el stetch cortado,de la RUTA 4 o 5 nos llevaría a otrta case y desde ese case ya no al stwich SERVO
para mover a las posisicones programadas.
Espero tu ayuda, muchas gracias.
Un saludo.
Pablo