Sunday, June 7, 2015

VW Project - ELT222 Final Code

Here is my final code for my ELT222. There are a lot of spots that are "commented" out because they are either works in progress or I have yet to go in and clean them up.

This code is the mutant hybrid of three programs. I basically consolidated three Arduinos (and their programs) in to one device (Mega) and one program..
Because each program was calculating the same values, I had to scoop up all the similar calculations and move it to the main loop function which sped up the system. From there, I was able to pass the calculations into the various sub-functions without increasing latency.

This project is far from done. I hope to continue working on it the rest of my time at Chemeketa.


*********Code*Begin***************
// R3
#include <SoftwareSerial.h>
#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 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 = 9; // Ignition ON/OFF
int pb = A0; // Pot to test charge levels
int lion = A1; // Pot to test charge levels
int gennie= 8; // DC Motor port
//int unoPhoneHome = A5; // Would you like to accept a collect call from Uno?
//int pbOUT = A3; // PB info to Mega @ A0
//int lionOUT = A4; // Lion info to Mega @ A1


// R2

int dcMeter = A6;// Takes in dc (genny) current
//int dcTemp = A4; // Are we going to die? // going to eventually figure out how to do this..
int specialRly = 22;// Relay that is connected direct to propulsion.
int genny = 24; // dcMotor
int allGood = 26;// yay we can drive!
int tempPB = 28;
int halt = 30;


void lcdsetup() {

 // Serial.begin(9600);

  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);
}

void r2setup() {

     pinMode (dcMeter, INPUT); // Input for dcMotor current while driving.
     pinMode (specialRly, OUTPUT); // controls relay for extra push
     pinMode (genny, OUTPUT);// dcMotor out to mega
     pinMode (allGood, OUTPUT); //yay
     pinMode (tempPB, OUTPUT);
     pinMode (halt, INPUT);
    // Serial.begin(9600);
}


void setup() {
  lcdsetup();
  r2setup();
  // 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 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 yoMamaFlo = analogRead(dcMeter);
    int ruff = yoMamaFlo /200 ;
  r2loop(ruff);

  int lcdpb = map(pbBatt, 0,1023, 0, 100);
  int lcdlion = map(lionBatt, 0,1023, -3, 100);
  lcdloop(lcdpb, lcdlion);
  int key = digitalRead(ignSW);

  if (key == HIGH){
//Send variables into decisions..
    decisions(pbtotal, liontotal);


                  }

          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 != 2){
   
              digitalWrite(ledChg,HIGH);
              delay(100);
              digitalWrite(ledChg, LOW);
        q++;
        delay(100);
       }
       digitalWrite(ledChg, LOW);
}


