Reverse DC motor direction with one button

Here is what I am trying to do :

  1. Press the "one" button on the IR remote and have it turn a motor for a period of time and then stop.
  2. Press the "one" button again on the IR remote and have it turn the motor (reverse) for a period of time and stop.

The code I have below works - Pressing "one" on the IR remote moves the motor and stops. Now I need to add a count (or change state) so it will preform the second part/second press. Right now I can press the "two" button to reverse the motor.

In the future there could be a 3rd and 4th stage to the "one" button and will want to replicate these same functions for other buttons on the IR remote.

TIA

#include <IRremote.h>

#define One_button 0xFF30CF     // code received from one button
#define Two_button 0xFF18E7     // code received from two button
#define Two_button 0xFF18E7     // code received from two button
#define Three_button 0xFF7A85   // code received from three button
#define Four_button 0xFF10EF    // code received from four button
#define Five_button 0xFF38C7    // code received from five button
#define Six_button 0xFF5AA5     // code received from six button
#define Seven_button 0xFF42BD   // code received from seven button
#define Eight_button 0xFF4AB5   // code received from eight button
#define Nine_button 0xFF52AD    // code received from nine button
#define code1 0xFF7A85          // code received from button no. 3
#define code2 0xFF5AA5          // code received from button no. 6
#define code3 0xFF52AD          // code received from button no. 9

// Define sensor pin
int RECV_PIN = 2;               //output pin of IR receiver to pin 2 of arduino

//initializing the pins for motors
int rear_elevation_motor1 = 11;               //pin 11 of arduino to pin 10 of L293D
int rear_elevation_motor2 = 12;               //pin 12 of arduino to pin 15 of L293D
int rear_rotate_motor1 = 6;                   //pin 6 of arduino to pin 2 of L293D
int rear_rotate_motor2 = 5;                   //pin 5 of arduino to pin 7 of L293D
//int front_elevation_motor1 = ;              //pin 6 of arduino to pin 2 of L293D
//int front_elevation_motor2 = ;              //pin 5 of arduino to pin 7 of L293D
//int front_rotate_motor1 = ;                 //pin 6 of arduino to pin 10 of L293D
//int front_rotate_motor2 = ;                 //pin 6 of arduino to pin 15 of L293D

// Define IR Receiver and Results Objects
IRrecv irrecv(RECV_PIN);  //Arduino will take output of IR receiver from pin 2
decode_results output;

unsigned long currentTime;
int onTime = 5250;
int onTime2 = 4200;

void setup()
{
  // Serial Monitor @ 9600 baud
  Serial.begin(9600);

  // Enable the IR Receiver
  irrecv.enableIRIn();



  //initializing all the pins where we have connected the motors as output pins
  pinMode(rear_elevation_motor1, OUTPUT);
  pinMode(rear_elevation_motor2, OUTPUT);


}

void loop()
{
  {
    if (irrecv.decode(&output)) {
      unsigned long value = output.value;
      switch (value) {
        case One_button:
          digitalWrite(rear_rotate_motor1, LOW);
          digitalWrite(rear_rotate_motor2, HIGH);
          currentTime = millis();
          break;
      }
      irrecv.resume();
    }
    if ((millis() - currentTime) >= onTime)
    {
      digitalWrite(rear_rotate_motor1, LOW);
      digitalWrite(rear_rotate_motor2, LOW);
    }
    {
      if (irrecv.decode(&output)) {
        unsigned long value = output.value;
        switch (value) {
          case Two_button:
            digitalWrite(rear_rotate_motor1, HIGH);
            digitalWrite(rear_rotate_motor2, LOW);
            currentTime = millis();
            break;
        }
        irrecv.resume();
      }
      if ((millis() - currentTime) >= onTime)
      {
        digitalWrite(rear_rotate_motor1, LOW);
        digitalWrite(rear_rotate_motor2, LOW);
      }
    { if (irrecv.decode(&output)) {
          unsigned long value = output.value;
          switch (value) {
            case Seven_button:
              digitalWrite(rear_elevation_motor1, HIGH);
              digitalWrite(rear_elevation_motor2, LOW);
              currentTime = millis();
              break;
          }
          irrecv.resume();
        }
        if ((millis() - currentTime) >= onTime2)
        {
          digitalWrite(rear_elevation_motor1, LOW);
          digitalWrite(rear_elevation_motor2, LOW);
        }
      }
    }
  }
}

What you need is a simple state machine. See the StateChangeDetection example in the IDE.

Basically you'll set a variable so it remembers what phase you are in, e.g. first time you see a 1 set it to 1, next time to 2 etc. Then your motor code switch works on the variable rather than directly on the IR code. I'd arrange each motor routine as separate function for tidiness but that's not essential.

Give it a try and report back if you have any problems.

Steve

Thanks - I'll give that a look!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.