Bob-Code

//------------------------------------------------------------------------------------------

CODE FOR ARDUINO MEGA

//------------------------------------------------------------------------------------------

#include <NewSoftSerial.h>
#include <TinyGPS.h>


//globals
int displayState;
char * charIn;
TinyGPS gps;
int gpsSpeed;
int increment;

const int groundpin = 18;             // analog input pin 4 -- ground
const int powerpin = 19;              // analog input pin 5 -- voltage
const int xpin = A3;                  // x-axis of the accelerometer
const int ypin = A2;                  // y-axis
const int zpin = A1;                  // z-axis (only on 3-axis models)

const int xRest = 358;//75 up
const int yRest = 361;
const int xOneG = 75;
const int yOneG = 78;

double maxLateralGs;

//methods
bool feedgps();


void setup() {
 
  maxLateralGs = 0.0;
 
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
 
  increment = 0;
  displayState = 0;

  Serial.begin(9600);//to debug
 
  Serial1.begin(9600);//used to communicate with the touch shield
  Serial2.begin(57600);
 
}





void loop() {
 
  if(Serial1.available()){
        char c = Serial1.read();
        if(c == '3'){
           displayState = 3;
        }else if(c == '5'){
           displayState = 5;
        }else if(c == '8'){
           displayState = 8;
        }else if(c == '0'){
           displayState = 0;
        }else{
           displayState = 0;
        }
    }
            
 //Serial.print(displayState);
 
  //int tempSpeed = -1;
  feedgps();
  int zeroSixtyState = 0;
  boolean zeroSixtyDone = false;
  long timerBegin = 0;
  int time = 0;
  switch(displayState){
    case 3:
       while(!zeroSixtyDone){
         feedgps();
         int tempSpeed = (int)gps.f_speed_mph();
         //float speedFloat = gps.f_speed_mph();
         //Serial.println(speedFloat);
         //int tempSpeed = random(60);
         //tempSpeed++;
         if(zeroSixtyState == 0){
           //digitalWrite(2, HIGH);
           if(tempSpeed == 0){
              zeroSixtyState = 1;
           }
         }else if(zeroSixtyState == 1){//stop state
             //digitalWrite(3, HIGH);
             if(tempSpeed > 2){
               timerBegin = millis();
               //delay(5000);
               zeroSixtyState = 2;
             }
         }else if(zeroSixtyState == 2){//wait for the speed to reach 60
            //digitalWrite(4, HIGH);
            if(tempSpeed >= 58){
              long timerEnd = millis();
              time = (timerEnd-timerBegin)-1000;
              zeroSixtyState = 3;
            }/*else{
              //state machine to send current speed
              Serial1.print('S');
              feedgps();
              while(!Serial1.available()){
              }
              feedgps();
              delay(10);
              if(Serial1.read() == 'S'){
                Serial1.print(tempSpeed);
              }
              feedgps();
              delay(25);
              feedgps();
              delay(25);
              feedgps();
              //end of state machine that sends current speed
            }*/
         }else if(zeroSixtyState == 3){//send start byte to touch shield
             //digitalWrite(5, HIGH);
             Serial1.print('Z');
             zeroSixtyState = 4;
         }else if(zeroSixtyState == 4){//wait for ack
             //digitalWrite(6, HIGH);
             if(Serial1.available()){
               if(Serial1.read() == 'Z'){
                   zeroSixtyState = 5;
                   Serial1.print(time);
                   zeroSixtyDone = true;
                   displayState = 0;
               }
            }
         }
       }
       break;
     case 5:
     {
       int state = 0;
       boolean done = false;
       char charIn;
       while(!done){
         if(state == 0){
            Serial1.print('Z');
            feedgps();
            state = 1;
         }else if(state == 1){
           feedgps();
            if(Serial1.available()){
               if(Serial1.read() == 'Z'){
                   state = 2;
               }
            }
         }else if(state == 2){
           feedgps();
           int gpsSpeed = (int)gps.f_speed_mph();
            if(gpsSpeed < 10){//this is stupid
               Serial1.print(0);
            }
            Serial1.print(gpsSpeed);
            done = true;
            displayState = 0;
         }  
       }
     }
        break;
      case 8:
      {
        int state = 0;
        boolean done = false;
        feedgps();
        //int timeout = millis();
        double xGs = getXGs();
        double yGs = getYGs();
        double totalLateralGs = sqrt((xGs*xGs)+(yGs*yGs));
       while(!done){
         /*if(millis()-timeout > 500){
           Serial1.flush();
           done = true;
           displayState = 0;
          }else */if(state == 0){
            Serial1.print('Z');
            feedgps();
            state = 1;
         }else if(state == 1){
           feedgps();
            if(Serial1.available()){
               if(Serial1.read() == 'Z'){
                   state = 2;
               }
            }
         }else if(state == 2){
            Serial1.print((int)(totalLateralGs*100));
            //Serial.println((int)(totalLateralGs*100));
            //Serial.println(totalLateralGs);
            done = true;
            displayState = 0;
         }  
       }
      }
        break;
     default:
        break;
  }


}