void chargeDamnYou (int pbC, int lionC){// Everthing is dead, charge it!
  blinkDamnit();
  if (pbC > 3){
    while (pbC > 4){ // charge to atleast 4
    motor(HIGH, LOW);
    }
            if (lionC > 3){
              while (lionC > 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(int decpb, int declion) {// Brains of the operation..

  //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 (decpb || declion >= 4){//Everythings good.

    digitalWrite(ledPBGood, HIGH);
    digitalWrite(ledLIONGood, HIGH);
    motor(LOW, LOW);
 
  }
            if (decpb < 3 && declion >= 4){//Lead acid needs some juice.
       
                digitalWrite(ledPBGood, LOW);
                digitalWrite(ledLIONGood, HIGH);
                motor(HIGH,LOW);
           
          }
              if (decpb >= 4 && declion <= 3){//Lead acids cool, but LiON needs some juice.
                digitalWrite(ledPBGood, HIGH);
                digitalWrite(ledLIONGood, LOW);
                motor(HIGH,HIGH);
             
         
              }
              if (decpb < 2 && declion < 2){// The "Oh Shit" protocol.. Charge Everything!!
                  digitalWrite(ledPBGood, LOW);
                  digitalWrite(ledLIONGood, LOW);
                  motor(HIGH,LOW);
                  chargeDamnYou (decpb, declion);
                //  while (decpb < 2 && declion < 2){
                 
                 //   lcd.setCursor(0, 1);lcd.print("Charge Batteries");
                   // delay(200);
                   // lcd.clear();
                 // }
                 
                 
              }
            //  loop();
       
}
void r2loop(int woof) {
/*  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(woof);
 
         
            }
}
void go(int flow){

 
  //  Serial.println("Flow_"); Serial.print(flow);
 
            if (flow >= 2){ // Movin on up!
                        digitalWrite(specialRly, HIGH);
                        digitalWrite(genny, HIGH);
                        digitalWrite(allGood, LOW);
                     
                       }
            else { //What went up is now going down..
                    digitalWrite(specialRly, LOW);
                    digitalWrite(genny, 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);
  }


}
//uint8_t i=0;
void lcdloop(int lead, int lith) {
  int prop = map (analogRead(A6), 0, 1023, 0 , 100);
  uint8_t i=0;
  //one();
  leadLCD(lead);
  lionLCD(lith);
  // 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);
    }
  }
//This if loop doesn't work..
  if (prop != 0){
    BUTTON_SELECT;
    /*lcd.clear();
    lcd.setCursor(11,1);
    lcd.print("      ");
    delay(1);
    lcd.print("F= "); lcd.print(prop);
    delay(10);*/
  }
}
void lionLCD(int second){
  //int second = analogRead(lion);
 int two = second; //9.5;
  //Serial.println(potTWO);
  int lasttwo = 0;
  Serial.println("LCD_");Serial.print(two);

  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 leadLCD(int first){
  int lastone = 2;
  //int first = analogRead(pb);

  int one = first; //10;
  //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(A6);

  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);
 }

}

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);
 }
}

Friday, May 15, 2015

LCD Proof of Concept


This video and code are rough cuts of what I hope to do with this configuration. Once I get more time, I am going to connect all three Arduinos together and see if I can get each to maintain functionality while displaying information on screen.
I may move the entire nested if statements out and into their own function in order to control refreshing the display or for use with the additional LCD screen.

Code Below:

