/* Dronestrober with BMS and charger when running on 4.5 volt or less it will strobe leds when run on more it will charge the battery to 4.2 volt, let it drop to 4.0 before recharge */ //sleep stuff #include #include //#include #ifndef cbi #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) #endif #ifndef sbi #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) #endif //for sleep mode watchdog timer volatile boolean f_wdt = 1; //const int floodSensors = 2; // the number of the Flood Sensor pin //const int voltpin = 1; // voltage sensor pin const int batpin = 3; // D3 and A3 for charging ans sensing battery voltage. const int ledpin = 4; // LED pin -> mosfet -> LED const int potpin = 1; // analogue input 1 (pin 2) for strobe setting, read only first 60 secs. or something. //battery data const int batmax = 4180; //max battery charge voltage mV const int batrecharge = 4000; //restart charge coltage mV const int batmin = 3150; //min battery charge voltage mV const int chargethreshold = 4400; //charge vs operation threshold //variables will change: int runmode=1; int wdduration=5; int blinks; int ond=20; //on delay for LED int itv; //interval 1 for LED sleepdelay int sitv; //interval 2 for LED sleepdelay void setup() { //sleep before sensing input voltage. setup_watchdog(9); // approximately 8 seconds system_sleep(); delay(10); //read input voltage if (readVcc() > chargethreshold) { runmode=1; //runmode for charging. } else { runmode=2; //runmode for strobing } pinMode(batpin, INPUT); pinMode(ledpin, OUTPUT); pinMode(potpin, INPUT); } void loop(){ if (f_wdt==1) { // wait for timed out watchdog / flag is set when a watchdog timeout occurs f_wdt=0; // reset flag } //check runmode //pinMode(batpin,INPUT); int sensorValue = analogRead(batpin); int voltage = (sensorValue * (5.0 / 1023)) * 1000; if (runmode==1) { if ((int)voltage > batmax) { runmode=3; } if (voltage < batmax) { // charge pinMode(batpin,OUTPUT); digitalWrite(batpin,HIGH); delay(1000); digitalWrite(ledpin,HIGH); delay(1); digitalWrite(ledpin,LOW); digitalWrite(batpin,LOW); pinMode(batpin,INPUT); } } if (runmode==2) { if (readVcc() > batmin) { // strobe if (blinks<250) { //stop measuring pot if over 250 blinks to lock blinkrate //read pot for sleeptimers blinks++; int pot = analogRead(potpin); //pot read value if (pot < 100) {itv=1;; sitv=1;} if (pot >= 100 && pot <=199) {itv=1;sitv=2;} if (pot >= 200 && pot <=299) {itv=1;sitv=3;} if (pot >= 300 && pot <=399) {itv=2;sitv=1;} if (pot >= 400 && pot <=499) {itv=2;sitv=2;} if (pot >= 500 && pot <=599) {itv=2;sitv=3;} if (pot >= 600 && pot <=699) {itv=3;sitv=1;} if (pot >= 700 && pot <=799) {itv=3;sitv=2;} if (pot >= 800 && pot <=899) {itv=3;sitv=3;} if (pot >= 900 && pot <=1024) {itv=4;sitv=2;} } //sleep if (wdduration==itv) { wdduration=itv+sitv; } else { wdduration=itv; } //blink digitalWrite(ledpin,HIGH); delay(ond); digitalWrite(ledpin,LOW); setup_watchdog(wdduration); system_sleep(); } else { setup_watchdog(9); system_sleep(); } } if (runmode==3) { if (voltage < batrecharge) { // charge runmode=1; } digitalWrite(ledpin,HIGH); delay(1); digitalWrite(ledpin,LOW); delay(5000); } } // set system into the sleep state // system wakes up when wtchdog is timed out void system_sleep() { cbi(ADCSRA,ADEN); // switch Analog to Digitalconverter OFF set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here sleep_enable(); sleep_mode(); // System sleeps here sleep_disable(); // System continues execution here when watchdog timed out sbi(ADCSRA,ADEN); // switch Analog to Digitalconverter ON } // 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms // 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec void setup_watchdog(int ii) { byte bb; int ww; if (ii > 9 ) ii=9; bb=ii & 7; if (ii > 7) bb|= (1<<5); bb|= (1<