Skip to content

Commit 5259329

Browse files
committed
Actualiza para Libro
1 parent 76ecd7e commit 5259329

File tree

8 files changed

+279
-0
lines changed

8 files changed

+279
-0
lines changed
123 KB
Binary file not shown.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*******************************************************************************************
2+
* Autor: Fco. Javier Rodriguez Navarro
3+
* WEB: www.pinguytaz.net
4+
*
5+
* Descripción: Ejemplo de programacion de un Motor paso a paso
6+
*
7+
* Librerias:
8+
*
9+
***********************************************************************************************/
10+
#include <Stepper.h>
11+
#define PULSADOR 2
12+
13+
const int pasosPorVuelta = 200;
14+
Stepper myStepper = Stepper(pasosPorVuelta, 8, 9, 10, 11);
15+
16+
17+
int estado = 1;
18+
bool antiguo = LOW;
19+
20+
21+
void setup()
22+
{
23+
Serial.begin(9600); // abre el Puerto serie y lanza los primeros envios
24+
25+
// Definimos Pines
26+
pinMode(PULSADOR, INPUT); // declara pulsador como entrada
27+
28+
Serial.println("---- INICIO -------");
29+
myStepper.setSpeed(100); //Velocidad del motor RPM
30+
31+
32+
}
33+
34+
void loop()
35+
{
36+
int val = 0;
37+
val =digitalRead(PULSADOR);
38+
myStepper.step(160);
39+
delay(2000);
40+
//myStepper.step(-90);
41+
//if ( (((estado % 2) == 0) && val == HIGH) || (((estado % 2) != 0) && val == LOW)) // Se cambia en estado par se pulsa y en impares se suelta
42+
if(antiguo != val) // Cambio de estado
43+
{
44+
antiguo = val;
45+
Serial.println("Realiza el estado " + String(estado++));
46+
47+
delay(1500); // Evita rebote
48+
}
49+
}
134 KB
Binary file not shown.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
/************************************************************************
3+
* Fco. Javier Rodriguez Navarro www.pinguytaz.net
4+
*
5+
* Ejemplo de movimiento de Motor continua con MOSFET y PWM.
6+
*
7+
*************************************************************************/
8+
#define MOTOR 3
9+
#define VELOCIDAD A0
10+
11+
void setup() // configuracion placa
12+
{
13+
// También cambiaríamos la velocidad del puerto de consola segun plataforma
14+
pinMode(MOTOR, OUTPUT);
15+
16+
Serial.begin(9600);
17+
}
18+
19+
void loop() //Se repite indefinidamente,
20+
{
21+
int potenciometro, velocidad;
22+
23+
Serial.println("Inicio Ejemplo de motor continua");
24+
25+
// Leemos Potenciometro
26+
potenciometro = analogRead(VELOCIDAD);
27+
velocidad = map(potenciometro,0,1023,0,10);
28+
29+
Serial.print("Leido: ");
30+
Serial.print(potenciometro);
31+
Serial.print(" velocidad: ");
32+
Serial.println(velocidad);
33+
34+
// Ponemos la velocidad en el motor.
35+
analogWrite(MOTOR,velocidad*25);
36+
37+
delay(1500);
38+
}
14.9 KB
Binary file not shown.
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*******************************************************************************************
2+
* Autor: Fco. Javier Rodriguez Navarro
3+
* WEB: www.pinguytaz.net
4+
*
5+
* Descripción: Ejemplo de programacion de un Servomotor
6+
* Se ha utilizado el motor MG-996R (180º)
7+
* Voltaje: 4,8V - 6V (600 - 1550mA)
8+
* Torque: 9Kg
9+
* Velocidad: 0,17s/60º
10+
* Periodo PWM 20ms (Cambio posi cada 20ms minimo)
11+
* Ancho banda muerta 5ms
12+
* Librerias: Servo.h
13+
*
14+
***********************************************************************************************/
15+
#include <Servo.h>
16+
17+
#define PINMOTOR 9
18+
#define PINBOTON 2
19+
20+
// Declaramos la variable para controlar el servo
21+
Servo servoMotor;
22+
#define PERIODO 20
23+
24+
25+
void setup()
26+
{
27+
Serial.begin(9600);
28+
pinMode(PINBOTON, INPUT); // declara pulsador como entrada
29+
30+
Serial.println("Se inicia");
31+
Serial.println(digitalRead(PINBOTON));
32+
servoMotor.attach(PINMOTOR);
33+
}
34+
35+
void loop()
36+
{
37+
bool val;
38+
static bool bantiguo = HIGH;
39+
static int estado=0;
40+
val =digitalRead(PINBOTON);
41+
42+
if(bantiguo != val) // Cambio de estado
43+
{
44+
bantiguo = val;
45+
realiza(estado++);
46+
delay(1500); // Evita rebote
47+
}
48+
delay(50);
49+
}
50+
51+
void realiza (int estado)
52+
{
53+
Serial.print("Realiza el estado ");
54+
Serial.println(estado);
55+
if(estado == 0)
56+
{
57+
Serial.println("Se realiza secuencia de inicializacion dejando en 90");
58+
Serial.println("Posicionamos de 0 - 180");
59+
mueve(0,0);
60+
delay(1000);
61+
mueve(180,0);
62+
delay(1000);
63+
Serial.println("Posicionamos inicio 90 con moviiento");
64+
mueve(90,0);
65+
}
66+
if(estado == 1)
67+
{
68+
Serial.println("Secuencia de mover a 60 y despues a 90");
69+
mueve(45,2);
70+
delay(2000);
71+
mueve(150,1);
72+
delay(2000);
73+
mueve(35,5);
74+
delay(10000);
75+
mueve(90,4);
76+
delay(2000);
77+
}
78+
}
79+
void mueve(int angulo, int tiempo)
80+
{
81+
float espera =0;
82+
int antiguo = servoMotor.read();
83+
int amover = abs(antiguo-angulo);
84+
85+
Serial.println("Movemos desde el "+String(antiguo) +" al angulo "+String(angulo)+ " en "+String(tiempo));
86+
Serial.println(amover);
87+
88+
if((tiempo*1000) - (amover*20) <= 0) // Maxima velocidad de giro.
89+
{
90+
Serial.print("Rapido ");
91+
servoMotor.write(angulo);
92+
delay(PERIODO); // espera waits 15 ms para posicionar
93+
return;
94+
}
95+
96+
if(antiguo < angulo) // Avanza
97+
{
98+
Serial.print("Alante ");
99+
for (int pos = antiguo; pos <= angulo; pos += 1)
100+
{
101+
servoMotor.write(pos);
102+
delay(tiempo*1000 / amover); // espera waits 15 ms para posicionar
103+
}
104+
}
105+
else
106+
{
107+
Serial.print("ATRAS ");
108+
for (int pos = antiguo; pos >= angulo; pos -= 1)
109+
{
110+
servoMotor.write(pos);
111+
delay(tiempo*1000 / amover); // espera waits 15 ms para posicionar
112+
}
113+
114+
servoMotor.write(angulo);
115+
delay(PERIODO); // espera waits 15 ms para posicionar
116+
}
117+
118+
return;
119+
}
121 KB
Binary file not shown.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*******************************************************************************************
2+
* Autor: Fco. Javier Rodriguez Navarro
3+
* WEB: www.pinguytaz.net
4+
*
5+
* Descripción: Ejemplo total con placa L298
6+
* El programa controla la velocidad y dirección del motor con un potenciometro
7+
* mitad hacia una dirección la otra mitad al otro, ademas tiene un boton
8+
* que indica si se para o pone en marcha.
9+
***********************************************************************************************/
10+
#define MINV 33
11+
12+
int ENA = 3; // La potencia y activación se controla con ENAB por lo tanti quitar el Jumper de ENB.
13+
// Control de la dirección 10 Alante, 01 Atras.
14+
int IN1 = 4;
15+
int IN2 = 5;
16+
int BOTON = 8;
17+
18+
void setup()
19+
{
20+
Serial.begin(9600);
21+
pinMode(ENA, OUTPUT);
22+
pinMode(IN1, OUTPUT);
23+
pinMode(IN2, OUTPUT);
24+
pinMode(BOTON, INPUT);
25+
26+
analogWrite(ENA,0);
27+
}
28+
29+
void loop()
30+
{
31+
static bool estado=false;
32+
int velocidad ;
33+
34+
int tempo = analogRead(A1);
35+
//Serial.println("Leido: "+String(tempo));
36+
velocidad = map(tempo, 0,1023,-100,100);
37+
//Serial.println("Velocidad: "+String(velocidad));
38+
39+
40+
int boton = digitalRead(BOTON);
41+
//Serial.println(boton);
42+
if (boton )
43+
{
44+
//Serial.println("Cambio estado");
45+
estado = !estado;
46+
if(estado) {mueve(velocidad);}
47+
else { parar(); }
48+
delay(1000);
49+
}
50+
if (estado) mueve(velocidad);
51+
delay(50);
52+
}
53+
54+
void parar() {analogWrite(ENA,0);}
55+
void mueve(int velocidad)
56+
{
57+
if (velocidad > 100 || velocidad < -100) {return; }
58+
// Ponemos la dirección
59+
if (velocidad < 0)
60+
{
61+
digitalWrite(IN1, LOW);
62+
digitalWrite(IN2, HIGH);
63+
velocidad = velocidad * -1;
64+
}
65+
else
66+
{
67+
digitalWrite(IN1, HIGH);
68+
digitalWrite(IN2, LOW);
69+
}
70+
velocidad = velocidad + MINV;
71+
analogWrite(ENA,velocidad);
72+
}
73+

0 commit comments

Comments
 (0)