Go Down

Topic: Leonardo + MIDI + Motor Shield - failed test - (Read 322 times) previous topic - next topic

Sdreoos

Hello everybody, I found this code on the web, it should be working using latest Adafruit Motor shield V2 (I2c based) on Arduino Leonardo (Teensy emulation) TeenOnArdu.
The code should be move the motors back\forward following the MIDI control change, but it doesn't. Everything seems working, no errors wihile loading the code, Leonardo is recognized by the DAW as MIDI device but when I try to send the first midi message, the MOTOR 1 start to move slowly in one direction and never stops. Is there anybody who can check this on its devices?

Thank you very much.

Code: [Select]
#include <Adafruit_MotorShield.h>

/*
 For 2 stepper motots on one motor shield controlled via midi controlers 60 and 61 where
       127 -> maximum speed
       65  -> minimum speed
       64  -> released motor
       63  -> minimum reverse speed
       0   -> maximum reverse speed
 
 For use with the Adafruit Motor Shield v2 http://www.adafruit.com/products/1438
 
 Code and documentation:
 http://liveelectronics.musinou.net/DCMotorsSerialControl.php

 ####################################################
 # 2016 by http://liveelectronics.musinou.net/ #
 ####################################################
 # Initially developed for Sentier Sonore http://musinou.net/menu/Installation/Sound-Installation/Sentier-sonore
 # Initial development during a residency at Chambre Blanche, Qu├ębec City http://www.chambreblanche.qc.ca/EN/actu.asp?cleLangue=1&cleActualiteType=2&cleActualite=1271613889
 #
 #
 */

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"

Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x60); //default adress of the shield
Adafruit_StepperMotor *stepper_1 = AFMS.getStepper(200, 1);
Adafruit_StepperMotor *stepper_2 = AFMS.getStepper(200, 2);

//led
int ledR = 13;
int ledL = 12;

// used for the steppers
int t_1 = 0;
int t_2 = 0;
int d_1 = 0;
int d_2 = 0;

//time management
int maxTimeBetweenSteps = 100; //number of ms maximun between steps
long previousMilli_for_t_1 = 0;
long previousMilli_for_t_2 = 0;


void setup() {
  AFMS.begin();

  pinMode(ledL, OUTPUT);
  pinMode(ledR, OUTPUT);

  usbMIDI.setHandleControlChange(OnControlChange);
}

void loop() {
  usbMIDI.read();
  unsigned long currentMillis = millis();

  if(currentMillis >= (previousMilli_for_t_1 + t_1))
  {
    previousMilli_for_t_1 = currentMillis;

    if(d_1 > 0)
    {
      stepper_1->onestep(FORWARD, DOUBLE);
      digitalWrite(ledL, LOW);
    }
    else if (d_1 < 0 )
    {
      stepper_1->onestep(BACKWARD, DOUBLE);
      digitalWrite(ledL, LOW);
    }
    else
    {
      stepper_1->release();
      digitalWrite(ledL, HIGH);
    }
  }


  if(currentMillis >= (previousMilli_for_t_2 + t_2))
  {
    previousMilli_for_t_2 = currentMillis;

    if(d_2 > 0)
    {
      stepper_2->onestep(FORWARD, DOUBLE);
      digitalWrite(ledR, LOW);
    }
    else if (d_2 < 0 )
    {
      stepper_2->onestep(BACKWARD, DOUBLE);
      digitalWrite(ledR, LOW);
    }
    else
    {
      stepper_2->release();
      digitalWrite(ledR, HIGH);
    }
  }
}

void OnControlChange(byte channel, byte value, byte control) {
 
  switch(control)
  {
  case 60:
    {
      if (value > 64)
      {
        t_1 =  map(value, 65, 127, maxTimeBetweenSteps, 0);
        d_1 = 1;
      }
      else if (value < 64)
      {
        t_1 =  map(value, 63, 0, maxTimeBetweenSteps, 0);
        d_1 = -1;
      }
      else
      {
        d_1 =0;
        t_1 =0;
      }

      break;
    }
  case 61:
    {
      if (value > 64)
      {
        t_2 =  map(value, 65, 127, maxTimeBetweenSteps, 0);
        d_2 = 1;
      }
      else if (value < 64)
      {
        t_2 =  map(value, 63, 0, maxTimeBetweenSteps, 0);
        d_2 = -1;
      }
      else
      {
        d_2 =0;
        t_2 =0;
      }

      break;
    }

  }
}

Go Up