//////////////////////////////////////////////
// RemoteXY include library //
//////////////////////////////////////////////
// RemoteXY select connection mode and include library
#define REMOTEXY_MODE__SOFTSERIAL
#include <SoftwareSerial.h>
#include <RemoteXY.h>
// RemoteXY connection settings
#define REMOTEXY_SERIAL_RX 3
#define REMOTEXY_SERIAL_TX 2
#define REMOTEXY_SERIAL_SPEED 9600
// Include the Servo library
#include <Servo.h>
Servo myservo;
Servo myservo2;
int lastValue;
int lastValue2;
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 100; // interval at which to blink (milliseconds)
// Declare the Servo pin
int servoPin = 9;
int servoPin2 = 10;
// Create a servo object
// RemoteXY configurate
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] =
{ 255, 6, 0, 0, 0, 37, 0, 10, 13, 0,
4, 128, 5, 4, 88, 7, 188, 26, 5, 32,
17, 26, 30, 30, 15, 26, 31, 5, 32, 53,
26, 30, 30, 12, 26, 31, 4, 0, 4, 14,
9, 45, 204, 26
};
// this structure defines all the variables and events of your control interface
struct {
// input variables
int8_t slider_1; // =0..100 slider position
int8_t joystick_1_x; // =-100..100 x-coordinate joystick position
int8_t joystick_1_y; // =-100..100 y-coordinate joystick position
int8_t joystick_2_x; // =-100..100 x-coordinate joystick position
int8_t joystick_2_y; // =-100..100 y-coordinate joystick position
int8_t slider_2; // =0..100 slider position
// other variable
uint8_t connect_flag; // =1 if wire connected, else =0
} RemoteXY;
#pragma pack(pop)
/////////////////////////////////////////////
// END RemoteXY include //
/////////////////////////////////////////////
//////////////////////////////////////////////
// START Motor Shield code //
//////////////////////////////////////////////
// Include for Adafruit Motor Shield
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);
// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *motor1 = AFMS.getMotor(1);
Adafruit_DCMotor *motor2 = AFMS.getMotor(2);
Adafruit_DCMotor *motor3 = AFMS.getMotor(3);
Adafruit_DCMotor *motor4 = AFMS.getMotor(4);
void runMotor(Adafruit_DCMotor *myMotor, int speed) {
if (speed > 0) {
myMotor->run(FORWARD);
myMotor->setSpeed(speed);
} else if (speed < 0) {
myMotor->run(BACKWARD);
myMotor->setSpeed(-speed);
} else {
myMotor->run(BRAKE);
myMotor->setSpeed(0);
}
}
//////////////////////////////////////////////
// END Motor Shield code //
//////////////////////////////////////////////
/////////////////////////////////////////////
// START Servo include //
/////////////////////////////////////////////
#include <Servo.h>
/////////////////////////////////////////////
// END Servo include //
/////////////////////////////////////////////
void setup()
{
RemoteXY_Init ();
Serial.begin(9600);
AFMS.begin(); // create with the default frequency 1.6KHz
RemoteXY.slider_1 = 50;
RemoteXY.slider_2 = 3;
myservo.attach(servoPin);
myservo2.attach(servoPin2);
lastValue = -1;
lastValue2 = -1;
}
void loop()
{
RemoteXY_Handler ();
//Left Wheel to Motor 1 (CCW)
runMotor(motor3, (map(RemoteXY.joystick_1_y, -100, 100, 255, -255)));
//Right Wheel to Motor 3 (CW)
runMotor(motor4, (map(RemoteXY.joystick_2_y, -100, 100, -255, 255)));
int value = RemoteXY.slider_1;
int value2 = RemoteXY.slider_2;
if (abs(value - lastValue) > 3)
{
myservo.attach(servoPin);
delay(100);
myservo.write(map(RemoteXY.slider_1, 0, 100, 0, 180));
delay(90);
myservo.detach();
}
if (abs(value2 - lastValue2) > 3) {
myservo2.attach(servoPin2);
delay(100);
myservo2.write(map(RemoteXY.slider_2, 0, 100, 0, 180));
delay(90);
myservo2.detach();
}
lastValue = RemoteXY.slider_1;
lastValue2 = RemoteXY.slider_2;
}