Friday, May 8, 2015

Final Code for the Start up and Initial Charge sequence..


Here is the 7th or 1000th (can't remember which, its been a rough month) permutation of my start up and initial charge sequence code.. There is a lot more to do but this is a HUGE step in the right direction..

Code below:

int ledPBGood = 13;// ON if charged
int ledLIONGood = 12;//On if Charged
int rlyPBLIONsw = 8;//Off for PB, ON for Lion
int ledChg = 6; // LED tells if your are needing some juice..
//int wallSW = 5; // ON/OFF for being plugged in
int ignSW = 5; // Ignition ON/OFF
int pb = A0; // Pot to test charge levels
int lion = A1; // Pot to test charge levels
int gennie= 10; // DC Motor port



void setup() {
  // put your setup code here, to run once:
  pinMode (ledChg, OUTPUT); // Blinking LED if we need more juice
  pinMode (ledPBGood, OUTPUT);// Charged LED for Lead Acid
  pinMode (ledLIONGood, OUTPUT); // Charged LED for LiON
  pinMode (rlyPBLIONsw, OUTPUT); // Battery Relay Switcher
  pinMode (ignSW, INPUT); // Ignition Switch
  pinMode (pb, INPUT); //Lead Acid Battery
  pinMode (lion, INPUT); // LiON Battery
  pinMode (gennie, OUTPUT);// dcMotor
Serial.begin(9600);// Use of Serial communications for debugging and diagnostics.
// I will probably use Serial communications to write to an SD card later for
//statistics purposes and fine tuning.
}

void loop() {
  //Start Car
  int key = digitalRead(ignSW);
  if (key == HIGH){
    decisions();// Battery Check and possible charge.
 
  }
  else {//Kill everything,
  digitalWrite(ledPBGood, LOW);
  digitalWrite(ledLIONGood, LOW);
  motor(LOW, LOW);

  }
  }


void blinkDamnit(){// Charge warning light function
           int q = 0;
       while(q != 5){  
     
              digitalWrite(ledChg,HIGH);
              delay(1000);
              digitalWrite(ledChg, LOW);
        q++;
        delay(200);
       }
       digitalWrite(ledChg, LOW);
}


void chargeDamnYou (int pb, int lion){// Everthing is dead, charge it!
  blinkDamnit();
  if (pb > 3){
    while (pb > 4){ // charge to atleast 4
    motor(HIGH, LOW);
 
    }
if (lion > 3){
  while (lion > 4){ // charge to atleast 4
    motor(HIGH, HIGH);
 
    }
}

}



  }//end of function

void motor(int mtr, int swt){// Motor and battery relay control
  digitalWrite(gennie,mtr);
  digitalWrite (rlyPBLIONsw, swt);

}

void decisions() {// Brains of the operation..
  int pbBatt = analogRead(pb);// Putting values into easier-to-manipulate storage space.
  int lionBatt = analogRead(lion);
  int pbtotal = pbBatt / 222; // value should be 204, edited for debugging
  int liontotal = lionBatt /210; // value should be 204, edited for debugging
  Serial.println("pb  ");//Output to Serial Monitor for debugging and diagnostics
  Serial.print(pbtotal);
  Serial.println("lion ");
  Serial.print(liontotal);
  if (pbtotal || liontotal >= 4){//Everythings good.
    digitalWrite(ledPBGood, HIGH);
    digitalWrite(ledLIONGood, HIGH);
    motor(LOW, LOW);
  }
            if (pbtotal < 4 && liontotal >= 4){//Lead acid needs some juice.
                digitalWrite(ledPBGood, LOW);
                digitalWrite(ledLIONGood, HIGH);
                motor(HIGH,LOW);
          }
              if (pbtotal >= 3 && liontotal <= 3){//Lead acids cool, but LiON needs some juice.
                digitalWrite(ledPBGood, HIGH);
                digitalWrite(ledLIONGood, LOW);
                motor(HIGH,HIGH);
           
              }
              if (pbtotal < 3 && liontotal < 3){// The "Oh Shit" protocol.. Charge Everything!!
                  digitalWrite(ledPBGood, LOW);
                  digitalWrite(ledLIONGood, LOW);
                  motor(HIGH,LOW);
                  chargeDamnYou (pbtotal, liontotal);
          }
 
}
 

No comments: