Skip to content

Instantly share code, notes, and snippets.

@IOT-123
Last active January 16, 2019 07:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IOT-123/db6891a0c93cebb511c9e45597091cc3 to your computer and use it in GitHub Desktop.
Save IOT-123/db6891a0c93cebb511c9e45597091cc3 to your computer and use it in GitHub Desktop.
Initial sketch for tracking light source - not optimized.
/*
* modified from code
* by Mathias Leroy
*
*/
#include <stdio.h>
#include <Servo.h>
Servo servoH;
Servo servoV;
int analogPinTopLeft = A0;
int analogPinTopRight = A1;
int analogPinBottomRight = A3;
int analogPinBottomLeft = A2;
int initAngleH =90;
int minAngleH =0;
int maxAngleH =180;
int initAngleV =90;
int minAngleV =30;
int maxAngleV =150;
int sesitivityH=100;
int sesitivityV=100;
int stepH=5;
int stepV=5;
bool debugOn = false;
bool servosOn = true;
int servoLoopDelay = 10;
int slowingDelay=0;
int angleH = initAngleH;
int angleV = initAngleV;
char printLine [50];
int valueTopLeft = 0;
int valueTopRight = 0;
int valueBottomRight = 0;
int valueBottomLeft = 0;
int averageTop = 0;
int averageRight = 0;
int averageBottom = 0;
int averageLeft = 0;
void setup()
{
if (servosOn){
servoH.attach(5);
servoH.write(initAngleH);
servoV.attach(11);
servoV.write(initAngleV);
}
if (debugOn){
Serial.begin(115200);
Serial.println("Ready! :-)");
}
}
void loop()
{
//return;
if (debugOn) Serial.println("<<< Start Loop ---");
getLightValues();
moveServos();
if (debugOn) Serial.println("--- End Loop >>>");
}
void getLightValues(){
valueTopLeft = analogRead(analogPinTopLeft);
valueTopRight = analogRead(analogPinTopRight);
valueBottomRight = analogRead(analogPinBottomRight);
valueBottomLeft = analogRead(analogPinBottomLeft);
if (debugOn) {
sprintf (printLine, "%d | %d \n", valueTopLeft, valueTopRight);Serial.print(printLine);
sprintf (printLine, "%d | %d \n", valueBottomLeft, valueBottomRight);Serial.print(printLine);
delay(3000);
}
averageTop = ( valueTopLeft + valueTopRight ) / 2;
averageRight = ( valueTopRight + valueBottomRight ) / 2;
averageBottom = ( valueBottomRight + valueBottomLeft ) / 2;
averageLeft = ( valueBottomLeft + valueTopLeft ) / 2;
if (debugOn) {
sprintf (printLine, "- %d - \n", averageTop);Serial.print(printLine);
sprintf (printLine, "%d - %d \n", averageLeft,averageRight);Serial.print(printLine);
sprintf (printLine, "- %d - \n", averageBottom);Serial.print(printLine);
delay(slowingDelay);
}
}
void moveServos(){
if (debugOn) {
Serial.print(angleH);
Serial.print("\n");
}
if ( (averageLeft-averageRight)>sesitivityH && (angleH-stepH)>minAngleH ) {
if (debugOn) Serial.print("going left");
if (debugOn) Serial.print("\n");
delay(slowingDelay);
for (int i=0; i < stepH; i++){
if (servosOn) servoH.write(angleH--);
delay(servoLoopDelay);
}
}
else if ( (averageRight-averageLeft)>sesitivityH && (angleH+stepH)<maxAngleH ) {
if (debugOn) Serial.print("going right");
if (debugOn) Serial.print("\n");
delay(slowingDelay);
for (int i=0; i < stepH; i++){
if (servosOn) servoH.write(angleH++);
delay(servoLoopDelay);
}
}
else {
if (debugOn) Serial.print("doing nothing");
if (debugOn) Serial.print("\n");
delay(slowingDelay);
}
if (debugOn) Serial.print(angleH);
if (debugOn) Serial.print("\n\n");
if (debugOn) Serial.print(angleV);
if (debugOn) Serial.print("\n");
if ( (averageTop-averageBottom)>sesitivityV && (angleV+stepV)<maxAngleV ) {
// going up
if (debugOn) Serial.print("going up");
if (debugOn) Serial.print("\n");
delay(slowingDelay);
for (int i=0; i < stepV; i++){
if (servosOn) servoV.write(angleV++);
delay(servoLoopDelay);
}
}
else if ( (averageBottom-averageTop)>sesitivityV && (angleV-stepV)>minAngleV) {
if (debugOn) Serial.print("going down");
if (debugOn) Serial.print("\n");
delay(slowingDelay);
for (int i=0; i < stepV; i++){
if (servosOn) servoV.write(angleV--);
delay(servoLoopDelay);
}
}
else {
if (debugOn) Serial.print("doing nothing");
if (debugOn) Serial.print("\n");
delay(slowingDelay);
}
if (debugOn) Serial.print(angleV);
if (debugOn) Serial.print("\n\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment