Arduino VB.NET Simple Keypad Reader

Submitted by CAAadmin on Sun, 07/24/2016 - 07:58
arduino vbnet keypad access

In this example, we will create a simple VB interface to record the data (pressed key) from an external simple key pad. ie. when ever a key on the external keypad is pressed, it will be shown in text box. (this data can be manipulated for other purposes like exports to excel, draw the graphs etc)

Please note that, these codes & hardwares are tried and tested at our end,but in case of any error please use the support forum for this particular project at http://www.computeraidedautomation.com/forum/14

For the sake of simplicity, I have divided this project in to two (1. Hardware programming & 2. Software Programming)

1. Hardware Programming

Please refer the previous article http://www.computeraidedautomation.com/article/how-use-simple-keypad-arduino for "How to connect Simple Keypad with Arduino"

2. Software Programming

Please refer the article http://www.computeraidedautomation.com/Arduino-and-VB.NET-Serial-port-Data-logger for "How to create the VB interface for Arduino data logging from external sensor" as the VB GUI preparation process is almost same as that tutorial.

Create a new project in VB

Add Combobox port selection, Connect Button, Text Box etc  as described in earlier articles (tagged : Arduino)

I have pasted the VB code below, just copy and paste the code to VB Code area, of your datalogger project (which can be downloaded from GITHUB or SOURCEFORGE)

Visual Basic Code

Imports System
Imports System.IO.Ports
Imports System.Threading

Public Class ArduinoVBNETDatalogger
    Dim comPORT As String
    Dim receivedData As String = ""

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Enabled = False
        comPORT = ""
        For Each sp As String In My.Computer.Ports.SerialPortNames
            comPort_ComboBox.Items.Add(sp)
        Next
    End Sub


    Private Sub comPort_ComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles comPort_ComboBox.SelectedIndexChanged
        If (comPort_ComboBox.SelectedItem <> "") Then
            comPORT = comPort_ComboBox.SelectedItem
        End If
    End Sub


    Private Sub connect_BTN_Click(ByVal sender As Object, ByVal e As EventArgs) Handles connect_BTN.Click
        If (connect_BTN.Text = "Connect") Then
            If (comPORT <> "") Then
                SerialPort1.Close()
                SerialPort1.PortName = comPORT
                SerialPort1.BaudRate = 9600
                SerialPort1.DataBits = 8
                SerialPort1.Parity = Parity.None
                SerialPort1.StopBits = StopBits.One
                SerialPort1.Handshake = Handshake.None
                SerialPort1.Encoding = System.Text.Encoding.Default
                SerialPort1.ReadTimeout = 10000
                SerialPort1.Open()
                connect_BTN.Text = "Dis-connect"
                Timer1.Enabled = True
                Timer_LBL.Text = "Timer: ON"
            Else
                MsgBox("Select a COM port first")
            End If
        Else
            SerialPort1.Close()
            connect_BTN.Text = "Connect"
            Timer1.Enabled = False
            Timer_LBL.Text = "Timer: OFF"
        End If
    End Sub


    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
        SerialPort1.Write("1")
        receivedData = ReceiveSerialData()
        If receivedData <> "nokey" Then
            RichTextBox1.Text &= receivedData
        End If

    End Sub


    Function ReceiveSerialData() As String
        Dim Incoming As String
        Try
            Incoming = SerialPort1.ReadExisting()
            If Incoming Is Nothing Then
                Return "nothing" & vbCrLf
            Else
                Return Incoming
            End If
        Catch ex As TimeoutException
            Return "Error: Serial Port read timed out."
        End Try

    End Function


    Private Sub clear_BTN_Click(ByVal sender As Object, ByVal e As EventArgs) Handles clear_BTN.Click
        RichTextBox1.Text = ""
        receivedData = 0
        SerialPort1.Write("0")
        Timer1.Enabled = False
    End Sub


End Class

 

Arduino Code

#include <Keypad.h>
const byte numRows= 4;
const byte numCols= 4;
char keymap[numRows][numCols]=
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[numRows] = {9,8,7,6};
byte colPins[numCols]= {5,4,3,2};
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
void setup()
{
Serial.begin(9600);
}

void loop()
{
char key1 = myKeypad.getKey();
if ( key1 != NO_KEY)
{
Serial.print( key1);
}
else
{
  Serial.print("nokey");
  }

}