Created
May 12, 2017 13:09
-
-
Save Peetz0r/8a51db4596f973c4d1129290a0c471b9 to your computer and use it in GitHub Desktop.
code op zonnepaneel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define PIN_BT_PWR 2 | |
#define PIN_SOLAR_CURRENT A7 | |
#define PIN_SOLAR_VOLTAGE A1 | |
#define PIN_BUTTON 3 | |
uint32_t calibratedZero = 0; | |
bool firstrun = true; | |
void setup() { | |
pinMode(PIN_BT_PWR, OUTPUT); | |
pinMode(PIN_BUTTON, INPUT_PULLUP); | |
digitalWrite(PIN_BT_PWR, HIGH); | |
Serial.begin(115200); | |
} | |
void loop() { | |
uint32_t currentRaw = 0; | |
uint32_t voltageRaw = 0; | |
for(uint16_t i = 0; i < 1000; i++) { | |
currentRaw += analogRead(PIN_SOLAR_CURRENT); | |
voltageRaw += analogRead(PIN_SOLAR_VOLTAGE); | |
delay(1); | |
} | |
if(digitalRead(PIN_BUTTON) == LOW || firstrun) { | |
calibratedZero = currentRaw; | |
Serial.println("Gekalibreerd."); | |
} else { | |
int16_t currentAvg = ((currentRaw*1.0-calibratedZero*1.0)/1000.0)/1024.0*50.0*-1000; | |
float voltageAvg = voltageRaw / 1024.0 * 5.0 * 0.0049; | |
float powerAvg = voltageAvg * currentAvg / 1000.0; | |
Serial.print(currentAvg); | |
Serial.print(" mA\t"); | |
Serial.print(voltageAvg); | |
Serial.print(" V\t"); | |
Serial.print(powerAvg); | |
Serial.println(" W"); | |
} | |
firstrun = false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment