Go Down

Topic: TX command code & RX respond code of module via serial com (Read 17064 times) previous topic - next topic

AWOL

Code: [Select]
high = high + input;
This will sum the ASCII value of whatever you enter - is that what you intended?

BTW, you can abbreviate it to
Code: [Select]
high += input;

Quote
by the input still yet ASCII although i already defined the input as byte.

Again, I don't understand what you mean by this.

mr_hacker90

i connected usb serial devices & check the input from arduino serial monitor by using X-CTU terminal. when i key in "10" the x-ctu terminal shows than the number is in ASCII.

mr_hacker90

by the way, how i'm gonna modify this program. the current program only one menu push button. but, i want to adjust it so that it can runs 2 menu push button. what i mean is, one main menu, and one sub menu.

Code: [Select]

//==========================================================================
// Author     : CYTRON TECHNOLOGIES SDN BHD
// Project             : Arduino Duemilanove
// Project description : Project_1: "Hello World" and utilize switch
//==========================================================================

#include <LiquidCrystal.h>

/*
  The circuit:
* LCD RS pin to digital pin 8
* LCD Enable pin to digital pin 9
* LCD D4 pin to digital pin 4
* LCD D5 pin to digital pin 5
* LCD D6 pin to digital pin 6
* LCD D7 pin to digital pin 7
* LCD R/W pin to ground
*/

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

int analogPin = A0;
int adc_key_old;
int adc_key_in;
int NUM_KEYS = 5;
int key=-1;
int adc_key_val[5] ={30, 150, 360, 535, 760};       


/*******************************************************************************
* PRIVATE FUNCTION: setup()
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Define of I/O pin as Input or Output
*
*******************************************************************************/
// The setup() method runs once, when the sketch starts
void setup ()
{
  // initialize serial:
  Serial.begin(57600);

 
  lcd.begin(16, 2);                         // set the lcd dimension
  lcd.clear();                              // LCD screen clear
  lcd.print("  CYTRON TECH.");              // display the text
  lcd.setCursor(0,1);                       // set lcd.setCursor (column,row)
  lcd.print(" Eg. LCD Shield");   
  delay(1500);                              // delay for 3000ms
  lcd.clear();
  lcd.setCursor(0,0);             
  lcd.print("Fingerprint Reader");
  lcd.setCursor(0,1);
  lcd.print("Select Address");
  adc_key_old = analogRead(analogPin);      // store the unpress key value
}


/*******************************************************************************
* PRIVATE FUNCTION: loop()
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Non-Stop looping
*
*******************************************************************************/
void loop()
{
  adc_key_in = analogRead(analogPin);     // read ADC value
  adc_key_in = get_key(adc_key_in);
 
  if (adc_key_in==0)
  {
    lcd.setCursor(0,1);                       // set lcd.setCursor (column,row)
    lcd.print("Menu 1");
  }
 
  else if (adc_key_in==1)
  {
    lcd.setCursor(0,1);                       // set lcd.setCursor (column,row)
    lcd.print("Menu 2");   
  }
 
  else if (adc_key_in==2)
  {
    lcd.setCursor(0,1);                       // set lcd.setCursor (column,row)
    lcd.print("Menu 3");   
  }

  else if (adc_key_in==3)
  { 
    lcd.setCursor(0,1);                       // set lcd.setCursor (column,row)
    lcd.print("Menu 4");   
  }

  else if (adc_key_in==4)
  {
    lcd.setCursor(0,1);                       // set lcd.setCursor (column,row)
    lcd.print("Menu 5");   
  }
}
 

/*******************************************************************************
* PRIVATE FUNCTION: get_key
*
* PARAMETERS:
* ~ integer
*
* RETURN:
* ~ unsigned int input
*
* DESCRIPTIONS:
* convert the ADC value to number between 0 to 4
*
*******************************************************************************/
int get_key(unsigned int input)
{
int k;
   
for (k = 0; k < NUM_KEYS; k++)
{
if (input < adc_key_val[k])
{
           
    return k;
        }
}
   
    if (k >= NUM_KEYS)
        k = -1;     // No valid key pressed
   
    return k;
}

