In the past,bus tickets were sold by conductors on the bus. Whenever there is rush hour in the morning and evening,the bus is always crowded with people, and it becomes a hassle for the conductor to collect and change.

With the development of science and technology, the smart bus ticketing machine on the unmanned ticket vending vehicle has brought a lot of convenience to our lives. You can swipe your card or insert a coin directly. After the card payment is successful, you will hear the corresponding prompt "standard card", or "student card". Different cards correspond to different charging standards.

Now, let's make a smart bus card machine! How can we achieve the function of swiping a card and then deduce money from it?
Learning Objectives
1. Learn the working principle and application field of two-dimensional code technology
2. Learn the two-dimensional code recognition function of HUSKYLENS
3. Use the two-dimensional code recognition function of HUSKYLENS to make a smart bus ticketing machine.
Preparation

Learning Content
Carefully observe the process of using the card, you will find that as long as the bus card is close to the machine, your money in the card will be automatically deducted. Different cards correspond to different charging standards. So as long as we find alternatives for card devices and cards, the project can be realized.

Ticketing machine—Two-dimensional code recognition function of HUSKYLENS
Bus card—Cards with two-dimensional code
Working Principle and Application Field of Object Tracking
1. What is two-dimensional code?
Two-dimensional codes are also called two-dimensional barcodes. There are many types of two-dimensional codes. The common two-dimensional code, QR Code, which refers to Quick Response, is a popular encoding mode on mobile devices. QR code can store more information, and can also represent more data types than traditional two-dimensional codes.

2. Working principle of two-dimensional code
In fact, the two-dimensional code is not mysterious. It mainly converts information into small black and white squares, and then fills them into a large square. How information and squares correspond to each other? Here we have to mention an invention of age---binary.

Through the binary system, each word, number, and symbol are "translated" into a string of "0" and "1". Use white squares to represent "0" and black squares to represent "1". Then arrange these white and black squares according to a specific rule to obtain a two-dimensional code. The essence of a two-dimensional code is to convert information (data) into a binary code, and then fill the binary code into this large square.

From the picture above, we can see that there are black squares in the upper right corner, upper left corner, and lower left corner of the QR code. What are these three squares used for? They are used for positioning, so that the information recorded in the QR code can be accurately identified whether you scan the code horizontally or vertically. Compared with the horizontal direction of the barcode, the two-dimensional code records information in two dimensions, increasing the amount of information storage.
3. Application field of two-dimensional recognition
The two-dimensional code is widely used in daily life, you can scan it to pay, enter a website, and so on.
E-Menu
Many restaurants have adopted the form of QR code electronic menu. Consumers can see the introduction and price of related dishes as long as they enter the online menu by scanning a QR code.

E-Coupon
The QR code can also be made into an electronic coupon, and the user can obtain the coupon or check which products are discounted by scanning it.

Anti-counterfeiting traceability
You can check the product production place by scanning the code.

Question: Do you also know that where are QR codes are used? Or do you think where can QR codes be used?
Demonstration of HUSKYLENS QR Code Recognition Function
1. Detect QR code
When HUSKYLENS detects QR codes, all the detected QR codes will be automatically selected by orange frames.

2. Learn QR codes
Next, make HuskyLens learn and recognize the standard card and student card.
Point the "+" symbol at the QR code, long or short press the "Learning button" to complete the first code learning. After releasing the "Learning button", the screen will display: "Press the button again to continue! Press other buttons to end. " To continue learning the next code, press the "Learning button" before the countdown ends. If you no longer need to learn other codes, press the "Function button" before the countdown ends, or do not operate and wait for the end of the countdown.

In this project, we are going to learn multiple QR codes. Therefore, we need to short press the “Learning Button” before the countdown ends. Then, point the “+” at the second QR code, pressing or holding the “Learning Button” to complete this learning. And so on.
The code ID is in the same order as the code entered. That is, the learned codes will be tagged as "background: ID1", " background: ID2", " background: ID3", and so on. Moreover, different codes correspond to different frame colors.
3. Recognize QR codes
When the HUSKYLENS encounters the learned QR codes again, there will be a colored frame to select them and display their IDs on the screen. The size of the frame can change with the size of QR codes and track them automatically.

