It was hard to pick a proper subject as there is a little to say.
The program below is going to control shifting on a small vehicle via switches. The button push will cause the signal to stay HIGH on pin 5 only or pin 5 and pin 3. I decided to debug the program by adding Serial.print to key locations so I could follow the logic. And here is the problem, with Serial.begin enabled the program works perfectly with no peculiar behavior. If I comment it out, like it is below, the program is very buggy and unreliable. This is the first program I wrote besides playing with simple LED's so maybe someone more experienced can speculate how the Serial.print may affect the code so I may try and fix it.
In essence Serial.begin disabled means code is unreliable, Serial.begin enabled means code works perfectly with extensive testing.
Thank you for you're time!
unsigned long downshiftDuration=300; //Time spent downshifting
unsigned long downshiftDeadTime=400; //Dead time after downshifting before able to shift again
unsigned long upshiftCutDelay=100; //delay before beginning cutting engine power when upshifting
unsigned long upshiftDelay=0; //delay before beginning applying CO2 when upshifting
unsigned long upshiftCutDuration=400; //time spent cutting engine power when upshifting
unsigned long upshiftDuration=500; //time spent applying CO2 when upshifting
unsigned long upshiftDeadTime=250; //Dead time after both cutting power and applying CO2 are finished
unsigned long debounce_timer=0; //Debounce timer and booleans for logic
unsigned long debounce_time=20;
boolean debounce_on=false;
boolean debounce_off=false;
boolean engineCut=false; //Logic to determine up shifting state
boolean engineUpshift=false;
int eventState='0';
unsigned long triggerTime=0;
unsigned long longestShiftTime=0;
int relay_upshift=3;
int relay_downshift=7;
int engine_signal=5;
int right_paddle=2;
int left_paddle=6;
int debounce_right=0;
int debounce_left=0;
void setup()
{
pinMode(relay_upshift, OUTPUT);
pinMode(engine_signal, OUTPUT);
pinMode(right_paddle, INPUT);
pinMode(left_paddle, INPUT);
pinMode(relay_downshift, OUTPUT);
//Serial.begin (9600);
}
void loop()
{
//event
switch(eventState)
{
case '0': //waiting for button press
Serial.println("0-0");
Serial.println(digitalRead(right_paddle));
if (digitalRead(right_paddle)==HIGH && (debounce_on==false))
{
Serial.println("debounce set right\n");
debounce_on=true;
debounce_timer=millis();
}
else if (digitalRead(left_paddle)==HIGH && (debounce_on==false))
{
Serial.println("debounce set left");
debounce_on=true;
debounce_time=millis();
}
else
{
if (digitalRead(right_paddle)==HIGH && (millis() >= (debounce_timer + debounce_time)))
{
triggerTime=millis(); //get start time
eventState='1';
longestShiftTime = max(upshiftCutDelay + upshiftCutDuration, upshiftDelay + upshiftDuration);
engineCut=false;
engineUpshift=false;
}
else if (digitalRead(left_paddle)==HIGH && (millis() >= (debounce_timer + debounce_time)))
{
Serial.print("4\n");
triggerTime=millis();
eventState='3';
digitalWrite(relay_downshift,HIGH);
}
}
break;
case '1': //run upshift timing logic
//Cutting engine power
Serial.print("case 1\n");
if (engineCut == false)
{
Serial.print("5\n");
if (millis() >= triggerTime + upshiftCutDelay)
{
Serial.print("engine signal\n");
//begin cutting engine power
engineCut=true;
digitalWrite(engine_signal,HIGH);
}
}
else
{
Serial.print("6\n");
if (millis() >= (triggerTime + upshiftCutDelay + upshiftCutDuration))
{
//stop cutting engine power
digitalWrite(engine_signal,LOW);
}
}
Serial.print("7\n");
//Applying CO2 shifting
if (engineUpshift == false)
{
Serial.print("8\n");
if (millis() >= triggerTime + upshiftDelay)
{
Serial.print("9\n");
//begin applying CO2
engineUpshift=true;
digitalWrite(relay_upshift,HIGH);
}
}
else
{
Serial.print("10\n");
if (millis() >= triggerTime + upshiftDelay + upshiftDuration)
{
Serial.print("11\n");
//stop applying CO2
digitalWrite(relay_upshift,LOW);
}
}
if (millis() >= triggerTime + longestShiftTime)
{
Serial.print("12\n");
//upshifting + dead time has elapsed
eventState='2';
triggerTime=millis();
}
break;
case '2': //waiting for dead time after end of button press
//and wait for button to be unpressed before alowing state to be reset to 0
if (digitalRead(right_paddle)==LOW && (debounce_off==false))
{
debounce_off=true;
debounce_timer=millis();
}
else if (digitalRead(right_paddle)==LOW && (millis() >= debounce_timer + debounce_time))
if (millis() >= (triggerTime + upshiftDeadTime) && digitalRead(right_paddle)==LOW)
{
eventState='0';
debounce_on=false;
debounce_off=false;
}
break;
case '3': //wait for proper time to stop applying downshift CO2
if ( millis() >= (triggerTime + downshiftDuration) && digitalRead(left_paddle)==LOW)
{
eventState='4';
digitalWrite(relay_downshift,LOW);
triggerTime=millis();
}
break;
case '4'://waiting for dead time after end of button press
//and wait for button to be unpressed before alowing state to be reset to 0
if (millis() >= (triggerTime + downshiftDeadTime) && left_paddle==LOW)
{
debounce_on=false;
eventState='0';
}
break;
}
}