Last active
June 28, 2018 13:38
-
-
Save Duckle29/fa46a28c1df28cdfb8ae9528878026a6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include "currentsens.h" | |
Current::Current(uint8_t pin, uint16_t sample_period, int8_t debug_pin) | |
{ | |
_pin = pin; | |
_sample_period = sample_period; | |
pinMode(_pin, INPUT); | |
if(debug_pin >= 0) | |
{ | |
_debug_pin = debug_pin; | |
pinMode(_debug_pin, OUTPUT); | |
} | |
} | |
bool Current::sensor_present() | |
{ | |
uint16_t average_reading = 0; | |
for(uint8_t i=0; i<50; i++) | |
{ | |
average_reading += analogRead(_pin); | |
delay(10); // Delay serves as a yield as well | |
} | |
average_reading /= 50; | |
if(average_reading > 500 && average_reading < 600) | |
{ | |
return true; | |
} | |
return false; | |
} | |
void Current::init() | |
{ | |
sampler.attach_ms(_sample_period, this->_sample); | |
} | |
void Current::calibrate() | |
{ | |
uint32_t calibrate_start = millis(); | |
while(millis() - calibrate_start < 5000) | |
{ | |
yield(); | |
handle(); | |
} | |
_error = _last_avg_peak; | |
Serial.print("Error: ");Serial.println(_error); | |
} | |
static void Current::_sample() | |
{ | |
_raw_samples[_raw_sample_offset] = analogRead(_pin); | |
_raw_sample_offset++; | |
if(_raw_sample_offset >= _raw_sample_size) | |
{ | |
_raw_buffer_filled = true; | |
_raw_sample_offset = 0; | |
} | |
} |
This file contains hidden or 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
#pragma once | |
#include <Arduino.h> | |
#include <Ticker.h> | |
Ticker sampler; | |
class Current | |
{ | |
public: | |
// pin = adc pin, sample_period = delay between samples in uS; | |
Current(uint8_t pin, uint16_t sample_period = 20, int8_t debug_pin = -1); | |
void init(); | |
void calibrate(); // Run this at 0A to collect the 0A offset. | |
bool sensor_present(); // Checks if there's a sensor on the board. | |
private: | |
uint8_t _pin; | |
uint8_t _debug_pin; | |
static void _sample(); | |
uint16_t _sample_period = 1; // mS | Time between current samples | |
}; | |
This file contains hidden or 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
#include "currentsens.h" | |
Current current(PIN_CURRENT, 1, PIN_DEBUG); // 1ms between samples | |
void setup() | |
{ | |
if(current.sensor_present()) | |
{ | |
Serial.print("Current sensor present"); | |
// Calibrate current offset | |
display.set_status("Calibrating"); | |
delay(5000); // Delay to allow things to settle | |
current.calibrate(); | |
current.init(); // Init this after calibrate. | |
} | |
} | |
void loop() | |
{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment