I'm trying to convert an arduino code into matlab code. The code is for a solar tracker that uses four LDRs that are connected in such a way that the servo motor will move so the average readings on the LDR will be zero. I found it here: http://www.instructables.com/id/Arduino-Solar-Tracker/
I'm using it for our project but we are required to use matlab and have a GUI.
Arduino Code:
#include <Servo.h> // include Servo library
Servo horizontal; // horizontal servo
int servoh = 90; // stand horizontal servo
Servo vertical; // vertical servo
int servov = 90; // stand vertical servo
// LDR pin connections
// name = analogpin;
int ldrlt = A0; //LDR top left
int ldrrt = A1; //LDR top rigt
int ldrld = A2; //LDR down left
int ldrrd = A3; //ldr down rigt
int spd = A4; //speed
int tole = A5; //tolerance
void setup()
{
Serial.begin(9600);
// servo connections
// name.attacht(pin);
horizontal.attach(9);
vertical.attach(10);
}
void loop()
{
int lt = analogRead(ldrlt); // top left
int rt = analogRead(ldrrt); // top right
int ld = analogRead(ldrld); // down left
int rd = analogRead(ldrrd); // down rigt
int dtime = analogRead(spd)/20; // read potentiometers
int tol = analogRead(tole)/4;
int avt = (lt + rt) / 2; // average value top
int avd = (ld + rd) / 2; // average value down
int avl = (lt + ld) / 2; // average value left
int avr = (rt + rd) / 2; // average value right
int dvert = avt - avd; // check the difference of up and down
int dhoriz = avl - avr;// check the diffirence of left and right
if (-1*tol > dvert || dvert > tol) // check if the difference is in the tolerance else change vertical angle
{
if (avt > avd)
{
servov = ++servov;
if (servov > 180)
{
servov = 180;
}
}
else if (avt < avd)
{
servov= --servov;
if (servov < 0)
{
servov = 0;
}
}
vertical.write(servov);
}
if (-1*tol > dhoriz || dhoriz > tol) // check if the diffirence is in the tolerance else change horizontal angle
{
if (avl > avr)
{
servoh = --servoh;
if (servoh < 0)
{
servoh = 0;
}
}
else if (avl < avr)
{
servoh = ++servoh;
if (servoh > 180)
{
servoh = 180;
}
}
else if (avl = avr)
{
// nothing
}
horizontal.write(servoh);
}
delay(dtime);
}
SO far, this is the MATLAB code that I was able to workout:
clear all;clc;
c=arduino('COM31'); %Create Arduino Object
servoAttach(c,9); %Horizontal Servo
servoAttach(c,10); %Vertical Servo
servoStatus(c,9);
servoStatus(c,10);
servoh=90; %Horizontal Servo initial position
servov=90; %Vertical Servo initial position
tic
while toc < 60
lt=c.analogRead(0)
rt=c.analogRead(1)
ld=c.analogRead(2)
rd=c.analogRead(3)
spd=c.analogRead(4)/20
tol=c.analogRead(5)/4
avt = (lt + rt) / 2; % average value top
avd = (ld + rd) / 2; % average value down
avl = (lt + ld) / 2; % average value left
avr = (rt + rd) / 2; % average value right
dvert = avt - avd; % check the diffirence of up and down
dhoriz = avl - avr; % check the diffirence of left and right
%check if the diffirence is in the tolerance else change vertical angle
if (-1*tol > dvert || dvert > tol)
if (avt > avd)
servov = servov+1;
if servov > 180;
servov = 180;
end
servoWrite(c,10,servov);
end
else if (avt < avd)
servov = servov-1;
if (servov < 0)
servov = 0;
end
servoWrite(c,10,servov);
end
%servoWrite(c,10,servov);
end
%check if the diffirence is in the tolerance else change horizontal angle
if (-1*tol > dhoriz || dhoriz > tol)
if (avl > avr)
servoh = servoh-1;
if (servoh < 0)
servoh = 0;
end
servoWrite(c,9,servoh);
end
else if (avl < avr)
servoh = servoh+1;
if (servoh > 180)
servoh = 180;
end
servoWrite(c,9,servoh);
end
%servoWrite(c,9,servoh);
end
end
My problem is that the servos move very slowly in matlab. I'm not sure if this is because somthing is wrong with the code. I have even removed the pause code but it doesn't help. Also, I only used the tic toc for testing because I do not know what to replace the "void loop". For the GUI, I would want the program to run continuously until a stop button is pressed, and the delay(speed) of the servo and the tolerance of the LDRs will be varied using sliders. I'm having problem on returning the values of the slider to the main program.