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);
}
}