apuntes_Arduino/motores/README.md

623 lines
12 KiB
Markdown
Raw Permalink Normal View History

## Motores
- [DC](#dc)
- [9v](#bateria-9v)
2022-04-04 17:41:38 -04:00
- [PWM motor](#pwm-motor)
- [L298N y Joystick](#l298n-y-Joystick)
- [Puente H](#puente-h)
- [Puente H ISR](#puente-h-isr)
- [Servo](#servo)
- [Servo Joystick](#servo-joystick)
- [Servo Led](#servlo-led)
- [Servo Termo](#servo-termo)
- [Servos y potenciometros](#servos-y-potenciometros)
- [Brushless](#brushless)
- [Serial ESC](#serial-esc.ino)
- [Paso a paso](#paso-a-paso)
- [Control PaP](#control-pap)
2022-04-04 17:54:47 -04:00
- [Ajuste velocidad PaP](#ajuste-velocidad-pap)
- [PaP](#pap)
- [PaP 5v](#pap-5v)
- [Stepper KeyStudio](#stepper-keystudio)
----
## DC
### Bateria 9v
```c
int MOTOR=3;
int i;
byte velocidad = 1;
int pausa = 12000;
void setup(){
pinMode(MOTOR, OUTPUT);
//Serial.begin(115200);
analogWrite(MOTOR, 0);
}
void loop(){
/*
for(i=150; i<=255; i++){
analogWrite(MOTOR, i);
//Serial.print(" "+String(i));
delay(velocidad);
}
//Serial.println("");
delay(pausa);
for(i=255; i>=150; i--){
analogWrite(MOTOR, i);
//Serial.print(" "+String(i));
delay(velocidad);
}
//Serial.println("");
*/
analogWrite(MOTOR, 215);
delay(pausa);
analogWrite(MOTOR, 0);
delay(5000);
analogWrite(MOTOR, 235);
delay(pausa);
analogWrite(MOTOR, 0);
delay(5000);
analogWrite(MOTOR, 255);
delay(pausa);
/*
analogWrite(MOTOR, 255);
delay(pausa);
analogWrite(MOTOR, 0);
delay(velocidad);
*/
}
```
2022-04-04 17:41:38 -04:00
### PWM Motor
```c
#define fan_1 11
#define fan_2 10
int veloc = 5;
void setup() {
Serial.begin(115200);
pinMode(fan_1,OUTPUT);
pinMode(fan_2,OUTPUT);
}
void loop() {
analogWrite(fan_1, 100);
delay(2000);
analogWrite(fan_1, 100);
delay(5000);
analogWrite(fan_1, 255);
delay(10000);
}
```
### L298N y Joystick
```c
#define motor1 13
#define motor2 12
#define EnA 11
#define JoyX A0
#define JoyY A1
//boolean sentido = true;
int espera = 500;
void setup() {
pinMode(motor1, OUTPUT);
pinMode(motor2, OUTPUT);
pinMode(EnA, OUTPUT);
}
//Valor JoyX a la Izq de 524 a 0
//Valor JyY a la Der de 526 a 1023
void giro() {
int ejeX = analogRead(JoyX);
if(ejeX < 525) {
digitalWrite(motor2, LOW);
digitalWrite(motor1, HIGH);
analogWrite(EnA,map(ejeX,524,0,0,255));
} else if(ejeX > 525) {
digitalWrite(motor1, LOW);
digitalWrite(motor2, HIGH);
analogWrite(EnA,map(ejeX,526,1023,0,255));
} else {
detener();
}
}
void detener() {
digitalWrite(motor1, LOW);
digitalWrite(motor2, LOW);
}
void loop() {
giro();
}
```
### Puente H
```c
int motor1 = 6;
int motor2 = 7;
boolean sentido = true;
int espera = 500;
void setup() {
pinMode(motor1, OUTPUT);
pinMode(motor2, OUTPUT);
}
void giro() {
if(sentido) {
digitalWrite(motor2, LOW);
digitalWrite(motor1, HIGH);
} else if(!sentido) {
digitalWrite(motor1, LOW);
digitalWrite(motor2, HIGH);
} else {
digitalWrite(motor1, LOW);
digitalWrite(motor2, LOW);
}
}
void detener() {
digitalWrite(motor1, LOW);
digitalWrite(motor2, LOW);
}
void loop() {
detener();
delay(espera);
sentido = true;
giro();
delay(5000);
detener();
delay(espera);
sentido = false;
giro();
delay(5000);
}
```
### Puente H ISR
```c
int motor1 = 6;
int motor2 = 7;
int cambio = 2; // Pin ligado a interrupcion 0
volatile boolean sentido = true;
int espera = 500;
void setup() {
//pinMode(cambio, INPUT);
pinMode(motor1, OUTPUT);
pinMode(motor2, OUTPUT);
attachInterrupt(0, cambioGiro, RISING);
}
void giro() {
if(sentido) {
digitalWrite(motor2, LOW);
digitalWrite(motor1, HIGH);
} else if(!sentido) {
digitalWrite(motor1, LOW);
digitalWrite(motor2, HIGH);
} else {
digitalWrite(motor1, LOW);
digitalWrite(motor2, LOW);
}
}
void cambioGiro() {
digitalWrite(motor1, LOW);
digitalWrite(motor2, LOW);
sentido = !sentido;
}
void detener() {
digitalWrite(motor1, LOW);
digitalWrite(motor2, LOW);
}
void loop() {
detener();
delay(espera);
giro();
delay(5000);
}
```
-----
## Servo
### Servo Joystick
```c
#include <Servo.h>
Servo myservo_X;
void setup() {
myservo_X.attach(9);
Serial.begin(115200);
}
void loop() {
/*
for (byte j=20; j<140; j++){
myservo_X.write(j);
Serial.print("up j=");
Serial.println(j);
delay(25);
}
for (byte i=140; i>20; i--) {
myservo_X.write(i);
Serial.print("down i= ");
Serial.println(i);
delay(25);
}
*/
myservo_X.write(115);
delay(1000);
myservo_X.write(45);
delay(1000);
}
```
### Servo Led
```c
#include <Servo.h>
#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define LEDPin 13 // LED pin
boolean algo; // true si hay algo en rango
int i, j;
int maximumRange = 50; // Distancia maxima
int minimumRange = 10; // Distancia minima
long duration, distance; // Duración utílizada para calcular la distancia
Servo myservo; // instancia objero Servo
void setup() {
Serial.begin (9600);
myservo.attach(9); // pin asociado al objeto Servo
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDPin, OUTPUT);
}
void loop() {
sensar();
accion();
if(!algo) {
while(i<171) {
for (i=j; i<=170; i++) { // giro sentido antihorario
myservo.write(i); // mueve el servo a la posición indicada
sensar();
accion();
j = i;
if(algo) break;
}
}
}
while(i>10) {
if(!algo) {
for (j=i; j>=10; j--) { // giro sentido horario
myservo.write(j); // mueve el servo a la posición indicada
sensar();
accion();
i=j;
if(algo) break;
}
}
sensar();
accion();
}
}
void accion() {
if (distance >= maximumRange || distance <= minimumRange){
Serial.println("Nada por aquí");
digitalWrite(LEDPin, LOW);
algo = false;
} else {
algo = true;
Serial.print("Objeto a: ");
Serial.print(distance);
Serial.println(" cm.");
digitalWrite(LEDPin, HIGH);
}
}
void sensar() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // demora del eco, para cálculo de distancia
distance = duration/58.2;
}
```
### Servo Termo
```c
#include <Servo.h>
Servo myservo_X;
bool estado_termo;
byte medio;
void setup() {
myservo_X.attach(9);
Serial.begin(9600);
}
void loop() {
/* Movimiento Servo, accion On (120) - Off (45)
* mitad real (75?)
* actuar a 3 etapas por lado
myservo_X.write(120);
delay(1000);
Serial.println("ON");
myservo_X.write(45);
delay(1000);
Serial.println("OFF");
*/
termo(estado_termo);
}
void termo(bool estado) {
if (!estado) {
Serial.println("ON");
myservo_X.write(120);
delay(500);
myservo_X.write(110);
delay(500);
myservo_X.write(120);
delay(500);
myservo_X.write(110);
delay(500);
myservo_X.write(120);
delay(2000);
estado_termo = !estado_termo;
} else {
Serial.println("OFF");
myservo_X.write(45);
delay(500);
myservo_X.write(62);
delay(500);
myservo_X.write(45);
delay(500);
myservo_X.write(62);
delay(500);
myservo_X.write(45);
delay(2000);
estado_termo = !estado_termo;
}
}
```
### Servos y potenciometros
```c
#include <Servo.h>
#define srv1 3
#define srv2 4
#define potX A0
#define potY A1
Servo srX;
Servo srY;
void setup() {
srX.attach(srv1);
srY.attach(srv2);
}
void loop() {
srX.write(map(analogRead(potX), 0, 1023, 0, 180));
delay(50);
srY.write(map(analogRead(potY), 0, 1023, 0, 180));
delay(50);
}
```
----
## Brushless
### Serial ESC
```c
#include <Servo.h>
Servo Esc;
#define pinEsc 9
int pwm_esc = 0;
void setup() {
Serial.begin(9600);
Esc.attach(pinEsc);
Esc.writeMicroseconds(1000);
Serial.println("iniciando");
delay(2000);
}
void loop() {
while(Serial.available() > 0) {
pwm_esc = Serial.parseInt();
if(Serial.read()=='\n') {
Esc.writeMicroseconds(pwm_esc);
Serial.print("Valor ESC: ");
Serial.println(pwm_esc);
}
delay(20);
}
}
//SOLO Nueva Linea /n
//Caviar min=1150/max=1900
//Para disco duro test1
//min 1620
//max 1900
```
----
## Paso a Paso
### Control PaP
```c
// defines pins numbers
const int stepPin = 3;
const int dirPin = 4;
void setup() {
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
}
void loop() {
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < 200; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(1000); // One second delay
digitalWrite(dirPin,LOW); //Changes the rotations direction
// Makes 400 pulses for making two full cycle rotation
for(int x = 0; x < 400; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(1000);
}
```
### Ajuste velocidad PaP
```c
// Defines pins numbers
const int stepPin = 3;
const int dirPin = 4;
int customDelay,customDelayMapped; // Defines variables
void setup() {
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
digitalWrite(dirPin,HIGH); //Enables the motor to move in a particular direction
}
void loop() {
customDelayMapped = speedUp(); // Gets custom delay values from the custom speedUp function
// Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
digitalWrite(stepPin, HIGH);
delayMicroseconds(customDelayMapped);
digitalWrite(stepPin, LOW);
delayMicroseconds(customDelayMapped);
}
// Function for reading the Potentiometer
int speedUp() {
int customDelay = analogRead(A0); // Reads the potentiometer
// Convert read values of pots from 0 to 1023 into
// desireded delay values (300 to 4000)
int newCustom = map(customDelay, 0, 1023, 300,4000);
return newCustom;
}
```
### PaP
Simple Stepper Motor Control Exaple Code
```c
// defines pins numbers
const int stepPin = 3;
const int dirPin = 4;
void setup() {
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
}
void loop() {
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < 200; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(1000); // One second delay
digitalWrite(dirPin,LOW); //Changes the rotations direction
// Makes 400 pulses for making two full cycle rotation
for(int x = 0; x < 400; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(1000);
}
```
### PaP 5v
```c
#include <Stepper.h>
#define STEPS 100
Stepper stepper(STEPS, 8, 9, 10, 11);
int previous = 1;
void setup() {
stepper.setSpeed(90);
}
void loop() {
int val = analogRead(100);
stepper.step(val - previous);
previous = val;
delay(200);
}
```
### Stepper KeyStudio
```c
#include <Stepper.h>
#define STEPS 100
Stepper stepper(STEPS, 8, 9, 10, 11);
int previous = 0;
void setup() {
stepper.setSpeed(120);
Serial.begin(9600);
}
void loop() {
int val = analogRead(0);
Serial.println(val);
stepper.step(val - previous);
previous = val;
}
```