Tuesday, September 4, 2018

http://electronics-micros.com/software-hardware/plclib-arduino-keypad-input/

plcLib (Arduino): Inputting from a Keypad

A matrix keypad may be used as an input device, allowing PLC outputs to be turned on or off when particular keys are pressed. To illustrate the concept, a standard membrane keypad will be used, together with a freely downloadable keypad library, to drive a series of latched LED outputs.

Before trying the examples, you'll need to download and install the . The keyboard example files are available with Version 0.6 or later of the plcLib software.

Hardware Connections


We'll be using an Arduino Uno main board for the examples, which the plcLib software defines as having four inputs (X0–X3) and four outputs (Y0–Y3). The chosen keypad is an Adafruit membrane keypad with 12 keys, arranged as a matrix with four rows and three columns, hence requiring a further seven IO pins in total. The following image shows the selected input / output allocation in diagrammatic form.

A possible prototype board wiring arrangement is shown below (drawn using Fritzing).
 
The following listing takes a fairly standard keypad input program, and uses a custom latchKey()function to enable or disable an output bit based on momentary presses of the allocated Set and Reset keys.
/* Programmable Logic Controller Library for the Arduino and Compatibles

   Matrix Keypad with On/Off Control of Latched LED Outputs

   Arduino Uno Pin Connections:
   Input X0 - unused (Arduino pin A0)
   Input X1 - unused (Arduino pin A1)
   Input X2 - unused (Arduino pin A2)
   Input X3 - unused (Arduino pin A3)
   Output - LED connected to output Y0 (Arduino pin 3)
   Output - LED connected to output Y1 (Arduino pin 5)
   Output - LED connected to output Y2 (Arduino pin 6)
   Output - LED connected to output Y3 (Arduino pin 9)
   
   4X3 Matrix Keypad Connections:
   ROW1 - Arduino pin 8
   ROW2 - Arduino pin 7
   ROW3 - Arduino pin 4
   ROW4 - Arduino pin 2
   COL1 - Arduino pin 12
   COL2 - Arduino pin 11
   COL3 - Arduino pin 10

   Software and Documentation:
   http://electronics-micros.com/software-hardware/plclib-arduino/

*/

#include <Keypad.h>  // Include the Keypad library
#include <plcLib.h>  // Include the plcLib library

char keyPress = 0;   // Holds the currently pressed key value (if any)
const byte ROWS = 4; // Keypad has four rows
const byte COLS = 3; // Keypad has three columns

// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};

// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 8, 7, 4, 2 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 12, 11, 10 }; 

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup() {
  setupPLC();                 // Define input / output pins
}

void loop()
{
  keyPress = kpd.getKey();    // Read key pressed (if any)
  
  latchKey('1', '2', Y0);     // Keyboard latch, Set = '1', Reset = '2',
                              // output = Y0 (pin 3)
  
  latchKey('3', '4', Y1);     // Keyboard latch, Set = '3', Reset = '4',
                              // output = Y1 (pin 5)
  
  latchKey('5', '6', Y2);     // Keyboard latch, Set = '5', Reset = '6',
                              // output = Y2 (pin 6)
  
  latchKey('7', '8', Y3);     // Keyboard latch, Set = '7', Reset = '8',
                              // output = Y3 (pin 9)
}

// Keypad-based Set-Reset latch, outputting to a pin.
unsigned int latchKey(char en, char dis, int outPin) {
  if(keyPress) {
    if (keyPress == en) {
      digitalWrite(outPin, HIGH);
    }
    if (keyPress == dis) {
      digitalWrite(outPin, LOW);
    }
  } 
}
Source location: File > Examples > plcLib > Keypad > LedOnOff
The overall effect of the four latchKey() commands in the above sketch is to activate LEDs connected to outputs Y0Y1Y2 or Y3 when keys '1', '3', '5' or '7' are pressed, with keys '2', '4', '6' and '8' turning the corresponding outputs off again.
A slight change to the switch allocations allows a single input key to act as a master reset, as shown in the following extract.
void loop()
{
  keyPress = kpd.getKey();    // Read key pressed (if any)
  
  latchKey('1', '*', Y0);     // Keyboard latch, Set = '1', Reset = '*',
                              // output = Y0 (pin 3)
  
  latchKey('2', '*', Y1);     // Keyboard latch, Set = '2', Reset = '*',
                              // output = Y1 (pin 5)
  
  latchKey('3', '*', Y2);     // Keyboard latch, Set = '3', Reset = '*',
                              // output = Y2 (pin 6)
  
  latchKey('4', '*', Y3);     // Keyboard latch, Set = '4', Reset = '*',
                              // output = Y3 (pin 9)
}
Source location: File > Examples > plcLib > Keypad > LedStop
It is also possible to store the latch output bit in a user defined variable, rather than sending it directly to an output pin. An example based on this approach is given in the LedVariable example sketch, while the underlying approach is discussed in the Using Variables in Programs section.

No comments:

Post a Comment

สรุปงานที่ 5 Internet of Things (IoT)

Internet of Things (IoT) คืออะไร           Internet of Things (IoT) คือ  "อินเตอร์เน็ตในทุกสิ่ง" หมายถึง การที่อุปกรณ์ต่างๆ ส...