A program to control a solar tracking platform to keep solar panels pointed at the sun. Details of the hardware can be found at mdpub.com.
main.cpp
- Committer:
- omegageek64
- Date:
- 2013-07-08
- Revision:
- 0:c5d947408ea3
File content as of revision 0:c5d947408ea3:
#include "mbed.h" //Program to control a sun tracking platform for solar panels. //Details of the hardware can be found at mdpub.com void stop(); void moveW(); void moveE(); void resetE(); void readcells(); Serial pc(USBTX, USBRX); AnalogIn Ecell(p19), Wcell(p20); DigitalIn ELimit(p5), WLimit(p6); InterruptIn EInt(p8), WInt(p9); DigitalOut MotorOn(p22), Direction(p21); DigitalOut MyLed1(LED1), MyLed2(LED2); int AtReset; float Sunup, Threshold, RunTime; float Eval, Wval, Ambval; void stop() { //Stop the drive motor. The interrupt calls this if either limit switch opens. pc.printf("Stop!\n\r"); //Print "Stop!" to the serial terminal for calibration/troubleshooting purposes. Direction = 0; //Open the direction and motor drive relays. MotorOn = 0; } void moveW() { //Move the panel W. Direction = 0; //Set direction to W. MotorOn = 1; //Run the motor. pc.printf("Motor on W!\n\r"); //Print "Motor on W!" to the serial terminal for calibration/troubleshooting purposes. wait(RunTime); //Run the motor for RunTime. MotorOn = 0; //Turn the motor off. AtReset = 0; //Set AtReset to 0 because we are not all the way E. } void moveE() { //Move the panel E. Direction = 1; //Set direction to E. wait(0.5); MotorOn = 1; //Run the motor. pc.printf("Motor on E!\n\r"); //Print "Motor on E!" to the serial terminal for calibration/troubleshooting purposes. wait(RunTime); //Run the motor for RunTime. MotorOn = 0; //Open the motor power and direction relays. Direction = 0; } void resetE() { //Move the panel all the way E after dark so that the sun will shine on it after sunrise. while (!ELimit) { //While the E limit switch is closed. moveE(); } AtReset = 1; //Set AtReset to 1 since we are now all the way E. } void readcells() { //Read the values of the E & W cells 5 times each and average the values. int i; for(i = 0; i < 5; i++){ //Loop 5 times Wval += Wcell.read(); //Read the values Eval += Ecell.read(); } Wval = Wval / 5; //Average the values Eval = Eval / 5; } int main() { pc.printf("Powered Up\n\r"); //Print "Powered Up" to the serial terminal for calibration/troubleshooting purposes. EInt.rise(&stop); //Call motor stop if either limit switch opens. WInt.rise(&stop); AtReset = 0; //1=at E limit. Sunup = 0.08; //Ambient light threshold at which sun is considered to be up. Threshold = 0.035; //Max allowed difference between E & W inputs. RunTime = 1.25; //How long the motor shold run on each move. (about 6 deg/sec after slight turn-on delay) while(1) { //Loop forever. readcells(); //Read the values of the E & W cells. Ambval = Wval + Eval; //Find the ambient light level MyLed1 = ELimit; MyLed2 = WLimit; pc.printf("%f %f %f\r\n", Wval, Eval, Ambval); //Print the west, east, and ambient values to the serial terminal for calibration purposes. if ((Ambval > (Sunup + 0.4)) ){ //If the sun is up ... if (((Wval-Eval) > Threshold) && !WLimit){ //If the W cell is illuminated by more than the threshhold and the W limit swich is closed... moveW(); //Move the panel W. } if (((Eval-Wval) > Threshold) && !ELimit){ //If the E cell is illuminated by more than the threshhold and the E limit swich is closed... moveE(); //Move the panel E. } } if ((Ambval < Sunup) && !AtReset) { //If the sun is down and the panel is not reset to the E... resetE(); //Move the panel to the E reset position. } wait(60); //Kill time. Set to 5 seconds during calibration/testing. } }