Mostrando entradas con la etiqueta arduino. Mostrar todas las entradas
Mostrando entradas con la etiqueta arduino. Mostrar todas las entradas

Controlar motor DC variando velocidad con L298 driver

fuente: https://electronilab.co/tutoriales/tutorial-de-uso-driver-dual-l298n-para-motores-dc-y-paso-a-paso-con-arduino/


Control de un motor DC variando su velocidad

Si queremos controlar la velocidad del motor, tenemos que hacer uso de PWM. Este PWM será aplicado a los pines de activación de cada salida o pines ENA y ENB respectivamente, por tanto los jumper de selección no serán usados.

Esquema de conexión

L298N_1_mot_PWM

Código en Arduino

El programa controla la velocidad de un motor DC aplicando PWM al pin ENB del módulo L298N.

/*
Ejemplo de control de motor DC usando modulo L298
http://electronilab.co/tienda/driver-dual-para-motores-full-bridge-l298n/
Creado 16/05/14
por Andres Cruz
ELECTRONILAB.CO
*/
int IN3 = 5; // Input3 conectada al pin 5
int IN4 = 4; // Input4 conectada al pin 4
int ENB = 3; // ENB conectada al pin 3 de Arduino
void setup()
{
pinMode (ENB, OUTPUT);
pinMode (IN3, OUTPUT);
pinMode (IN4, OUTPUT);
}
void loop()
{
//Preparamos la salida para que el motor gire en un sentido
digitalWrite (IN3, HIGH);
digitalWrite (IN4, LOW);
// Aplicamos PWM al pin ENB, haciendo girar el motor, cada 2 seg aumenta la velocidad
analogWrite(ENB,55);
delay(2000);
analogWrite(ENB,105);
delay(2000);
analogWrite(ENB,255);
delay(2000);
// Apagamos el motor y esperamos 5 seg
analogWrite(ENB,0);
delay(5000);
}

Control_pwm_L298_Electronilab.ino hosted with ❤ by GitHub 

Joystick con arduino Leonardo y teclas de flecha

#include <Button.h>
#include <Keyboard.h>

const int BOTON_UP = 4;
const int BOTON_DOWN = 5;
const int BOTON_LEFT = 2;
const int BOTON_RIGHT = 3;

const int BOTON_A = 10;
const int BOTON_B = 8;
const int BOTON_X = 9;

const int BOTON_COIN = 11;

const int BOTON_1P = 7;
const int BOTON_2P = 6;

Button btnUp = Button(BOTON_UP,PULLUP);
Button btnDown = Button(BOTON_DOWN,PULLUP);
Button btnLeft = Button(BOTON_LEFT,PULLUP);
Button btnRight = Button(BOTON_RIGHT,PULLUP);

Button btnA = Button(BOTON_A, PULLUP);
Button btnB = Button(BOTON_B, PULLUP);
Button btnX = Button(BOTON_X, PULLUP);

Button btnCoin = Button(BOTON_COIN, PULLUP);

Button btn1P = Button(BOTON_1P, PULLUP);
Button btn2P = Button(BOTON_2P, PULLUP);

void setup() {
  Keyboard.begin();
}

void loop() { 
  if (btnUp.isPressed()) { 
    Keyboard.press(KEY_UP_ARROW);
  } else {
    Keyboard.release(KEY_UP_ARROW);   
  }

  if (btnDown.isPressed()) { 
    Keyboard.press(KEY_DOWN_ARROW);
  } else {
    Keyboard.release(KEY_DOWN_ARROW);   
  }

  if (btnLeft.isPressed()) { 
    Keyboard.press(KEY_LEFT_ARROW);
  } else {
    Keyboard.release(KEY_LEFT_ARROW);   
  }

  if (btnRight.isPressed()) { 
    Keyboard.press(KEY_RIGHT_ARROW);
  } else {
    Keyboard.release(KEY_RIGHT_ARROW);   
  }

  if (btnA.isPressed()) { 
    Keyboard.press(97);
  } else {
    Keyboard.release(97);
  } 

  if (btnB.isPressed()) { 
    Keyboard.press(98);
  } else {
    Keyboard.release(98);
  } 

  if (btnX.isPressed()) { 
    Keyboard.press(120);
  } else {
    Keyboard.release(120);
  } 

  if (btnCoin.isPressed()) { 
    Keyboard.press(53);
  } else {
    Keyboard.release(53);
  } 

  if (btn1P.isPressed()) { 
    Keyboard.press(49);
  } else {
    Keyboard.release(49);
  } 

  if (btn2P.isPressed()) { 
    Keyboard.press(50);
  } else {
    Keyboard.release(50);
  } 
}












---------------------------------------------------------------------------------------------------------------
VERSION ANTERIOR


#include <Button.h>
#include <Keyboard.h>

const int BOTON_UP = 4;
const int BOTON_DOWN = 5;
const int BOTON_LEFT = 2;
const int BOTON_RIGHT = 3;

const int BOTON_X = 10;
const int BOTON_Y = 8;
const int BOTON_Z = 9;

const int BOTON_A = 11;
const int BOTON_B = 12;

const int BOTON_1P = 7;
const int BOTON_2P = 6;

Button btnUp = Button(BOTON_UP,PULLUP);
Button btnDown = Button(BOTON_DOWN,PULLUP);
Button btnLeft = Button(BOTON_LEFT,PULLUP);
Button btnRight = Button(BOTON_RIGHT,PULLUP);

Button btnX = Button(BOTON_X, PULLUP);
Button btnY = Button(BOTON_Y, PULLUP);
Button btnZ = Button(BOTON_Z, PULLUP);

Button btnA = Button(BOTON_A, PULLUP);
Button btnB = Button(BOTON_B, PULLUP);

Button btn1P = Button(BOTON_1P, PULLUP);
Button btn2P = Button(BOTON_2P, PULLUP);

void setup() {
  Keyboard.begin();
}

void loop() { 
  if (btnUp.isPressed()) {   
    Keyboard.press(KEY_UP_ARROW);
  } else {
    Keyboard.release(KEY_UP_ARROW);     
  }

  if (btnDown.isPressed()) {   
    Keyboard.press(KEY_DOWN_ARROW);
  } else {
    Keyboard.release(KEY_DOWN_ARROW);     
  }

  if (btnLeft.isPressed()) {   
    Keyboard.press(KEY_LEFT_ARROW);
  } else {
    Keyboard.release(KEY_LEFT_ARROW);     
  }

  if (btnRight.isPressed()) {   
    Keyboard.press(KEY_RIGHT_ARROW);
  } else {
    Keyboard.release(KEY_RIGHT_ARROW);     
  }

/*  if (btnEnter.isPressed()) {   
    Keyboard.press(90);
  } else {
    Keyboard.release(90);
  } 
*/
  if (btnX.isPressed()) {   
    Keyboard.press(65);
  } else {
    Keyboard.release(65);
  } 

  if (btnY.isPressed()) {   
    Keyboard.press(66);
  } else {
    Keyboard.release(66);
  } 

  if (btnZ.isPressed()) {   
    Keyboard.press(88);
  } else {
    Keyboard.release(88);
  } 

  if (btnA.isPressed()) {   
    Keyboard.press(53);
  } else {
    Keyboard.release(53);
  } 

/*
  if (btnB.isPressed()) {   
    Keyboard.press(11);
  } else {
    Keyboard.release(11);
  } 
*/
  if (btn1P.isPressed()) {   
    Keyboard.press(49);
  } else {
    Keyboard.release(49);
  } 

  if (btn2P.isPressed()) {   
    Keyboard.press(50);
  } else {
    Keyboard.release(50);
  } 


}

Como resetear arduino Leonardo

Cuando se le cargo a Arduino Leonardo un programa que impide que se pueda volver a cargar otro sketch (ej, se le tiro un codigo que envia KEY DOWN todo el tiempo e impide utilizar el IDE), la solucion es:

1. Desconectar el usb
2. Mantener presionado el boton rojo de reset
3. Conectar el usb
4. Ir al IDE, no va a aparecer el puerto hasta que se suelte el boton de reset.
5. Soltar y apretar varias veces brevemente el boton de reset hasta que aparezca el puerto en el IDE de Arduino.
6. Compilar el sketch en el IDE (manteniendo el boton rojo presionado)
7. Cuando este haciendo el upload, soltar el boton rojo


A4988 con Arduino



Codigo:

/*     Simple Stepper Motor Control Exaple Code
 *    
 *  by Dejan Nedelkovski, www.HowToMechatronics.com
 *
 */
// defines pins numbers
const int stepPin = 12;   //3;
const int dirPin = 11;    //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(1000);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(1000);
  }
  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 < 200; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(1000);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(1000);
  }
  delay(1000);
}


