Small DC Motor, H-Bridge and IR sensor

Hi all, I hope every body is well. I have a very annoying problem!

I have as suggested in the title a small dc motor salvaged from a CD-ROM, a salvaged IR sensor from an old DVD player and have used a 556 timer as an H-Bridge (as desribed here -->555 / 556 H bridge – Electro Bob). Ooh and i am on an Arduino Mega (1280).

If i run this code it works as expected, the motor spins one way, stops, spins tother way, stops... so on and so forth.

void setup() {
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
}

void loop() {
  analogWrite(8, 75);      //pwm for lower speed
  digitalWrite(9, LOW);
  delay(3000);
  digitalWrite(8, LOW);
  delay(2000);
  digitalWrite(8, LOW);
  analogWrite(9, 75);      //pwm for lower speed
  delay(3000);
  digitalWrite(9, LOW);
  delay(2000);
}

I decided that i would like to control the direction of spin via a remote control (funky uh!!!) so set about connecting and testing the IR reciever that i salvaged and after a minute or two had it fully functioning as expected using Ken Sharriff's IRremote library here-->Using arbitrary remotes with the Arduino IRremote library.

After successfully turning an led on and of using two buttons on my remote i connected up the H-Bridge and motor and adjusted the code to this:

#include <IRremote.h>

int RECV_PIN = 4;
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
  pinMode(8, OUTPUT);    // "A"
  pinMode(9, OUTPUT);    // "B"
  irrecv.enableIRIn(); // Start the receiver
  Serial.begin(9600);
}

void loop() {
  if (irrecv.decode(&results)) {
    switch (results.value) {
    case 0xFFD827:       // play button on my remote
      analogWrite(8, 75);      //pwm for lower speed
      digitalWrite(9, LOW);
      delay(3000);
      digitalWrite(8, LOW);
      break;
    case 0xFF02FD:       // stop button on my remote
      digitalWrite(8, LOW);
      analogWrite(9, 75);      //pwm for lower speed
      delay(3000);
      digitalWrite(9, LOW);
      break; 
    }
    irrecv.resume();
  }
}

Fired everything up and success!!! Well sort of lolol!!! I press the play button on my remote and voila the motor spins for a few seconds and stops! I then pressed the stop button on my remote and got a grand total of 'zilch'!!! So i made sure i was looking for the correct remote code for stop and even output to the serial monitor to make sure everything was fine. All OK there! So I swapped the codes for the play and stop buttons around and sure enough the stop button worked but now the play button doesn't. Only one thing I can put it down to folks....my code sucks!!!lololol!!

So here I am asking the experts for help! (sorry for waffling on, and thanks for your time!):

Regards,

Ian

Hi,i am not sure where your project is going wrong,i did nearly the same thing reclaimed parts etc and had a similar problem so i will post my working code for you to look at it may help you never know.By replacing HIGH with LOW i got reverse.

// IR 2 motor test

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

 
int E1 = 6;
int M1 = 7;
int E2 = 5;
int M2 = 4;

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()

{

pinMode(M1, OUTPUT);
pinMode(M2, OUTPUT);
Serial.begin(9600); // for serial monitor output
irrecv.enableIRIn(); // Start the receiver
 
}

void loop()

{

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

  {
{
      if (results.value == 0x687CBE2L) {   // 0x687CBE2L button press on remote control
      Serial.write("1st motor ON\n");
      
    digitalWrite(M2,HIGH);    
    digitalWrite(M1,HIGH); 

analogWrite(E1, 200); //PWM Speed Control
    
    delay(100);                       // waits 1s for the motor to reach the speed 
      }
            
    else if (results.value == 0x687CBEAL) {  //Press off button on remote control
      Serial.write("1st motor off\n");
         
    analogWrite(E1, 0); //PWM Speed Control

    delay(100);                       // waits 1s for the motor to reach the speed 
    
    
          if (results.value == 0x687CBD0) {   // On button press on remote control
      Serial.write("2nd motor ON\n");
    
    digitalWrite(M2,HIGH); 

analogWrite(E2, 200); //PWM Speed Control
    
    delay(100);                       // waits 1s for the motor to reach the speed 
      }
            
    else if (results.value == 0x687CBD6) {  //Press off button on remote control
      Serial.write("2nd motor off\n");
         
    analogWrite(E2, 0); //PWM Speed Control

    delay(100);                       // waits 1s for the motor to reach the speed 
    
     
    }
  
   
      }
}
    Serial.println(results.value, HEX); // display it on serial monitor in hexadecimal

    irrecv.resume(); // receive the next value
    
          if (results.value == 0x687CBD0) {   // On button press on remote control
      Serial.write("2nd motor ON\n");
    
    digitalWrite(M2,HIGH); 

analogWrite(E2, 200); //PWM Speed Control
    
    delay(100);                       // waits 1s for the motor to reach the speed 
      }
            
    else if (results.value == 0x687CBD6) {  //Press off button on remote control
      Serial.write("2nd motor off\n");
         
    analogWrite(E2, 0); //PWM Speed Control

    delay(100);                       // waits 1s for the motor to reach the speed 
    
     
    }
  
  }  // Your loop can do other things while waiting for an IR command

}

Jabar

Hi Jabar, Thanks for your response I will give your code a go!