the delays seem'd to be needed or everything went haywire... flickering lights and gauge needles galore,
my friend has now re-written the code using the state machine method, so mucho thanks for the pointers (i did read your site about the state machine a few days ago Mr Gammon, it went over my head a little, but my friend had no problems with it, so our latest code is:
// Last updated: March 14th, 2012 - 21:40PM GMT
// Using state machine pattern - based on Nick Gammon code
typedef enum { NONE, GOT_I, GOT_O, GOT_R, GOT_o, GOT_T, GOT_S, GOT_B, GOT_H, GOT_b, GOT_h } states;
states state = NONE;
unsigned int currentValue;
void setup ()
{
Serial.begin (115200);
TCCR4B = (TCCR4B & 0xF8) | 0x01 ;
state = NONE;
pinMode(22, OUTPUT);
pinMode(23, OUTPUT);
pinMode(24, OUTPUT);
pinMode(25, OUTPUT);
pinMode(26, OUTPUT);
pinMode(27, OUTPUT);
}
// Lights
void Read_BusStopLight(const unsigned int value)
{
digitalWrite(22,Serial.read() - '0');
}
void Read_Blinker(const unsigned int value)
{
digitalWrite(24,Serial.read() - '0');
}
void Read_Highbeam(const unsigned int value)
{
digitalWrite(25,Serial.read() - '0');
}
void Read_Battery(const unsigned int value)
{
digitalWrite(26,Serial.read() - '0');
}
void Read_Handbrake(const unsigned int value)
{
digitalWrite(27,Serial.read() - '0');
}
// Gauges
void Read_Temperature(const unsigned int value)
{
analogWrite(7,map(value,0,100,0,255));
}
void Read_Rpm(const unsigned int value)
{
tone(9, map(value,0,5700,55,423));
}
void Read_Oil(const unsigned int value)
{
analogWrite(8,map(value,0,50,0,255));
}
void handlePreviousState()
{
switch (state)
{
case GOT_R:
Read_Rpm(currentValue);
break;
case GOT_o:
Read_Oil(currentValue);
break;
case GOT_T:
Read_Temperature(currentValue);
break;
case GOT_S:
Read_BusStopLight(currentValue);
break;
case GOT_B:
Read_Blinker(currentValue);
break;
case GOT_H:
Read_Highbeam(currentValue);
break;
case GOT_b:
Read_Battery(currentValue);
break;
case GOT_h:
Read_Handbrake(currentValue);
break;
case GOT_I:
digitalWrite(23,HIGH);
break;
case GOT_O:
digitalWrite(23,LOW);
break;
}
currentValue = 0;
}
void processIncomingByte (const byte c)
{
if (isdigit (c))
{
currentValue *= 10;
currentValue += c - '0';
}
else
{
handlePreviousState ();
switch (c)
{
case 'I':
state = GOT_I;
break;
case 'O':
state = GOT_O;
break;
case 'R':
state = GOT_R;
break;
case 'o':
state = GOT_o;
break;
case 'T':
state = GOT_T;
break;
case 'S':
state = GOT_S;
break;
case 'B':
state = GOT_B;
break;
case 'H':
state = GOT_H;
break;
case 'b':
state = GOT_b;
break;
case 'h':
state = GOT_h;
break;
default:
state = NONE;
break;
}
}
}
void loop()
{
if (Serial.available())
processIncomingByte (Serial.read());
}