Fuente: https://chenfuguo.gitbooks.io/arduino/content/Shields/a4988Controller.html
             http://howtomechatronics.com/

Compilar y subir GRBL 0.9 a arduino

Bajar el zip de este lugar:

https://github.com/grbl/grbl

luego descomprimirlo, y armar un .zip que solo tenga la carpeta que contiene los examples, los .h y los .c (este zip es el que luego hay que importar como libreria en Sketch / include library)

Primero hay que borrar la EPROM (ver este post para saber como hacerlo: http://expressraider.blogspot.com.uy/2015/02/arduino-midi-drums-paso-paso.html)

Instrucciones para subir el grbl:

https://github.com/grbl/grbl/wiki/Compiling-Grbl

Luego el grbl controller configurarlo a 115200

Interfaz para Ambiloop looper con Arduino / controlar click, short hold y long hold sin debounce

Interfaz para ambiloop con Arduino

Dos botones. Se detectan click, short hold, long hold, controlando debounce.

Click del boton1: cambia de track
Click del boton2: comienza / detiene grabación en el track actual
Short hold del boton2: borra el track actual
Long hold del boton2: borra todos los tracks, y selecciona el track 1


#define CLICK 1
#define SHORT 2
#define LONG 3
#define IDLE 0

const int buttonPin = 4;    
int buttonState;           
int lastButtonState = LOW; 

const int button2Pin = 5;    
int button2State;           
int lastButton2State = LOW; 

int estadoBoton1;
int estadoBoton2;

long lastDebounceTime = 0;
long debounceDelay = 30;  
long tiempoIni, tiempoFin;
bool boton1ON, short1ON, long1ON;

long lastDebounceTime2 = 0;
long debounceDelay2 = 30;  
long tiempoIni2, tiempoFin2;
bool boton2ON, short2ON, long2ON;

String tracks[8] = {"1", "2", "3", "4", "5", "6", "7", "8"};
int trackActual = 0;

void setup() {
  pinMode(buttonPin, INPUT);
  boton1ON = false;
  short1ON = false;
  long1ON = false;    
  estadoBoton1 = IDLE;
  estadoBoton2 = IDLE;
}

void loop() {
  bool boton1 = ValidarBoton1();
  bool boton2 = ValidarBoton2();
  
  switch (estadoBoton1)
  {
    case CLICK: 
      trackActual++;
      if (trackActual > 8)
        trackActual = 1;
        
      Keyboard.print(tracks[trackActual-1]);
      estadoBoton1 = IDLE;
      break;
  }

  switch (estadoBoton2)
  {
    case CLICK: 
      Keyboard.print("r"); 
      estadoBoton2 = IDLE; 
      break;
    case SHORT: 
      Keyboard.print("e"); 
      estadoBoton2 = IDLE;
      break;
    case LONG: 
      Keyboard.print("1e2e3e4e5e6e7e8e1 ");
      trackActual = 1;
      estadoBoton2 = IDLE;
      break;   
  }  
}

bool ValidarBoton1()
{
  int reading = digitalRead(buttonPin);

  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  } 
  
  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
      buttonState = reading;

      if (buttonState == HIGH) {          
        tiempoIni = millis();

        while (buttonState == HIGH)
        {
          buttonState = digitalRead(buttonPin);
          delay(1);
        }
        tiempoFin = millis();

        if (tiempoFin - tiempoIni > 1500)
          estadoBoton1 = LONG;  
        else if (tiempoFin - tiempoIni > 500)
          estadoBoton1 = SHORT;
        else 
          estadoBoton1 = CLICK;
      }
    }
  }
  
  lastButtonState = reading;
}  

bool ValidarBoton2()
{
  int reading2 = digitalRead(button2Pin);

  if (reading2 != lastButton2State) {
    lastDebounceTime2 = millis();
  } 
  
  if ((millis() - lastDebounceTime2) > debounceDelay2) {
    if (reading2 != button2State) {
      button2State = reading2;

      if (button2State == HIGH) {          
        tiempoIni2 = millis();

        while (button2State == HIGH)
        {
          button2State = digitalRead(button2Pin);
          delay(1);
        }
        tiempoFin2 = millis();
        
        if (tiempoFin2 - tiempoIni2 > 1500)
          estadoBoton2 = LONG;  
        else if (tiempoFin2 - tiempoIni2 > 500)
          estadoBoton2 = SHORT;
        else 
          estadoBoton2 = CLICK;
      }
    }
  }
  
  lastButton2State = reading2;
}