Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I. Wire up the joystick to your Arduino and write a program to print the joystic

ID: 3879869 • Letter: I

Question

I. Wire up the joystick to your Arduino and write a program to print the joystick position to the serial port every 100 milliseconds a. Connect the joystick to the Arduino using this table: Arduino GND 5V A0 A1 JoystickMeaning Ground Power GND +5V VRx VRy Xaxis pin Y axis pin II. Create a motor driver that uses a joystick to control speed and direction a. Connect the H-bridge to the Arduino b. Before connecting the external power supply to the H-bridge, have a TA check your wiring: incorrectly wiring these components can seriously damage your Arduino Pushing the joystick up will increase the forward speed of the motor by an amount proportional to the offset of the joystick and pushing the joystick down will increase the reverse speed of the motor by an amount proportional to the offset of the joystick c. d. If the joystick is within 10% of its center value, the motor should be stopped Show the voltage of one of the joystick axes and the motor voltage variation using 2 channels of an oscilloscope. e. III. Attach a servo to your Arduino Connect the control pin of the servo to a PWM-capable pin a. Connect the servo's power and ground pins to the lab station's power supply, but do not turn the power on until a TA checks your wiring b. Pushing the joystick to the left will rotate the servo counterclockwise by a value by an amount proportional to the offset of the joystick and pushing the joystick to the right will rotate the servo clockwise by a value by an amount proportional to the offset of the joystick c. d. Show the servo signal on an oscilloscope. IV. After demonstrating your solutions, turn off the power supply and oscilloscope.

Explanation / Answer

1.

int Xpin = A0; //Define X axis pin to A0
int Ypin = A1; //Define Y axis pin to A1
int Xval = 0; //Value from X-axis
int Yval = 0; //Value from Y-axis
void setup()
{
Serial.begin(9600); //Initialise Serial Monitor
}

void loop()
{
Xval = analogRead(Xpin); //Read X-axis value
Yval = analogRead(Ypin); //Read Y-axos value
//Print to Serial monitor
Serial.print("X-axis value: ");
Serial.println(Xval);
Serial.print("Y-axis value: ");
Serial.println(Yval);
//100ms delay
delay(100);
}

2.

int Xpin = A0; //Define X axis pin to A0
int Ypin = A1; //Define Y axis pin to A1
int Xval = 0; //Value from X-axis
int Yval = 0; //Value from Y-axis
int speed = 0; //Speed of motor
void setup()
{
Serial.begin(9600); //Initialise Serial Monitor
//Initially Set H-bridge to off
digitalWrite(0, LOW);
digitalWrite(1, LOW);
analogWrite(3,speed);
}

void loop()
{
Xval = analogRead(Xpin); //Read X-axis value
Yval = analogRead(Ypin); //Read Y-axos value
//Print to Serial monitor
Serial.print("X-axis value: ");
Serial.println(Xval);
Serial.print("Y-axis value: ");
Serial.println(Yval);
//Forward Direction
if(Yval > 550)
{
//Set motor to forward direction
digitalWrite(0,LOW);
digitalWrite(1,HIGH);
//Map speed to range
speed = map(Yval, 550, 1023, 0, 255);
//Write to PWM pin
analogWrite(3, speed);
}
//Backward Direction
else if(Yval < 470)
{
//Set motor to backward direction
digitalWrite(0,HIGH);
digitalWrite(1,LOW);
//map speed to range
speed = map(Yval, 470, 0, 0, 255);
//Write to PWM pin
analogWrite(3, speed);
}
//OFF
else
{
digitalWrite(0,LOW);
digitalWrite(1,LOW);
speed = 0;
analogWrite(3, speed);
}
  
//100ms delay
delay(100);
}

3.

#include <Servo.h>

Servo servo_test; //Servo instance

int Xpin = A0; //Define X axis pin to A0
int Ypin = A1; //Define Y axis pin to A1
int Xval = 0; //Value from X-axis
int Yval = 0; //Value from Y-axis
int angle = 0; //Angle of servo
void setup()
{
Serial.begin(9600); //Initialise Serial Monitor
servo_test.attach(3); //Attach Servo to pin 3
}

void loop()
{
Xval = analogRead(Xpin); //Read X-axis value
Yval = analogRead(Ypin); //Read Y-axos value
//Print to Serial monitor
Serial.print("X-axis value: ");
Serial.println(Xval);
Serial.print("Y-axis value: ");
Serial.println(Yval);
//Right Direction
if(Xval > 511)
{
angle = map(Xval,511,1023,0,179); //Map servo to clockwise angle
servo_test.write(angle); //Write to servo
}
//Left Direction
else
{
angle = map(Xval,511,0,179,0); //Map servo to counter-clockwise angle
servo_test.write(angle); //Write to servo
}
  
//100ms delay
delay(100);
}

Hope this helps. Do Upvote! :)