Advice on Adafruit motor shield for DC motor control with touchosc

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
cheesesteak22
 
Posts: 3
Joined: Sat Sep 15, 2012 12:00 am

Advice on Adafruit motor shield for DC motor control with touchosc

Post by cheesesteak22 »

I am a beginner with arduino, don;t know much about programming etc..

I have a very specific project I want to complete - I have a CD drive with 3 DC motors - one controls the tray, one controls the laser/slide, and one controls the cd spinner. I was surprised that the motors were all DC as they are usually steppers in my experience, but I digress. I currently have an arduino Uno, adafruit motor shield and touchosc on my iPhone. I am looking for a good tutorial on how I can use my iPhone to control the direction of the motors with each at a set speed. Eventually I would like to reassemble this cd drive into a mutant pan and tilt-like device, and while some servos would be much easier for this - this is what i'm working with for now.

I have been looking everywhere for a tutorial on how to do this or something similar, but I cant even get to the point where the motors do anything but a loop. If someone has some advice on where I can learn how to control the direction of these motors (I am using the AFMotor library, fyi), I would really appreciate it. I think I have the touchOS program down, but advice on integrating that would be appreciated as well. Thanks!

User avatar
adafruit_support_bill
 
Posts: 88090
Joined: Sat Feb 07, 2009 10:11 am

Re: Advice on Adafruit motor shield for DC motor control with touchosc

Post by adafruit_support_bill »

I'm not familiar with TouchOS, but can help you with the motor-shield side. How are you communicating the control signals from your phone to the Arduino?

cheesesteak22
 
Posts: 3
Joined: Sat Sep 15, 2012 12:00 am

Re: Advice on Adafruit motor shield for DC motor control with touchosc

Post by cheesesteak22 »

I can now control the motors via the serial monitor thanks to an example sketch I found in another post, but it still is not syncing with the code from the Processing application that interfaces with the touchosc app. I really just need a control interface for the motors that is not command line based...if there is a way to do this without the touchosc app, for example from a web terminal with push buttons or sliders, I would greatly appreciate any suggestions.

Ideally the motors would spin for a length of time that is set by user input (via a knob or slider), then automatically stop. I know a servo would be much better for this, but I imagine there must be a way to do this with the DC motors.

Thanks!

Arduino code:

Code: Select all

#include <AFMotor.h>


AF_DCMotor motor(1, MOTOR12_1KHZ); 
AF_DCMotor motor2(2, MOTOR12_1KHZ);

int input;


void setup() {
  Serial.begin(9600);           
  Serial.println("FridgeBUDDY");
  motor.setSpeed(0.04);
  motor.run(RELEASE);
       
}
int speedval;
void loop()
{
  if (Serial.available()){
    input = Serial.read();
    switch (input){
case 'W':
  
            motor.run(FORWARD);
            motor.setSpeed(4);
            delay(100);
            motor.run(RELEASE);
            motor.setSpeed(0);
        
case 'speedval':
  
            motor.run(BACKWARD);
            for (speedval=0; speedval<255; speedval += 5)
           {
            motor.setSpeed(speedval);  
            delay(3);
            }
            break;

case '0':
           motor.run(RELEASE);
         
           motor.setSpeed(0);
       
          
          
case 'D':
  
            motor2.run(FORWARD);
            for (speedval=0; speedval<255; speedval += 5)
           {
            motor2.setSpeed(speedval);  
            delay(3);
            }
            break;
case 'A':
  
            motor2.run(BACKWARD);
            for (speedval=0; speedval<255; speedval += 5)
           {
            motor2.setSpeed(speedval);  
            delay(3);
            }
            break;
case '-':

          motor2.run(RELEASE);
          motor2.setSpeed(0);
break;
 

  }}}
Processing code:

Code: Select all

import oscP5.*;
import netP5.*;
import processing.serial.*;
Serial arduinoPort;
OscP5 oscP5;

float [] fader = new float [3];

void setup() {
  oscP5 = new OscP5(this,8000);
  arduinoPort = new Serial(this, Serial.list()[0], 9600);
}

void oscEvent(OscMessage theOscMessage) {

    String addr = theOscMessage.addrPattern();
   
       if(addr.indexOf("/1/fader") !=-1){
       String list[] = split(addr,'/');
     int  xfader = int(list[2].charAt(5) - 0x30);
     if(theOscMessage.get(0).floatValue() !=0){
     fader[xfader]  = theOscMessage.get(0).floatValue();
     }  
    }
}

void draw() {
//---------------------------------Motor A
 if(fader[1] > 0.65){
    arduinoPort.write("W");
  }
   if(fader[1] < 0.35){
    arduinoPort.write("S");
  }
  //--------------------------------Motor B
   if(fader[2] > 0.65){
    arduinoPort.write("A");
  }
   if(fader[2] < 0.35){
    arduinoPort.write("D");
  }
  //----------------------------stop commands
     if(fader[1] < 0.65 && fader[1] > 0.35 ){
    arduinoPort.write("0");
  }
       if(fader[2] < 0.65 && fader[2] > 0.35 ){
    arduinoPort.write("-");
  }
 
}

User avatar
adafruit_support_bill
 
Posts: 88090
Joined: Sat Feb 07, 2009 10:11 am

Re: Advice on Adafruit motor shield for DC motor control with touchosc

Post by adafruit_support_bill »

I can now control the motors via the serial monitor
Any program that sends those same characters over the serial port will work. There are hundreds of ways to do this. If you already have Processing set up on your machine, that might be the easiest way to go.
Ideally the motors would spin for a length of time that is set by user input (via a knob or slider), then automatically stop.
You won't get much precision or repeatability doing this without some sort or feedback. At a minimum, you might want to consider some limit switches so you don't damage the mechanism by driving it too far in one direction.

cheesesteak22
 
Posts: 3
Joined: Sat Sep 15, 2012 12:00 am

Re: Advice on Adafruit motor shield for DC motor control with touchosc

Post by cheesesteak22 »

I have a limit switch that I could put on. The only issue at the moment is that I can not get processing to communicate with the arduino- I know the link between the iphone and processing works, but for some reason the command sent from Processing does not make the motors move. I think this has something to do with the code, and as I said before, I know almost nothing about code.

I would be satisfied if I could set it up so that each time I send a command the motor runs in ____ direction for _____ ms. I have seen examples online of using the arduino directly to power the motors with an H bridge, but with my adafruit motor shield I know there must be a way to write the code using AFMotor instead.

Is there any way you could look at the code I posted above and see if there are any obvious reasons why they aren't communicating?

Also, could you kindly give me an example of code that I could insert in the arduino program that have the motor run for ___ ms and then stop every time I send the command?

Thanks!

User avatar
adafruit_support_bill
 
Posts: 88090
Joined: Sat Feb 07, 2009 10:11 am

Re: Advice on Adafruit motor shield for DC motor control with touchosc

Post by adafruit_support_bill »

Is there any way you could look at the code I posted above and see if there are any obvious reasons why they aren't communicating?
One possibility is that it is not opening the correct COM port. You have to figure out which COM port the Arduino is on and open that one.
Also, could you kindly give me an example of code that I could insert in the arduino program that have the motor run for ___ ms and then stop every time I send the command?
The 'W" case in the code you posted before was pretty close. Although most small DC motors don't run well at such slow speeds. You also need to insert a 'break;' statement after each case, or they will just all run together.

Try something like:

Code: Select all

case 'W':
            motor.setSpeed(128);
            motor.run(FORWARD);
            delay(100);
            motor.run(RELEASE);
            break;

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”