 TM16xxRAW tm(3,4,5);
 
 
-volatile uint32_t pbutton = 0;
-
+volatile byte but = 0;
+volatile byte obut = 1;
+unsigned long blinkTime = 0;
 
 void setup() {
-  Serial.begin(38400);
+  //Serial.begin(38400);
   //optional:brightness(0:7))
   tm.begin();
 }
 
+byte count = 24;
 
 void loop() {
-  randomLed2();
-  uint32_t pbutton = tm.getButtons();
-  if (pbutton != 0) {
-    for (byte i=24; i>=0; i--){
-      if (bitRead(pbutton,i) == 1){
-        Serial.println(i+1,DEC);
-        break;
+  //randomLed2();
+  if (millis() -  blinkTime > 15){
+    blinkTime = millis();
+    ledefx1(count);
+    if (count > 63){
+      count = 24;
+    } 
+    else {
+      count++;
+    }
+  }
+
+  uint32_t allButtons = tm.getButtons();
+  if (allButtons != 0) {
+    if (obut != but){
+      for (byte i=24; i>=0; i--){
+        if (bitRead(allButtons,i) == 1){
+          but = i+1;
+          obut = but;
+          if (tm.getLed(i)){
+            tm.setLed(i,0,true);
+          } 
+          else {
+            tm.setLed(i,1,true);
+          }
+          //Serial.println(i+1,DEC);
+          break;
+        }
       }
     }
+  } 
+  else {
+    but = 0;
   }
+
+
 }
 
 

  tm.setLed(led,val,true);  
  delay(5);
}

/*
very basic demo to demostrate how easy drive a tm1638 with this library.
A tm1638 has a led matrix 8x8 (64 led) connected and 25 switches, only 3 pins
needed by arduino. The demo will detect switches and turn on/off corrispondent led
(from 0 to 24), the other leds are free to blink. You will notice that everithing works
pretty fast and with any disturb between key scan and leds even if share same pins!
I've noticed that apparently no debounce it's needed.
*/

#include <TM16xxRAW.h>


