Arduino Solar Tracker System (Project Codes)






#include <Servo.h> // include Servo library

Servo horizontal; // horizontal servo
int servoh = 90; // kite adjust nak berapa degree untuk starting

Servo vertical; // vertical servo
int servov = 90; // kite adjust nak berapa degree untuk starting


// name = analogpin;
int ldrlt = 0; //LDR top left
int ldrrt = 1; //LDR top rigt
int ldrld = 2; //LDR down left
int ldrrd = 3; //ldr down rigt

void setup()
{
  Serial.begin(9600);
// servo connections (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 kelajuan = analogRead(5)/20; // read speed
int tolerence = analogRead(5)/4; // read sensitivity

int avl = (lt + ld) / 2; // average value left
int avr = (rt + rd) / 2; // average value right
int avt = (lt + rt) / 2; // average value top
int avd = (ld + rd) / 2; // average value down

int dhoriz = avl - avr;// check the diffirence og left and rigt
int dvert = avt - avd; // check the diffirence of up and down


if (-1*tolerence > dhoriz||dhoriz > tolerence) // check if the diffirence is in the tolerance else change horizontal angle
{
if (avl > avr)
{
servoh = --servoh;
if (servoh < 0)
{
servoh = 0; //servo horizantal static
}
}
else if (avl < avr)
{
servoh = ++servoh;
if (servoh > 160) //servo akan bergerak dari kanan ke kiri sebanyak 160 degree
{
servoh = 160;
}
}
else if (avl == avr) //average apabila bahagian kiri vertikal dan bahagian kanan vertikal sama,solar akan static
{
// nothing
}
horizontal.write(servoh);
}


if (-1*tolerence > dvert || dvert > tolerence) // check if the diffirence is in the tolerance else change vertical angle
{
if (avt > avd)
{
servov = ++servov;
if (servov > 100)// servo akan bergerak dari atas ke bawah sebanyak 100 degree
{
servov = 100;
}
}
else if (avt < avd)
{
servov= --servov;
if (servov < 0)
{
servov = 0;
}
}
vertical.write(servov);
}


delay(kelajuan);
}

Comments