Forked from chetankothari/dual_axis_three_ldr_sun_tracker.ino
Last active
October 25, 2016 14:22
-
-
Save hrj/037f18bcc8c67a4f2d6ebf0bfbc778f3 to your computer and use it in GitHub Desktop.
Dual axis sun tracker using three LDR and Arduino Uno
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Authored by the "HeadBanger" team at SHD India. | |
// Shared under MIT License | |
#include <Servo.h> | |
#define NUM_PINS 3 | |
int sensorPins[NUM_PINS] = {A0, A1, A2}; | |
int sensorValues[NUM_PINS] = {0, 0, 0}; | |
Servo lrservo; | |
Servo tbservo; | |
void setup() { | |
Serial.begin(9600); //sets serial port for communication | |
lrservo.attach(10, 544, 2260); // attaches the servo on pin 9 to the servo object | |
tbservo.attach(9, 544, 2260); // attaches the servo on pin 9 to the servo object | |
lrservo.write(90); | |
tbservo.write(90); | |
} | |
char str[1024]; | |
int lrangle = 90; | |
int tbangle = 90; | |
void readInputs() { | |
for (int i = 0; i < NUM_PINS; i++) { | |
sensorValues[i] = sensorValues[i] * 0.6 + analogRead(sensorPins[i]) * 0.4; | |
} | |
} | |
int getAngle(int a, int b, int init, int fast, int min, int max) { | |
int angle = init; | |
if (a < b) { | |
angle += (b - a) > 20 ? fast : (b - a) > 4 ? fast / 2 : 0; | |
} else { | |
angle -= (a - b) > 20 ? fast : (a - b) > 4 ? fast / 2 : 0; | |
} | |
if (angle < min) { | |
angle = min; | |
} else if (angle > max) { | |
angle = max; | |
} | |
return angle; | |
} | |
void operate() { | |
readInputs(); | |
int top = sensorValues[1]; | |
int bottom = sensorValues[2]; | |
int left = sensorValues[0]; | |
int right = (sensorValues[1] + sensorValues[2]) / 2; | |
tbangle = getAngle(top, bottom, tbangle, 8, 30, 170); | |
lrangle = getAngle(left, right, lrangle, 8, 20, 160); | |
sprintf(str, "%d %d %d, TB : %d %d, LR: %d %d lrangle: %d, tbangle: %d", sensorValues[0], sensorValues[1], sensorValues[2], top, bottom, left, right, lrangle, tbangle); | |
Serial.println(str); | |
lrservo.write(lrangle); | |
tbservo.write(tbangle); | |
} | |
void loop() { | |
operate(); | |
delay(5); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment