68 lines
2.0 KiB
C++
68 lines
2.0 KiB
C++
#include <Arduino.h> // Include the Arduino library
|
|
|
|
// Pin numbers defined
|
|
const int pinD3 = 3;
|
|
const int pinA1 = A1; // Define A1 pin as A1
|
|
|
|
// Constants for ADC to current conversion
|
|
const float referenceVoltage = 5.0; //5V logic
|
|
const int adcMax = 1023;
|
|
const float voltsToAmps = 1.0 / 1.2; // Conversion factor: 1A per 1.2V
|
|
|
|
// Threshold current value in amperes
|
|
const float currentThreshold = 1.1;
|
|
const int consecutiveLimit = 3; // Number of consecutive readings needed to stop the motor
|
|
|
|
void setup() {
|
|
// Initialize the serial communication
|
|
Serial.begin(9600);
|
|
|
|
// Set pin D3 as an output to turn on and off cream whipper
|
|
pinMode(pinD3, OUTPUT);
|
|
|
|
// Set pin D3 to HIGH (5V)
|
|
digitalWrite(pinD3, HIGH);
|
|
Serial.println("Pin D3 set to HIGH (5V)");
|
|
|
|
int consecutiveHighCurrent = 0; // Counter for consecutive high current readings
|
|
|
|
// 150-second countdown so cream whipper won't run forever if you put unwhippable liquids in it
|
|
for (int i = 150; i > 0; i--) {
|
|
// Read the value from A1
|
|
int adcValue = analogRead(pinA1);
|
|
|
|
// Convert ADC value to voltage
|
|
float voltage = (adcValue * referenceVoltage) / adcMax;
|
|
|
|
// Convert voltage to current
|
|
float current = voltage * voltsToAmps;
|
|
|
|
// Print countdown time and current value
|
|
Serial.print("Time left: ");
|
|
Serial.print(i);
|
|
Serial.print(" seconds, Current: ");
|
|
Serial.print(current);
|
|
Serial.println(" A");
|
|
|
|
// Check if current exceeds the threshold
|
|
if (current > currentThreshold) {
|
|
consecutiveHighCurrent++;
|
|
} else {
|
|
consecutiveHighCurrent = 0; // Reset counter if current is below threshold
|
|
}
|
|
|
|
// Stop the motor if the threshold is exceeded 3 times in a row
|
|
if (consecutiveHighCurrent >= consecutiveLimit) {
|
|
digitalWrite(pinD3, LOW);
|
|
Serial.println("Motor stopped due to high current!");
|
|
break; // Exit the countdown loop
|
|
}
|
|
|
|
delay(1000); // Wait 1 second
|
|
}
|
|
|
|
// Ensure pin D3 is set to LOW (0V) at the end
|
|
digitalWrite(pinD3, LOW);
|
|
Serial.println("Pin D3 set to LOW (0V)");
|
|
}
|