"Embedded Systems" Lab - 1 " ": User Interactions
"Embedded Systems" Lab - 1 " ": User Interactions
“Embedded systems”
Lab_1 “User Interactions “
Chișinău, 2020
Contents
1. Domain ............................................................................................................................... 3
2. Components ........................................................................................................................ 4
3. Description.......................................................................................................................... 9
4. Proof ................................................................................................................................. 13
5. Conclusion ........................................................................................................................ 17
6. Annex................................................................................................................................ 18
6.4. Led.cpp.......................................................................................................................... 20
2|Page
1. Domain
The goal of this laboratory work is to learn how to create and use an application while
focusing on user interaction. This particular feature of the project helps to better understand
user experience.
In general terms, user interaction focuses on the design of the interaction behavior of the
system. The design defines the system’s interactivity and functionality, its responses to user
interactions, the simple and complex workflows along with system state changes, and the
prevention of user errors.
A simple output of the data will not satisfy the final user, so in order to make a good
embedded system application, there is a need to use various types of user interaction in order
to facilitate the functionality of the application.
3|Page
2. Components
2.1.A light-emitting diode (LED)
is a semiconductor light source that emits light when current flows through it. Electrons in the
semiconductor recombine with electron holes, releasing energy in the form of photons. The
color of the light (corresponding to the energy of the photons) is determined by the energy
required for electrons to cross the band gap of the semiconductor. White light is obtained by
using multiple semiconductors or a layer of light-emitting phosphor on the semiconductor
device.
4|Page
2.2.A push-button
is a simple switch mechanism to control some aspect of a machine or a process. Buttons are
typically made out of hard material, usually plastic or metal. The surface is usually flat or
shaped to accommodate the human finger or hand, so as to be easily depressed or pushed.
Buttons are most often biased switches, although many un-biased buttons still require a spring
to return to their un-pushed state. Terms for the "pushing" of a button include pressing,
depressing, mashing, slapping, hitting, and punching.
5|Page
Fig. 2.3 Keypad
Initially all switches are assumed to be released. So, there is no connection between the rows
and columns. When any one of the switches are pressed, the corresponding row and column
are connected (short circuited). This will drive that column pin (initially high) low. Using this
logic, the button press can be detected. The colors red and black is for logic high and low
respectively. Here are the steps involved in determining the key that was pressed.
6|Page
2.4.LCD DISPLAY I2C
It has 16 pins and the first one from left to right is the Ground pin. The second pin is the
VCC which we connect the 5 volts pin on the Arduino Board. Next is the Vo pin on which we
can attach a potentiometer for controlling the contrast of the display.
Next, The RS pin or register select pin is used for selecting whether we will send commands
or data to the LCD. For example if the RS pin is set on low state or zero volts, then we are
sending commands to the LCD like: set the cursor to a specific location, clear the display, turn
off the display and so on. And when RS pin is set on High state or 5 volts we are sending data
or characters to the LCD.
Next comes the R / W pin which selects the mode whether we will read or write to the
LCD. Here the write mode is obvious and it is used for writing or sending commands and data
to the LCD. The read mode is used by the LCD itself when executing the program which we
don’t have a need to discuss about it in this tutorial.
Next is the E pin which enables the writing to the registers, or the next 8 data pins from D0
to D7. So through this pins we are sending the 8 bits data when we are writing to the registers
or for example if we want to see the latter uppercase A on the display we will send 0100 0001
to the registers according to the ASCII table.
And the last two pins A and K, or anode and cathode are for the LED back ligh tAfter all
we don’t have to worry much about how the LCD works, as the Liquid Crystal Library takes
7|Page
care for almost everything. From the Arduino’s official website you can find and see the
functions of the library which enable easy use of the LCD. We can use the Library in 4 or 8 bit
mode. In this tutorial we will use it in 4 bit mode, or we will just use 4 of the 8 data pins.
8|Page
3. Description
The general logic of the program is represented in figure 3.. In this case, first a condition
is checked. This condition consists of verifying if the button was pressed. If it wasn’t pressed,
than the program will wait until the first user interaction. In case the button was pressed, than
the program goes to another check point which is ‘isLEDon()’. This condition is needed in
order to check if the led is on, then it should be turned off, and vice-versa: if the led is off, then
9|Page
it should be turned off. After this condition is checked, the program goes back to the first check
and waits until the button is pressed again by the user.
10 | P a g e
In the flowchart from figure 3.2. is represented the logical work of my program. First,
there is the need to check if serial is available, in order not to wait for user's command and stop
the loop. If the result of this check is true, the program must scan the char that user input, if
this char is equals to ‘\n’ then the program will check if user input “led on” if the return is true
pe led will be turned on and at Serial monitor we will se messge “LED ON”. If result of first
check is false the will be checked if user input is equals with “led off” and at Serial monitor we
will se messge “LED OFF”. if the return is true led will be turned off. If the return is also flase
then will be print “unknown command”;
11 | P a g e
As it is shown in figure 3.3, the flowchart of the last task. From the starting point it is
necessary to check if the specific tab on 4x4 keypad was pressed. If user press on “C” then user
input will be deleted (clear input) if other key is pressed than this char will be added to a string
and a counter will be increased. When the counter will be equals to password that is set in the
program then User input password will be checked. If the result will be true at the screen will
be seen message “correct” and green led will be turned on, in case the input password is not
correct at the screen will pe printed “incorrect” and red led will be turned on. After 1 second
delay this information will be clear and led will be turned off, and keypad will listen for the
next user input.
12 | P a g e
4. Proof
13 | P a g e
Figure 5.4. Turn off led by pressing button
14 | P a g e
Figure 5.5. Input code from KeyPad
15 | P a g e
Figure 5.6. Check if the code is correct turn on Green Led
16 | P a g e
5. Conclusion
During this laboratory work I have learned the basic usage of some components as Arduino,
LED, buttons, resistors, LCD and Key Pad, and implementation of STDIO.
17 | P a g e
6. Annex
6.1. StdLib.h
#ifndef stdLib_h
#define stdLib_h
#include "stdio.h"
#include "Stream.h"
class stdLib
{
private:
FILE *_fp = NULL;
static int _putchar(char, FILE *);
static int _getchar(FILE *);
public:
void open(Stream &);
void open(Print &);
void close();
};
#endif
6.2. StdLib.cpp
#include "stdLib.h"
#include <Arduino.h>
#include <Print.h>
static Stream *_stream_ptr;
static Print *_print_ptr;
18 | P a g e
void stdLib::close()
{
if (_fp)
{
fclose(_fp);
_fp = NULL;
_stream_ptr = NULL;
}
};
int stdLib::_putchar(char c, FILE *_fp)
{
// if (c == '\n')
// {
// _putchar((char)'\r', _fp);
// }
if (_stream_ptr)
{
_stream_ptr->write(c);
}
else if (_print_ptr)
{
_print_ptr->write(c);
}
return (1);
};
6.3. Led.h
#ifndef led_h
#include <Arduino.h>
class Led
{
public:
int pin;
Led(int pin1);
void turnLedOn();
void turnLedOff();
};
19 | P a g e
#endif
6.4. Led.cpp
#include <led.h>
Led::Led(int pin1)
{
pin = pin1;
}
void Led::turnLedOn()
{
digitalWrite(pin, HIGH);
}
void Led::turnLedOff()
{
digitalWrite(pin, LOW);
}
6.5. Button.h
#ifndef led_h
#include <Arduino.h>
class Button
{
public:
int pin;
Button(int pin1);
int getState();
};
#endif
6.6. Button.cpp
#include <Button.h>
Button::Button(int pin1)
{
pin = pin1;
}
int Button::getState()
{
return digitalRead(pin);
}
20 | P a g e
6.7. main.cpp
#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <stdlib.h>
#include <string.h>
#include <led.h>
#include <Button.h>
#include <Keypad.h>
int buttonStateNow = 0;
int stateBefore = 0;
int LED_ON = 0;
String inputString;
String data;
String masterPassword = "01234567";
byte count = 0;
char customKey;
int passwordLength;
char hexaKeys[ROWS][COLS] = {
{'7', '8', '9', '/'},
{'4', '5', '6', '*'},
{'1', '2', '3', '-'},
{'C', '0', '=', '+'}};
stdLib stdl;
LiquidCrystal_I2C lcd(0x27, 16, 2);
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COL
S);
Led redLEd(RED_LED_PIN);
Led greenLEd(GREEN_LED_PIN);
Button button(BUTTON_PIN);
void setup()
21 | P a g e
{
pinMode(RED_LED_PIN, OUTPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
passwordLength = masterPassword.length();
Serial.begin(9600);
lcd.init();
lcd.backlight();
}
void loop()
{
buttonStateNow = button.getState();
if (buttonStateNow != stateBefore)
{
if (buttonStateNow == HIGH and LED_ON == 0)
{
redLEd.turnLedOn();
LED_ON = 1;
}
if (inputString.equals("led on"))
{
redLEd.turnLedOn();
printf("\nLED ON\n");
LED_ON = 1;
inputString = "";
}
22 | P a g e
else if (inputString.equals("led off"))
{
redLEd.turnLedOff();
printf("\nLED OFF\n");
LED_ON = 0;
inputString = "";
}
else if (inputString.length() != 0)
{
printf("\nUnknown command\n");
inputString = "";
}
}
stdl.close();
}
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
customKey = customKeypad.getKey();
if (customKey)
{
if (customKey == 'C')
{
lcd.clear();
data = "";
count = 0;
}
else
{
lcd.setCursor(0, 1);
data += customKey;
lcd.print(data);
count++;
}
}
if (count == passwordLength)
{
lcd.clear();
if (data.equals(masterPassword))
{
greenLEd.turnLedOn();
stdl.open(lcd);
printf("Correct");
stdl.close();
delay(1000);
23 | P a g e
greenLEd.turnLedOff();
}
else if (data.length() != 0)
{
stdl.open(lcd);
printf("Incorrect");
stdl.close();
redLEd.turnLedOn();
delay(1000);
redLEd.turnLedOff();
}
lcd.clear();
data = "";
count = 0;
}
}
24 | P a g e