Tuesday, May 26, 2015

Progress Update





I haven't made any real HUGE leaps as far as code or wiring is concerned. I am striving to find ways to get these three devices to play nicely together.
I hope to have some three way communications or something similar in my next update..

Here is the code:
(I left all the "greyed" out code in there as well)

// R3
#include <SoftwareSerial.h>

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 = A5; // Would you like to accept a collect call from Uno?
int pbOUT = A4; // PB info to Mega @ A0
int lionOUT = A5; // Lion info to Mega @ A1
SoftwareSerial toMeg(2,3); // Data connection to Mega

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 (pbOUT, OUTPUT); // Output to Mega A0
  pinMode (lionOUT, OUTPUT); // Output to Mega A1
 // pinMode (unoPhoneHome, INPUT); // Uno's personal phone..
toMeg.begin(9600);
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){
toMeg.write("On");
    decisions();


                  }

          else {//Kill everything,
          digitalWrite(ledPBGood, LOW);
          digitalWrite(ledLIONGood, LOW);
          digitalWrite(rlyPBLIONsw, LOW);
          digitalWrite(ledChg, 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);
  Serial.println("BattOne"); Serial.print(pbBatt);
  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 lead = map(pbBatt, 0, 1023, 0, 255);
  //int lith = map(lionBatt, 0, 1023, 0, 255);
  //Serial.println("lead"); Serial.print(lead); Serial.print("LeadOut");
  analogWrite(pbOUT, pbBatt);
  analogWrite(lionOUT, lionBatt);
/*
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 < 3 && liontotal >= 4){//Lead acid needs some juice.
       
                digitalWrite(ledPBGood, LOW);
                digitalWrite(ledLIONGood, HIGH);
                motor(HIGH,LOW);
           
          }
              if (pbtotal >= 4 && liontotal <= 3){//Lead acids cool, but LiON needs some juice.
                digitalWrite(ledPBGood, HIGH);
                digitalWrite(ledLIONGood, LOW);
                motor(HIGH,HIGH);
             
         
              }
              if (pbtotal < 2 && liontotal < 2){// The "Oh Shit" protocol.. Charge Everything!!
                  digitalWrite(ledPBGood, LOW);
                  digitalWrite(ledLIONGood, LOW);
                  motor(HIGH,LOW);
                  chargeDamnYou (pbtotal, liontotal);
          }
}
//}



*************************
R2 Code

// R2

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!
int tempPB = 13;
int halt = 12;
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
     pinMode (tempPB, OUTPUT);
     pinMode (halt, INPUT);
     Serial.begin(9600);
}

void loop() {
/*  int stopSign = digitalRead(halt);
 if (stopSign == LOW){
  breaking(true, 2000);
 }
   else {
     breaking(false, 0);
   }
 */
int vroomVroom = 1;// change later to reflect driving
if (vroomVroom == 1){
    go();
 
         
            }
}
void go(){
  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);
                    }
}
void breaking(bool wait, int time){
  //digitalWrite(tempPB, HIGH);


  if (wait == true){
    digitalWrite(tempPB, HIGH);
    delay(time);
    digitalWrite(tempPB, LOW);
  }
  else {
    digitalWrite (tempPB, LOW);
  }


}


************************
Mega Code


//Arduino Mega

/***********************
This is example code borrowed from adafruit.
Once I have things working the way I want,
I will rewrite the code to make it
"cleaner".


/*********************

Example code for the Adafruit RGB Character LCD Shield and Library

This code displays text on the shield, and also reads the buttons on the keypad.
When a button is pressed, the backlight changes color.

**********************/

// include the library code:
#include <Wire.h>
#include <SoftwareSerial.h>
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>

// The shield uses the I2C SCL and SDA pins. On classic Arduinos
// this is Analog 4 and 5 so you can't use those for analogRead() anymore
// However, you can connect other I2C sensors to the I2C bus and share
// the I2C bus.
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();

// These #defines make it easy to set the backlight color
#define RED 0x1
#define YELLOW 0x3
#define GREEN 0x2
#define TEAL 0x6
#define BLUE 0x4
#define VIOLET 0x5
#define WHITE 0x7
int potONE = A0;//Connect to Lead-Acid(A5 Mega) Battery from Mega
int potTWO = A1;//Connect to Lithium Ion Battery (A6 Mega) from Mega
int potTHREE = A2; //Connect to R3 (A5 on R3) to get pull from propulsion

