Monday, May 11, 2015

Another Progress Report....



Another step in the right direction tonight.. Managed to get the two arduinos to talk to each other (unilaterally). Chuck was kind enough to let me check out an additional UNO and a shiny new Mega 2560! The Mega is where I placing the main program that will monitor and regulate the banks of batteries. The UNO sends a signal to the Mega when there is a significant draw from the batteries and then the UNO takes command of the relays, switches the DC Motor on and supplements the batteries until the workload on the batteries decreases.


Mega Code: (Sounds like a cool name for a band)


int ledPBGood = 13;// ON if charged
int ledLIONGood = 12;//On if Charged
int rlyPBLIONsw = 11;//Off for PB, ON for Lion
int ledChg = 10; // 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= 7; // DC Motor port
int dcMeter = A3;// Takes in dc (gennie) currant
int unoPhoneHome = A15; // Would you like to accept a collect call from Uno?


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
  pinMode (unoPhoneHome, INPUT); // Uno's personal phone..

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();
 
 
                  }
 
          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 / 200; // value should be 204, edited for debugging
  int liontotal = lionBatt /200; // 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);

int ohOH = analogRead (unoPhoneHome); //Uno Calling Mega
int ohCrap = ohOH / 204; //One bit is all you get..
if (ohCrap > 2){ //What the hill is going on?!?
   motor(HIGH,LOW);
}
else {
  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);
          }
}
}



/*******************************/
/*******Flower Box of Death*******/
/******************************/

UNO Code: (Not a very cool name for a band..)
This unit will eventually monitor temp and initial propulsion load on batteries.





int dcMeter = A3;// Takes in dc (gennie) current
int dcTemp = A4; // Are we going to die? // going to eventually figure out how to do this.. 
int specialRly = 3;// Relay that is connected direct to propulsion.
int gennie = 10; // dcMotor
int allGood = 6;// yay we can drive!
void setup() {
  
     pinMode (dcMeter, INPUT); // Input for dcMotor current while driving.
     pinMode (specialRly, OUTPUT); // controls relay for extra push
     pinMode (gennie, OUTPUT);// dcMotor out to mega
     pinMode (allGood, OUTPUT); //yay
     Serial.begin(9600);
}

void loop() {
  
int vroomVroom = 1;// change later to reflect driving
if (vroomVroom == 1){
    int yoMamaFlo = analogRead(dcMeter);
    int flow = yoMamaFlo /200 ;
    
    Serial.println("Flow_"); Serial.print(flow);
    
            if (flow >= 2){ // Movin on up!
                        digitalWrite(specialRly, HIGH);
                        digitalWrite(gennie, HIGH);
                        digitalWrite(allGood, LOW);
                        
                       }
            else { //What went up is now going down.. 
                    digitalWrite(specialRly, LOW);
                    digitalWrite(gennie, LOW);
                    digitalWrite(allGood, HIGH);
                    }
            
            }
}



No comments: