Code: Select all
//#include <Servo.h>
#include <EEPROM.h>
//Servo startdrive;
//gas turbine ecu code start
// John Warnes combined and tested code combining adafruits LCD sheild code and Nathan Nissen's RPM code.
// November 2011 Mega 2560 240x320 adafruit touchsheild display
#include "TFTLCD.h"
#include "TouchScreen.h"
#if not defined USE_ADAFRUIT_SHIELD_PINOUT
#error "For use with the shield, make sure to #define USE_ADAFRUIT_SHIELD_PINOUT in the TFTLCD.h library file"
#endif
// These are the pins for the shield!
#define YP A1 // must be an analog pin, use "An" notation!
#define XM A2 // must be an analog pin, use "An" notation!
#define YM 7 // can be a digital pin
#define XP 6 // can be a digital pin
#define TEMP_SENSOR_PIN 4
#define TEMP_SENSOR_PIN 5
#define ANALOG_VOTLAGE_REFERENCE 5
#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940
// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
// Color definitions
#define BLUE 0x001F
#define BLACK 0x0000
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define LTGREEN 0x3F11
#define JJCOLOR 0x4444
#define ARROW 0x8888
TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, 0);
int TIT = 0;
int TOT = 0;
int waittime = 0;
int throttle =12;
int idlespeed = 0;
int warmuptime = 0;
int rampup = 0;
int rampdn = 0;
int running = false;
int ignitionon = false;
int gas = false;
int dies = false;
int debouncetime = 0;
int fuelpump = 22; // pin output for fuelpump
int gassol = 23; // pin output for gas line solenoid
int ignition = 24; // pin output for ignition relay
int starter = 12; // pin output for starter unit PWM 12
int startduty = 0;
volatile unsigned long rpmcount;
unsigned long rpm;
unsigned long currenttime;
unsigned long runtime;
unsigned long starttime;
unsigned long gasruntime;
void rpm_fun(){
rpmcount++;
}//Each rotation, this interrupt function is run once, so take that into consideration for calculating RPM
void setup(void) {
//startdrive.attach(12);
pinMode(fuelpump, OUTPUT);
pinMode(gassol, OUTPUT);
pinMode(ignition, OUTPUT);
pinMode(starter, OUTPUT);
Serial.begin(9600);
attachInterrupt(0, rpm_fun, FALLING);
rpmcount = 0;
rpm = 0;
//for (int i = 0; i < 512; i++) //wipes eeprom if required
//EEPROM.write(i, 0);
tft.reset();
tft.initDisplay();
tft.setRotation(1);
pinMode(13, OUTPUT);
welcomescreen();
delay(500);
mainscreen();
rampup = EEPROM.read(0); //retreives values from eeprom and writes to the screen
tft.setTextColor(YELLOW);
tft.setTextSize(1);
tft.fillRect(63, 178, 25, 10, BLACK);
tft.setCursor(66, 180);
tft.println(rampup,DEC);
rampdn = EEPROM.read(1);
tft.fillRect(71, 192, 20, 10, BLACK);
tft.setCursor(74, 195);
tft.println(rampdn,DEC);
idlespeed = EEPROM.read(2);
tft.fillRect(73, 208, 15, 10, BLACK);
tft.setCursor(75, 210);
tft.println(idlespeed,DEC);
warmuptime = EEPROM.read(3);
tft.fillRect(86, 223, 35, 10, BLACK);
tft.setCursor(93, 225);
tft.println(warmuptime,DEC);
}
#define MINPRESSURE 4
#define MAXPRESSURE 600
void loop()
{
digitalWrite(13, HIGH);
Point p = ts.getPoint();
digitalWrite(13, LOW);
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
//startdrive.write(pos);
if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
p.x = map(p.x, TS_MINX, TS_MAXX, tft.height(), 0);
p.y = map(p.y, TS_MINY, TS_MAXY, tft.width(), 0);
//Serial.print("("); Serial.print(p.x); Serial.print(", "); Serial.print(p.y); Serial.println(")");
if (p.x < 190 && p.y < 160) { // if "E" is pressed run the update eeprom section, and load the current settings into memory for storage.
if (p.x > 165 && p.y > 130){
updateeeprom();
}
}
if (p.x < 45 && p.y > 150) { //if the correct area on the screen is pushed for that button, it increments that variable and updates the screen.
tft.setTextColor(YELLOW);
tft.setTextSize(1);
if (p.y < 195) {
rampup--;
tft.fillRect(63, 178, 25, 10, BLACK);
tft.setCursor(66, 180);
tft.println(rampup,DEC);
}
else if(p.y < 240) {
rampdn--;
tft.fillRect(71, 192, 20, 10, BLACK);
tft.setCursor(74, 195);
tft.println(rampdn,DEC);
}
else if(p.y < 285) {
idlespeed--;
tft.fillRect(73, 208, 15, 10, BLACK);
tft.setCursor(75, 210);
tft.println(idlespeed,DEC);
}
else {
warmuptime--;
tft.fillRect(86, 223, 35, 10, BLACK);
tft.setCursor(93, 225);
tft.println(warmuptime,DEC);
}
}
if (p.x > 45 && p.x < 90) {
tft.setTextColor(YELLOW);
tft.setTextSize(1);
if (p.y >150) {
if (p.y < 195) {
tft.fillRect(63, 178, 25, 10, BLACK);
rampup++;
tft.setCursor(66, 180);
tft.println(rampup,DEC);
}
else if(p.y < 240) {
rampdn++;
tft.fillRect(71, 193, 20, 10, BLACK);
tft.setCursor(74, 195);
tft.println(rampdn,DEC);
}
else if(p.y < 285) {
idlespeed++;
tft.fillRect(73, 208, 15, 10, BLACK);
tft.setCursor(75, 210);
tft.println(idlespeed,DEC);
}
else {
warmuptime++;
tft.fillRect(86, 223, 35, 10, BLACK);
tft.setCursor(93, 225);
tft.println(warmuptime,DEC);
}
}
} // END of arrow button settings input section.
currenttime = millis(); // keeps time of how long ecu has been running, time events are recorded off this for processing
if (debouncetime > 100) {
debouncetime = 0;
}
if (p.x > 160 && p.y < 90 && debouncetime > 2) // a press on the start button, commences only if the button hasnt been already pressed recently (debounce)
{
running = !running;
debouncetime = 0; //debounces the start button
starttime = currenttime; //records the time the engine started
if (running == true) { // if START button is pressed, print the red stop button.
tft.fillCircle(50,50, 42, RED);
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.setCursor(28, 42);
tft.println("STOP");
}
else if (running == false) { // if STOP button is pressed, print the green start button.
tft.fillCircle(50,50, 42, GREEN);
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.setCursor(20, 42);
tft.println("START");
}
}
}
waittime = waittime++;
//int throttleread = analogRead(A15); //throttle pot connected to pin 15 0 -- 5 volt input 5k pot.
//throttle = map(throttleread, 0, 1023, 1, 100);
if(running == true)
{
ignitionon = running;
//starts gas at engine start
digitalWrite(ignition, HIGH); //starts ignition at engine start
analogWrite(starter, startduty);
if (rpm > 2000) { //ensures gas is only injected if engine turning faster than 2000 rpm.
digitalWrite(gassol, HIGH);
gas = running;
}
}
if(running == false)
{
runtime = 0;
starttime = 0;
startduty = 0;
digitalWrite(fuelpump, LOW); //if not running, keeps all fuel, gas and spark off
digitalWrite(gassol, LOW);
digitalWrite(ignition, LOW);
analogWrite(starter, 0);
dies = running;
ignitionon = running;
gas = running;
}
if(running == true && runtime > 20000 && rpm > 2000) //20 seconds after start switchover fuel check this later
{
digitalWrite(gassol, LOW);
dies = running;
gas = !running;
digitalWrite(fuelpump, HIGH);
}
if (waittime == 200 ) {
showpos();
}
if (waittime == 400 ) {
readT2();
}
if (waittime == 500 ) {
updateb();
}
if (waittime == 600 ) {
readT1();
}
if (waittime > 800 )
{ //take snapshot of rpm data periodically
waittime = 0;
runtime = currenttime - starttime; //total of running time is the total ecu time minus the time START was pressed. keeps the runtime updated.
if(running == false)
{
(runtime = 0);
}
Serial.print("Runtime is "); Serial.println(runtime,DEC); Serial.print("gasruntime is"); Serial.println(gasruntime,DEC); Serial.print("currenttime is"); Serial.println(currenttime,DEC); Serial.print("Starttime is"); Serial.println(starttime,DEC);
debouncetime = debouncetime++;
if (running == true)
{if (startduty < 254 ) // if engine starting, ramp up starter effort untill full power.
{
startduty = startduty+3; // 3 is rate of starter power increase
analogWrite(starter, startduty);}
}
detachInterrupt(0);//Don't process interrupts during calculations
rpm = 100*rpmcount; //1000 if using the 4017 divide by 10 counter
rpmcount = 0;
attachInterrupt(0, rpm_fun, FALLING); //Restart the interrupt processing
tft.fillRect(123, 12, 123, 34, BLACK);//erase box and print main RPM display
tft.setCursor(125, 15);
tft.setTextSize(4);
tft.setTextColor(YELLOW);
tft.println(rpm,DEC);
tft.fillRect(101, 164, 25, 10, BLACK);
tft.setTextSize(1);
tft.setTextColor(YELLOW);
tft.setCursor(102, 165);
tft.println(startduty,DEC);
}
}
void mainscreen()
{
tft.fillScreen(BLACK);//fills screen with black
tft.fillCircle(50,50, 42, GREEN);//draws start button
tft.drawCircle(50, 50, 44, WHITE);
tft.drawCircle(50, 50, 45, YELLOW);
tft.drawRect(120, 10, 200, 38, WHITE); //RPM main display
tft.drawRect(155, 50, 165, 28, WHITE);//TIT BOX (OI! keep it clean)
tft.drawRect(155, 80, 165, 28, WHITE);//Tot box
tft.drawRect(155, 110, 165, 28, WHITE);//tht box
tft.drawRect(1, 100, 18, 18, WHITE); //IGN box
tft.drawRect(1, 120, 18, 18, WHITE); //GAS box
tft.drawRect(1, 140, 18, 18, WHITE); //DIES box
tft.drawRect(123, 50, 30, 30, WHITE); //eeprom store button box
tft.drawRect(155, 157, 40, 40, WHITE);//1st box
tft.drawRect(197, 157, 40, 40, WHITE);//2nd box
tft.drawRect(239, 157, 40, 40, WHITE);//3rd box
tft.drawRect(280, 157, 40, 40, WHITE);//4th box
tft.drawRect(155, 200, 40, 40, WHITE);//5th box
tft.drawRect(197, 200, 40, 40, WHITE);//6th box
tft.drawRect(239, 200, 40, 40, WHITE);//7th box
tft.drawRect(280, 200, 40, 40, WHITE);//8th box
tft.fillRect(157, 159, 36, 36, LTGREEN);//fill 1st box
tft.fillRect(199, 159, 36, 36, LTGREEN);//fill 2nd box
tft.fillRect(241, 159, 36, 36, LTGREEN);//fill 3rd box
tft.fillRect(282, 159, 36, 36, LTGREEN);//fill 4th box
tft.fillRect(157, 202, 36, 36, LTGREEN);//fill 5th box
tft.fillRect(199, 202, 36, 36, LTGREEN);//fill 6th box
tft.fillRect(241, 202, 36, 36, LTGREEN);//fill 7th box
tft.fillRect(282, 202, 36, 36, LTGREEN);//fill 8th box
tft.fillTriangle(175, 161, 160, 191, 190, 191, ARROW);//Arrow 1st box
tft.fillTriangle(217, 161, 202, 191, 232, 191, ARROW);//Arrow 2nd box
tft.fillTriangle(260, 161, 245, 191, 274, 191, ARROW);//Arrow 3rd box
tft.fillTriangle(285, 191, 300, 161, 315, 191, ARROW);//Arrow 4th box
tft.fillTriangle(160, 205, 175, 236, 190, 206, ARROW);//Arrow 5th box
tft.fillTriangle(202, 205, 217, 236, 232, 206, ARROW);//Arrow 6th box
tft.fillTriangle(245, 205, 260, 236, 274, 206, ARROW);//Arrow 7th box
tft.fillTriangle(285, 205, 300, 236, 315, 206, ARROW);//Arrow 8th box
tft.setTextColor(WHITE); //print START in green circle
tft.setTextSize(2);
tft.setCursor(21, 42);
tft.println("START");
tft.setTextColor(YELLOW); //print info next to output indicator boxes
tft.setCursor(22, 102);
tft.println("Ign");
tft.setCursor(22, 122);
tft.println("Gas");
tft.setCursor(22, 142);
tft.println("Dies");
tft.setTextSize(1);
tft.setCursor(155, 145);
tft.println("Ramp Up");
tft.setCursor(200, 145);
tft.println("Ramp Dn");
tft.setCursor(247, 145);
tft.println("Idle");
tft.setCursor(279, 145);
tft.println("Warm up");
tft.setCursor(4, 165);
tft.println("Starter effort =");
tft.setCursor(4, 180);
tft.println("Ramp up =");
tft.setCursor(4, 195);
tft.println("Ramp down =");
tft.setCursor(4, 210);
tft.println("Idlespeed =");
tft.setCursor(88, 210);
tft.println("00 rpm");
tft.setCursor(4, 225);
tft.println("Warm up time =");
tft.setTextSize(4);
tft.setCursor(222, 15);
tft.println(" RPM");
tft.setTextSize(3);
tft.setCursor(129, 54);
tft.println("E");
tft.setTextColor(GREEN);
tft.setCursor(230, 53);
tft.println("C TIT");
tft.setTextColor(RED);
tft.setTextSize(2);
tft.setCursor(218, 81);
tft.println("o");
tft.setCursor(230, 83);
tft.setTextSize(3);
tft.println("C TOT");
tft.setTextColor(BLUE);
tft.setCursor(247, 113);
tft.println(" THT");
tft.setTextSize(2);
tft.setCursor(218, 51);
tft.setTextColor(GREEN);
tft.println("o");
tft.setCursor(220, 114);
tft.setTextColor(BLUE);
tft.setTextSize(3);
tft.println("%");
}
void readT1()
{
int raw1 = analogRead(4); //read and calculate and display 1st thermocouple
TIT = 3 * ( 5.0 * raw1 * 100.0) / 1024.0; //3 is temp multiplier so we can read up to 15 volts (with 5 volt max input pin)
tft.fillRect(157, 52, 58, 23, BLACK);//erase box
tft.setCursor(160, 53);
tft.setTextSize(3);
tft.setTextColor(GREEN);
tft.println(TIT,DEC);
}
void readT2()
{
int raw2 = analogRead(5); //read and calculate and display 2nd thermocouple
int TOT = 3 * ( 5.0 * raw2 * 100.0) / 1024.0;
tft.fillRect(157, 81, 60, 24, BLACK);//erase box and print second temp display
tft.setCursor(160, 83);
tft.setTextColor(RED);
tft.setTextSize(3);
tft.println(TOT,DEC);
if (currenttime < 8000) {
rpm = 0;
TIT = 0;
TOT = 0;
}//avoids false rpm counting at startup
}
void showpos()
{
tft.fillRect(157, 111, 62, 25, BLACK);//erase box and print tht pos display
tft.setCursor(160, 114);
tft.setTextSize(3);
tft.setTextColor(BLUE);
tft.println(throttle,DEC);
}
void welcomescreen()
{
tft.fillScreen(BLACK);
tft.fillRect(71, 70, 190, 100, JJCOLOR);
tft.drawString(85, 100, "J", WHITE, 5);
delay(200);
tft.drawString(147, 100, "G", WHITE, 5);
delay(200);
tft.drawString(210, 100, "T", WHITE, 5);
delay(200);
tft.drawRect(69, 68, 192, 102, WHITE);
delay(200);
tft.drawRect(67, 66, 196, 106, WHITE);
tft.drawString(105, 195, "John Warnes - 2011", WHITE);
tft.drawString(85, 220, "John's Gas Turbine Project", WHITE);
}
void updateeeprom() //stores these values into eeprom memory when "E" is pressed
{
EEPROM.write(0, rampup);
EEPROM.write(1, rampdn);
EEPROM.write(2, idlespeed);
EEPROM.write(3, warmuptime);
}
void updateb()
{
if (ignitionon == true) {
tft.fillRect(3, 101, 14, 15, YELLOW);
tft.drawVerticalLine(3, 88, 12, BLACK);
}
if (ignitionon == false) {
tft.fillRect(3, 101, 14, 14, BLACK);
}
if (gas == true) {
tft.fillRect(3, 122, 14, 14, YELLOW);
}
if (gas == false) {
tft.fillRect(3, 122, 14, 14, BLACK);
}
if (dies == true) {
tft.fillRect(3, 142, 14, 14, YELLOW);
}
if (dies == false) {
tft.fillRect(3, 142, 14, 14, BLACK);
}
}