// InputHysteresis.ino Turn a motor On and Off based on Battery Voltage.
// We want the motor to run only when the battery voltage exceeds a preset level.
// We also want a potentiometer to be able to set the motor speed when the motor is enabled.
// Define the baud rate for the serial port.
#define BAUD_RATE 9600
// This assumes A0 for Battery Voltage input and A1 for Motor Speed input.
// Battery Voltage allows a digital out for the motor to run or not run.
// Motor Speed controls a PWM output siganl to the motor.
#define INPUT1_PIN A0
#define INPUT2_PIN A1
// PWM Constants
const int analogInPin = A1; // Analog input pin that the potentiometer is attached to
const int pwmOutPin = 5; // Analog output pin that the LED is attached to
// The Arduino Uno built in LED is used for test purposes pin 13.
// The Arduino Uno pin 12 is used to enable or disable the motor.
#ifndef LED_BUILTIN
#define LED_BUILTIN 13
#endif
/****************************************************************/
// Global variables.
// The state of the motor enable output pin can be captured with only two values, High or Low Battery Voltage.
// Battery Voltage is represented by a single bit.
// The following statement defines two symbolic values, one for each possible state.
enum state_t { IS_LOW, IS_HIGH };
// Declare the state variable as a symbolic value.
enum state_t state = IS_LOW;
// The hysteretic response is defined by using two thresholds.
const int high_threshold = 881;
const int low_threshold = 819;
// The PWM Functions
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
int percentValue = 0; // pump speed percent full scale
// Standard Arduino initialization function to configure the system.
void setup()
{
// initialize the Serial port
Serial.begin( BAUD_RATE );
// configure our trivial I/O
pinMode( LED_BUILTIN, OUTPUT );
pinMode( 12, OUTPUT);
// the LED and pin 12 start out High to match the initial state.
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(12, HIGH);
}
/****************************************************************/
// Standard Arduino polling function.
void loop()
{
// Read the PWM value of the input potentiometer.
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// map pump speed percent of full scale
percentValue = map (outputValue, 0, 255, 0, 100);
// change the analog out value:
analogWrite(pwmOutPin, outputValue);
// print the results to the Serial Monitor:
Serial.print("\t Speed Input = ");
Serial.print(sensorValue);
Serial.print("\t Speed Output = ");
Serial.print(outputValue);
Serial.print("\t Motor Speed Percentage = ");
Serial.println(percentValue);
// wait 500 milliseconds before the next loop for the analog-to-digital
// converter to settle after the last reading:
delay(500); // Allow 0.5 Second and continue
// Read the Battery Voltage level.
int input1 = analogRead(INPUT1_PIN);
float battVolt = (input1 * 5.0) / 1023 * 3;
Serial.print ("\t Battery Voltage is ");
Serial.print (battVolt);
Serial.println (" Volts");
delay(1000);
if (state == IS_HIGH) {
if (input1 < low_threshold) {
Serial.print("Low observed at input level ");
Serial.println(input1);
Serial.println("Transitioning to the IS_LOW state.");
state = IS_LOW;
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(12, LOW);
}
} else { // state must be IS_LOW
if (input1 > high_threshold) {
Serial.print("High observed at input level ");
Serial.println(input1);
Serial.println("Transitioning to the IS_HIGH state.");
state = IS_HIGH;
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(12, HIGH);
}
}
}
/****************************************************************/