Adafruit Motor Shield code help!

Hey guys, here’s my situation: I’m using this code here to read button presses from my TV remote:

#include <IRremote.h> // use the library

int receiver = 11; // pin 1 of IR receiver to Arduino digital pin 11

IRrecv irrecv(receiver); // create instance of 'irrecv'

decode_results results;

void setup()

{

  Serial.begin(9600); // for serial monitor output

  irrecv.enableIRIn(); // Start the receiver

}

void translateIR() // takes action based on IR code received


{

  switch(results.value)

  {

    case 0x70: Serial.println("Menu/Exit"); break;

    case 0xA90: Serial.println("Power"); break;

    case 0x290: Serial.println("Mute"); break;

    case 0x90: Serial.println("Ch +"); break;

    case 0x890: Serial.println("Ch -"); break;

    case 0xA70: Serial.println("Select/OK"); break;

    case 0xC90: Serial.println("Vol -"); break;

    case 0x490: Serial.println("Vol +"); break;
    
    case 0x710: Serial.println("Guide"); break;

    case 0x2F0: Serial.println("Up");break;
    
    case 0xAF0: Serial.println("Down");break;
    
    case 0x2D0: Serial.println("Left");break;
    
    case 0xCD0: Serial.println("Right"); break;

    case 0x910: Serial.println("0"); break;

    case 0x5D0: Serial.println("Info"); break;

    case 0x10: Serial.println("1"); break;

    case 0x810: Serial.println("2"); break;

    case 0x410: Serial.println("3"); break;

    case 0xC10: Serial.println("4"); break;

    case 0x210: Serial.println("5"); break;
  
    case 0xA10: Serial.println("6"); break;
    
    case 0x610: Serial.println("7"); break;
    
    case 0xE10: Serial.println("8"); break;
    
    case 0x110: Serial.println("9"); break;
    
    case 0xD10: Serial.println("Enter"); break;
    
    case 0x9B0: Serial.println("Page Dwn"); break;
    
    case 0x1B0: Serial.println("Page Up"); break;
    
    case 0xA50: Serial.println("Input"); break;
    
    case 0xDD0: Serial.println("-/Prev.Ch"); break;
    
    case 0x425: Serial.println("CC"); break;
    
    case 0xCB0: Serial.println("Play"); break;
    
    case 0x5B0: Serial.println("Record"); break;
    
    case 0x2B0: Serial.println("Pause"); break;
    
    case 0xAB0: Serial.println("Stop"); break;
    
    case 0xEB0: Serial.println("Skip Back"); break;
    
    case 0x6B0: Serial.println("Skip Forward"); break;
    
    case 0xFFFFFFFF: break;

    
    default:  Serial.print("Other button:"); Serial.println(results.value,HEX);

  }

  delay(100);

  //lcd.clear();

}

void loop()

{

  if (irrecv.decode(&results)) // have we received an IR signal?

  {

    translateIR();

    for (int z=0; z<2; z++) // ignore 2nd and 3rd signal repeat

    {

      irrecv.resume(); // receive the next value

    }

  }

}

Now, with this code in mind, how can I use my TV remote, along with the Adafruit Motor Shield and the AFmotor library, to control these things:

  1. The ON/OFF status of a motor.

  2. The speed of said motor.

  3. The direction of the motor.

I’m really new to all this and can’t program to save my life. So, I knew to come here for help.

If I didn’t explain anything well enough for you to understand, please ask me to explain!! Thanks!!

Anyone? I hope I explained everything well enough.... If not, ask me!

switch(results.value)

