Pages: [1]   Go Down
Author Topic: Arduino UNO R3 and Motor Shield R3  (Read 3129 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, Have an Arduino UNO R3 and a Motor Shield R3.  Need to control 2 DC motors (direction and speed) using an IR Remote Control.  I have been able to control speed and direction of each motor with out the IR Remote Control, But, When I add the IR code I am only able to get Speed fast (HIGH) .  Will appreciate any suggestions, especially Code examples.

Thanks
Mike
Logged

0
Offline Offline
Shannon Member
****
Karma: 200
Posts: 11718
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

First get the IR code working without the motors (say driving LEDs), so you are sure that's working.

If there are still problems perhaps its a hardware issue (are the motor and logic power supplies separate?)
Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, I think that that is what I have done.  I have three sketches, one IR that works fine and one motor shield that also works fine (direction, and speed of both motors and all combos of them.  But when I add Motor shield stuff to the IR I loose the speed control.  I'll try to copy my code in here!
Code:
/*
 * IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv
 * An IR detector/demodulator must be connected to the input RECV_PIN.
 * Version 0.1 July, 2009
 * Copyright 2009 Ken Shirriff
 8 Modified January 2012 by MLempJr. to add the control of the two
 * motors.  Initially just a few of the 14 keys available on my \
 * remote.
 * http://arcfn.com
 */
#include <IRremote.h>
int RECV_PIN = A2;
long myValue = 0;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
  Serial.begin(9600);
  pinMode(A2, INPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);  // A Brake
  pinMode(8, OUTPUT);  // B Brake
  pinMode(13, OUTPUT);  // B dir
  pinMode(11, OUTPUT);  // B Speed
  pinMode(12, OUTPUT);  // A dir
  pinMode(3, OUTPUT);   // A Speed
 
 
  irrecv.enableIRIn(); // Start the receiver

}
  int get = 250;
  int motorFoward() {
  analogWrite(13,255);   // B forward
  analogWrite(11, get);  // B Speed
  // analogWrite(11, 192);  // B Speed
  analogWrite(12, 0);    // A forward
  // analogWrite(3, 192);   // A Speed
  analogWrite(3, get);   // A Speed
  Serial.println(get);
  Serial.println("run foreward");
  // delay(350);
}
int motorBackward() {
  analogWrite(13, 0);    // B Reverse
  analogWrite(11, get);  // B Speed
  analogWrite(12, 128);  // A Reverse
  analogWrite(3, get);   // A Speed
  Serial.println(get);
  Serial.println("go backward");
  // delay(350);
}
void loop() {
  if (irrecv.decode(&results)) {

      myValue = results.value;
      long r1c1 = 16738455;
      long r1c2 = 16754775;
      long r1c3 = 16722135;
      long r2c1 = 16740495;
      long r2c2 = 16756815;
      long r2c3 = 16724175;
      long r3c1 = 16732335;           
      long r3c2 = 16748655;
      long r3c3 = 16716015;
      long r4c1 = 16736415;     
      long r4c2 = 16752735;
      long r4c3 = 16720095;
      long r5c1 = 16744575;     
      long r5c3 = 16711935;       
         
  Serial.println(myValue);
     if (myValue == r1c1)
  { 
    Serial.println(" r1 c1");
    analogWrite(10, 1);
  }
    if (myValue == r1c2)
  { 
    Serial.println(" r1 c2");
      analogWrite(9, 0);
      analogWrite(8, 0);     
      motorFoward();
      // delay(350);
  }
    if (myValue == r1c3)
  { 
    Serial.println(" r1 c3");
        analogWrite(10, 0);
  }
     if (myValue == r2c1)
  { 
    Serial.println(" r2 c1");
      analogWrite(9, 255);
   
  }
    if (myValue == r2c2)
  { 
    Serial.println(" r2 c2");
  }
    if (myValue == r2c3)
  { 
    Serial.println(" r2 c3");
        Serial.println(" r2 c1");
      analogWrite(8, 255);
  }
    if (myValue == r3c1)
  { 
    Serial.println(" r3 c1");
        get = get + 5;
    Serial.println(get);
  }
    if (myValue == r3c2)

  { 
    Serial.println(" r3 c2");
         analogWrite(8, 0);
         analogWrite(9, 0);
          motorBackward();

  }
    if (myValue == r3c3)
  { 
    Serial.println(" r3 c3");
        get = get - 5;
    Serial.println(get);

  }
      if (myValue == r5c3) // Stop
  { 
    Serial.println(" r5 c3");
    Serial.println(" Stop ");
    analogWrite(11, 0);
    analogWrite(3, 0); 
    delay(350);
  }
    delay(1000);   
    irrecv.resume(); // Receive the next value
  }
}


/as I said before, any suggestions will be greatly appreciated.

Mike
« Last Edit: February 01, 2012, 05:49:10 pm by mlempjr » Logged

0
Offline Offline
Shannon Member
****
Karma: 200
Posts: 11718
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry but without seeing the two working sketches we can only guess about that code - we don't know anything about the IR or the motor wiring but the working sketches would provide that information.

There is a '#' button for quoting code, BTW
Code:
// like this


As I said if both bits work separately and you've combined them OK it will be a hardware problem - I asked a question and you haven't answered it yet, hint...
« Last Edit: January 31, 2012, 09:04:28 am by MarkT » Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Mark,
First, I do not know how to connect the logic power seperately.  The Motor shield has two connections and when the 12 vold supply is connected there the system seems to work and the Arduino responds.  If I unplug the USB to the Arduino it still works.  I could connect a 5V supply to the Arduino (about to try that) ... tried that and no difference.   Still don't know if there are some jumpers that need to be set on the Motor Shield.  Still surprises me that either program works with the same wireing but when I add one to the other ZAP only fast speed works.

Are you suggesting that I should use more comments?  You're so right.  It is a bad habbit.  You know I think I know what I'm doing!
Here is the PWM code that works+++++++++++++++++++++++++++++++++++

Code:
/*
 * Crawler motor
 */
void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);           // set up Serial library at 9600 bps
  //pinMode(0, INPUT);
  pinMode(13, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(3, OUTPUT);
}
int get = 15;
int motorFoward() {
  analogWrite(11, get);
  analogWrite(3, get);
  delay(1000);
  digitalWrite(11, LOW);
  digitalWrite(3, LOW);  
  delay(1000);
  digitalWrite(13, HIGH);
  digitalWrite(12, LOW);
  Serial.println(get);
  delay(1000);
}