/***********************
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 <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 Battery from Mega
int potTWO = A1;//Connect to Lithium Ion Battery from Mega
int potTHREE = A2; //Connect to R3 to get pull from propulsion

void setup() {
  pinMode (potONE, INPUT);
  pinMode (potTWO, INPUT);
  pinMode (potTHREE, INPUT);
  // Debugging output
  Serial.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() {

  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/10.23;
  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/10.23;

  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);
 }
}

Wednesday, May 13, 2015

Had to share..





Upgraded to a bigger more functional toolbox today! My last toolbox was a second hand thing I got at GoodWill. It served it's purpose but its time to move on..

Now when Chuck is tossing me cool new gear, I'll have a place to put where I don't have to worry about it getting messed up. Plus I can store my current projects in the bottom part without having to disassemble everything and then have to start all over again...

Anyone looking to get this toolbox can find it at Home Dept.

Here is the link the toolbox.  <-- Click Here







More about the project:
I am currently working on getting an LCD display to give me real-time updates on lead-acid battery, lithium ion battery, current pull from the engine and other status updates from the arduinos. Chuck was kind enough to give me a couple of options concerning LCDs (one adafruit LCD Shield and a sleeker parallel LCD screen) to choose from. Once I figure out how to get the parallel screen to run, I can better make a decision of which to go with. At this point I am leaning towards the adafruit shield simple because I have the option of using the stock buttons to pull up information. 

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);
                    }
            
            }
}



What if we go up hills?



I wrote a program that will not only answer the above question, it will address any instance that we are taking more juice than what the batteries can provide..

Code Below:



int dcMeter = A3;// Takes in dc (gennie) current
int dcTemp = A4; // Are we going to die?
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
 pinMode (allGood, OUTPUT); //yay
 Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
int vroomVroom = 1;// change later to reflect driving
if (vroomVroom == 1){
int yoMamaFlo = analogRead(dcMeter);
//Serial.println(" yoMamaFlo_"); Serial.print( yoMamaFlo);
int flow = yoMamaFlo /200 ;
 Serial.println("Flow_"); Serial.print(flow);
if (flow >= 2){
  
  
  motor(HIGH,HIGH);
  digitalWrite(allGood, LOW);
  
 }
else{
motor (NULL,LOW);// return control back to primary program
digitalWrite(allGood, HIGH);
}

}
}

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

}

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);
          }
 
}
 

Wednesday, April 29, 2015

Very basic controls for the VW Bug..


This was just a proof of concept for the VW Bug project.. Now that I have gotten this to work, I can slowly add more elements into the mix..

Code:
(This is just proof of concept code)(The serial outputs are solely for debugging purposes)

int ledPB = 13;
int ledLION = 12;
int ignON;//5
int pbBatt;//A0
int lionBatt;//A1
int dcMotor; //7
void setup() {
  // put your setup code here, to run once:
pinMode (A0, INPUT);
pinMode (A1, INPUT);
pinMode (ledPB, OUTPUT);
pinMode (ledLION, OUTPUT);
pinMode (5, INPUT);
pinMode (7, OUTPUT);

Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:


 int ignVal = digitalRead(4);
 if (int ignVal = digitalRead(4) == HIGH){
   startup();
   Serial.println (ignVal);
 }
 else{
   dcMtrCrl(0);
 }


}

void dcMtrCrl (int x) {
  digitalWrite(7, x);
 
}
void startup(){
  int pbVal = analogRead(A0);
int lionVal = analogRead(A1);
int pbCharged = 0;
int pbtotal = pbVal / 204;
int lionCharged = 0;
int liontotal = lionVal / 204;

  if (liontotal >= 4){
      lionCharged = 1;
      Serial.println("lion");
      Serial.print(liontotal);
        }
    else {
      lionCharged = 0;
     
      dcMtrCrl(0);
     
    }
  if (pbtotal >= 4){
      pbCharged = 1;
      Serial.println("pb");
      Serial.print(pbtotal);
    }
    else {
      pbCharged = 0;
     
      dcMtrCrl(0);
    }
if (pbCharged == 1 || lionCharged == 1){
  dcMtrCrl(HIGH);
}
else {
  dcMtrCrl(0);
}
}


Monday, April 27, 2015

ELT222 Lab 04



This was a difficult one. However, I found out after about 2 hours of troubleshooting that I had one character wrong that was making the whole program not work correctly..


Code examples I used were from:
http://www.instructables.com/id/Arduino-Webserver-Control-Lights-Relays-Servos-etc/

Here is the code:
#include <SPI.h>
#include <Ethernet.h>

int led = 3;

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x09, 0x63 };
byte ip[] = { 192, 168, 1, 21 };

EthernetServer server(80); //server port
String readString;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
pinMode(led, OUTPUT);

// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());

}

void loop() {

EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();

//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.print(c);
}

if (c == '\n') {
Serial.println(readString);

client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
client.println("<HTML>");
client.println("<HEAD>");
client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
client.println("<TITLE>Jimi's Over Complicated Light Switch</TITLE>");
client.println("</HEAD>");
client.println("<BODY>");
client.println("<hr />");
client.println("<br />");
client.println("<H1>Arduino with Ethernet Shield</H1><br />");
client.println("<H2>ELT222 Lab04</H2>");
client.println("<br />");
client.println("<a href=\"/?button1on\"\">Turn On LED</a>");
client.println("<a href=\"/?button1off\"\">Turn Off LED</a><br />");

client.println("</BODY>");
client.println("</HTML>");
delay(1);
//stop client
client.stop();
//controls the Arduino if you press the buttons
if (readString.indexOf("?button1on") > 0){
digitalWrite(led, HIGH);
}
if (readString.indexOf("?button1off") > 0){
digitalWrite(led, LOW);
}

//clearing string for next read
readString="";

}
}
}
}
}