@@ -11,7 +11,6 @@ enum {INTERVAL, BULB, INTERVALBULB, TRIGGER}; 
  *  TODOs
  */
 
-// Running/Shutter LEDs (through digital pots)
 // Easter egg!
 // Speed up encoder more, later in the 'minute' range
 // Slow down encoder in second range/normally
@@ -20,6 +19,7 @@ enum {INTERVAL, BULB, INTERVALBULB, TRIGGER}; 
 // Fix when trigger is triggered and someone (ROBB) stops it (and it keeps open)
 // Why does trigger reset not get put back when we're done triggering!?
 // External power supply?
+// preferences: save if we want to use the LED, contrast, etc. in eeprom
 
 /*
  *  Pin Assignment
@@ -61,12 +61,12 @@ int ledRPot = 3; 
 int ledGPot = 1;
 int ledBPot = 0;
 
-int timerPot = 4;
+int timerPot = 2;
 int contrastPot = 5;
 
 /////////////////////////////
 
-int writing = 0;
+volatile int writing = 0;
 
 int currentMode = INTERVAL, selected = 0, changeSelected = 0;
 
@@ -93,9 +93,9 @@ volatile int contrastValue = 1000; 
 void pulsePin(int pin, int value)
 {
        digitalWrite(pin, !value);
-       delay(1);
+       delayMicroseconds(1);
        digitalWrite(pin, value);
-       delay(1);
+       delayMicroseconds(1);
        digitalWrite(pin, !value);
        delay(1);
 }
@@ -151,39 +151,35 @@ byte write_pot(int address, int value) 
 
 void lcdDataWrite(byte a)
 {
+       if(writing)
+               return;
+               
        writing = 1;
        shiftOut(lcdData, lcdClock, LSBFIRST, 0x20 + ((a >> 4) & 0xF));
-       digitalWrite(lcdEnable, HIGH);
-       delayMicroseconds(1);
-       digitalWrite(lcdEnable, LOW);
-
-       delay(1);
+       pulsePin(lcdEnable, HIGH);
 
        shiftOut(lcdData, lcdClock, LSBFIRST, 0x20 + (a & 0xF));
-       digitalWrite(lcdEnable, HIGH);
-       delayMicroseconds(1);
-       digitalWrite(lcdEnable, LOW);
-       
+       pulsePin(lcdEnable, HIGH);
+
        delay(1);
+       
        writing = 0;
 }
 
 void lcdCommandWrite(int a)
 {
+       if(writing)
+               return;
+               
        writing = 1;
        shiftOut(lcdData, lcdClock, LSBFIRST, (a >> 4) & 0xF);
-       digitalWrite(lcdEnable, HIGH);
-       delayMicroseconds(1);
-       digitalWrite(lcdEnable, LOW);
-
-       delay(1);
+       pulsePin(lcdEnable, HIGH);
 
        shiftOut(lcdData, lcdClock, LSBFIRST, a & 0xF);
-       digitalWrite(lcdEnable, HIGH);
-       delayMicroseconds(1);
-       digitalWrite(lcdEnable, LOW);
+       pulsePin(lcdEnable, HIGH);
        
        delay(1);
+       
        writing = 0;
 }
 
@@ -247,7 +243,7 @@ void lcdInit() 
        lcdCommandWrite(0x80); delay(1);
        
        pinMode(lcdPower, OUTPUT);
-       digitalWrite(lcdPower, LOW);
+       digitalWrite(lcdPower, HIGH);
 }
 
 void encoderInit()
@@ -273,7 +269,7 @@ void setup (void) 
        digitalPotInit();
        lcdInit();
        encoderInit();
-       writeLED(128,0,0);
+       writeLED(0,0,128);
        
        write_pot(timerPot, 255);
 }
@@ -354,8 +350,8 @@ void updateTimeRepresentations() 
 
 void doEncoderA()
 {
-//     if(running)
-//             return;
+       if(running || writing)
+               return;
                
        noInterrupts();
        delayMicroseconds(3000); // maximum bounce time, accd. to spec.
@@ -383,8 +379,8 @@ void doEncoderA() 
 
 void doEncoderB()
 {
-//     if(running)
-//             return;
+       if(running || writing)
+               return;
        
        noInterrupts();
        delayMicroseconds(3000);
@@ -414,11 +410,11 @@ void switchModes() 
 {
        unsigned long diff = (millis() - lastModeUpdate);
        
-       lastModeUpdate = millis();
-       
        if(diff < 300) // careful about the overflow...
                return;
        
+       lastModeUpdate = millis();
+       
        currentMode++;
        if(currentMode > 3)
                currentMode = 0;
@@ -432,6 +428,15 @@ void switchModes() 
                digitalWrite(triggerReset, HIGH);
        else
                digitalWrite(triggerReset, LOW);
+       
+       if(currentMode == TRIGGER)
+               writeLED(200, -1, 0);
+       else if(currentMode == BULB)
+               writeLED(0, -1, 128);
+       else if(currentMode == INTERVALBULB)
+               writeLED(0, -1, 128);
+       else if(currentMode == INTERVAL)
+               writeLED(0, -1, 128);
                
        
        updateHeader = 1;
@@ -443,10 +448,10 @@ void switchSelected() 
        {
                unsigned long diff = (millis() - lastSelectedUpdate);
                
-               lastSelectedUpdate = millis();
-               
                if(diff < 300) // careful about the overflow...
                        return;
+                       
+               lastSelectedUpdate = millis();
 
                changeSelected = 1;
        }
@@ -458,13 +463,24 @@ void toggleRunning() 
 {
        unsigned long diff = (millis() - lastToggleRunning);
        
-       lastToggleRunning = millis();
-       
        if(diff < 300) // careful about the overflow...
                return;
        
+       lastToggleRunning = millis();
+       
        running = !running;
        
+       if(running)
+       {
+               writeLED(200, -1, -1);
+       }
+       else
+       {
+               writeLED(0,-1,-1);
+               if(currentMode == TRIGGER)
+                       writeLED(200, -1, -1);
+       }
+       
        updateEncoder = 1;
 }
 
@@ -504,7 +520,7 @@ int fadeUp = 1; 
 
 void updateContrast()
 {
-       write_pot(contrastPot, map(contrastValue, 0, 1000, 50, 255));
+       write_pot(contrastPot, map(contrastValue, 0, 1000, 25, 60));
 }
 
 void writeLED(int r, int g, int b)
@@ -523,7 +539,7 @@ void loop(void) 
        {
                updateContrast();
                
-               if(--contrastValue == 50)
+               if((contrastValue -= 1) <= 0)
                        fadeUp = 0;
        }
                
@@ -585,17 +601,19 @@ void loop(void) 
        
        if(running)
        {
-               digitalWrite(lcdPower, HIGH);
+               digitalWrite(lcdPower, LOW);
                
                if(currentMode == TRIGGER)
                {
                        if(analogRead(triggerInput) < 100) // 100 might change with different resistors, make sure it works!
                        {
+                               writeLED(-1, 128, -1);
                                digitalWrite(triggerReset, HIGH);
                                delay(100); // this should probably be at least the time of the delay from signal (in the 555)...
                                digitalWrite(triggerReset, LOW);
                                delay(10);
                                digitalWrite(triggerReset, HIGH);
+                               writeLED(-1, 0, -1);
                        }
                        
                        return;
@@ -609,6 +627,7 @@ void loop(void) 
                
                if(diff > (adjustedLapseTime * 1000)) // careful about the overflow...
                {
+                       writeLED(-1, 128, -1);
                        digitalWrite(cameraShutter, LOW);
                        
                        if(currentMode == INTERVAL)
@@ -625,11 +644,13 @@ void loop(void) 
                        
                        if(currentMode == BULB)
                                running = 0;
+                       
+                       writeLED(-1, 0, -1);
                }
        }
        else
        {
-               digitalWrite(lcdPower, LOW);
+               digitalWrite(lcdPower, HIGH);
                
                if(!digitalRead(buttonB))
                        switchModes();
@@ -640,6 +661,7 @@ void loop(void) 
                {
                        selected = !selected;
                        changeSelected = 0;
+                       updateEncoder = 1;
                }
        }
 }
\ No newline at end of file