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);
}
}
No comments:
Post a Comment