首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >第一次按下Arduino模拟操纵杆按钮

第一次按下Arduino模拟操纵杆按钮
EN

Stack Overflow用户
提问于 2018-10-24 22:59:19
回答 1查看 499关注 0票数 0

我在BrainTest()函数中遇到问题,当我加载它时,开始的文本不会出现在LCD上,它会直接显示到:

if (按钮== 0) { lcd.clear(); lcd.setCursor(0,0); Lcd.print(“让我们开始”); 延迟(1400); lcd.clear(); lcd.setCursor(0,0); Lcd.print(“测试”);}

我还认为在函数的开头选择“是”或“否”是有问题的。

任何帮助都将不胜感激..。

代码语言:javascript
复制
#include <LiquidCrystal.h>

// Arduino pins number
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output
const int LCD_RS = 7;
const int LCD_Enable = 8;
const int LCD_D4 = 9;
const int LCD_D5 = 10;
const int LCD_D6 = 11;
const int LCD_D7 = 12;
LiquidCrystal lcd(LCD_RS, LCD_Enable, LCD_D4, LCD_D5, LCD_D6, LCD_D7);

// Basic vars
int none = 0;
String Apps[3] = {"App select","Credits","Test your brain"};
int CurrentApp = 0;
int Yaxis = 1;
int Xaxis = 1;
int HiCh = 0;
int button;
int JXaxis;
int JYaxis;
int MaxHi;


void AppSelect() {  //          APPSELECT
  lcd.setCursor(0,0);
  lcd.print("App select menu");
  lcd.setCursor(0,1);
  lcd.print(Apps[HiCh]);
  if (button == 0) {
    SelectApp();
  }
  if (JYaxis <= 2) {
    if (HiCh != 0) {
      HiCh = HiCh - 1;
      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print(Apps[HiCh]);
      delay(300);
    }
  }
  if (JYaxis >= 7) {
    if (HiCh != 1) {
      HiCh = HiCh + 1;
      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print(Apps[HiCh]);
      delay(300);
    }
  }
}

void Credits() {  //          CREDITS
  MaxHi = 0;
  Serial.print("- Credits app loading \n");
  lcd.clear();
  lcd.setCursor(9,0);
  lcd.print("Credits");
  lcd.setCursor(0,1);
  lcd.print("Made by Alexandre Bergeron");
  Serial.print("- Credits app loaded \n");
  delay(1300);
  lcd.scrollDisplayLeft();
  delay(600);
  lcd.scrollDisplayLeft();
  delay(600);
  lcd.scrollDisplayLeft();
  delay(600);
  lcd.scrollDisplayLeft();
  delay(600);
  lcd.scrollDisplayLeft();
  delay(600);
  lcd.scrollDisplayLeft();
  delay(600);
  lcd.scrollDisplayLeft();
  delay(600);
  lcd.scrollDisplayLeft();
  delay(600);
  lcd.scrollDisplayLeft();
  delay(600);
  lcd.scrollDisplayLeft();
  delay(600);
  lcd.scrollDisplayLeft();
  delay(600);
  lcd.scrollDisplayLeft();
  delay(1500);
  CurrentApp = 0;
  lcd.clear();
}


void BrainTest() {  //          BRAINTEST
  MaxHi = 1;
  HiCh = 0;
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Are you ready?");
  lcd.setCursor(0,1);
  lcd.print("Yes");
  for (;;) {
    button = digitalRead(SW_pin);
    if (HiCh == 0) {
      lcd.setCursor(0,1);
      lcd.print("Yes");
      if (button == 0) {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Let's start");
        delay (1400);
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("done");
      }
    }
    if (HiCh == 1) {
      lcd.setCursor(0,1);
      lcd.print("No");
      if (button == 0) {
        CurrentApp = 0;
      }
    }
    break;
  }
}


void setup() {  //          SETUP
  Serial.begin(9600);
  Serial.print("[2J");
  Serial.print("  Serial Monitor opened \n \n");
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  Serial.print("- App selector menu \n");
  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);
}


void SelectApp() {  //          SELECTAPP
  switch (HiCh) {
    case (0):
      CurrentApp = 0;
      AppSelect();
      break;
    case (1):
      CurrentApp = 1;
      Credits();
      break;
    case (2):
      CurrentApp = 2;
      BrainTest();
      break;
    default:
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Error");
      Serial.print("- App loading error \n");
      break;
  }
}


void loop() {  //          LOOP
  Serial.print(HiCh);
  button = digitalRead(SW_pin);
  int JYaxis = analogRead(Y_pin) / 128;
  int JXaxis = analogRead(X_pin) / 128;
  if (JYaxis >= 7) {
    if (HiCh != 0) {
      HiCh = HiCh - 1;
      delay(300);
    }
  }
  if (JYaxis <= 2) {
    if (HiCh != MaxHi) {
      HiCh = HiCh + 1;
      delay(300);
    }
  }
  if (CurrentApp == 0) { 
    MaxHi = 2;
    lcd.setCursor(0,0);
    lcd.print("App select menu");
    lcd.setCursor(0,1);
    lcd.print(Apps[HiCh]);
    if (button == 0) {
      SelectApp();
    }
    if (JYaxis >= 7) {
      if (HiCh != 0) {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("App select menu");
        lcd.setCursor(0,1);
        lcd.print(Apps[HiCh]);
        delay(300);
      }
    }
    if (JYaxis <= 2) {
      if (HiCh != 2) {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("App select menu");
        lcd.setCursor(0,1);
        lcd.print(Apps[HiCh]);
        delay(300);
      }
    }
  }
}

