Rotating Spotlight by Riley and Philip
by PHS_Engineering in Circuits > Arduino
427 Views, 1 Favorites, 0 Comments
Rotating Spotlight by Riley and Philip
For our midterm project, we decided to create a rotating spotlight controlled by a button and a joystick. The button turned on the flashlight, and the joystick controls the movement of it on the X and Y axis.
Supplies
Wires, steele supports, one large breadboard, one small breadboard, two servo motors, an arduino flashlight, a joystick, a button, and screws, to hold it all together.
Base
We created our metal base out of three of the same metal objects. We created a rotating base along the x axis using one of the motors, and then attached the other motor to the top, causing it to make the flashlight rotate on the y axis. We also glued the small breadboard to the side of the base to allow for easy wiring and to keep everything organized.
Controller
For our controller, we created a metal base, and then screwed the button onto one side of it. We then placed a piece of rubber on the other side, and zip tied the joystick on top of that. We wired the button and joystick on the large breadboard.
Wiring
We did the majority of our coding on the large breadboard. The joystick, button, and flashlight are wired on the large breadboard. The motors are wired on the small breadboard, and then connected to the large breadboard. From the large breadboard, everything is wired to our Arduino.
Coding
Our coding was pretty difficult. We had to map each motor to a different axis on the joystick, and create peramiters for how fast the motors will go, and how sensitive the joystick is. We also had to use digital write to allow for the light to turn on and off at the press of a button. Our code is below.
#include
#define LED_PIN 2 #define BUTTON_PIN A5
Servo servo1;
Servo servo2;
int xjoystick = A0;
int yjoystick = A1;
int xdirection;
int ydirection;
int servo1_pos=90;
int servo2_pos=90;
int c = 0;
void setup ( )
{
Serial.begin (9600) ;
servo1.attach (9) ;
servo2.attach (11) ;
servo1.write (servo1_pos);
servo2.write (servo2_pos); pinMode (xjoystick, INPUT) ;
pinMode (yjoystick, INPUT) ;
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
}
void loop ( )
{
xdirection = analogRead (xjoystick) ;
ydirection = analogRead (yjoystick) ;
if (xdirection < 300)
{ if (servo1_pos < 10)
{
} else
{servo1_pos = servo1_pos - 20;
servo1.write ( servo1_pos ) ;
delay (100);}
}
if (xdirection > 700)
{ if (servo1_pos > 180) {
}
else
{
servo1_pos = servo1_pos + 20;
servo1.write ( servo1_pos ) ;
delay (100) ; }
}
if (ydirection < 300)
{
if (servo2_pos < 10)
{
}
else
{
servo2_pos = servo2_pos - 20;
servo2.write ( servo2_pos );
delay (100);
}
}
if (ydirection > 700)
{
if (servo2_pos > 180)
{
}
else
{
servo2_pos = servo2_pos + 20;
servo2.write(servo2_pos) ;
delay (100) ;
}
}
if (analogRead(BUTTON_PIN) == HIGH)
{
digitalWrite(LED_PIN, HIGH);
c = c+1;
} if (analogRead(BUTTON_PIN) == HIGH){
digitalWrite(LED_PIN, LOW);
}
}
Conclusion
In conclusion, we got our project to work how we wanted it to. We are satisfied with our results in each step. Next time if we were to do something different we would start researching and working on the coding earlier. Attached is the final video of our project.