//-------------------------
//         METHODS
//-------------------------

bool feedgps()
{
  while (Serial2.available())
  {
    if (gps.encode(Serial2.read()))
      return true;
  }
  return false;
}

double getXGs(){
   int x = analogRead(xpin);
   if(x < xRest){
      return ((double)(xRest-x))/((double)xOneG);
   }else{
      return ((double)(x - xRest))/((double)xOneG);
   }
}

double getYGs(){
   int y = analogRead(ypin);
   if(y < yRest){
      return ((double)(yRest - y))/((double)yOneG);
   }else{
      return ((double)(y-yRest))/((double)yOneG);
   }
}

 


 

 

//---------------------------------------------------------------------------------------------------------------

CODE FOR TOUCHSHIELD SLIDE

//------------------------------------------------------------------------------------------------------------------

 

//
//#include <floatToString.h>
// Two-way Serial Communications

//

// TouchShield Slide <-> Arduino

//

// An open-souce Shadowlord Project

// www.IamShadowlord.com

//



/////////////////////////////////////////

//     Code for TouchShield Slide      //

/////////////////////////////////////////



// Globals

unsigned int Xstore;

unsigned int Ystore;

boolean flag = false;

int displayState;

int speed;

char *charIn;

float maxLateralGs;



// Prepare Screen and Serial

void setup()

{
  maxLateralGs = 0;
 
  speed = 0;
 
  displayState = 1;

  background(0,0,0);

  stroke(0,255,0);

  fill(0,0,0);
 
  rect(0,0,319,120);
 
  text("0-60 time",100,60,14);
 
  //rect(161,0,158,120);
 
  //text("Speedometer", 175, 60, 11);
 
  rect(0,121,319,118);
 
  text("G-forces", 100, 180, 14);
 
  //rect(161,121,158,118);
 
  //text("Quarter", 185, 170, 14);
  //text("Mile", 195, 190,14);

   

  Serial.begin(9600);

  delay(100);

}



void loop()