AWOL

Yes, ASCII is what I would expect them to be in.
Having them in EBCDIC would make things very difficult.

mr_hacker90

#64
Jan 17, 2013, 01:05 pm Last Edit: Jan 17, 2013, 04:06 pm by mr_hacker90 Reason: 1

Yes, ASCII is what I would expect them to be in.
Having them in EBCDIC would make things very difficult.


it is quite difficult to make the input become byte although the input variable i had defined as byte. plus with the reader itself connected with uno via serial. seems like quite not possible unless i use another serial terminal devices. thus, i decided to use switch instead of the serial monitor. i have a LCD shield embedded together with 5 switches. the manufacturer included together simple program how to use the switch. but, it is only one menu. i want to adjust it so that it can become 2 menu. 1 main menu & 1 sub menu.

let say I want to have two menu on my LCD, when power on, it shows the main menu
1. XXX
2.YYY
3.ZZZ
so if I press SW1, then it will go to menu 1
then in menu 1, it will have another option :
1. AAA
2.BBB
3.CCC
so how can I program so that I can press the sw 1 again to go to the option AAA ? based with the arduino program code that i attached in previous post.

mr_hacker90

below code is already functioning. but, may i ask. is there any other way that i can make the code become less line. i think there is another way. but, i don't have any idea how to do it. i think the value of adc_key_in can be made into for loop so that if counter 0, it will call function deleteFinger0();
but i don't have any clue on how to do it. i would be glad if someone could give me a hint on how to do it. thank you.

Code: [Select]

//==========================================================================
// Author     : CYTRON TECHNOLOGIES SDN BHD
// Project             : Arduino Duemilanove
// Project description : Project_1: "Hello World" and utilize switch
//==========================================================================

#include <LiquidCrystal.h>

/*
  The circuit:
* LCD RS pin to digital pin 8
* LCD Enable pin to digital pin 9
* LCD D4 pin to digital pin 4
* LCD D5 pin to digital pin 5
* LCD D6 pin to digital pin 6
* LCD D7 pin to digital pin 7
* LCD R/W pin to ground
*/

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

int analogPin = A0;
int adc_key_old;
int adc_key_in;
int NUM_KEYS = 5;
int key=-1;
int adc_key_val[5] ={30, 150, 360, 535, 760 };       
               
byte deleteFingerPrint0[] = {0x4D, 0x58, 0x10, 0x03, 0x42, 0x00, 0x00, 0xFA}; // 8 bytes
byte deleteFingerPrint1[] = {0x4D, 0x58, 0x10, 0x03, 0x42, 0x00, 0x01, 0xFB}; // 8 bytes
byte deleteFingerPrint2[] = {0x4D, 0x58, 0x10, 0x03, 0x42, 0x00, 0x02, 0xFC}; // 8 bytes
byte deleteFingerPrint3[] = {0x4D, 0x58, 0x10, 0x03, 0x42, 0x00, 0x03, 0xFD}; // 8 bytes
byte deleteFingerPrint4[] = {0x4D, 0x58, 0x10, 0x03, 0x42, 0x00, 0x04, 0xFE}; // 8 bytes

byte respondCorrect[] = {0x4D, 0x58, 0x30, 0x01, 0x01, 0xD7}; // 6 bytes

byte operationSuccessDelete[] = {0x4D, 0x58, 0x30, 0x02, 0x42, 0x31, 0x4A}; // 7 bytes
byte parameterErrorDelete [] = {0x4D, 0x58, 0x30, 0x02, 0x42, 0x35, 0x4E}; // 7 bytes
byte respond[6],operation[7],a,f,g;
                   
                   
/*******************************************************************************
* PRIVATE FUNCTION: setup()
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Define of I/O pin as Input or Output
*
*******************************************************************************/
// The setup() method runs once, when the sketch starts
void setup ()
{
  // initialize serial:
  Serial.begin(57600);
 
  lcd.begin(16, 2);                         // set the lcd dimension
  lcd.print("  CYTRON TECH.");              // display the text
  lcd.setCursor(0,1);                       // set lcd.setCursor (column,row)
  lcd.print(" Eg. LCD Shield");   
  delay(1500);                              // delay for 3000ms
  lcd.clear();
  adc_key_old = analogRead(analogPin);      // store the unpress key value
}