int motorBackward() {
  // get = 80;
  analogWrite(11, get);
  analogWrite(3, get);  
  delay(1000);
  digitalWrite(11, LOW);
  digitalWrite(3, LOW);  
  delay(1000);
  digitalWrite(13, LOW);
  digitalWrite(12, HIGH);  
  Serial.println(get);
  delay(1000);
}

void loop()                     // run over and over again
{
  motorFoward();
  motorBackward();
  get = get + 5;
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
And here is the IR code that works, My remote has 5 rows the first 4 of which have three buttons and the fifth has only two.''

Code:
/*
  RemoteDecode sketch
 Infrared remote control signals are decoded
 */
#include <IRremote.h>           // IR remote control library
long irKeyCodes[14];            // holds the codes for each key
IRrecv irrecv(A2);    // create the IR library
decode_results results;         // IR data goes here
void setup()
{
  Serial.begin(9600);
  pinMode(A2, INPUT);
  pinMode(9, OUTPUT);
  irrecv.enableIRIn();              // Start the IR receiver
  learnKeycodes();                  // learn remote control key  codes
  // Serial.println("Press a remote key");
  pinMode(13, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(3, OUTPUT);
}

void loop()
{
  long prevValue = -1;
}
/*
 * get remote control codes
 */
void learnKeycodes()
{
  while(irrecv.decode(&results))   // empty the buffer
    irrecv.resume();
  Serial.println("Ready to Start ");
  long prevValue = -1;
  int i=0;
  while( i <22)
  {
    while(true)
    {
      if( irrecv.decode(&results) )
      {
          if(results.value != -1 && results.value != prevValue)
          {
            showReceivedData();
            irKeyCodes[i] = results.value;
            i = i + 1;
            prevValue = results.value;
            irrecv.resume(); // Receive the next value
            break;
          }
        irrecv.resume(); // Receive the next value
      }
    }
  }
  Serial.println("Learning complete");
}
/*
 * converts a remote protocol code to a logical key code
 * (or -1 if no digit received)
 */
int convertCodeToKey(long code)
{
  for( int i=0; i <22; i++)
  {
    if( code == irKeyCodes[i])
    {
      return i; // found the key so return it
    }
  }
  return -1;
}
/*
 * display the protocol type and value
 */
void showReceivedData()
{
      long myValue = -1;
      long r1c1 = 16738455;
      long r1c2 = 16754775;
      long r1c3 = 16722135;
      long r2c1 = 16740495;
      long r2c2 = 16756815;
      long r2c3 = 16724175;
      long r3c1 = 16732335;           
      long r3c2 = 16748655;
      long r3c3 = 16716015;
      long r4c1 = 16736415;     
      long r4c2 = 16752735;
      long r4c3 = 16720095;
      long r5c1 = 16744575;     
      long r5c3 = 16711935;
      myValue = results.value;
  Serial.println( myValue);
   if (myValue == r1c1)
  { 
    Serial.println(" r1 c1");
  }
    if (myValue == r1c2)
  { 
    Serial.println(" r1 c2");
      delay(1000);
      Serial.println(" got to this place");
   
  }
    if (myValue == r1c3)
   
  { 
    Serial.println(" r1 c3");
  }
     if (myValue == r2c1)
  { 
    Serial.println(" r2 c1");
  }
    if (myValue == r2c2)
  { 
    Serial.println(" r2 c2");
  }
    if (myValue == r2c3)
  { 
    Serial.println(" r2 c3");
  }
    if (myValue == r3c1)
  { 
    Serial.println(" r3 c1");
  }
    if (myValue == r3c2)
  { 
    Serial.println(" r3 c2");
  }
    if (myValue == r3c3)
  { 
    Serial.println(" r3 c3");
  }
     if (myValue == r4c1)
  { 
    Serial.println(" r4 c1");
  }
    if (myValue == r4c2)
  { 
    Serial.println(" r4 c2");
  }
    if (myValue == r4c3)
  { 
    Serial.println(" r4 c3");
  }
    if (myValue == r5c1)
  { 
    Serial.println(" r5c1");
  }
      if (myValue == r5c3)
  { 
    Serial.println(" r5 c3 ");
  }
   
  }
========================================
Thanks for all of your time.

Mike
« Last Edit: February 01, 2012, 05:47:42 pm by mlempjr » Logged

Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 32
Posts: 1204
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

you MUST use the "#" button (in the toolbar above the text entry field) when submitting code. Its not just to look pretty, it ensures that if you write
Code:
[i]
I can see that you have "i" as a subscript to an array. Without the code field it just means - from here start italicizing the text in the browser.

Note that you can now edit your own posts. (Normally discouraged, but usefull to remove an embarrasing typo - or a forgotten code-tag ) Hit the edit/pen button, then highlight the code section, then hit the code button. Or include manually the code-begin and end tags
Logged

Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 32
Posts: 1204
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

(Note - I have not reveiwed your last submission yet, so this reply is to your original post)

Well, if you say the decoding works without the motor ... and what you have here is the same IR receive code, then we've likely got a hardware problem.

OR

you may exceed RAM space (the 2K limit). This is easily checked, include and print the value from the "memoryfree" library.

Otherwise there are bits of your code that ... ehr .. surprise me.
Code:
      myValue = results.value;
      long r1c1 = 16738455;
      long r1c2 = 16754775;
      long r1c3 = 16722135;
      long r2c1 = 16740495;
 :
         
  Serial.println(myValue);
     if (myValue == r1c1)
  { 
    Serial.println(" r1 c1");
    analogWrite(10, 1);
  }
    if (myValue == r1c2)
  { 
    Serial.println(" r1 c2");
      analogWrite(9, 0);
You probably do not get what you expect? Writing "16738455" is treated as an int and may be truncated to 16 bits, ie 26775. But you say it has worked? Anyhow, but a suffix of "L" behind the constant and it is a long value.

Replace this one-shot use of variables (eating a little each of that 2K) in your "if"s directly
Code:
if ( myValue == 16738455L )
   {  whatever ; }
Actually and better, you should use the case-switch statment. It does support long, as long as you rember the trailing L. OK I was not 100% sure, so I wrote a tiny test
Code:
void setup() {Serial.begin(9600) ;}
long lv = 333333L ;
void loop() {
  delay(1234) ;
  switch(lv) {
    case 123456L: Serial.println("123456") ; break ;
    case 234567L: Serial.println("234567") ; break ;
    default: Serial.println("Eh?") ;
  }
  if ( lv== 123456L ) lv = 234567 ; else lv = 123456L ;
}

Each of the strings in println will also eat of the 2K RAM - try to (temporarily) comment the println() out.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks,
As you may have guessed, I have much more experience with the visual basic programing language than with the "C" family, especially the Arduino version of "C".  I can't figure out how there could be a hardware porblem if both of the individual sketches work.  I could see where there could be a memory problem but would that allow the program to work but not change the speed?  Seems a bit unlikely, but I still don't know much about the Motor Shield.  And thanks for the heads up about the "#"  I thought that all those icons must be therre for something!!

I do have a MEGA which would help me determing if the memory limitation is an issue, but it is too large for the physical application that I was hoping to accomplish.  Oh well, time will tell.

Mike
 
Logged

Pages: [1]   Go Up
Jump to: