This document contains Arduino and MATLAB code for controlling dual-axis and single-axis servo motors. The dual-axis code uses four light dependent resistors (LDRs) to sense light levels and control two servos to adjust the horizontal and vertical positions. The single-axis code uses two LDRs to sense light levels and control one servo to adjust the horizontal position until the LDR readings are within a set error tolerance. Both codes take continuous LDR readings, calculate differences, and adjust the servo positions accordingly.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
190 views
Arduino Code Dual Axis
This document contains Arduino and MATLAB code for controlling dual-axis and single-axis servo motors. The dual-axis code uses four light dependent resistors (LDRs) to sense light levels and control two servos to adjust the horizontal and vertical positions. The single-axis code uses two LDRs to sense light levels and control one servo to adjust the horizontal position until the LDR readings are within a set error tolerance. Both codes take continuous LDR readings, calculate differences, and adjust the servo positions accordingly.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2
ARDUINO CODE DUAL AXIS MATLAB CODE DUAL AXIS ARDUINO CODE SINGLE AXIS MATLAB CODE SINGLE
ARDUINO CODE SINGLE AXIS MATLAB CODE SINGLE AXIS
#include <Servo.h> // include Servo library clear all; clear all; #include <Servo.h> //including clc; clc; the library of servo motor Servo horizontal; // horizontal servo int servoh = 90; // stand horizontal servo c=arduino('COM31'); %Create Arduino c = arduino() Object Servo sg90; //initializing a Servo vertical; // vertical servo variable for servo named sg90 servoAttach(c,9); int servov = 90; // stand vertical servo servoAttach(c,9); %Horizontal Servo servoStatus(c,9); servoAttach(c,10); %Vertical Servo int initial_position = 90; //Declaring // LDR pin connections the initial position at 90 servoh=90; // name = analogpin; servoStatus(c,9); int ldrlt = A0; //LDR top left servoStatus(c,10); int ldrrt = A1; //LDR top rigt int LDR1 = A0; //Pin at which tic int ldrld = A2; //LDR down left servoh=90; %Horizontal Servo initial LDR is connected while toc<60 int ldrrd = A3; //ldr down rigt position int spd = A4; //speed servov=90; %Vertical Servo initial position R1=c.analogRead(0); int LDR2 = A1; //Pin at which int tole = A5; //tolerance R2=c.analogRead(1); LDR is connected tic while toc < 60 error=5; void setup() int error = 5; //initializing { lt=c.analogRead(0) variable for error diff1= abs(R1-R2); Serial.begin(9600); rt=c.analogRead(1) diff2= abs(R2-R1); // servo connections ld=c.analogRead(2) int servopin=9; // name.attacht(pin); rd=c.analogRead(3) if((diff1<=error) || horizontal.attach(9); spd=c.analogRead(4)/20 (diff2<=error)) void setup() vertical.attach(10); tol=c.analogRead(5)/4 else if(R1>R2) { } servoh=servoh-1; avt = (lt + rt) / 2; % average value top sg90.attach(servopin); // attaches end void loop() avd = (ld + rd) / 2; % average value down the servo on pin 9 if(R1<R2) { avl = (lt + ld) / 2; % average value left pinMode(LDR1, INPUT); //Making servoh=servoh+1; int lt = analogRead(ldrlt); // top left avr = (rt + rd) / 2; % average value right the LDR pin as input end int rt = analogRead(ldrrt); // top right pinMode(LDR2, INPUT); servoWrite(c,9,servoh); int ld = analogRead(ldrld); // down left dvert = avt - avd; % check the diffirence of sg90.write(initial_position); //Move end int rd = analogRead(ldrrd); // down rigt up and down servo at 90 degree delay(100); dhoriz = avl - avr; % check the diffirence of delay(2000); // giving a end int dtime = analogRead(spd)/20; // read left and right delay of 2 seconds potentiometers } int tol = analogRead(tole)/4; %check if the diffirence is in the tolerance void loop() int avt = (lt + rt) / 2; // average value top else change vertical angle { int avd = (ld + rd) / 2; // average value if (-1*tol > dvert || dvert > tol) int R1 = analogRead(LDR1); // down if (avt > avd) reading value from LDR 1 int avl = (lt + ld) / 2; // average value left servov = servov+1; int avr = (rt + rd) / 2; // average value right if servov > 180; int R2 = analogRead(LDR2); // servov = 180; reading value from LDR 2 int dvert = avt - avd; // check the end difference of up and down servoWrite(c,10,servov); int dhoriz = avl - avr;// check the end int diff1= abs(R1 - R2); // diffirence of left and right Calculating the difference between else if (avt < avd) the LDR's if (-1*tol > dvert || dvert > tol) // check if servov = servov-1; the difference is in the tolerance else change if (servov < 0) vertical angle servov = 0; int diff2= abs(R2 - R1); { end if (avt > avd) servoWrite(c,10,servov); if((diff1 <= error) || (diff2 <= error)) { end { servov = ++servov; //if the difference is under the if (servov > 180) %servoWrite(c,10,servov); error then do nothing { end } servov = 180; else { } if(R1 > R2) } %check if the diffirence is in the tolerance { else if (avt < avd) else change horizontal angle initial_position = -- { if (-1*tol > dhoriz || dhoriz > tol) initial_position; //Move the servo servov= --servov; if (avl > avr) towards 0 degree if (servov < 0) servoh = servoh-1; } { if (servoh < 0) if(R1 < R2) servov = 0; servoh = 0; { } end initial_position = } servoWrite(c,9,servoh); ++initial_position; //Move the servo vertical.write(servov); end towards 180 degree } } else if (avl < avr) } if (-1*tol > dhoriz || dhoriz > tol) // check servoh = servoh+1; sg90.write(initial_position); // write if the diffirence is in the tolerance else if (servoh > 180) the position to servo change horizontal angle servoh = 180; { end if (avl > avr) servoWrite(c,9,servoh); delay(100); { end } servoh = --servoh; if (servoh < 0) %servoWrite(c,9,servoh); { servoh = 0; end } } end else if (avl < avr) { servoh = ++servoh; if (servoh > 180) { servoh = 180; } } else if (avl = avr) { // nothing } horizontal.write(servoh); } delay(dtime); }