Click here to Skip to main content
15,867,686 members
Articles / Internet of Things / Arduino
Tip/Trick

Data Input and Output with Arduino

Rate me:
Please Sign up or sign in to vote.
4.43/5 (3 votes)
3 Jan 2016CPOL2 min read 26.5K   383   11  
An example of how to interact with the user through the keypad, LCD and Buzzer

Introduction

This is a small tutorial about how to interface the Arduino with basic I/O devices.
For this, we present a small project of a game to teach "multiplication tables" where the information is typed on a keypad and displayed on a LCD.

Background

For this project, we'll need the following elements:

  • An Arduino board UNO compatible
  • Protoboard
  • 4x4 Keypad
  • 2x16 LCD
  • 1k Potentiometer
  • 100 Ohms resistor
  • 5v Piezzo Buzzer
  • Jumper wires

In the picture below, we see how these components will be mounted:

Image 1

Points of Interest

  1. The 100 ohms resistor is needed to prevent sound distortion.
  2. The pot is required to adjust the LCD visibility.
  3. The analog pins are used as digital pins.
  4. Regarding the keypad, you will not need external resistors because the library uses the internal pullup resistors (activated via software).

Using the Code

The game of "multiplication table" works as follows:

  1. The system randomly chooses two numbers to be multiplied and displays them on the LCD.
  2. The system waits for user response.
  3. The user types your response on the keyboard and press the "#" key.
  4. The system checks whether the answer is correct or not, and displays the result on the second line of the LCD through emoticons and sounds.
  5. The user presses the "#" key.
  6. The system returns to the step 1.

Click here for a video demonstration.

Go to the sketch:

C++
/*
 * Data input and output whit Arduino, Keypad 4x4, LCD 2x16 and buzzer
 * 2015, by Jose Augusto Cintra (jose.cintra@html-apps.info)
 */

#include <Keypad.h>
#include <LiquidCrystal.h>
#include "pitches.h" // Musical Notes

//keyPad definitions
const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad
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}; //Rows 0 to 3
byte colPins[numCols]= {5,4,3,2}; //Columns 0 to 3
Keypad keyPad= Keypad(makeKeymap(keyMap), rowPins, colPins, numRows, numCols);

//LCD Display
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);

//Buzzer
int buzzPin = 10; //connect a 100ohms resistor on this pin

void setup() {
  lcd.begin(16, 2);
  pinMode(buzzPin,OUTPUT); //Buzzer pin
  randomSeed(analogRead(7));//Starts the random number generator on this analogic pin
}

void loop() {
  lcd.clear();
  lcd.setCursor(0, 0);
  //Choose two numbers and displays them on the LCD
  int num1 = random(2, 11);
  int num2 = random(2, 11);
  int result = num1 * num2;
  String mult = String(num1) + " x " + String(num2) + " = ";
  lcd.print(mult);
  // Waiting for user response and evaluates the result
  int hit = readVal().toInt();
  lcd.setCursor(0, 1);
  if (result == hit){
    lcd.print(":-)");
    playHappy();
  }
  else {
    lcd.print(":-(     " + String(result));
    playAlarm();
  }
  hit = readVal().toInt(); //Just waiting for you to press any key to continue
}

//Data input: Enter the values and press "#"
String readVal(){
  String myString = "";
  char keyPressed = keyPad.getKey();
  while (keyPressed != '#'){
    keyPressed = keyPad.getKey();
    if ((keyPressed != NO_KEY) && (keyPressed != '#')) {
      myString.concat(keyPressed);
      lcd.print(keyPressed);
      playTone();
      }   
    }  
  return(myString);  
  }

//Success: Play "Happy Birthday"
void playHappy(){
  int melody[] = {NC4,NC4,ND4,NC4,NF4,NE4,
                NC4,NC4,ND4,NC4,NG4,NF4,
                NC4,NC4,NC5,NA4,NF4,NE4,ND4,
                NAS4,NAS4,NA4,NF4,NG4,NF4  
                 };
  int noteDurations[] = {6,12,4,4,4,2,
                       6,12,4,4,4,2,
                       6,12,4,4,4,4,2,
                       6,12,4,4,4,2,    
                       };
  for (int thisNote = 0; thisNote < 12; thisNote++) {
    int noteDuration = 1000/noteDurations[thisNote];
    tone(buzzPin, melody[thisNote],noteDuration);
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    noTone(buzzPin);
  }
}

//Error: Play a alarm
void playAlarm(){
for (int thisNote = 150; thisNote < 1000; thisNote += 1)
  {
    tone(buzzPin, thisNote, 10);
    delay(1);
  }
  for (int thisNote = 1000; thisNote > 150; thisNote -= 1)
  {
    tone(buzzPin, thisNote, 10);
    delay(1);
  }
}

//Beeps on key presses
void playTone(){
  tone(buzzPin, 150, 10);
  }

Points of Interest

  1. The libraries (keypad.h and LiquidCrystal.h) are available via the Arduino IDE library manager.
  2. The pitches.h file contains definitions of musical notes used in the song "Happy Birthday to You" and is available for download above at the top of the tip.
  3. The KeyMap array defined the keyboard characters.
  4. The randomSeed function initializes the random number generator via the analog pin A7.

History

  • 03/01/2015 - First version
  • 04/01/2015 - Minor fixes

References

Conclusion

I hope that with the project presented here, the beginner in Arduino (like me) can evolve his/her learning.

See you soon.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Brazil Brazil
I am a software developer focused on Mathematics, IoT and Games.
Homepage: HTML Apps
Blog: www.josecintra.com/blog

Comments and Discussions

 
-- There are no messages in this forum --