I have currently written a code which effectively uses 3 out of 4 channels with an R/C remote found here http://www.tetrixrobotics.com/RC_Controllers/Wireless_Joystick_Gamepad_System
to interface with an arduino and Adafruit motorshield. My problem is that as soon as I try to add the 4th channel and make it do something it causes one set of motor to jitter and not run smoothly as if it is constantly receiving and then not receiving power.
here is the code that works
#include <Servo.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
int ch1;
int ch2;
int ch3;
int ch4;
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *driveWheel1 = AFMS.getMotor(1);
Adafruit_DCMotor *driveWheel2 = AFMS.getMotor(2);
Adafruit_DCMotor *armMotor1 = AFMS.getMotor(3);
Adafruit_DCMotor *armMotor2 = AFMS.getMotor(4);
Servo claw;
int servoPin = 9;
int pos = 0;
Servo arm;
int servoPin2 = 10;
int pos2 = 0;
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
long onTime1 = 100;
long onTime2 = 100;
void setup()
{
AFMS.begin();
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
Serial.begin(9600);
driveWheel1->setSpeed(180);
driveWheel2->setSpeed(180);
armMotor1->setSpeed(255);
armMotor2->setSpeed(255);
claw.attach(servoPin);
arm.attach(servoPin2);
}
void loop()
{
unsigned long currentMillis = millis();
ch1 = pulseIn(A0, HIGH, 25000);
ch2 = pulseIn(A1, HIGH, 25000);
ch3 = pulseIn(A2, HIGH, 25000);
ch4 = pulseIn(A3, HIGH, 25000);
if(ch4 > 1500)
{
Serial.println("Right Switch: Engaged");
driveWheel1->run(FORWARD);
driveWheel2->run(BACKWARD);
}
if(ch4 < 1400)
{
Serial.println("Right Switch: Disengaged");
driveWheel1->run(BACKWARD);
driveWheel2->run(FORWARD);
}
if((ch4 > 1400) && (ch4 < 1500))
{
Serial.println("Right Switch: Neutral");
driveWheel1->run(RELEASE);
driveWheel2->run(RELEASE);
}
if(ch3 > 1500)
{
Serial.println("Left Switch: Forward");
armMotor1->run(FORWARD);
armMotor2->run(FORWARD);
Serial.println("bend forward");
if(currentMillis - previousMillis2 >= onTime2)
{
for(pos2 = 0; pos2 < 170; pos2++)
{
arm.write(pos2);
}
previousMillis2 = currentMillis;
}
}
if(ch3 < 1400)
{
Serial.println("Left Switch: BACKWARD");
armMotor1->run(BACKWARD);
armMotor2->run(BACKWARD);
Serial.println("bend backward");
if(currentMillis - previousMillis2 >= onTime2)
{
for(pos2 = 170; pos2 > 0; pos2--)
{
arm.write(pos2);
}
}
}
if((ch3 > 1400) && (ch3 < 1500))
{
Serial.println("Left Switch: Neutral");
Serial.println("normal");
armMotor1->run(RELEASE);
armMotor2->run(RELEASE);
}
if(ch1 > 1500)
{
Serial.println("grab");
if(currentMillis - previousMillis1 >= onTime1)
{
for(pos = 0; pos < 170; pos++)
{
claw.write(pos);
}
previousMillis1 = currentMillis;
}
}
if(ch1 < 1350)
{
Serial.println("Release");
if(currentMillis - previousMillis1 >= onTime1)
{
for(pos = 170; pos > 0; pos--)
{
claw.write(pos);
}
previousMillis1 = currentMillis;
}
}
if((ch1 > 1350) && (ch2 < 1500))
{
Serial.println("nothing");
}
}
and here is the one that causes problems by adding the 4th channel
#include <Servo.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
int ch1;
int ch2;
int ch3;
int ch4;
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *driveWheel1 = AFMS.getMotor(1);
Adafruit_DCMotor *driveWheel2 = AFMS.getMotor(2);
Adafruit_DCMotor *armMotor1 = AFMS.getMotor(3);
Adafruit_DCMotor *armMotor2 = AFMS.getMotor(4);
Servo claw;
int servoPin = 9;
int pos = 0;
Servo arm;
int servoPin2 = 10;
int pos2 = 0;
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
long onTime1 = 100;
long onTime2 = 100;
void setup()
{
AFMS.begin();
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
Serial.begin(9600);
driveWheel1->setSpeed(180);
driveWheel2->setSpeed(180);
armMotor1->setSpeed(255);
armMotor2->setSpeed(255);
claw.attach(servoPin);
arm.attach(servoPin2);
}
void loop()
{
unsigned long currentMillis = millis();
ch1 = pulseIn(A0, HIGH, 25000);
ch2 = pulseIn(A1, HIGH, 25000);
ch3 = pulseIn(A2, HIGH, 25000);
ch4 = pulseIn(A3, HIGH, 25000);
if(ch4 > 1500)
{
Serial.println("Right Switch: Engaged");
driveWheel1->run(FORWARD);
driveWheel2->run(BACKWARD);
}
if(ch4 < 1400)
{
Serial.println("Right Switch: Disengaged");
driveWheel1->run(BACKWARD);
driveWheel2->run(FORWARD);
}
if((ch4 > 1400) && (ch4 < 1500))
{
Serial.println("Right Switch: Neutral");
driveWheel1->run(RELEASE);
driveWheel2->run(RELEASE);
}
if(ch3 > 1500)
{
Serial.println("Left Switch: Forward");
armMotor1->run(FORWARD);
armMotor2->run(FORWARD);
Serial.println("bend forward");
if(currentMillis - previousMillis2 >= onTime2)
{
for(pos2 = 0; pos2 < 170; pos2++)
{
arm.write(pos2);
}
previousMillis2 = currentMillis;
}
}
if(ch3 < 1400)
{
Serial.println("Left Switch: BACKWARD");
armMotor1->run(BACKWARD);
armMotor2->run(BACKWARD);
Serial.println("bend backward");
if(currentMillis - previousMillis2 >= onTime2)
{
for(pos2 = 170; pos2 > 0; pos2--)
{
arm.write(pos2);
}
}
}
if((ch3 > 1400) && (ch3 < 1500))
{
Serial.println("Left Switch: Neutral");
Serial.println("normal");
armMotor1->run(RELEASE);
armMotor2->run(RELEASE);
}
if(ch1 > 1500)
{
Serial.println("grab");
if(currentMillis - previousMillis1 >= onTime1)
{
for(pos = 0; pos < 170; pos++)
{
claw.write(pos);
}
previousMillis1 = currentMillis;
}
}
if(ch1 < 1350)
{
Serial.println("Release");
if(currentMillis - previousMillis1 >= onTime1)
{
for(pos = 170; pos > 0; pos--)
{
claw.write(pos);
}
previousMillis1 = currentMillis;
}
}
if((ch1 > 1350) && (ch2 < 1500))
{
Serial.println("nothing");
}
if(ch2 > 1500)
{
Serial.println("go forward");
driveWheel1->run(FORWARD);
driveWheel2->run(FORWARD);
}
if(ch2 < 1350)
{
Serial.println("go backward");
driveWheel1->run(BACKWARD);
driveWheel2->run(BACKWARD);
}
if((ch2 > 1350) && (ch1 < 1500))
{
Serial.println("stay still");
driveWheel1->run(RELEASE);
driveWheel2->run(RELEASE);
}
}