Day_17
Comment: Initially, Mathew and I had trouble figuring out which sides of the joysticks correspond to which analog values. We serial printed the raw data and moved the joystick around to figure out the axis. After we figured the axis we did some calibration to the joystick so that it works near perfect.
// Arduino pin numbers
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output
int M1_Left = 12; //Motor Input 1
int M1_Right = 11; //Motor Input 2
int M2_Left = 10; //Motor Input 1
int M2_Right = 9; //Motor Input 2
void setup() {
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH);
Serial.begin(115200);
}
void loop() {
Serial.print("Switch: ");
Serial.print(digitalRead(SW_ pin));
Serial.print("\n");
Serial.print("X-axis: ");
Serial.print(analogRead(X_ pin));
Serial.print("\n");
Serial.print("Y-axis: ");
Serial.println(analogRead(Y_ pin));
Serial.print("\n\n");
delay(500);
if (analogRead(X_pin) >520)
{
forward(255);
}
else if (analogRead(X_pin)<470)
{
reverse(255);
}
else if ( analogRead(Y_pin)>500)
{
analogWrite (M2_Left, 0); //Motor Input 1
analogWrite(M2_Right , 255); //Motor Input 2
//analogWrite (M2_Left, 0); //Motor Input 1
// analogWrite(M2_Right , 0); //Motor Input 2
}
else if ( analogRead(Y_pin)<485)
{
analogWrite (M1_Left, 0); //Motor Input 1
analogWrite(M1_Right , 255); //Motor Input 2
//analogWrite (M2_Left, 0); //Motor Input 1
// analogWrite(M2_Right , 0); //Motor Input 2
}
else
stop();
}
void forward (int rate)
{
int inPin1 = LOW;
int inPin2 = rate;
analogWrite(M1_Left, inPin1);
analogWrite(M1_Right , inPin2);
analogWrite(M2_Left, inPin1);
analogWrite(M2_Right , inPin2);
}
void reverse (int rate)
{
int inPin1 = rate;
int inPin2 = LOW;
analogWrite(M1_Left, inPin1);
analogWrite(M1_Right , inPin2);
analogWrite(M2_Left, inPin1);
analogWrite(M2_Right , inPin2);
}
void stop()
{
int inPin1 = LOW;
int inPin2 = LOW;
digitalWrite(M1_Left, LOW);
digitalWrite(M1_Right , LOW);
digitalWrite(M2_Left, LOW);
digitalWrite(M2_Right , LOW);
Comments
Post a Comment