{

case 0x70: Serial.println("Menu/Exit"); digitalWrite(13, HIGH); // here you can put on or off your motor break;

Got it???

I don't think you understood... I know how to do that, but how do I change the speed of said motor? It seems to be pre-defined in the setup using motor.setSpeed(speed).

So you want to know how to use the motor shield?

See these pages: http://www.ladyada.net/make/mshield/ http://www.ladyada.net/make/mshield/use.html

Create a motor class for every motor you have, and use:

motor.setSpeed(128);        // 0...255
moror.run(FORWARD);      // FORWARD, BACKWARD, RELEASE

There are also cheap clones of the Adafruit motorshield on Ebay.

Could I have, say, motor.setSpeed(val); and increment Val every time I press a button?

anyone? please help!

Why not try it and see.

I suggest to use the AdaFruit library, and don't switch the output pins directly. The AdaFruit library is complicated, and it uses it's own code to create PWM, but you don't have to look inside the library, just use it.

You could do something like this:

int direction = FORWARD;
int speed = 0;

........

    case 0xC90: 
      Serial.println("Vol -");
      speed -= 20;
      motor.setSpeed (speed);
      break;

    case 0x490: 
      Serial.println("Vol +");
      speed += 20;
      motor.setSpeed (speed);
      break;
    
    case 0x910: 
      Serial.println("0"); 
      speed = 0;
      motor.setSpeed(speed);
      break;

    case 0x10: 
      Serial.println("1"); 
      speed = 25;
      motor.setSpeed(speed);
      break;

    case 0x810: 
      Serial.println("2"); 
      speed = 50;
      motor.setSpeed(speed);
      break;

    case 0x9B0: 
      Serial.println("Page Dwn"); 
      direction = BACKWARD;
      motor.run(direction);
      break;
    
    case 0x1B0: 
      Serial.println("Page Up");
      direction = FORWARD;
      motor.run(direction);
      break;

I Think that the code will be easier if you keep the switch statement to send the Serial.println messaged, but to control the motor after the switch statement with a lot of 'if' statements. So you can test for numer 0...9, and set the motor speed.

dxw00d: Why not try it and see.

The thing is that I don't have the shield yet. But it's going to arrive soon.

Krodal: I suggest to use the AdaFruit library, and don't switch the output pins directly. The AdaFruit library is complicated, and it uses it's own code to create PWM, but you don't have to look inside the library, just use it.

You could do something like this:

int direction = FORWARD;
int speed = 0;

........

    case 0xC90:       Serial.println("Vol -");       speed -= 20;       motor.setSpeed (speed);       break;

    case 0x490:       Serial.println("Vol +");       speed += 20;       motor.setSpeed (speed);       break;         case 0x910:       Serial.println("0");       speed = 0;       motor.setSpeed(speed);       break;

    case 0x10:       Serial.println("1");       speed = 25;       motor.setSpeed(speed);       break;

    case 0x810:       Serial.println("2");       speed = 50;       motor.setSpeed(speed);       break;

    case 0x9B0:       Serial.println("Page Dwn");       direction = BACKWARD;       motor.run(direction);       break;         case 0x1B0:       Serial.println("Page Up");       direction = FORWARD;       motor.run(direction);       break;




I Think that the code will be easier if you keep the switch statement to send the Serial.println messaged, but to control the motor after the switch statement with a lot of 'if' statements. So you can test for numer 0...9, and set the motor speed.

This helped so much! However, what would it look like if I had multiple motors? Can I change just the speed of one individual motor?

Sure, that's what the motorshield is for!

You have to create multiple classes during initialization: http://www.forums.adafruit.com/viewtopic.php?f=31&t=16928

There's one more thing to know about the motorshield: You can piggy-back extra L293D on top of the others to increase the maximum current: http://blog.tinyenormous.com/2010/06/23/how-to-piggyback-driver-chips-on-ladyadas-arduino-motor-shield/

I'm only going to be using about 10V, will I need to piggyback the chips? And what does the "motor1.run(RELEASE);" line do? Does it basically initialize the motor?

The RELEASE command will release the voltages, and let the motor come to a stop by itself. The BRAKE command is not yet implemented by the AdaFruit library, so use the RELEASE for a stop. Since the L293D uses transistors instead of mosfets, it can't do a nice 'brake' to stop the motor anyway.

10V is no problem. The L293D is limited for the current. It can only do 600mA continuously. You could measure the current of the motor, and block the motor to see it the current increases.

Once you have it running, even a motor of 200mA will heat up the L293D driver, that's normal.

So, to 'BRAKE' it, I could make it go backwards for just about 10 microseconds? Something like that?

See, here's what I'm doing: I have an old RC car that I took everything out of except the motors. There are two motors and a 9.6V, 800mAh battery pack. This raises the question: Does each motor use 400mA out of the 800mA? Or is that not how it works?

I read that some people reverse the direction for a short time to make a quick brake. But I have not tried that myself. About 10 microseconds? I would start with 500 milliseconds.

The battery pack doesn't have anything to do with the current through the motors. You have to measure the current through the motors.

Okay. And another thing: How can I make a good kill switch? This is a pretty fast RC car, and my little TV remote doesn't have an extraordinary range on it. Once it gets out of range, I need it to stop. Any ideas on how I could do that? Is there another way to control it wirelessly, but with a further range??

Bump. Any help?