/*
PROJECT TITLE: CAMERA CONTROLLER
Description: This circuit utilizes an Arduino NANO microcontroller along with
components including a Bipolar stepper motor, an A4988 Stepper Motor driver,
a Servo motor, and an Analog joystick. The Analog joystick serves as the interface
for controlling the direction of a camera or mobile phone remotely.
JoyStick controls
up, down, left, right arrow keys - alignment
spacebar/pushbutton - return to default position
*/
#include <AccelStepper.h>
#include <Servo.h>
AccelStepper stepper(AccelStepper::DRIVER, 4, 5); //to connect to non-default gpio pins
Servo myServo;
int Y = A0;
int YValue = 0;
int X = A1;
int XValue = 0;
int SW = A2;
int SWValue = 0;
int servoPin = 6;
int currPos = 90;
int tempPos;
void setup() {
Serial.begin(9600);
stepper.setMaxSpeed(1000); // Set the maximum speed in steps per second
stepper.setAcceleration(500); // Set the acceleration in steps per second per second
myServo.attach(servoPin);
initialise();
}
void initialise() {
stepper.runToNewPosition(0+2);
currPos = 90;
myServo.write(currPos);
}
void loop() {
XValue = analogRead(X);
XValue = map(XValue,0,1023,1000,0);
YValue = analogRead(Y);
YValue = map(YValue,0,1023,0,180);
SWValue = analogRead(SW);
Serial.print("XValue: ");
Serial.print(XValue);
Serial.print("\t");
Serial.print("YValue: ");
Serial.print(YValue);
Serial.print("\t");
Serial.print("SWValue: ");
Serial.println(SWValue);
delay(200);
if (XValue == 1000) {
stepper.runToNewPosition(stepper.currentPosition()+4);
}
else if(XValue == 0) {
stepper.runToNewPosition(stepper.currentPosition()-4);
}
else {
stepper.runToNewPosition(stepper.currentPosition());
}
if (YValue == 180) {
tempPos = currPos-4;
myServo.write(tempPos);
currPos = tempPos;
}
else if(YValue == 0) {
tempPos = currPos+4;
myServo.write(tempPos);
currPos = tempPos;
}
else {
myServo.write(currPos);
}
if(SWValue == 0) {
initialise();
}
}