#include <LiquidCrystal.h>
#define TRIGGER_PIN A0
#define ECHO_PIN A1
#define FAN_PIN A2
#define POT_PIN A5 // Potentimetter
#define VIEW_SWITCH_PIN 6
#define DEL_PIN 5
#define SEL_PIN 4
#define DOWN_PIN 3
#define CANCEL_PIN 7
#define UP_PIN 2
#define MOTOR A2
// Enum of UI
enum DisplayMode {
MENU_VIEW,
ORDER_VIEW
};
// Initialization LCD
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int pressed = 0;
String menuItems[] = {"1.Hamburger $5", "2.Sushi $3", "3.Sandwitch $6", "4.Ice cream $2.5", "5.Cake $10"};
int selected[]; //What User Selected
int itemCount = 0; //Selected items
bool userPresent = false;
unsigned long leaveTime = 0;
DisplayMode currentView = MENU_VIEW; //UI mode
int currentMenuItem = 0;
// 按钮状态枚举(需外接按键,示例中未实现具体引脚)
enum Button {
BTN_UP,
BTN_DOWN,
BTN_SEL,
BTN_DEL,
BTN_CANCEL,
BTN_SWITCH
};
void setup() {
lcd.begin(16, 2);
Serial.begin(115200);
pinMode(FAN_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
analogWrite(FAN_PIN, 0); //The fan is off at first
for(int i = 0; i < 6; i++){
pinMode(2 + i, INPUT);
Serial.print("We have set ");
Serial.println(2 + i);
}
for(int i = 0; i < 3; i++){
pinMode(8 + i, OUTPUT);
Serial.print("We have set ");
Serial.println(8 + i);
}
for(int i = 0; i < 1; i++){
pinMode(13 + i, OUTPUT);
Serial.print("We have set 13");
}
}
void loop() {
handlePresenceDetection();
handleFanControl();
handleInterfaceSwitch();
handleNavigation();
updateDisplay();
delay(100);
}
// Detect whether user exists
void handlePresenceDetection() {
int distance = distanceOfUltraSonar();
bool presence = (distance > 0 && distance < 152); // Detect user in 5 feets.
if (presence != userPresent) {
digitalWrite(FAN_PIN, presence ? LOW : HIGH); // If
if (presence) {
onUserArrive();
} else {
onUserLeave();
}
userPresent = presence;
}
}
// Control of fan
void handleFanControl() {
if (!userPresent) {
analogWrite(FAN_PIN, 0); // Turn off when user leave
return;
}
int potValue = analogRead(POT_PIN); // Read potentemetter
int fanSpeed = 0;
// tap position
if (potValue < 341) { // 0-33% Tiny
fanSpeed = 330;
} else if (potValue < 682) { // 34-66% Medium
fanSpeed = 681;
} else { // 67 to 100% Big
fanSpeed = 1023;
}
analogWrite(FAN_PIN, fanSpeed);
}
//Awitch the UI
void handleInterfaceSwitch() {
if (digitalRead(VIEW_SWITCH_PIN) == HIGH) { //When the button is pressed
currentView = (currentView == MENU_VIEW) ? ORDER_VIEW : MENU_VIEW;
lcd.clear();
delay(150);
}
}
void updateDisplay() {
lcd.setCursor(0, 0);
if (currentView == MENU_VIEW) {
// UI of menu
lcd.print("Menu:");
lcd.setCursor(0, 1);
lcd.print(menuItems[currentMenuItem]); // display now item.
if(pressed == 2 && currentMenuItem > 0 && pressed != lastPressed){
currentMenuItem = currentMenuItem - 1;
}else if(pressed == 3 && currentMenuItem > 0 && pressed != lastPressed){
currentMenuItem = currentMenuItem + 1;
}else if(pressed == 4 && currentMenuItem > 0 && pressed != lastPressed){
for(int i = 0; i < 15; i ++){
if(selected[i] != 0)
selected[i] = currentMenuItem;
}
}
else{pressed == 5 && currentMenuItem > 0 && pressed != lastPressed}
for(int i = 0; i < 15; i ++){
if(selected[i] == currentItem)
selected[i] = 0;
}
}
} else
{
// UI of Order
lcd.setCursor(0, 0);
for(int i = 0; i < itemCount; i++){
lcd.print("Order:");
lcd.print(selected[i]); //Show the number of items
lcd.setCursor(0, 1);
lcd.print(" Total:$");
lcd.print(calculateTotal()); // Show total price
delay(1000);
}
}
}
}
// Total price
int calculateTotal() {
int total = 0;
for (int i = 0; i < itemCount; i++) {
int index = getSelectedIndex(i);
String priceStr = menuItems[index].substring(menuItems[index].indexOf('$') + 1); // 提取价格
total += priceStr.toInt();
}
return total;
}
// 获取第n个选中项的索引(示例中简化处理,实际需维护选中列表)
int getSelectedIndex(int n) {
// 示例中仅返回第一个选中项的索引(实际需用数组存储选中项)
// 完整实现需扩展selectedItems为数组并记录选中状态
return 0; // 简化处理,实际需替换为真实逻辑
}
// When user arrive
void onUserArrive() {
Serial.println("New User coming");
currentView = MENU_VIEW; // 默认显示菜单
lcd.clear();
}
// When user quit
void onUserLeave() {
//Serial.println("No user now");
}
double distanceOfUltrasonar(){
analogWrite(TRIGGER_PIN, 0);
delayMicroseconds(2);
analogWrite(TRIGGER_PIN, 1023);
delayMicroseconds(10);
analogWrite(TRIGGER_PIN, 0);
duration = pulseIn(ECHO_PIN, 1023);
distance = (duration * 0.343)/2;
Serial.print("Distance:");
Serial.println(distance);
return distance
}
int scanButton() {
//Scan all buttons
delay(150);
for (int i = 2; i < 9; i++) {
pressed = i;
if (digitalRead(i == HIGH) { //When a button is pressed
break;
}
if(i > 7)
pressed = 0;// No pressed
}
return pressed
}Up
Down
Sel
Del
Switch
Cancel
Fan(No motor,
so we use brightness
to describle the speed of motor)