         pinMode ( i, INPUT);
         digitalWrite ( i, HIGH );   // pull-up
         s_counters[i] = 0;
+        io_direction[i] = SQ_FALSE;
+        io_last_read[i] = SQ_FALSE;
+        io_last_written[i] = SQ_FALSE;
+        io_counter_enable[i] = SQ_FALSE;
+        io_trig_h2l[i] = SQ_TRUE;
+        io_trig_l2h[i] = SQ_FALSE;
     }
     SequantoAutomation::init();
 }
 
-unsigned int inputs = PIND | (((unsigned int)PINB) << 8 );
-unsigned int changed, trigged = 0;
+//unsigned int inputs = PIND | (((unsigned int)PINB) << 8 );
+//unsigned int changed, trigged = 0;
 
 void loop ( void )
 {
     int i;
     SequantoAutomation::poll();
-
-    inputs = (PIND | (((unsigned int)PINB) << 8 )) & (~io_direction);
-    changed = inputs ^ io_last_read;
-    trigged = changed & io_counter_enable & (((io_trig_h2l ^ inputs ) & io_trig_h2l) | (((~io_trig_l2h) ^ inputs ) & io_trig_l2h));
-    io_last_read = inputs;
-
+    
     for ( i = PIN_OFFSET ; i < NUMBER_OF_PINS ; i++ )
     {
-        if ( ( changed & PIN_MASK(i) ) != 0 )
+        if ( io_direction[i] == SQ_FALSE )
         {
-            //sq_digital_pin_updated ( i, digital_pin_get(i) );
-        }
-        if ( ( trigged & PIN_MASK(i) ) != 0 )
-        {
-            s_counters[i]++;
+          SQBool read = digitalRead(i) == HIGH ? SQ_TRUE : SQ_FALSE;
+          if ( read != io_last_read[i] )
+          {
+             //sq_digital_pin_updated ( i, digital_pin_get(i) );
+          
+            if ( io_counter_enable[i] == SQ_TRUE )
+            {
+              if ( read == SQ_FALSE && io_trig_h2l[i] == SQ_TRUE )
+              {
+                  s_counters[i]++;
+              }
+              if ( read == SQ_TRUE && io_trig_l2h[i] == SQ_TRUE )
+              {
+                  s_counters[i]++;
+              }
+            }
+            io_last_read[i] = read;
+          }
         }
     }
 }

};

static int s_counters[NUMBER_OF_PINS];


static unsigned int io_direction = 0;   // All input on startup; for safety reasons.
static unsigned int io_last_read = IO_MASK; // Pull-up on all; all high on startup.
static unsigned int io_last_written = 0;    // Pull-up on all; all high on startup.
static unsigned int io_counter_enable = 0;  // No counters enabled
static unsigned int io_trig_h2l = IO_MASK;  // default counter edge
static unsigned int io_trig_l2h = 0;

static const char DIGITAL_PIN_INPUT_TEXT[] SQ_CONST_VARIABLE = "Input";
static const char DIGITAL_PIN_OUTPUT_TEXT[] SQ_CONST_VARIABLE = "Output";

void update_direction_mask( void )
{
    io_direction = (DDRD | (((unsigned int)DDRB) << 8 )) & IO_MASK;
}

#define PIN_MASK(pin) (((unsigned int)1) << pin)

SQBool digital_pin_get ( int _pin )
{
    if ((io_direction& PIN_MASK(_pin)) != 0)
    {
        return ((io_last_written & PIN_MASK(_pin)) != 0);
    }
    else
    {
        return ((io_last_read & PIN_MASK(_pin)) != 0);
    }
}



const char * digital_direction_get ( int _pin )
{
    if ( io_direction[_pin] == SQ_TRUE )
    {
        sq_get_constant_string_copy(DIGITAL_PIN_OUTPUT_TEXT, buff);
    }