//softwareserial connections
SoftwareSerial fmR3(15,14); // Rx/Tx from R3
SoftwareSerial fmR2(17,16); // Rx/Tx from R2

void setup() {
  pinMode (potONE, INPUT);
  pinMode (potTWO, INPUT);
  pinMode (potTHREE, INPUT);
  // Debugging output
  Serial.begin(9600);
  fmR3.begin(9600);
  fmR2.begin(9600);
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);

  // Print a message to the LCD. We track how long it takes since
  // this library has been optimized a bit and we're proud of it :)
/*  int time = millis();
  lcd.print("Hello, world!");
  time = millis() - time;
  Serial.print("Took "); Serial.print(time); Serial.println(" ms");
  */
  lcd.setBacklight(WHITE);
}

uint8_t i=0;
void loop() {
  //one();
  lead();
  lion();
  // print the number of seconds since reset:
 // lcd.print(millis()/1000);
/*
  uint8_t buttons = lcd.readButtons();

  if (buttons) {
    lcd.clear();
    lcd.setCursor(0,0);
    if (buttons & BUTTON_UP) {
      lcd.print("Not Used");
      lcd.setBacklight(RED);
      Serial.println("This space for rent");
    }
    if (buttons & BUTTON_DOWN) {
      lcd.print("Not Used");
      lcd.setBacklight(YELLOW);
      Serial.println("this space for rent");
    }
    if (buttons & BUTTON_LEFT) {
      lcd.print("LEFT ");
      lcd.setBacklight(GREEN);
    }
    if (buttons & BUTTON_RIGHT) {
      lcd.print("RIGHT ");
      lcd.setBacklight(TEAL);
    }
    if (buttons & BUTTON_SELECT) {
      lcd.clear();
      propulsion();
      delay(2000);
      lcd.setBacklight(VIOLET);
    }
  }
  */
}
void lion(){
  int second = analogRead(potTWO);
  int two = second /200;
  Serial.println(potTWO);
  int lasttwo = 0;


  if (two != lasttwo){
//  delay(750);
  //  lcd.clear();



  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):

  lcd.setCursor(6, 1); lcd.print("   ");
  delay(10);
  lcd.setCursor(0, 1);
  lcd.print("LiON "); lcd.print(two); lcd.setCursor(8, 1); lcd.print("%");//lcd.print(" %");
  }
  else {
   lcd.setCursor(0, 1); lcd.print("Li Battery Dead");
  delay(750);
  lcd.setCursor(0, 1);lcd.print("                ");
   delay(1);
 }
}
void lead(){
  int lastone = 0;
  int first = analogRead(potONE);

  int one = first /200;
  Serial.println(first);

  if (one != lastone){
  //delay(750);
   // lcd.clear();



  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):

  lcd.setCursor(6, 0); lcd.print("   ");
  delay(10);
  lcd.setCursor(0, 0);
  lcd.print("Lead "); lcd.print(one); lcd.setCursor(8, 0); lcd.print("%");//lcd.print(" %");
  }
 else {
   lcd.setCursor(0, 0); lcd.print("PB Battery Dead");
  delay(750);
  lcd.setCursor(0, 0);lcd.print("                ");
   delay(1);
 }
  //lcd.setCursor(0, 8); lcd.print("%");
}
void propulsion(){
  int lastTHREE = 0;
  int third = analogRead(potTHREE);

  int three =third/10.23;

  if (three != lastTHREE){
  //delay(750);
   // lcd.clear();



  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):

  lcd.setCursor(6, 0); lcd.print("   ");
  delay(10);
  lcd.setCursor(0, 0);
  lcd.print("Draw "); lcd.print(three); lcd.setCursor(8, 0); lcd.print("%");//lcd.print(" %");
  }
 else {
 
   lcd.setCursor(0, 0); lcd.print("Zero Draw");
  delay(1750);
  lcd.setCursor(0, 0);lcd.print("                ");
  delay(1);
   lcd.setCursor(0, 0); lcd.print("Zero Draw");
  delay(1750);
  lcd.setCursor(0, 0);lcd.print("                ");
  delay(1);
 }
}

No comments: