# MOTORES

# Motores paso a paso

Biblioteca stepper (opens new window)

# Biblioteca stepper

# 28BYJ-48 Motor + ULN2003 Driver

#include <Stepper.h>

#define IN1 19 // Pines driver
#define IN2 18
#define IN3 5
#define IN4 17

#define PASOS 2048 // Pasos - revoluciones
#define NUMPASOS 100
int velocidad = 5;
Stepper myStepper(PASOS, IN1, IN3, IN2, IN4);

# void setup()

myStepper.setSpeed(velocidad);

# void loop()

  // Movemos el motor un número determinado de pasos
  stepper.step(NUMPASOS);
  delay(2000);

# Motores DC L298N

// Motor A
int motor1Pin1 = 27; 
int motor1Pin2 = 26; 
int enable1Pin = 14; 

// Setting PWM properties
const int freq = 30000;
const int pwmChannel = 0;
const int resolution = 8;
int dutyCycle = 200;

void setup() {
  // sets the pins as outputs:
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(enable1Pin, OUTPUT);
  
  // configure LED PWM functionalitites
  ledcSetup(pwmChannel, freq, resolution);
  
  // attach the channel to the GPIO to be controlled
  ledcAttachPin(enable1Pin, pwmChannel);

  Serial.begin(115200);

  // testing
  Serial.print("Testing DC Motor...");
}

void loop() {
  // Move the DC motor forward at maximum speed
  Serial.println("Moving Forward");
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH); 
  delay(2000);

  // Stop the DC motor
  Serial.println("Motor stopped");
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  delay(1000);

  // Move DC motor backwards at maximum speed
  Serial.println("Moving Backwards");
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW); 
  delay(2000);

  // Stop the DC motor
  Serial.println("Motor stopped");
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  delay(1000);

  // Move DC motor forward with increasing speed
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW);
  while (dutyCycle <= 255){
    ledcWrite(pwmChannel, dutyCycle);   
    Serial.print("Forward with duty cycle: ");
    Serial.println(dutyCycle);
    dutyCycle = dutyCycle + 5;
    delay(500);
  }
  dutyCycle = 200;
}

# Biblioteca servo.h

# Servo

# Importar biblioteca, pin, instancia y variable

#include <Servo.h>

#define SERVOPIN 13

Servo mi_servo;  // Instancia, 12 objetos como máximo

int posicion = 0;    // Posición inicial

# void setup()

  mi_servo.attach(SERVOPIN));

# void loop()

  for (posicion = 0; posicion <= 180; posicion += 1) { // De 0 a 180 grados, un paso = un grado
    mi_servo.write(posicion);
    delay(15);
  }
  for (posicion = 180; posicion >= 0; posicion -= 1) { //  De  180 a 0 grados
    myservo.write(posicion); 
    delay(15);                       
  }