Visualizzazione post con etichetta Pulsantiera. Mostra tutti i post
Visualizzazione post con etichetta Pulsantiera. Mostra tutti i post

martedì 13 gennaio 2015

Password inserita da Keypad.


La libreria Password permette di inserire una password da keypad.

      
      
      #include <Password.h> //http://www.arduino.cc/playground/uploads/Code/Password.zip
      #include <Keypad.h> //http://www.arduino.cc/playground/uploads/Code/Keypad.zip
      #include <LiquidCrystal.h>
    
      Password password = Password( "1234" );
    
      LiquidCrystal lcd(8, 9, 10, 11, 12, 13);//pin ARDUINO MEGA 2560
    
      const byte RIGHE = 4; 
      const byte COLONNE = 4; 
      
      char keys[RIGHE][COLONNE] = {{'1','2','3','A'},
                                   {'4','5','6','B'},
                                   {'7','8','9','C'},
                                   {'F','0','E','D'}};
      
      byte rowPins[RIGHE]   = {14, 15, 16, 17}; //pin ARDUINO MEGA 2560
      byte colPins[COLONNE] = {18, 19, 20, 21};;//pin ARDUINO MEGA 2560

      Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, RIGHE, COLONNE );
      
      void setup()
           { lcd.begin(20, 4); 
             keypad.addEventListener(keypadEvent); }
      
     
      void loop(){
     
        keypad.getKey();
        lcd.setCursor(0, 0);
        lcd.print("Ins.Codice e press F");
   
      }//-------------Fine LOOP------------------------------
      
      void keypadEvent(KeypadEvent eKey){
           switch ( keypad.getState())
                  { case PRESSED:
                    switch ( eKey )
                           { case 'F': checkPassword(); 
                             break;
                            case 'E': password.reset();
                             break;
                            default: password.append(eKey);}}
      }//------------ FINE Void -------------------------------
      
      void checkPassword() {
           if ( password.evaluate())
              { lcd.setCursor(0, 1);
                lcd.print("Impianto Inserito   "); }
          else
              { lcd.setCursor(0, 1);
                lcd.print("Codice Errato        ");   }
     
      }//------------ FINE Void -------------------------------

venerdì 14 novembre 2014

Encoder




Perchè mai debbo complicarmi la vita usando un encoder rotativo?
Se devo impostare un valore posso usare un comune potenziometro o due pulsanti UP e DOWN.
Di seguito un listato. Necessita di due resistense da 10Kohm fra + Vcc ed i pin dell'encoder.
Nello sketch non c'è, ma ci vuole perchè è pur sempre un dispositivo meccanico, l'antirimbalzo.


      #include <LiquidCrystal.h>
     
      LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
      int encoderPin1 = 2;
      int encoderPin2 = 3;
      int ledPin1     = 5; 
       
      volatile int lastEncoded = 0;
      volatile long encoderValue = 0;
       
      long lastencoderValue = 0;
       
      int lastMSB = 0;
      int lastLSB = 0;
       
      void setup() 
      
           { pinMode(encoderPin1, INPUT);
             pinMode(encoderPin2, INPUT);
             pinMode(ledPin1, OUTPUT);
             digitalWrite(encoderPin1, HIGH); 
             digitalWrite(encoderPin2, HIGH); 
             analogWrite (ledPin1,LOW);
             attachInterrupt(0, updateEncoder, CHANGE);
             attachInterrupt(1, updateEncoder, CHANGE);
             lcd.begin(16, 2);
             Serial.begin (9600);                     }
       
      void loop(){
         
         //  delay(200); 
        Serial.println(encoderValue);
        lcd.setCursor(0, 0);
        lcd.print(encoderValue);

        
        if ( encoderValue > 255 ) 
           { encoderValue = 255; 
             lcd.clear();
           delay (20);       }
       
        if ( encoderValue < 0 )  
           { encoderValue = 0; 
         lcd.clear();
       delay(20);}
       
        analogWrite(ledPin1,encoderValue);
        Serial.println(encoderValue);
       
 
  }  //--------------- Fine LOOP-------------------      
        
       
       
       
       
       
       
       
       
       
       
       
      void updateEncoder(){
        int MSB = digitalRead(encoderPin1); 
        int LSB = digitalRead(encoderPin2); 
       
        int encoded = (MSB << 1) |LSB; 
        int sum  = (lastEncoded << 2) | encoded; 
       
        if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue ++;
        if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue --;
       
        lastEncoded = encoded; 
      }

domenica 20 luglio 2014

Pulsantiera su un solo ingresso.


NOTA
Nel listato che segue le resistenze non hanno il valore di cui sopra. Esso si riferisce alla shield LCD della DFRobat con pulsanti.

