Stap 4: programma
Zie bijgevoegde Ino bestand
/* SOLAR TRACKER 1.0 * by Mathias Leroy * Novembre 2014 * */// INITIALISATION //------------------------------------------------ #include // sprintf #include // Servo library Servo servoH; // pin09 horizontal servo Servo servoV; // pin10 vertical servo// analog read pin connections int analogPinTopLeft = 0; // yellow green int analogPinTopRight = 1; // gray blue int analogPinBottomRight = 2; // white black int analogPinBottomLeft = 3; // red brown// PARAMETERS //------------------------------------------------ int initAngleH =80; int minAngleH =0; int maxAngleH =170; int initAngleV =90; int minAngleV =50; int maxAngleV =150;int slowingDelay=50;int sesitivityH=30; int sesitivityV=30; int stepH=10; int stepV=10;// VARIABLES //------------------------------------------------ int angleH =initAngleH; int angleV =initAngleV;char printLine [50];int valueTopLeft = 0; int valueTopRight = 0; int valueBottomRight = 0; int valueBottomLeft = 0;// SETUP //------------------------------------------------ void setup() { servoH.attach(9); // servoH.write(initAngleH); servoV.attach(10); // servoV.write(initAngleV); Serial.begin(9600); Serial.println("Ready! :-)"); }// LOOP //------------------------------------------------ void loop() { Serial.println("<<< Start Loop ---"); // read values valueTopLeft = analogRead(analogPinTopLeft); valueTopRight = analogRead(analogPinTopRight); valueBottomRight = analogRead(analogPinBottomRight); valueBottomLeft = analogRead(analogPinBottomLeft); // print values // sprintf (printLine, "%d | %d \n", valueTopLeft, valueTopRight);Serial.print(printLine); // sprintf (printLine, "%d | %d \n", valueBottomRight, valueBottomLeft);Serial.print(printLine); // delay(3000); // compute averages int averageTop = ( valueTopLeft + valueTopRight ) / 2; int averageRight = ( valueTopRight + valueBottomRight ) / 2; int averageBottom = ( valueBottomRight + valueBottomLeft ) / 2; int averageLeft = ( valueBottomLeft + valueTopLeft ) / 2; // print averages 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); // Horizontal decision & action Serial.print(angleH); Serial.print("\n"); if ( (averageLeft-averageRight)>sesitivityH && (angleH-stepH)>minAngleH ) { // going left Serial.print("going left"); Serial.print("\n"); delay(slowingDelay); for (int i=0; i < stepH; i++){ servoH.write(angleH--); delay(50); } } else if ( (averageRight-averageLeft)>sesitivityH && (angleH+stepH) // vertical decision & action Serial.print(angleV); Serial.print("\n"); if ( (averageTop-averageBottom)>sesitivityV && (angleV-stepV)>minAngleV ) { // going up Serial.print("going up"); Serial.print("\n"); delay(slowingDelay); for (int i=0; i < stepV; i++){ servoV.write(angleV--); delay(50); } } else if ( (averageBottom-averageTop)>sesitivityV && (angleV+stepV) Serial.println("--- End Loop >>>"); }