Components and supplies
RobotGeek Light Sensor
Rotary potentiometer (generic)
Arduino UNO
Base Shield V2
Apps and platforms
Arduino IDE
Project description
Code
CODE FOR ARDUINO SUNFLOWER
arduino
1#include <Servo.h> // include Servo library 2 3Servo horizontal; // horizontal servo 4int servoh = 90; // stand horizontal servo 5 6Servo vertical; // vertical servo 7int servov = 90; // stand vertical servo 8 9// LDR pin connections 10// name = analogpin; 11int ldrrd =0; 12int ldrld =1; 13int ldrlt = 2; 14int ldrrt = 3; 15 16 17 18void setup() 19{ 20 Serial.begin(9600); 21// servo connections 22// name.attacht(pin); 23 horizontal.attach(9); 24 vertical.attach(10); 25} 26 27void loop() 28{ 29 int lt = analogRead(ldrlt); // top left 30 int rt = analogRead(ldrrt); // top right 31 int ld = analogRead(ldrld); // down left 32 int rd = analogRead(ldrrd); // down rigt 33 34 int dtime = analogRead(4)/20; // read potentiometers 35int tol = analogRead(5)/4; 36 37int avt = (lt + rt) / 2; // average value top 38int avd = (ld + rd) / 2; // average value down 39int avl = (lt + ld) / 2; // average value left 40int avr = (rt + rd) / 2; // average value right 41 42int dvert = avt - avd; // check the diffirence of up and down 43int dhoriz = avl - avr;// check the diffirence og left and rigt 44 45if (-1*tol > dvert || dvert > tol) // check if the diffirence is in the tolerance else change vertical angle 46{ 47if (avt > avd) 48{ 49servov = ++servov; 50if (servov > 180) 51{ 52servov = 180; 53} 54} 55else if (avt < avd) 56{ 57servov= --servov; 58if (servov < 0) 59{ 60servov = 0; 61} 62} 63vertical.write(servov); 64} 65 66if (-1*tol > dhoriz || dhoriz > tol) // check if the diffirence is in the tolerance else change horizontal angle 67{ 68if (avl > avr) 69{ 70servoh = --servoh; 71if (servoh < 0) 72{ 73servoh = 0; 74} 75} 76else if (avl < avr) 77{ 78servoh = ++servoh; 79if (servoh > 180) 80{ 81servoh = 180; 82} 83} 84else if (avl == avr) 85{ 86// nothing 87} 88horizontal.write(servoh); 89} 90delay(dtime); 91} 92
CODE FOR ARDUINO SUNFLOWER
arduino
1#include <Servo.h> // include Servo library 2 3Servo horizontal; 4 // horizontal servo 5int servoh = 90; // stand horizontal servo 6 7Servo vertical; 8 // vertical servo 9int servov = 90; // stand vertical servo 10 11// LDR pin 12 connections 13// name = analogpin; 14int ldrrd =0; 15int ldrld =1; 16int ldrlt 17 = 2; 18int ldrrt = 3; 19 20 21 22void setup() 23{ 24 Serial.begin(9600); 25// 26 servo connections 27// name.attacht(pin); 28 horizontal.attach(9); 29 vertical.attach(10); 30} 31 32void 33 loop() 34{ 35 int lt = analogRead(ldrlt); // top left 36 int rt = analogRead(ldrrt); 37 // top right 38 int ld = analogRead(ldrld); // down left 39 int rd = analogRead(ldrrd); 40 // down rigt 41 42 int dtime = analogRead(4)/20; // read potentiometers 43int 44 tol = analogRead(5)/4; 45 46int avt = (lt + rt) / 2; // average value top 47int 48 avd = (ld + rd) / 2; // average value down 49int avl = (lt + ld) / 2; // average 50 value left 51int avr = (rt + rd) / 2; // average value right 52 53int dvert = 54 avt - avd; // check the diffirence of up and down 55int dhoriz = avl - avr;// check 56 the diffirence og left and rigt 57 58if (-1*tol > dvert || dvert > tol) // check 59 if the diffirence is in the tolerance else change vertical angle 60{ 61if (avt 62 > avd) 63{ 64servov = ++servov; 65if (servov > 180) 66{ 67servov = 180; 68} 69} 70else 71 if (avt < avd) 72{ 73servov= --servov; 74if (servov < 0) 75{ 76servov = 0; 77} 78} 79vertical.write(servov); 80} 81 82if 83 (-1*tol > dhoriz || dhoriz > tol) // check if the diffirence is in the tolerance 84 else change horizontal angle 85{ 86if (avl > avr) 87{ 88servoh = --servoh; 89if 90 (servoh < 0) 91{ 92servoh = 0; 93} 94} 95else if (avl < avr) 96{ 97servoh 98 = ++servoh; 99if (servoh > 180) 100{ 101servoh = 180; 102} 103} 104else if (avl 105 == avr) 106{ 107// nothing 108} 109horizontal.write(servoh); 110} 111delay(dtime); 112} 113 114
Downloadable files
CONNECTIONS
CONNECTIONS
Documentation
CONNECTIONS
CONNECTIONS
CONNECTIONS
CONNECTIONS
Comments
Only logged in users can leave comments