/*******************************************************************************
* PRIVATE FUNCTION: loop()
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Non-Stop looping
*
*******************************************************************************/
void loop()
{
  lcd.setCursor(0,0);             
  lcd.print("Fingerprint Reader");
  lcd.setCursor(0,1);
  lcd.print("Select Address");

  adc_key_in = analogRead(analogPin);     // read ADC value
  adc_key_in = get_key(adc_key_in);
 
  if (adc_key_in==0)
  {
    deleteFinger0();
    correctRespond();
    operationSuccesful();
  }
 
  else if (adc_key_in==1)
  {
    deleteFinger1();
    correctRespond();
    operationSuccesful();
  }
 
  else if (adc_key_in==2)
  {
    deleteFinger2();
    correctRespond();
    operationSuccesful();
  }

  else if (adc_key_in==3)
  { 
    deleteFinger3();
    correctRespond();
    operationSuccesful();
  }

  else if (adc_key_in==4)
  {
    deleteFinger4();
    correctRespond();
    operationSuccesful();
  }
}
 

/*******************************************************************************
* PRIVATE FUNCTION: get_key
*
* PARAMETERS:
* ~ integer
*
* RETURN:
* ~ unsigned int input
*
* DESCRIPTIONS:
* convert the ADC value to number between 0 to 4
*
*******************************************************************************/
int get_key(unsigned int input)
{
int k;
   
for (k = 0; k < NUM_KEYS; k++)
{
if (input < adc_key_val[k])
{
           
    return k;
        }
}
   
    if (k >= NUM_KEYS)
        k = -1;     // No valid key pressed
   
    return k;
}

void deleteFinger0(){
  // set the cursor to column 0, line 1
  lcd.setCursor(0, 1);
  // Print a message to the LCD.
  lcd.print("Del Fingerprint1");

  // write the 8bytes array of the command code
  Serial.write(deleteFingerPrint0, sizeof(deleteFingerPrint0));
  delay(1500);
}

void deleteFinger1(){
  // set the cursor to column 0, line 1
  lcd.setCursor(0, 1);
  // Print a message to the LCD.
  lcd.print("Del Fingerprint2");

  // write the 8bytes array of the command code
  Serial.write(deleteFingerPrint1, sizeof(deleteFingerPrint1));
  delay(1500);
}

void deleteFinger2(){
  // set the cursor to column 0, line 1
  lcd.setCursor(0, 1);
  // Print a message to the LCD.
  lcd.print("Del Fingerprint3");

  // write the 8bytes array of the command code
  Serial.write(deleteFingerPrint2, sizeof(deleteFingerPrint2));
  delay(1500);
}

void deleteFinger3(){
  // set the cursor to column 0, line 1
  lcd.setCursor(0, 1);
  // Print a message to the LCD.
  lcd.print("Del Fingerprint4");

  // write the 8bytes array of the command code
  Serial.write(deleteFingerPrint3, sizeof(deleteFingerPrint3));
  delay(1500);
}

void deleteFinger4(){
  // set the cursor to column 0, line 1
  lcd.setCursor(0, 1);
  // Print a message to the LCD.
  lcd.print("Del Fingerprint5");

  // write the 8bytes array of the command code
  Serial.write(deleteFingerPrint4, sizeof(deleteFingerPrint4));
  delay(1500);
}

