Merging two codes into one

Hey guys, I am relatively new to Arduino and I have two separate working codes and want to merge them into one. So basically I am using an UNO R3 and a NEMA 17 stepper motor. In one code I am controlling the stepper motor with an IR Remote and the second code I am using a hand gesture module to control the motor in the exact same way but I am unsure of how to merge it so I can control the motor with both the IR Remote and with Hand Gestures simultaneously. Please help!

IR Remote Code:

#include "Stepper.h"
#include "IRremote.h"


/*----- Variables, Pins -----*/
#define STEPS  200   // Number of steps per revolution of Internal shaft
int  Steps2Take;  // 2048 = 1 Revolution
int receiver = 5; // Signal Pin of IR receiver to Arduino Digital Pin 6

/*-----( Declare objects )-----*/
// Setup of proper sequencing for Motor Driver Pins
// In1, In2, In3, In4 in the sequence 1-3-2-4

Stepper small_stepper(STEPS, 8, 9, 10, 11);
IRrecv irrecv(receiver);    // create instance of 'irrecv'
decode_results results;     // create instance of 'decode_results'

void setup()
{ 
  irrecv.enableIRIn(); // Start the receiver
}

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

  {
    switch(results.value)

    {

      case 0xFFE01F: // UP button pressed
                      small_stepper.setSpeed(50); //Max seems to be 700
                      Steps2Take  =  2000;  // Rotate CW
                      small_stepper.step(Steps2Take);
                      delay(2000); 
                      break;

      case 0xFF906F: // DOWN button pressed
                      small_stepper.setSpeed(50);
                      Steps2Take  =  -2000;  // Rotate CCW
                      small_stepper.step(Steps2Take);
                      delay(2000); 
                      break;
                      
    }
    
      irrecv.resume(); // receive the next value
  }  


}/* --end main loop -- */

Hand Gesture Code:

#include <Stepper.h>
#include "RevEng_PAJ7620.h"



// Create gesture sensor driver object
RevEng_PAJ7620 sensor = RevEng_PAJ7620();
#define STEPS  200  
int  Steps2Take;  


Stepper small_stepper(STEPS, 8, 9, 10, 11);  
// *********************************************************************
void setup()
{
 
  Serial.begin(115200);

  Serial.println("PAJ7620 sensor demo: Recognizing all 9 gestures.");

  if( !sensor.begin() )           // return value of 0 == success
  {
    Serial.print("PAJ7620 I2C error - halting");
    while(true) { }
  }

  Serial.println("PAJ7620 init: OK");
  Serial.println("Please input your gestures:");
}


// *********************************************************************
void loop()
{
  Gesture gesture;                  // Gesture is an enum type from RevEng_PAJ7620.h
  gesture = sensor.readGesture();   // Read back current gesture (if any) of type Gesture

  switch (gesture)

{ 
  
    case GES_UP:
{
      {
        small_stepper.setSpeed(70); 
                      Steps2Take  =  2000;  // Rotate CW
                      small_stepper.step(Steps2Take);
                      delay(2000); 
                      break;
        Serial.print("GES_UP");
        break;
      }

    case GES_DOWN:
      {
        small_stepper.setSpeed(70);
                      Steps2Take  =  -2000;  // Rotate CCW
                      small_stepper.step(Steps2Take);
                      delay(2000); 
                      break;
        Serial.print("GES_DOWN");
        break;
      }

    case GES_NONE:
      {
        break;
      }
  }

  if( gesture != GES_NONE )
  {
    Serial.print(", Code: ");
    Serial.println(gesture);
  }

  delay(100);
}
}

Hello
You are not alone. Take a view here:

Have a nice day and enjoy coding in C++.
Дайте миру шанс!

You’re going to learn some new tricks here.
In both programs you have several delay()s
These are basically incompatible with your ultimate goal.

You’ve heard about millis() timing, now’s a good time to read about it and get cooking.

I didn’t look closely at your code, but there’s a good chance you’ll need to look into states and the switch-case statement.

Sorry is anybody able to put the code together for me? I am still so lost ): After this Uni assessment I'm never touching an arduino again ahhahaha

Wow how to encourage people to do your homework for you!

So basically you are wanting us to cheat for you in order to get a degree. Well speaking as an ex university lecturer, I do hope no one cheats for you as it will contribute to the ever lowering standards we sadly find in higher education today. Be very ashamed of your self.

People here are very willing to help you learn, they are less keen on being taken for a mug.

1 Like

sorry grumpy mike

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