#define analogInPin A0
#define PWM1 3
#define DIR1 5
#define BATTERY A1
uint16_t sensorValue,outputValue,percentValue;
uint32_t batteryVoltage; //32 bit to prevent overflow
uint32_t tick=0;
void setup() {
Serial.begin(115200);
Serial.println("\n\n\n\nHello");
pinMode(PWM1,OUTPUT); //set the PWM pin to output
pinMode(analogInPin,INPUT); //set the potentiometer pin to input
pinMode(BATTERY,INPUT); //set the battery pin to input
pinMode(DIR1,OUTPUT); //set the dir pin to output
digitalWrite(DIR1,HIGH); //set it high - change HIGH to LOW if motor goes wrong way
}
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(PWM1, outputValue);
batteryVoltage=analogRead(BATTERY);
if((millis()-tick)>100){ //display 10 times per second
tick=millis();
Serial.print("Potentiometer Value = ");
Serial.println(sensorValue,DEC);
//convert battery reading to voltage (in milliVolts)
batteryVoltage*=15000; //*5000*3
batteryVoltage/=1024;
Serial.print("Battery Voltage = ");
Serial.println(batteryVoltage,DEC);
}
}