void correctRespond() {
  // Now, reply contains all 6 bytes
  for(byte x=0; x<6; x++)
  {
    while(Serial.available() == 0) {}; // Wait for a byte
    respond[x] = Serial.read();
   
    a = memcmp(respondCorrect, respond, sizeof(respondCorrect));

    if (a==0) {
      lcd.clear();
      // set the cursor to column 0, line 0
      lcd.setCursor(0, 0);
      // Print a message to the LCD.
      lcd.print("Correct Respond");
      }
 
    else {
      lcd.clear();
      // set the cursor to column 0, line 0
      lcd.setCursor(0, 0);
      // Print a message to the LCD.
      lcd.print("Wrong Respond");
      }
  }
  delay(1500);
}

void operationSuccesful () {
  // Now, reply contains all 7 bytes
  for(byte x=0; x<7; x++)
  {
    while(Serial.available() == 0) {}; // Wait for a byte
    operation[x] = Serial.read();
   
    f = memcmp(operationSuccessDelete, operation, sizeof(operationSuccessDelete));
    g = memcmp(parameterErrorDelete, operation, sizeof(parameterErrorDelete));
   
    if (f==0) {
      lcd.clear();
      // set the cursor to column 0, line 0
      lcd.setCursor(0, 0);
      // Print a message to the LCD.
      lcd.print("Operation Success");
      }
     
    else if (g==0) {
      lcd.clear();
      // set the cursor to column 0, line 0
      lcd.setCursor(0, 0);
      // Print a message to the LCD.
      lcd.print("Parameter Error");
      }
   
    else  {
      lcd.clear();
      // set the cursor to column 0, line 0
      lcd.setCursor(0, 0);
      // Print a message to the LCD.
      lcd.print("Please Restart");
      }
  }
  delay(1500);
}

PaulS

Quote
i think the value of adc_key_in can be made into for loop so that if counter 0, it will call function deleteFinger0();

Why do you have 5 nearly identical functions? One would suffice. Pass it arguments.

Perhaps you've observed that functions like digitalRead() don't have numeric suffixes.
The art of getting good answers lies in asking good questions.

mr_hacker90


Why do you have 5 nearly identical functions? One would suffice. Pass it arguments.

Perhaps you've observed that functions like digitalRead() don't have numeric suffixes.


in each deletefinger() function, it has it own command code need be transmitted via serial.write. perhaps if has other way, i could learn a new thing too.

PaulS

Quote
in each deletefinger() function, it has it own command code need be transmitted via serial.write. perhaps if has other way, i could learn a new thing too.

You could make that a 2D array, instead. Then, one argument would be an index that would define which row to use.

Or. use a switch statement to select which array to send. But, don't write 5 functions.
The art of getting good answers lies in asking good questions.

mr_hacker90


You could make that a 2D array, instead. Then, one argument would be an index that would define which row to use.

Or. use a switch statement to select which array to send. But, don't write 5 functions.


2D array? i think my experience in programming is just shallow. by the way, for the switch case. i understand that one. i'm gonna try it. but, perhaps i could learn how to do 2D array.

michinyon

Code: [Select]


Re: TX command code & RX respond code of module via serial com
« Reply #3 on: January 15, 2013, 11:11:51 AM »
Bigger Bigger Smaller Smaller Reset Reset Reply with quoteQuote
Code:

  while(Serial.available() == 0){
    delay(10);
  }

Can you describe the exact purpose for the delay()? Does it matter whether the while loop iterates 10 times in 100 milliseconds, or 10,000,000?

If you know (or think/hope) that the response is going to be 6 bytes, wait for 6,

Then, of course, you still need to read all 6 of them.


He is calling it from setup().   Not from loop().   It is only going to run this function once.   So he has to wait here.

michinyon

Code: [Select]
because the correct command respond is 6 bytes but wrong command respond is 7 bytes. that is the reason why i put 7 sir.

Code:

byte correctRespond[] = {0x4D, 0x58, 0x30, 0x01, 0x01, 0xD7};
byte wrongrespond[] = {0x4D, 0x58, 0x30, 0x02, 0x40, 0x35, 0x4C);


This is misconceived.    Any response,  which is not the correct response,  is the wrong response.

Go Up