{"id":123,"date":"2019-09-10T12:21:02","date_gmt":"2019-09-10T12:21:02","guid":{"rendered":"http:\/\/techmind.dk\/sustainability\/?p=123"},"modified":"2019-10-01T12:17:27","modified_gmt":"2019-10-01T12:17:27","slug":"solar-trolley-c-sketch","status":"publish","type":"post","link":"http:\/\/techmind.dk\/sustainability\/energi-forbrug\/solar-trolley-c-sketch\/","title":{"rendered":"Solar Tracker Trolley C Sketch"},"content":{"rendered":"\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" src=\"http:\/\/techmind.dk\/sustainability\/wp-content\/uploads\/2019\/10\/trolley-1024x768.jpg\" alt=\"\" class=\"wp-image-128\" width=\"249\" height=\"186\" srcset=\"http:\/\/techmind.dk\/sustainability\/wp-content\/uploads\/2019\/10\/trolley-1024x768.jpg 1024w, http:\/\/techmind.dk\/sustainability\/wp-content\/uploads\/2019\/10\/trolley-300x225.jpg 300w, http:\/\/techmind.dk\/sustainability\/wp-content\/uploads\/2019\/10\/trolley-768x576.jpg 768w, http:\/\/techmind.dk\/sustainability\/wp-content\/uploads\/2019\/10\/trolley.jpg 1195w\" sizes=\"(max-width: 249px) 100vw, 249px\" \/><\/figure>\n\n\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;TimeLib.h&gt; \/\/https:\/\/github.com\/PaulStoffregen\/Time\n#include &lt;EEPROM.h&gt;\n\n\n\/\/ Suntracker addon with actuator, for solarTrolley\n\/\/ open source suntracker project for higher performance solarpanels\n\n\/\/ Arduino IDE 1.18 This version is a standalone version of the webbased solartracker\n\/\/ You need to make some kind of data view mode trough serial\/usb or embed some code for at display if you will used the polulo current chip data\n\n\/\/ Author: http:\/\/techmind.dk michael pedersen\n\/\/ Project home: http:\/\/techmind.dk\/tag\/suntracker\/\n\n\/\/ Start Date: 24. feb 2019\n\/\/ License: coyright\n\/\/ Version: 08b \n\/\/ Update: 01\n\n\n\/\/***********************************************************************\n\n\/\/Define solartracker ports, valiables and static\n\n\/\/Digital ports\n\/\/ 0 , 1 recerved for serial com\n\/\/const byte interruptPin = 2; \/\/ interrupt Data pin from actuator position encoder\nconst int motorController_IN3_OUT3 = 3; \/\/ HIGH = go WEST\nconst int motorController_IN4_OUT4 = 4; \/\/ HIGH = go EAST\n\n\/\/D in\/out 5 NC\/free\nconst int dipSwitchWest = 6;\nconst int ledAlive = 7;\nconst int dipSwitchEast = 8;\nconst int ledCheck = 9;\n\n\n\/\/Analog ports\nint solarVoltSensor = 0;\n\/\/int free = 1;\nint lightSensorEast = 2; \/\/LDR\nint lightSensorWest = 3; \/\/LDR\nint solarCurrentSensor = 4;\nint actuatorCurrentSensor = 5;\n\n\/\/Variables\nlong lowCounter = 0;\nint waitMinutesToNextTrackUpdate = 1; \/\/This value is in minutes 1 equal one minut 5 eual 5 minuttes. In my application it is Set to 3 or 5 minutes\nbool testModeSet = 0;\nbool onWayHomeToWest = 0;  \nbool east = 0;\nbool west = 1;\nbool acuatorCurrentExceeded; \/\/1 = ok, 0 = overcurrent, Physical Limit, stop actuator\n\nbool dipSwitchWestState = 0;\nbool dipSwitchEastState = 0;\n\nint optoPositionTheValue = 0;\nint solarCurrentValue = 0;\nint actuatorCurrentSensorValue; \/\/ Stop actuator +-15 from 520\nint lightSensorEastValue;\nint lightSensorWestValue;\n\nint addrE2 = 0; \/\/eeprom start adress\nbyte suntrackerPositionFromE2;\n\n  \n\/\/volatile int state = LOW;      \/\/ The input state toggle\n\n\/\/Declare metodes\n\nvoid goHomeWest(); \/\/go all the way back to west start position\n\nvoid goHomeEast(); \/\/go all the way back to west start position\nvoid goToWest();\nvoid goToEast();\nvoid stopActuator(); \nvoid testMode();\n\n\/\/********************\n\/\/Initialize and setup\n\/\/********************\n\nvoid setup(){\n  Serial.begin(9600);\n  Serial.println(&quot;Hello world, i am suntracker and alive ;) &quot;);\n  \n  \/\/Initialize Solartracker ports and variables\n  pinMode(ledAlive, OUTPUT);\n  pinMode(ledCheck, OUTPUT);\n  pinMode(dipSwitchWest, INPUT);\n  pinMode(dipSwitchEast, INPUT);\n\n  \/\/ Initialize Motor controller ports\n  pinMode(motorController_IN3_OUT3, OUTPUT); \/\/ \n  pinMode(motorController_IN4_OUT4, OUTPUT);  \/\/ \n  \n  \/\/ Initialize, make sure actuator is stopped\n  stopActuator();\n  \n  digitalWrite(ledCheck, HIGH);\n  \n  \/\/Attach the interrupt to the input pin and monitor for ANY Change\n  \/\/attachInterrupt(digitalPinToInterrupt(interruptPin), stateChange, CHANGE);\n  \n  \/\/Set lowcounter\n  time_t t = now(); \/\/ store the current time in time variable t \n  minute(t);        \/\/ returns the minute for the given time t \n  lowCounter =minute(t);\n\n  \/\/ set test mode\n  testModeSet = LOW; \/\/set to HIGH to enter test mode\n  onWayHomeToWest = LOW;\n\n  blinkGreenLEDFast();\n  digitalWrite(ledAlive, LOW);\n  acuatorCurrentExceeded = HIGH;\n}\n          \n\/\/**********************************\n\/\/ The loop where everything happens\n\/\/**********************************\nvoid loop(){ \/\/Main state maschine\n\n      Serial.println(optoPositionTheValue);\n  if (!testModeSet)\n    { \/\/ Run normal  \n      blinkGreenLEDSlow();\n      Serial.println(&quot;Run mode&quot;);\n      checkDualDipswitchPress(); \/\/ Enter testmode, press both endstop dipswitches at the same time  \n      checkSuntrackerCurrentPosition();\n      gotoNextSuntrackerPosition();\n      time_t t = now();\n    Serial.println(second(t));\n                             \n    }\n    \n    else\n    \n    { \/\/ Go into Testmode\n      blinkRedLEDFast();\n      Serial.println(&quot;Test mode&quot;);\n      testMode();\n      digitalWrite(ledAlive, LOW);\n      checkDualDipswitchPress(); \/\/ Leave testmode, press both endstop dipswitches at the same time\n    }\n}\n\nvoid gotoNextSuntrackerPosition()\n  {\n    \/\/ *** Start test light level east \/ west  \n    lightSensorEastValue = analogRead(lightSensorEast);\n    lightSensorWestValue  = analogRead(lightSensorWest);\n\n    Serial.print(lightSensorEastValue);\n    Serial.print(&quot;.....&quot;);\n    Serial.println(lightSensorWestValue);\n\n    checkActuatorPhysicalLimit();\n    Serial.print(dipSwitchEastState);\n    Serial.print(&quot;.....&quot;);\n    Serial.println(dipSwitchWestState);\n\n      \n    \/\/Check where to turn the suntracker to, to optain most light fotons\n    if (lightSensorWestValue &gt; lightSensorEastValue)\n    {\n      time_t t = now();\n      if ((lowCounter+waitMinutesToNextTrackUpdate) == (minute(t)))\n      {\n           \n              goToWest(); \n              Serial.println(&quot;Moving West&quot;);\n                         \n            \n      }  \n    }\n    else if (lightSensorWestValue &lt; lightSensorEastValue)\n    {\n      time_t t = now();\n     if ((lowCounter+waitMinutesToNextTrackUpdate) == (minute(t)))\n      {       \n           \n              goToEast();\n              Serial.println(&quot;Moving east&quot;);\n              \n            \n      }     \n    }\n    else if (lightSensorWestValue = lightSensorEastValue)\n    {\n      \/\/LDR east and West equal, so stop actuator and copy minutes here and now til lowCounter, the wait offset data, not a counter, sorry\n      stopActuator(); \n      time_t t = now();    \n      lowCounter =minute(t);\n    }\n  }\n\nvoid checkActuatorPhysicalLimit()\n  {\n    dipSwitchEastState = digitalRead(dipSwitchEast); \/\/ read dipswitch East 0 = pressed = end stop\n    dipSwitchWestState = digitalRead(dipSwitchWest); \/\/ read dipswitch west 0 = pressed = end stop     \n  }\n  \nvoid checkDualDipswitchPress() \/\/ Toggle testmode flag\n  {\n    \n       \/\/ Both dipswitches pressed enter testmode\n      dipSwitchEastState = digitalRead(dipSwitchEast); \/\/ read dipswitch west 0 = pressed = end stop  \n        if (!dipSwitchEastState)\n          {\n            dipSwitchWestState = digitalRead(dipSwitchWest); \/\/ read dipswitch west 0 = pressed = end stop\n            if (!dipSwitchWestState)\n             {\n                testModeSet = !testModeSet;          \n             }\n          }\n  \n  } \n\n  void goToWest()\n{\n  digitalWrite(ledCheck, HIGH);\n  checkActuatorPhysicalLimit();\n  actuatorCurrentCheck();\n\n  if (acuatorCurrentExceeded)\n    {\n      if (dipSwitchWestState)\n        {\n          digitalWrite(motorController_IN3_OUT3, HIGH);  \n          digitalWrite(motorController_IN4_OUT4, LOW);  \n        }\n        else\n        {\n          stopActuator();\n        } \n      }\n     \n    else\n    {\n      stopActuator();\n    }\n}\n\nvoid goToEast()\n{\n  checkActuatorPhysicalLimit();\n  actuatorCurrentCheck();\n\n  if (acuatorCurrentExceeded)\n    {\n      if (dipSwitchEastState)\n        {\n          digitalWrite(ledCheck, HIGH);\n          digitalWrite(motorController_IN3_OUT3, LOW);  \n          digitalWrite(motorController_IN4_OUT4, HIGH);    \n        }\n    \n        else\n        {\n          stopActuator();\n        }\n    }\n    else\n    {\n      stopActuator();\n    }\n\n    \n}\n\nvoid actuatorCurrentCheck()\n{\n  actuatorCurrentSensorValue = analogRead(actuatorCurrentSensor);\n  Serial.println(actuatorCurrentSensorValue);\n  \n  if (actuatorCurrentSensorValue &gt; 540)\n    {\n      acuatorCurrentExceeded = LOW;  \n      Serial.println(&quot;acuator Current Exceeded!!&quot;);    \n    }\n        \n   else if (actuatorCurrentSensorValue &lt; 495)\n    {\n      acuatorCurrentExceeded = LOW;  \n      Serial.println(&quot;acuator Current Exceeded!!&quot;);    \n    }  \n    \n    \n}\n\nvoid stopActuator()\n{\n  digitalWrite(motorController_IN3_OUT3, LOW);  \n  digitalWrite(motorController_IN4_OUT4, LOW);  \n  digitalWrite(ledCheck, LOW);\n}\n\nvoid checkSuntrackerCurrentPosition()\n  {\n     suntrackerPositionFromE2 = EEPROM.read(addrE2);\/\/write EEPROM.write(addr, val);\n     \/\/optoPositionThePhotoTransistor interrupt data pin in \n     \/\/optoPositionTheValue\n     \n          \n  }\n\nvoid testMode()\n{\n \n \n\n  stopActuator();\n  acuatorCurrentExceeded = HIGH;  \n  \n\n  \n}\nvoid blinkGreenLEDFast()\n  {\n     \/\/ Blink, Great solar tracker is alive\n    digitalWrite(ledAlive, HIGH);\n    delay(100);\n    digitalWrite(ledAlive, LOW);\n    delay(100);\n   \n  }\n\nvoid blinkGreenLEDSlow()\n  {\n     \/\/ Blink, Great solar tracker is alive\n    digitalWrite(ledAlive, HIGH);\n    delay(300);\n    digitalWrite(ledAlive, LOW);\n    delay(300);\n   \n  }\n  \nvoid blinkRedLEDSlow()\n  {\n     \/\/ Blink, Great solar tracker is alive\n    digitalWrite(ledCheck, HIGH);\n    delay(300);\n    digitalWrite(ledCheck, LOW);\n    delay(300);\n   \n  }\n\nvoid blinkRedLEDFast()\n  {\n     \/\/ Blink, Great solar tracker is alive\n    digitalWrite(ledCheck, HIGH);\n    delay(100);\n    digitalWrite(ledCheck, LOW);\n    delay(100);\n    \n  }\n<\/pre>","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"_links":{"self":[{"href":"http:\/\/techmind.dk\/sustainability\/wp-json\/wp\/v2\/posts\/123"}],"collection":[{"href":"http:\/\/techmind.dk\/sustainability\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/techmind.dk\/sustainability\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/techmind.dk\/sustainability\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/techmind.dk\/sustainability\/wp-json\/wp\/v2\/comments?post=123"}],"version-history":[{"count":4,"href":"http:\/\/techmind.dk\/sustainability\/wp-json\/wp\/v2\/posts\/123\/revisions"}],"predecessor-version":[{"id":148,"href":"http:\/\/techmind.dk\/sustainability\/wp-json\/wp\/v2\/posts\/123\/revisions\/148"}],"wp:attachment":[{"href":"http:\/\/techmind.dk\/sustainability\/wp-json\/wp\/v2\/media?parent=123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/techmind.dk\/sustainability\/wp-json\/wp\/v2\/categories?post=123"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/techmind.dk\/sustainability\/wp-json\/wp\/v2\/tags?post=123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}