Objectifs du projet

  • Introduction à la commande d’une porte blindée
  • Savoir commander un actionneur linéaire
  • Savoir lire un clavier 4×4
  • Savoir mesurer le courant d’une charge avec Arduino
  • Savoir programmer la commande d’une porte blindée
  • Etc.

L’objectif du tuto est de tester le clavier 4×4 avec Arduino. Voir le tuto pour les détails techniques.

Architecture matérielle

Architecture matérielle de la commande d'une porte blindée

Programme Arduino

#include <Keypad.h>
#define MaxChar 4
#define TimeOut 4000 //4000mS
int code[MaxChar];
const int CodeSecret[MaxChar]={1,2,3,4};

void setup()
{
// Affichage
Serial.begin(115200);
}


void loop()
{
// Lecture & Affichage du code
int a=getCode(code,MaxChar, TimeOut,0, 0);
Serial.print(a); Serial.print(", ");
for(int i=0;i<MaxChar; i++)Serial.print(code[i]);

// Vérification
int v=verifCode(code, CodeSecret,MaxChar);
Serial.print(" ,CodeIsValid=");Serial.println(v);
}

int getCode(int *codeout,int num_c, unsigned long timeOut_ms,int init_val, int valid_val)
{
for(int i=0;i<num_c;i++)codeout[i]=0; // Init saisie
long Tstart=(long)millis();
int id_code=0;
while(1)
{
// Code validé
int c=lireClavier();
if(c!=-1)
{
codeout[id_code]=c;
id_code++; id_code=id_code%num_c;
if (id_code==0) return 1;
}
if(valid_val!=0)return 1;

// Init saisié
if(init_val!=0)
{
for(int i=0;i<num_c;i++)codeout[i]=0;
return -1;
}

// TimeOut
long Tstop=(long)millis();
Tstop=Tstop-Tstart;
if(Tstop>=timeOut_ms){return -1;}
}
}

int lireClavier()
{
// Num lignes/colonnes
const byte numL= 4;
const byte numC= 4;

// Map du clavier
char keymap[numL][numC]={{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}};

// MSB-PORTA: lignes 0 à 3
byte rowPins[numL] = {29,28,27,26};

// LSB-PORTA: colonnes 0 à 3
byte colPins[numC] = {25,24,23,22};

//Init Clavier
static Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numL, numC);

// Lecture du clavier
char code = myKeypad.getKey();
if (code == NO_KEY){return -1;}
else{
switch(code){
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
default: return -10;
}
}
}

int verifCode(int *code_in, int *code_secret, int nchar)
{
int j=0;
for (int i=0;i<nchar; i++)
{
if(code_in[i]==code_secret[i])j++;
}
if(j==nchar)return 1;
else return 0;
}

Obtenir le livre « Codage en C du GRAFCET avec ARDUINO« 

Click to rate this post!
[Total: 2 Average: 5]

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Retour en haut

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

FPGA | Arduino | Matlab | Cours will use the information you provide on this form to be in touch with you and to provide updates and marketing.