#include <Servo.h>
const int servo1 = 3; // first servo
const int servo2 = 10; // second servo
const int joyH = 3; // L/R Parallax Thumbstick
const int joyV = 4; // U/D Parallax Thumbstick
int servoDrive;
int servoTurn;
int servoVal1; // variable to read the value from the analog pin
int servoVal2;
int servoVal3;
int servoVal4;
Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo
void setup() {
// Servo
myservo1.attach(servo1); // attaches the servo
myservo2.attach(servo2); // attaches the servo
// Inizialize Serial
Serial.begin(9600);
}
void loop(){
// Display Joystick values using the serial monitor
outputJoystick();
servoDrive = analogRead(joyV);
servoTurn = analogRead(joyH);
servoVal1 = map(servoTurn, 0, 1023,1000, 1750); // scale it to use it with the servo (result between 0 and 180)
servoVal2 = map(servoTurn, 0, 1023, 1250, 1750); // scale it to use it with the servo (result between 70 and 180)
// scale it to use it with the servo (result between 0 and 180)
servoVal3 = 435 + servoVal4 - servoDrive;
servoVal4 = map(servoDrive, 0, 1023, 1250, 1750);
// scale it to use it with the servo (result between 70 and 180)
myservo2.write(servoVal3); // sets the servo position according to the scaled value
myservo1.write(servoVal4);
// Read the horizontal joystick value (value between 0 and 1023)
//servoTurn = analogRead(joyH);
//servoDrive = analogRead(joyV);
myservo2.write(servoVal1); // sets the servo position according to the scaled value
myservo1.write(servoVal2);
delay(15); // waits for the servo to get there
}
/**
* Display joystick values
*/
void outputJoystick(){
Serial.print(analogRead(joyV));
Serial.print ("----------------");
Serial.print(analogRead(joyH));
Serial.println ("----------------");
}
this is the code i have now, it works when the two parts are separate. forwards and backwards:
#include <Servo.h>
const int servo1 = 3; // first servo
const int servo2 = 10; // second servo
const int joyH = 3; // L/R Parallax Thumbstick
const int joyV = 4; // U/D Parallax Thumbstick
int servoDrive;
int servoTurn;
int servoVal;
int servoVal1; // variable to read the value from the analog pin
int servoVal2;
int servoVal3;
int servoVal4;
Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo
void setup() {
// Servo
myservo1.attach(servo1); // attaches the servo
myservo2.attach(servo2); // attaches the servo
// Inizialize Serial
Serial.begin(9600);
}
void loop(){
// Display Joystick values using the serial monitor
outputJoystick();
servoDrive = analogRead(joyV);
// scale it to use it with the servo (result between 0 and 180)
servoVal3 = 435 + servoVal4 - servoDrive;
servoVal4 = map(servoDrive, 0, 1023, 1250, 1750);
// scale it to use it with the servo (result between 70 and 180)
myservo2.write(servoVal3); // sets the servo position according to the scaled value
myservo1.write(servoVal4);
delay(15); // waits for the servo to get there
}
/**
* Display joystick values
*/
void outputJoystick(){
Serial.print(analogRead(joyH));
Serial.print ("---");
Serial.print(analogRead(joyV));
Serial.println ("----------------");
}
and zero point turns:
#include <Servo.h>
const int servo1 = 3; // first servo
const int servo2 = 10; // second servo
const int joyH = 3; // L/R Parallax Thumbstick
const int joyV = 4; // U/D Parallax Thumbstick
int servoDrive;
int servoTurn;
int servoVal1; // variable to read the value from the analog pin
int servoVal2;
int servoVal3;
int servoVal4;
Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo
void setup() {
// Servo
myservo1.attach(servo1); // attaches the servo
myservo2.attach(servo2); // attaches the servo
// Inizialize Serial
Serial.begin(9600);
}
void loop(){
// Display Joystick values using the serial monitor
outputJoystick();
servoTurn = analogRead(joyH);
servoVal1 = map(servoTurn, 0, 1023,1000, 1750); // scale it to use it with the servo (result between 0 and 180)
servoVal2 = map(servoTurn, 0, 1023, 1250, 1750); // scale it to use it with the servo (result between 70 and 180)
// Read the horizontal joystick value (value between 0 and 1023)
//servoTurn = analogRead(joyH);
myservo2.write(servoVal1); // sets the servo position according to the scaled value
myservo1.write(servoVal2);
delay(15); // waits for the servo to get there
}
/**
* Display joystick values
*/
void outputJoystick(){
Serial.print(analogRead(joyV));
Serial.print ("----------------");
Serial.print(analogRead(joyH));
Serial.println ("----------------");
}
for some reason (its one i cant find) whenever the 2 parts of the code are together like then one at the top, all of the code for the Forwards and backwards portion are ignored and the robot cannot move forward. is there anything in the code that makes this Occur? if not does anyone have any advice? as always any help would be appreciated and anyone that has posted so far has been of extreme help.