Introduction of Voice Recorder Module

What is voice recorder module?
The audio recording and playback module is an integrated audio module that combines both recording and playback functions. It is equipped with a button and a microphone, allowing for one-button recording directly via the button. Note: The audio module can record audio for up to 10 seconds. Pressing the record button will turn on the yellow indicator light, signaling the start of the recording.
Application of voice recorder module in project
In the project, the audio recording and playback module needs to be used together with a speaker. The passive speaker is used to play pre-recorded audio, adding more life and soul to our creation. How do we use the audio recording and playback module? Let’s give it a try!
First, let’s learn how to use the audio module to record sound. The recording method is very simple, and no programming is required. Just connect the power, and it’s ready to use.
Hardware connection

Note: When recording, the IO expansion board needs to be powered.
Recording
There is an "rec" recording button on the voice recorder module. Press this button to start recording, and release the button to end the recording. Next, record an audio by yourself, the audio content is "Payment successful, get in please."

Project Practice
We will finish the project in two steps. First, use multiple QR codes recognition function of HUSKYLENS to learn and recognize the bus cards with QR codes, and then display the fees that should be paid for each card on the HUSKYLENS screen.
Based on the previous step, the balance display and voice broadcast functions are added. Each time the card is successfully recognized, the corresponding fee will be subtracted from the balance, and then the voice broadcast " Payment successful, get in please.", which basically realizes the function of the ticketing machine.
Task1: Charge by cards
HUSKYLENS learns and recognizes the bus card with a QR code. Write a program to display the fee on the HUSKYLENS screen.
Task 2: Voice broadcast " Payment successful, get in please."
On the basis of task 1, the balance display and voice broadcast function are added. When the learned bus card is recognized, the voice broadcast is performed. It is assumed that each card is charged with 20 yuan, and the corresponding fee will be deducted on the basis of the remaining fee once the payment is successful.
Task1: Charge by card
Hardware connection

Program design
Function instruction:
Different types of bus cards, such as student cards, and standard cards are designed for different groups of people. Each type of bus card corresponds to a charging standard (take 2 yuan as an example, the charging standard and discount can be modified by yourself):

Before designing the program, it has defaulted that HUSKYLENS has learned different bus cards (for specific operations, please refer to the demonstration of HUSKYLENS QR code recognition function).
Flowchart analysis

Sample program

Operating effect
When a certain kind of card is recognized, the corresponding fee to be deducted will be displayed on the HUSKYLENS screen. The mid-note Do played in the program is mainly used as a reminder. As shown in the figure, the student card pays 1 yuan, and the standard card pays 1.5 yuan.

Task2: Voice broadcasting “payment successful”
Hardware connection

Program design
Function instruction:
In this task, I take the elderly card as an example, and add two new functions on the basis of task 1: When the HUSKYLENS camera recognizes the QR code bus card, the voice broadcast "Payment successes, get in please.", and the fee and the balance will be displayed on the screen.
Homework: The following flowchart and program only realize the function of the student card, the rest of the standard cards need to be realized by yourself!
Flowchart analysis

Sample program:

Operating effect
After HUSKYLENS successfully recognizes the QR code bus card, it will play " Payment successful, get in please." and then display the fee and the balance in the card on the screen.

Project Review
This project mainly uses QR code recognition technology to recognize the corresponding bus card type and output the corresponding ID. Then calculate the fees through different charging standards, so as to realize charging different types of cards according to different payment standards. In Task 2, the functions of voice broadcast and balance display are also added, which simulates the working principle of the smart bus ticketing machine.
Project Development
Although there is a balance display function of the smart bus card machine project, when the balance in the card is insufficient, there is no notification. How to remind users when the balance is insufficient?
Note: You need to use conditional judgment statements to judge the balance, use a buzzer to prompt according to the judgment result, or use an audio module+a passive speaker.
