I've attempted to clean the code further as well as label everything more clearly.
I got rid of the ULmap at the moment as it still won't compile.
I also attempted to use a fucntion in start mode to get rid of all the clutter. I don't know if I implemented it correctly. Please let me know. Thanks
//--- NEWMAN180 1/4/11
//------------------------------------------------------------------------
#include <LiquidCrystal.h>
volatile unsigned int revPeriod; // Rotation Time
volatile unsigned long prevTDCtime; // Previous time Hall Sensor Picked up
volatile unsigned long TDCtime; // Current TDC Time
volatile unsigned long nextTDC; // Predicted next TDC Time
volatile unsigned long rpm; // Used to calculate RPM
volatile unsigned int crankAngle; // Crankshaft Angle out of 360 degrees
unsigned int a; // first crank angle of 180
unsigned int a1; // second crank angle of 180
unsigned int b; // first crank angle of 360
unsigned int b1; // second crank angle of 360
boolean TDCinterrupt1 = false; // States very first interrupt is ignored
const int timingPin = 14; // Timing Button Assignment
const int maintbuttonPin = 15; // Maint Button Assignment
const int pushstartPin = 16; // Push Start Button Assignment
int pushstartState = 0; // Push Start Button State
int maintbuttonState = 0; // Maint Button State
int buttonstate = 0; // Used to count button pushes for Timing
int buttonpushcounter = 0;
int lastbuttonstate = 0;
int start; // Used when declaring Start Mode Function
int timing; // Used when declaring Timing Mode Function
int maint; // Used when declaring Maint Mode Function
const int V17Pin = 9; // Valves 1 and 7 assigned to Pin 3 Intake
const int V35Pin = 11; // Valves 3 and 5 assigned to Pin 4 Intake
const int V28Pin = 10; // Valves 2 and 8 assigned to Pin 5 Exhaust
const int V46Pin = 12; // Valves 4 and 6 assigned to Pin 6 Exhaust
LiquidCrystal lcd (8,7,6,5,4,3);
void setup()
{
// Communicate to Serial Port
Serial.begin (9600);
lcd.begin(16,2);
pinMode (V17Pin, OUTPUT); // Valve 1&7 as Output
pinMode (V35Pin, OUTPUT); // Valve 3&5 as Output
pinMode (V28Pin, OUTPUT); // Valve 2&8 as Output
pinMode (V46Pin, OUTPUT); // Valve 4&6 as Output
pinMode (pushstartPin, INPUT); // Push Start Button Input
pinMode (timingPin, INPUT); // Timing Button Input
pinMode (maintbuttonPin, INPUT); // Maint Button Input
rpm = 0;
nextTDC = 1;
lcd.setCursor(0,0);
lcd.print("ON");
attachInterrupt(0,TDCinterrupt, RISING); // Interrupt 0 is Pin 2 Hall Effect Sensor
digitalWrite(2,HIGH);
delay(3500);
}
void TDCinterrupt()
{
TDCinterrupt1 = true; // First interrupt has passed
prevTDCtime = TDCtime; // save last TDC
TDCtime = micros(); // save THIS TDC
revPeriod = TDCtime - prevTDCtime; // calculate revolution period
if (prevTDCtime > TDCtime) {
revPeriod = TDCtime - prevTDCtime + 2147483648;} // correct for micros() overflow every 71 minutes
nextTDC = TDCtime + revPeriod;
rpm = 60000000L/revPeriod; // Calculate RPM divide rotation period by the difference of micros to minute
}
void loop ()
{
if ( TDCinterrupt1)
{
cli // Disable Interrupt
crankAngle = map(micros(), TDCtime, nextTDC, 0, 360); // Change time into degrees
sei(); // Enable Interrupt
crankAngle = constrain(crankAngle, 0, 360); // Limit crankshaft vales from 0 to 360
}
//---------------------------------- Button Counter Control
buttonstate = digitalRead(timingPin);
// Update Button Counter
if ((buttonstate != lastbuttonstate) && (buttonstate == HIGH)){ buttonpushcounter++;} // If the buttonstate is greater than last, and it is high, increase counter
lastbuttonstate = buttonstate;
if( buttonpushcounter > 9) {buttonpushcounter = 0;} // If buttonstate has been pushed more than 9 times, reset to 0
//--------------------------------------- Button Timing Control
timing = timingcontrol(); //
timing; // Calling Timing Control Function
//-------------------------------- Timing Center
if ( crankAngle >= a && crankAngle <= a1)// If the crankshaft is inbetween 1 to 179 degrees of revolution then a and a1 from timing function
{
digitalWrite(V17Pin, HIGH); // Fire Valve 1 and 7 (Intake)
digitalWrite(V46Pin, HIGH); // Fire Valve 4 and 6 (Exhaust)
Serial.println("Intake (1,7) Exhaust (4,6) Fired 100%"); // Print Status and Percent of Valve Open
lcd.setCursor(12,1);
lcd.print ("++");
}
else
{
digitalWrite(V17Pin, LOW); // Don't Fire Valve 1 and 7 (Intake)
digitalWrite(V46Pin, LOW); // Don't Fire Valve 4 and 6 (Exhaust)
lcd.setCursor(12,1);
lcd.print (" ");
}
if ( crankAngle >= b && crankAngle <= b1) // If the crankshaft is inbetween 181 to 359 degrees of revolution then use b and b1 from timing function
{
digitalWrite(V35Pin, HIGH); // Fire Valve 3 and 5 (Intake)
digitalWrite(V28Pin, HIGH); // Fire Valve 2 and 8 (Exhaust)
lcd.setCursor(14,1);
lcd.print ("++");
Serial.println("Intake (3,5) Exhaust (2,8) Fired 100%"); // Print Status and Percent of Valve Open
}
else
{
digitalWrite(V35Pin, LOW); // Don't Fire Valve 3 and 5 (Intake)
digitalWrite(V28Pin, LOW); // Don't Fire Valve 2 and 8 (Exhaust)
lcd.setCursor(14,1);
lcd.print (" ");
}
//------------------------------- RPM Display
lcd.setCursor(1,1);
lcd.print(rpm,DEC);
//------------------------------- START MODE
start = startmode();
start; // Calling Start Mode function
//--------------------------------Maitenance Mode
maint = maintmode();
maint; // Calling Maint Mode Function
}
//-------------------------Start Mode Function
int startmode (){
if (TDCtime == 0){
pushstartState = digitalRead(pushstartPin);
if (pushstartState == HIGH){
delay(3000);
Serial.print ("** Start MODE **");
lcd.clear();
lcd.setCursor(3,0);
lcd.print ("Start Mode");
// Open All Valves for 3.5 Seconds
digitalWrite( V17Pin, HIGH);
digitalWrite( V35Pin, HIGH);
digitalWrite( V28Pin, HIGH);
digitalWrite( V46Pin, HIGH);
lcd.setCursor(0,1);
delay(3500);
// Close All Valves and wait for 15 seconds
digitalWrite( V17Pin, LOW);
digitalWrite( V35Pin, LOW);
digitalWrite( V28Pin, LOW);
digitalWrite( V46Pin, LOW);
delay(15000);
lcd.clear();
lcd.print("Starting");
int i;
int n;
n = 5;
for (i = 0; i < n ; i++) { // Do the following function 5 times in a row. Open one intake and all exhaust
digitalWrite( V17Pin, HIGH);
digitalWrite( V28Pin, HIGH);
digitalWrite( V46Pin, HIGH);
delay(100);
digitalWrite( V28Pin, LOW);// Open and close exhaust every 10th of a second
delay(100);}
int j;
int m;
m = 5;
for (j = 0; j < m ; j++) { // Do the following function 5 times in a row. Open other intake and all exhaust
digitalWrite( V35Pin, HIGH);
digitalWrite( V28Pin, HIGH);
digitalWrite( V46Pin, HIGH);
delay(100);
digitalWrite( V46Pin, LOW);// Open and close exhaust every 10th of a second
delay(100);}
else{
lcd.setCursor(0,0);
lcd.print("START *KEY*");// If tdc time still equals 0, Please hit start button again
}
}
}
//-------------------------------- Timing Control Function
int timingcontrol()
{
a = 1;
b = 181;
a1 = 180 - 18 * buttonpushcounter; // a1 is upper end of first half of crank angle
if (buttonpushcounter ==0) a1 = a1 -1; // When buttonpushcounter equals 0, it demands a seperate function to equal 179 rather than 0
b1 = a1 + 180; // b1 is the upper end of the second half of crank angle
lcd.setCursor(12,0);
lcd.print(100-10*buttonpushcounter); // print value of timing
lcd.print("%");
}
//-------------------------------- Maint Mode Function
int maintmode() {
maintbuttonState = digitalRead(maintbuttonPin);
if (maintbuttonState == HIGH){
cli(); // disable interrupt
TDCtime = 0; // set all values to 0 to enforce no "automatic" rotation
nextTDC = 0;
prevTDCtime = 0;
lcd.clear();
lcd.print("Maintenance MODE");
digitalWrite(V28Pin,HIGH); // Open all Exhaust and stay open as long as maintbutton is high
digitalWrite(V46Pin,HIGH);
}
}