{

  gettouch();
 
  if(displayState == 1){//if it's on the home screen
    if( mouseX != Xstore || mouseY != Ystore ){
       if(mouseX > 0 && mouseX < 319 && mouseY > 0 && mouseY < 120){
          displayState = 2;
       }/*else if(mouseX > 160 && mouseX < 320 && mouseY > 0 && mouseY < 120){
          displayState = 4;
       }*/else if(mouseX > 0 && mouseX < 319 && mouseY > 120 && mouseY < 240){
          displayState = 7;
       }/*else if(mouseX > 160 && mouseX < 320 && mouseY > 120 && mouseY < 240){
          displayState = 9;
       }*/
    }
  }else if(displayState ==  3){
    if( mouseX != Xstore || mouseY != Ystore ){
      if(mouseX > 0 && mouseX < 100 && mouseY > 200 && mouseY < 240){
        displayState = 0;
      }
    }else{
       zeroSixtyTime();
       Serial.print('0');
       displayState = 6;//wait 0-60 screen after timing
    }
  }else if(displayState ==  5){
    if( mouseX != Xstore || mouseY != Ystore ){
      if(mouseX > 0 && mouseX < 100 && mouseY > 200 && mouseY < 240){
        displayState = 0;
      }
    }else{
      updateSpeed();
      Serial.print('0');
      Serial.flush();
    }
  }else if(displayState == 6){
    if( mouseX != Xstore || mouseY != Ystore ){
      if(mouseX > 0 && mouseX < 100 && mouseY > 200 && mouseY < 240){
        displayState = 0;
      }else if(mouseX > 220 && mouseX < 320 && mouseY > 200 && mouseY < 240){
         displayState = 3;
      }
    }
  }else if(displayState == 8){
     if( mouseX != Xstore || mouseY != Ystore ){
      if(mouseX > 0 && mouseX < 100 && mouseY > 200 && mouseY < 240){
        displayState = 0;
        maxLateralGs = 0;
      }else if(mouseX > 220 && mouseX < 320 && mouseY > 200 && mouseY < 240){
         displayState = 7;
         maxLateralGs = 0;
      }
    }else{
      updateGs();
      Serial.print('0');
      Serial.flush();
    }
  }//else if

  Xstore = mouseX;

  Ystore = mouseY;


  switch (displayState) {
    case 0://write home screen
      background(0,0,0);
      fill(0,0,0);
      rect(0,0,319,120);
      text("0-60 time",100,60,14);
      //rect(161,0,158,120);
      //text("Speedometer", 175, 60, 11);
      rect(0,121,319,118);
      text("G-forces", 100, 180, 14);
      //rect(161,121,158,118);
      //text("Quarter", 185, 170, 14);
      //text("Mile", 195, 190,14);
      displayState = 1;
      delay(25);
      break;
    case 1://wait home screen
      break;
    case 2://write 0-60 screen
      background(0,0,0);
      rect(0,200,100,41);
      text("Back",15,220,14);
      rect(220,200,100,41);
      text("Reset",235,220,14);
      displayState = 3;
      delay(25);
      break;
    case 3://wait 0-60 screen
      break;
    case 4://write speedometer screen
      background(0,0,0);
      rect(0,200,100,240);
      text("Back",15,220,14);
      text("mph",220,140,22);
      displayState = 5;
      delay(25);
      break;
    case 5://wait speedometer screen
      
      break;
    case 6:
        //wait while in 0-60 display state
      break;
    case 7:
    {
      background(0,0,0);
      rect(0,200,100,240);
      text("Back",15,220, 14);
      rect(220,200,100,41);
      text("Reset",235,220,14);
      displayState = 8;
      delay(25);
    }
      break;
    case 8:
    {
      //wait state for g-force screen
    }
      break;
    case 9:
    {
      background(0,0,0);
      rect(0,200,100,240);
      text("Back",15,220, 14);
      //rect(220,200,100,41);
      //text("Reset",235,220,14);
      displayState = 10;
      delay(25);
    }
      break;
     case 10:
     {
       //wait in the quarter mile state
     }
       break;
    default:
      break;
      
  }

}

