#define grinderRelay 3
#define acs712 A0
#define grinderDirPositive 8
#define grinderStepPositive 4
const unsigned int stepTime = 3; // interval for stepping using millis
// unsigned long previousTime1 = 0;
// unsigned long previousTime2 = 0;
// unsigned long startTime = 0;
// unsigned long currentTime = 0;
unsigned long grinderMotorOnTime = 1000;
bool grinderRunFlag = false;
int16_t offset;
const float maxCurrent = .1;
float current;
void setup() {
Serial.begin(115200);
pinMode(acs712, INPUT);
pinMode(grinderRelay, OUTPUT);
pinMode(grinderStepPositive, OUTPUT);
pinMode(grinderDirPositive, OUTPUT);
digitalWrite(grinderRelay, LOW); // Disable grinder motor via relay
Serial.println();
Serial.println("Starting...");
Serial.println("Calibrating ACS712 :");
offset = analogRead(acs712);
Serial.print("0 - Reading ");
Serial.println(offset);
int16_t measureMin = offset;
int16_t measureMax = offset;
for(uint8_t measureIndex = 1; measureIndex < 10; measureIndex++)
{
int16_t measure = analogRead(acs712);
measureMin = min(measure, measureMin);
measureMax = max(measure, measureMax);
offset += measure;
Serial.print(measureIndex);
Serial.print(" - Reading ");
Serial.println(measure);
delay(100);
}
offset /= 10;
Serial.print("Offset: ");
Serial.print(offset);
Serial.print(" Min:");
Serial.print(measureMin);
Serial.print(" Max: ");
Serial.println(measureMax);
delay(2000); // 2 seconds to read the Offset
digitalWrite(grinderRelay, HIGH); // enable grinder motor via relay
digitalWrite(grinderDirPositive, HIGH); // Direction counter clockwise
}
void checkCurrent() {
current = float(analogRead(acs712) - offset) / 27.027; //gives Ampere
Serial.println(current,2);
}
void loop() {
// Run Grinder forward for a bit
checkCurrent();
while (abs(current) <= maxCurrent) {
digitalWrite(grinderStepPositive, LOW);
delay ( stepTime);
digitalWrite(grinderStepPositive, HIGH);
delay ( stepTime);
// checkCurrent();
}
}