Nothing Special   »   [go: up one dir, main page]

Enhanced Dual Axis Program

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

#include <Servo.

h> //includes the servo library


#include<LiquidCrystal.h> //includes the lcd library
#define sensor A0

#defineVOLTA1
#define LUX A3

LiquidCrystallcd(2,3,4,5,6,7);

floatTemperature,temp,volt,volts,lux,Temp; int
temp1, value,analog_value;

bytedegree[8] =
{

0b00011,
0b00011,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000
};
Servomyservo;
intpos=90;//settheinitialpositionoftheHorizontalmovement,controllingfirstservo motor
intldr1=A0;//setldr1topinA0asaninteger
intldr2=A1;//setldr2topinA1asaninteger
int tolerance = 2; // allowable tolerance setting - so as to prevent servo motor constantly
in motion
intx;//setxasabsoluteofldr2-ldr1
inty;//setyasabsoluteofldr1-ldr2
Servo myservo2;

intpos2=90;//initialpositionoftheVerticalmovementcontrollingsecondservomotor int ldr3


= A2; // set ldr 3 to pin A2 as an integer

intldr4=A3;//setldr4topinA3asaninteger int ldr5 =


A4;//set ldr 5 to pin A4 as an integer

int tolerance2 = 2; //allowable tolerance setting - so as to prevent servo motor constantly


in motion
intp;//setpasabsoluteofldr4-ldr3
intq;//setqasabsoluteofldr3-ldr4
boolean dark = false;

constintDAWN=600;
constintDUSK=1800;

void setup()
{

lcd.begin(16,2);
lcd.createChar(1,degree);
Serial.begin(9600);
lcd.setCursor(0,0);
lcd.print("SunTracking");
lcd.setCursor(0,1);
lcd.print("System");
delay(2000);
lcd.clear();
lcd.setCursor(0,0);

lcd.print("ByAminu,BelloBodinga");
lcd.setCursor(0,1);
lcd.print("12210310012");
delay(2000);

lcd.clear();
myservo.attach(9); // attaches the servo on digital pin 9 to the horizontal movement servo
motor
pinMode(ldr1,INPUT);//setldr1pinasaninput
pinMode(ldr2,INPUT);//setldr2pinasaninput

myservo.write(pos);// writethestartingpositionofthehorizontalmovementservomotor
delay(2000);//2seconddelaytoallowthesolarpaneltomovetoitsstaringposition before
comencing solar tracking
myservo2.attach(10);//attachestheservoondigitalpin10totheverticalmovement servo motor
pinMode(ldr3,INPUT);//setldr3pinasaninput
pinMode(ldr4,INPUT);//setldr4pinasaninput

myservo2.write(pos2);//writethestartingpositionoftheverticalmovementservomotor
delay(2000);//2seconddelaytoallowthesolarpaneltomovetoitsstaringposition before
comencing solar tracking
}
void loop()
{

/*---------Temperature--------*/
float reading=analogRead(sensor);
Temperature=reading*(5.0/1023.0)*100;
delay(10);

/* Voltage */
temp1=analogRead(VOLT);
volts= (temp1/511.5)*5;
delay(10);

/*-----LightIntensity--------*/
value=analogRead(LUX);
volt=(value/1023.0)*5;
lux=((2500/volt)-500)/3.3;
delay(10);

/*------DisplayResult--------*/
lcd.clear();
lcd.setCursor(0,0);
lcd.print("T:");
lcd.print((int)analog_value);
lcd.write(1);

lcd.print("C");
lcd.setCursor(8,0);
lcd.print("V:");
lcd.print(volts);
lcd.setCursor(0,1);
lcd.print("LDR1: ");
lcd.print((int)lux);
lcd.print(" Lux");
Serial.println((int)Temp);
Serial.println(volts);
Serial.println((int)lux);
delay(500);

intval1= analogRead(ldr1);//read thevalue ofldr 1


int val2 = analogRead(ldr2); // read the value of ldr 2int
val3 = analogRead(ldr3); // read the value of ldr 2int
val4 = analogRead(ldr4); // read the value of ldr
4x=abs(val1-val2);//computexasabsoluteofldr1-ldr2
y=abs(val2-val1);//computeyasabsoluteofldr2-ldr1
p=abs(val3-val4);//computepasabsoluteofldr3-ldr4 q=
abs(val4 - val3); //compute qas absolute of ldr4-ldr3

//Tocheckday/nightsituations
/*
if(light)

{
if(analogRead(ldr5)<DUSK)
{
dark=true;
//stufftodowhenitchangesfromlighttodark
}
}
else
{
if(analogRead(ldr5)>DAWN)
{

dark=false;
//stufftodowhenitchangesfromdarktolight
}
}
*/

//Controllingeast-westmovementfortheservo.i.ehorizontaltracking if((x <=


tolerance) || (y <= tolerance)) {
//noservomotorhorizontalmovementwilltakeplaceiftheldrvalueiswithinthe allowable
tolerance
}else{
if(val1>val2)//ifldr1sensesmorelightthanldr2
{
pos = pos+1; // decrement the 90 degree poistion of the horizontal servo motor - this will
move the panel position Eastward
}
if(val1<val2)//ifldr2sensesmorelightthanldr1
{
pos = pos-1; // increment the 90 degree position of the horizontal motor - this will move
the panel position Westward
}
delay(500);
}
if(pos>180)

{
pos=180;//resetthehorizontalpostionofthe motorto180ifittriestomovepastthis point
}
if(pos<0)

{
pos=0;//resetthehorizontalpositionofthemotorto0ifittriestomovepastthispoint
}

myservo.write(pos);//writethestartingpositiontothehorizontalmotor
delay(50);

//Controllingnorth-southmovementfortheservo.i.everticaltracking if((p <=


tolerance) || (q <= tolerance)) {

//noservomotorverticalmovementwilltakeplaceiftheldrvalueiswithinthe allowable
tolerance
}else{
if(val3>val4)//ifldr3sensesmorelightthanldr4
{
pos2 = pos+1; // decrement the 90 degree poistion of the vertical servo
motor - this will move the panel position Northward
}
if(val4>val3)//ifldr4sensesmorelightthanldr3
{
pos2 = pos-1; // increment the 90 degree position of the vertical motor -
this will movethe panel position Southward
}
delay(900);
}
//
resettheverticalpostionofthemotorto180ifittriestomovepastthispo
int if(pos2 > 180)

{
pos2=180;
}
//
resettheverticalpositionofthemotorto0ifittriestomovepastthispoin
t if(pos2 < 0)

pos2=0;
}

myservo2.write(pos2);//
writethestartingpositiontotheverticalmovementmotor delay(50);

You might also like