void zeroSixtyTime(){
  char charIn = 0;
  byte i = 0;
  char stringIn[32] = "";
  int inputState = 0;
 
  boolean done = false;
  Serial.print('3');
 
  while(!done){
     if(inputState == 0){
        if(Serial.available()){
           char c = Serial.read();
           if(c == 'Z'){
              Serial.print('Z');
              inputState = 1;
           }else if(c == 'S'){
              Serial.print('S');
              delay(30);
              char speedIn = 0;
              byte j = 0;
              char speedString[32] = "";
              while(Serial.available()){
                  speedIn = Serial.read();
                  speedString[j] = speedIn;
                  j += 1;
              }
              background(0,0,0);
              speedString[j] = 'm';
              speedString[j+1] = 'p';
              speedString[j+2] = 'h';
              text(speedString, 75, 110, 30, 30);
              rect(0,200,100,41);
              text("Back",15,220,14);
              rect(220,200,100,41);
              text("Reset",235,220,14);
           }
        }
     }else if(inputState == 1){
      delay(50);
    while(Serial.available()) {

    charIn = Serial.read();

    stringIn[i] = charIn;

    i += 1;

  }
  done = true;
    }

  }
  stringIn[i] = 'm';
  stringIn[i+1] = 's';
   //rect(80,100,160,40);
   background(0,0,0);
   rect(0,200,100,41);
   text("Back",15,220,14);
   rect(220,200,100,41);
   text("Reset",235,220,14);
   text(stringIn, 75, 110, 30, 30);
   delay(25);
 
}


void updateGs(){
   //int timeout = millis();
   Serial.print('8');
   int state = 0;
   char charIn = 0;
   byte i = 0;
   char stringIn[32] = "";
   //int timeout = millis();
   boolean done = false;
   while(!done){
      if(state == 0){
          if(Serial.available()){
             if(Serial.read() == 'Z'){
                Serial.print('Z');
                state = 1;
             }
          }
       }else if(state == 1){
           delay(25);
           while(Serial.available()) {
               charIn = Serial.read();
               stringIn[i] = charIn;
               i += 1;
               done = true;
               state = 2;
            }
       }
   }
   if(stringIn[0]){
   float update = largerThanPreviousGs(stringIn);
   if(update != 0){
     background(0,0,0);
     rect(0,200,100,41);
     text("Back",15,220,14);
     rect(220,200,100,41);
     text("Reset",235,220,14);
     stringIn[i] = 'G';
     stringIn[i+1] = 's';
     if(i == 1){
       text(".0",55,110,30);
       text(stringIn,115,110,30);
     }else if(i == 2){
       text(".",55,110,30);
       text(stringIn,75,110,30);
     }else{
       stringIn[i+2] = stringIn[i+1];
       stringIn[i+1] = stringIn[i];
       stringIn[i] = stringIn[i-1];
       stringIn[i-1] = stringIn[i-2];
       stringIn[i-2] = '.';
       text(stringIn,60,110,30);
     }
   }
   }
   delay(25);
   
}


void updateSpeed(){
   char charIn = 0;
  byte i = 0;
  char stringIn[32] = "";
  int inputState = 0;
 
  Serial.print('5');
  boolean done = false;
  while(!done){
    if(inputState == 0){
       if(Serial.available()){
          if(Serial.read() == 'Z'){
             Serial.print('Z');
             inputState = 1;
          }
       }
    }else if(inputState == 1){
        if(Serial.available()){
           charIn = Serial.read();
             stringIn[0] = charIn;
             inputState = 2;
        }
    }else if(inputState == 2){
       if(Serial.available()){
          charIn = Serial.read();
            stringIn[1] = charIn;
            inputState = 4;
            done = true;
       }
    }
  }



  // Clear screen and display the serial buffer as text

  if (stringIn[0])

  {

   //background(0,0,0);
   rect(120,120,80,40);
   text(stringIn, 140,140, 30, 25);
   delay(25);

  }
}

float largerThanPreviousGs(char s[]){
    if(maxLateralGs == 0){
       int gs = atoi(s);
       float current = ((float)gs)/((float)100);
       maxLateralGs = current;
       return current;
    }else{
       int gs = atoi(s);
       float current = ((float)gs)/((float)100);
       if(current > maxLateralGs){
         maxLateralGs = current;
         return current;
       }else{
          return 0;
       }
    }
}