/*
Una serie di 5 resistenze fanno capo al pin A0.
5 Pulsanti, ciascuno ad ogni pressione cortocircuita una o
più resistenze variando il valore di tensione su A0.

*/
    #include <LiquidCrystal.h>
    
    LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
    
    # define Pulsanti  A0  
    
    long Value=   0;
 
    
    void setup()  
    {  pinMode(Pulsanti, INPUT);    
       lcd.begin(16, 2);       
       Serial.begin(9600);      } 
       
    void loop() {
      
      Value = analogRead(Pulsanti);
       
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("VALUE");
        lcd.setCursor(12, 0);
        lcd.print(Value);
        Serial.print("VALUE  ");
        Serial.println(Value);
    
      if (Value >1020) 
           {lcd.setCursor(13, 0);
            lcd.setCursor(0, 1);
            lcd.print("TASTI OFF "); 
            Serial.print("TASTI OFF  ");  }
      
      if (Value >=0 && Value <=20) 
           {lcd.setCursor(13, 0);
            lcd.setCursor(0, 1);
            lcd.print("TASTI UNO"); 
            Serial.print("TASTI UNO");  }  
            
       if (Value >=90 && Value <=110) 
           {lcd.setCursor(13, 0);
            lcd.setCursor(0, 1);
            lcd.print("TASTO DUE"); 
            Serial.print("TASTO DUE");  }    
   
        if (Value >=240 && Value <=265) 
           {lcd.setCursor(13, 0);
            lcd.setCursor(0, 1);
            lcd.print("TASTO TRE"); 
            Serial.print("TASTO TRE");  }
            
        if (Value >=390 && Value <=415) 
           {lcd.setCursor(13, 0);
            lcd.setCursor(0, 1);
            lcd.print("TASTO QUATTRO"); 
            Serial.print("TASTO QUATTRO");  } 
            
        if (Value >=600 && Value <=700) 
           {lcd.setCursor(13, 0);
            lcd.setCursor(0, 1);
            lcd.print("TASTO CINQUE"); 
            Serial.print("TASTO CINQUE");  }             
            
    delay(300);
    
    }

Breve commento:La pressione di un pulsante varia il valore di una 
delle due resistenze del partitore, non rimane quindi che leggere
 il valore analogico e porre le condizioni "if" se il valore è 
compreso fra -10% e +10% (o altra tolleranza).

giovedì 7 febbraio 2013

KeyInput



#include <Keypad.h>
  const byte ROWS = 4; 
  const byte COLS = 4; 
  char keys[ROWS][COLS] =
  {{'1','2','3','C'},
   {'4','5','6','D'},
   {'7','8','9','E'},
   {'A','0','B','F'}};
  byte rowPins[ROWS] = { 4, 3, 2, 1}; 
  byte colPins[COLS] = { 8, 7, 6, 5}; 
  Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

int ValoreF = 0;
int Valore1 = 123;

void setup() 
   {Serial.begin(9600);}

void loop() {

   char  key = keypad.getKey();

   else 
     {int number = key - 48;
      ValoreF = ValoreF * 10 + number;
      if (Valore1 < ValoreF)
         { Serial.print("OK"); }} 
  }  


Inserire un valore con Keypad, metodo teorico perchè conviene di gran lunga un encoder rotativo o un comune potenziometro.

domenica 2 dicembre 2012

Tastiera.Pilotare uno o più led.

     /*
     author Alexander Brevig

     #include <Keypad.h>

     # define LED1 3
     # define LED2 4
   
     const byte RIGHE    = 4; 
     const byte COLONNE = 4; 
   
     char Tasti[RIGHE][COLONNE] =
       {{'1','2','3','C'},
        {'4','5','6','D'},
        {'7','8','9','E'},
        {'A','0','B','F'}};
      
     byte righe[RIGHE] = {8, 7, 6, 5}; 
     byte colonne[COLONNE] = {12, 11, 10, 9}; 

     // Creazione della tastiera
     Keypad cKeypad = Keypad( makeKeymap(Tasti),
                 righe, colonne, RIGHE, COLONNE); 

     void setup() 
      {pinMode     (LED1, OUTPUT);
       pinMode     (LED2,  OUTPUT);  
       digitalWrite(LED1,   HIGH);
       digitalWrite(LED2,   HIGH);
       Serial.begin(9600);       }
  
   void loop(){
        char Key = cKeypad.getKey();
  
         if (Key)
         {switch (Key)
                  {case 49: //valore ascii del numero 1
                   digitalWrite(LED1, HIGH);
                   break;
                   case 50: //valore ascii del numero 2
                   digitalWrite(LED2, HIGH);
                   break;
                   case 52: //valore ascii del numero 4
                   digitalWrite(LED1, LOW);
                   break;
                   case 53: //valore ascii del numero 5
                   digitalWrite(LED2, LOW);
                   break;     }
          Serial.println(Key);}
 }

Tastiera.Lettura da Keypad


   /*
     KEYPAD R0  R1   R2   R3  C0  C1  C2  C3
     ARDUINO 8   7    6    5   12  11  10  9
     */
     #include <Keypad.h>

     const byte RIGHE   = 4; 
     const byte COLONNE = 4; 
   
     char Tasti[RIGHE][COLONNE] =
       {{'1','2','3','C'},
        {'4','5','6','D'},
        {'7','8','9','E'},
        {'A','0','B','F'}};
      
     byte righe[RIGHE] = {8, 7, 6, 5}; 
     byte colonne[COLONNE] = {12, 11, 10, 9}; 

     // Creazione della tastiera
     Keypad cKeypad = Keypad( makeKeymap(Tasti),
                    righe, colonne, RIGHE, COLONNE); 

     void setup() 
      {Serial.begin(9600); }
  
   void loop(){
        char Key = cKeypad.getKey();
  
         if (Key)
         {Serial.println(Key);}
 }

Se la tastiera in vostro posesso ha altre lettere o numeri diversi basta modificare lo sketch
     char Tasti[RIGHE][COLONNE] =
       {{'1','2','3','C'},
        {'4','5','6','D'},
        {'7','8','9','E'},
        {'A','0','B','F'}};

Per conoscere i pin della tastiera occorre fare riferimento al datasheet.
Solitamente,guardando la tastiera ed iniziando a sinistra: il primo pin è la Colonna0 (C0) a seguire,C2,C3,la Riga0(RO),la R1,R2 ed R3.

E' comunque, con tentativi ed andando ad esclusione scoprire la piedinatura.