Go Down

Topic: Problems with IR sensor and DC motors (Read 2237 times) previous topic - next topic

NicolaBastianello

to Jack: sorry for my bad english! i just wanted to say that all the pins work good, so it's not a problem of the arduino or the shield.

to johnwasser: sadly it didn't!! i really cannot understand! here's the code, i just changed the type of the variable speed_a

Code: [Select]
/////////////////////////////////////////
#include <IRremote.h>
/////////////////////////////////////////
#define DIR_A 12
#define SPEED_A 3
#define BRAKE_A 9
/////////////////////////////////////////
#define ON_OFF 0x827D58A7 //brakes engaged/disengaged
#define BASS_UP 0x827D6897 //increase speed
#define BASS_DOWN 0x827D40BF //decrease speed
#define PLAY_PAUSE 0x827DF20D //inverts spinning direction
#define LONG_PRESS 0xFFFFFFFF
/////////////////////////////////////////
int recv_pin=5;
IRrecv irrecv(recv_pin);
decode_results results;

//these variables are modified according
//to the IR code read
int dir_a=HIGH;
int brk_a=HIGH;
int speed_a=0;

//this led is on when the motor is working
int led_pin=7;

//keeps in memory which was the last signal
//received, to repeat it if is a command which
//increases or decreases speed
int last_command=0x0;
/////////////////////////////////////////

void setup() {
  irrecv.enableIRIn(); //enables IR receiver
  //defines pins
  pinMode(led_pin,OUTPUT);
  pinMode(DIR_A,OUTPUT);
  pinMode(BRAKE_A,OUTPUT);
  pinMode(SPEED_A,OUTPUT);
  //writes initial values of direction speed and brakes
  digitalWrite(DIR_A,dir_a);
  digitalWrite(BRAKE_A,brk_a);
  analogWrite(SPEED_A,speed_a);
}

void loop() {
  //waits for a signal
  if (irrecv.decode(&results)) {
    //passes the signal to the decode method to act consequently
    decode_command(results.value);
    //refreshes the situation, which means
    //writes again the dir speed and brakes value
    refresh();
    //let the receiver free to read onother signal
    irrecv.resume();
  }
}
/////////////////////////////////////////
void decode_command(int value) {
  switch (value) {
    //enables or disables brakes to stop/start the motor
    case ON_OFF:
      brk_a=!brk_a;
      last_command=ON_OFF;
    break;
    //increases speed
    case BASS_UP:
      speed_a+=10;
      if (speed_a>255) {
        speed_a=255;
      }
      last_command=BASS_UP;
    break;
    //decreases speed
    case BASS_DOWN:
      speed_a-=10;
      if (speed_a<0) {
          speed_a=0;
      }
      last_command=BASS_DOWN;
    break;
    //inverts spinning direction
    case PLAY_PAUSE:
      dir_a=!dir_a;
      last_command=PLAY_PAUSE;
    break;
    case LONG_PRESS:
      if (last_command==BASS_UP) {
        speed_a+=10;
        if (speed_a>255) {
          speed_a=255;
        }
      }
      else if (last_command==BASS_DOWN) {
        speed_a-=10;
        if (speed_a<0) {
          speed_a=0;
        }       
      }
    break;
  }
}
/////////////////////////////////////////
void refresh() {
  digitalWrite(DIR_A,dir_a);
  digitalWrite(BRAKE_A,brk_a); 
  digitalWrite(led_pin,!brk_a);
  analogWrite(SPEED_A,speed_a);
}
/////////////////////////////////////////

jack wp

Well I can't thing of anything else to ask (you never answer what I ask anyway), and no more suggestions to test with, (you did not use any of my suggestions). You say whats not wrong, so you must have a good idea what is wrong then.

Good Luck, Jack

johnwasser

I'm starting to wonder if the IR library is using Pin 3 (IR Send) even though you are not doing any IR sending.

Try bending Pin 3 of the motor shield and jumpering it to an unused PWM output.  Then switch your 'speed_a' definition to that other pin.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

NicolaBastianello

to jack: sorry we did not understand each other! I followed all your suggestion, I tried separately all the parts of hardware and software, like you told to do. i found no problems at all!
to johnwasser: i will try as soon as possible, thanks! maybe i could modify the library cutting away the sending part

akeom

Old thread, but: I think I post this problem as well here since it might be related. When I run my DC motor, the IR signal get's jammed somehow. Setup: Keyes L298, ENA=Pin10, IN1=Pin8, IN2=Pin12. IR receiver=Pin5. Keyes IR Remote. I power the Arduino Mini Pro 16 through the L298. JLINKE 12V/10A power source. Motor has no capacitor filters installed (I'll try that next)

When the motor runs, the IR's attached LED lights up to indicate a signal has been received, and the actually sent remote codes are not deciphered correctly any more.

When I disrupt the motor's power supply, receiving codes works perfectly.


Go Up