Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save giljr/181e2d5a4e8ed82a5030776450011b40 to your computer and use it in GitHub Desktop.
Save giljr/181e2d5a4e8ed82a5030776450011b40 to your computer and use it in GitHub Desktop.
#define DIR 2
#define STEP 3
#define BATT A0
#define M0 4
#define M1 5
#define M2 6
#define _TRUE 1
#define _FALSE 0
int dPause = 100;
int dirLevel = HIGH;
int battery_voltage;
byte low_bat = 0;
/* in production comment debug = _TRUE */
boolean debug = _TRUE;
//boolean debug = _FALSE;
void setup() {
pinMode(DIR, OUTPUT);
pinMode(STEP, OUTPUT);
pinMode(M0, OUTPUT);
pinMode(M1, OUTPUT);
pinMode(M2, OUTPUT);
pinMode( BUTTON, INPUT );
if (debug) {
Serial.begin(9600);
Serial.println( "BATT Test: Stop Range level 8v < -- > 10v5" );
Serial.println( "BATT Output Must be above > 1050" );
}
}
void loop()
{
/*
Load the battery voltage to the battery_voltage variable.
Resistor voltage divider => (3.3k + 3.3k)/2.2k = 2.5
12.5V equals ~5V @ Analog 0.
12.5V equals 1023 analogRead(0).
1250 / 1023 ~= 1.222
The variable battery_voltage holds 1050 if the battery voltage is 10.5V.
DEBUG: Please, to debug batt voltage keep USB TTL cable connected:
when the Arduino is transmitting the USB TTL adapter will receive.
If battery_voltage is below 10.5V and higher than 8.0V
Turn on the led if battery voltage is to low
Set the low_bat variable to 1 - for future routine
*/
battery_voltage = analogRead(BATT) * (1250 / 1023.);
if (debug) {
Serial.println(battery_voltage);
delay(2000);
}
if (battery_voltage < 1050 && battery_voltage > 800) {
digitalWrite(13, HIGH);
low_bat = 1;
}
delay(10);
/* motor FULL throttle */
digitalWrite(M0, LOW );
digitalWrite(M1, LOW );
digitalWrite(M2, LOW );
digitalWrite( DIR, dirLevel );
/* Motor rotates if there is enough battery power.
Otherwise the LED 13 lights indicating that the battery is discharged */
if (!low_bat)
{
stepGo();
}
}
void stepGo() {
digitalWrite(STEP, HIGH);
delayMicroseconds(dPause);
digitalWrite(STEP, LOW);
delayMicroseconds(dPause);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment