How to make Dual Axis Solar Tracker with Ldr sensor Using Arduino

 







            CODE 

#include <Servo.h>


int pos1 = 0;    // Position for the first servo

int pos2 = 0;    // Position for the second servo

Servo servo1;    // First servo

Servo servo2;    // Second servo


void setup() {

  pinMode(2, INPUT);  // Pin 2 for controlling servo1

  pinMode(3, INPUT);  // Pin 3 for controlling servo1

  pinMode(4, INPUT);  // Pin 4 for controlling servo2

  pinMode(5, INPUT);  // Pin 5 for controlling servo2

  servo1.attach(9);   // Pin 9 for servo1

  servo2.attach(10);  // Pin 10 for servo2

}


void loop() {

  // Control for servo1 using pins 2 and 3

  if (digitalRead(2) == HIGH && pos1 < 180) {

    pos1++;

    servo1.write(pos1);

    delay(15);

  } 

  else if (digitalRead(3) == HIGH && pos1 > 0) {

    pos1--;

    servo1.write(pos1);

    delay(15);

  }

  

  // If both pin2 and pin3 are LOW, servo1 stays at the current position

  else if (digitalRead(2) == LOW && digitalRead(3) == LOW) {

    servo1.write(pos1);  // Maintain the current position

  }

  

  // If both pin2 and pin3 are HIGH, servo1 stays at the current position

  else if (digitalRead(2) == HIGH && digitalRead(3) == HIGH) {

    servo1.write(pos1);  // Maintain the current position

  }


  // Control for servo2 using pins 4 and 5

  if (digitalRead(4) == HIGH && pos2 < 180) {

    pos2++;

    servo2.write(pos2);

    delay(15);

  } 

  else if (digitalRead(5) == HIGH && pos2 > 0) {

    pos2--;

    servo2.write(pos2);

    delay(15);

  }


  // If both pin4 and pin5 are LOW, servo2 stays at the current position

  else if (digitalRead(4) == LOW && digitalRead(5) == LOW) {

    servo2.write(pos2);  // Maintain the current position

  }

  

  // If both pin4 and pin5 are HIGH, servo2 stays at the current position

  else if (digitalRead(4) == HIGH && digitalRead(5) == HIGH) {

    servo2.write(pos2);  // Maintain the current position

  }

}


Post a Comment

Previous Post Next Post

Contact Form