如果你认为我应该改变问题中的任何东西,告诉我,我会改正的。另外,如果您发现我的代码有其他问题或升级,请告诉我,我会考虑它们。

现在,SelectApp();函数运行良好,但在BrainTest();函数中,第一个问题不起作用。我不能在答案之间切换..。

这是新代码

代码语言:javascript
复制
#include <LiquidCrystal.h>

// Arduino pins number
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output
const int LCD_RS = 7;
const int LCD_Enable = 8;
const int LCD_D4 = 9;
const int LCD_D5 = 10;
const int LCD_D6 = 11;
const int LCD_D7 = 12;
LiquidCrystal lcd(LCD_RS, LCD_Enable, LCD_D4, LCD_D5, LCD_D6, LCD_D7);

// Basic vars
int none = 0;
String Apps[3] = {"App select","Credits","Test your brain"};
int CurrentApp = 0;
int Yaxis = 1;
int Xaxis = 1;
int HiCh = 0;
int button;
int JXaxis;
int JYaxis;
int MaxHi;


void AppSelect() {  //          APPSELECT
  lcd.setCursor(0,0);
  lcd.print("App select menu");
  lcd.setCursor(0,1);
  lcd.print(Apps[HiCh]);
  if (button == 0) {
    SelectApp();
  }
  if (JYaxis <= 2) {
    if (HiCh != 0) {
      HiCh = HiCh - 1;
      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print(Apps[HiCh]);
      delay(300);
    }
  }
  if (JYaxis >= 7) {
    if (HiCh != 1) {
      HiCh = HiCh + 1;
      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print(Apps[HiCh]);
      delay(300);
    }
  }
}

void Credits() {  //          CREDITS
  MaxHi = 0;
  Serial.print("- Credits app loading \n");
  lcd.clear();
  lcd.setCursor(9,0);
  lcd.print("Credits");
  lcd.setCursor(0,1);
  lcd.print("Made by Alexandre Bergeron");
  Serial.print("- Credits app loaded \n");
  delay(1300);
  lcd.scrollDisplayLeft();
  delay(600);
  lcd.scrollDisplayLeft();
  delay(600);
  lcd.scrollDisplayLeft();
  delay(600);
  lcd.scrollDisplayLeft();
  delay(600);
  lcd.scrollDisplayLeft();
  delay(600);
  lcd.scrollDisplayLeft();
  delay(600);
  lcd.scrollDisplayLeft();
  delay(600);
  lcd.scrollDisplayLeft();
  delay(600);
  lcd.scrollDisplayLeft();
  delay(600);
  lcd.scrollDisplayLeft();
  delay(600);
  lcd.scrollDisplayLeft();
  delay(600);
  lcd.scrollDisplayLeft();
  delay(1500);
  CurrentApp = 0;
  lcd.clear();
}


void BrainTest() {  //          BRAINTEST
  MaxHi = 1;
  HiCh = 0;
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Are you ready?");
  lcd.setCursor(0,1);
  lcd.print("Yes");
  for (;;) {
    button = digitalRead(SW_pin);
    if (HiCh == 0) {
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Are you ready?");
      lcd.setCursor(0,1);
      lcd.print("Yes");
      if (button == 0) {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Let's start");
        delay (1400);
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("done");
      }
    }
    if (HiCh == 1) {
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Are you ready?");
      lcd.setCursor(0,1);
      lcd.print("No");
      if (button == 0) {
        CurrentApp = 0;
      }
    }
    break;
  }
}


void setup() {  //          SETUP
  Serial.begin(9600);
  Serial.print("[2J");
  Serial.print("  Serial Monitor opened \n \n");
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  Serial.print("- App selector menu \n");
  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);
}


void SelectApp() {  //          SELECTAPP
  switch (HiCh) {
    case (0):
      CurrentApp = 0;
      AppSelect();
      break;
    case (1):
      CurrentApp = 1;
      Credits();
      break;
    case (2):
      CurrentApp = 2;
      BrainTest();
      break;
    default:
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Error");
      Serial.print("- App loading error \n");
      break;
  }
}


void loop() {  //          LOOP
  Serial.print(HiCh);
  button = digitalRead(SW_pin);
  int JYaxis = analogRead(Y_pin) / 128;
  int JXaxis = analogRead(X_pin) / 128;
  if (JYaxis >= 7) {
    if (HiCh != 0) {
      HiCh = HiCh - 1;
  delay(300);
    }
  }
  if (JYaxis <= 2) {
    if (HiCh != MaxHi) {
      HiCh = HiCh + 1;
      delay(300);
    }
  }
  if (CurrentApp == 0) { 
    MaxHi = 2;
    lcd.setCursor(0,0);
    lcd.print("App select menu");
    lcd.setCursor(0,1);
    lcd.print(Apps[HiCh]);
    if (button == 0) {
      while (button == 0) {
        delay (200);
        break;
      }
      SelectApp();
    }
    if (JYaxis >= 7) {
      if (HiCh != 0) {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("App select menu");
        lcd.setCursor(0,1);
        lcd.print(Apps[HiCh]);
        delay(300);
      }
    }
    if (JYaxis <= 2) {
      if (HiCh != 2) {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("App select menu");
        lcd.setCursor(0,1);
        lcd.print(Apps[HiCh]);
        delay(300);
      }
    }
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-25 08:44:23

你处理这个按钮完全错误。在选择应用程序后,您有微秒的时间来释放按钮。所以看起来它被压了,因为它是。你从没检查过它是否被释放过。

我建议从超级简单的程序开始:数一下按钮被按了多少次,然后发送给串行程序。

除非你能让这个简单的按钮按下计数器,否则你就不能创建你想要创建的任何东西。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52979034

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档