
// STOP_FUNCTION_TEST

// Read sensors

int Sensepinleft = 4 ;                           // Define PIN A4 for left Sensor
int Sensepinright = 3 ;                          // Define PIN A3 for right Sensor
int Sensevalueleft;                              // Define variable for sensorvalue left
int Sensevalueright;                             // Define variable for sensorvalue right

// Status LED
int Ledbat = 3;                                  // Define PIN 3 for digital output red LED
int Ledstart = 4;                                // Define PIN 4 for digital output green LED

// Read Battery voltage
int Voltpin = 0;                                 // Define PIN A0 for reading battery voltage
float Volt;                                      // Define variable for voltage
float Voltvalue;                                 // Define variable for avarage voltage calculation
float Voltlow = 10;                              // Define variable and setup for minimum operation voltage
int I;                                           // Define variable for LOOP counter
int I_bat = 20;                                  // Define variable for battery status (low or high)

void setup() {                                   // Setup

  // Print
  Serial.begin(9600);                            // Start the serial communication

  // Status LEDs
  pinMode(Ledbat, OUTPUT);                       // Define Ledbat PIN as OUTPUT
  pinMode(Ledstart, OUTPUT);                     // Define Ledstart PIN as OUTPUT  
  
}


void loop() {                                   // Start main programm

digitalWrite(Ledstart, HIGH);                   // Switch green status LED ON

// Read Voltpin for 10 times and calculate average voltvalue
Voltvalue = 0;
for (I = 0;I<10; I++){
 Volt = analogRead(Voltpin);
 Voltvalue = Voltvalue + Volt;
 delay(10);
 Serial.println(I);                             // Print LOOP counter
}
  Voltvalue = Voltvalue / 10;                   // calculate average value
  Voltvalue = Voltvalue * 0.02765;              // 0.02765 is factor for voltagedevider 22K / 4,7K

 Serial.print("Battery voltage = ");                 // Print text without linefeed
 Serial.println(Voltvalue);                     // Print avarage voltvalue with linefeed                    
 Serial.println(" ");                           // Print nothing as a linefeed
  
 
if (Voltvalue < Voltlow) {                      // Make dicission: Batteryvoltage O.K or low
  I_bat = 1;                                    // If battery voltage is low set status 1
  Stop();                                       // If battery is low go to “Stop()” function
} 

  delay(500);                                   // Wait 500ms

}


/////////FUNCTIONS///////////

void Stop() {                                   // Stop the ARDUMOWER if battery is low

digitalWrite(Ledstart, LOW);                    // Switch green status LED OFF               

while (I_bat < 10)                              // As long as batterystatus is low, stay here
{
  // flash red status LED
digitalWrite(Ledbat, HIGH);
delay(100);
digitalWrite(Ledbat, LOW); 
delay(500);

Serial.println("Low Battery");                  // Print "Low battery" message        

}
}



