 
 void setup()
 {
-    Serial.begin(9600);
+    Serial.begin(SERIAL_SPEED);
     Serial.println("Starting...");
 
     pinMode(PIN_PWM, OUTPUT);
     analogWrite(PIN_PWM, 0);
 
     Serial.println("Setup complete.");
-}
-
-byte read_ser()
-{
-    // block until we have something
-    while (1)
-    {
-        if (Serial.available())
-        {
-            byte c = Serial.read();
-            return c;
-        }
-    }
-}
-
-// we need CMD0 CMD1 <cmd> <value> sequence from serial port
-// so implementing a simple state machine here
-byte read_cmd(byte *value)
-{
-    byte c;
-
-    while (1)
-    {
-        // seek for cmd0
-        while (read_ser() != CMD_START1)
-            ;
-        // seek for cmd1
-        if (read_ser() != CMD_START2)
-            continue;
-
-        // read cmd byte
-        c = read_ser();
-        // read value
-        *value = read_ser();
-        return c;
-    }
 }
 
 void loop()

#include <Arduino.h>

#define PIN_PWM 4

// we define a dead simple protocol: 0x00 0xFF followed with command byte and value.
// Right now we only define command byte 0x01 (:

            analogWrite(PIN_PWM, val);
    }
}

// we need CMD0 CMD1 <cmd> <value> sequence from serial port
// so implementing a simple state machine here
byte read_cmd(byte *value)
{
    byte c;

    while (1)
    {
        // seek for start sequence
        while (read_byte() != CMD_START1)
            ;

        // next in sequence
        if (read_byte() != CMD_START2)
            continue;

        // read cmd byte
        c = read_byte();
        // read value
        *value = read_byte();
        return c;
    }
}

byte read_byte()
{
    // Brutal: stay in busyloop until we have something
    while (1)
    {
        if (Serial.available())
        {
            byte c = Serial.read();
            return c;
        }
    }
}
// EOF
