【问题标题】:Arduino Analog joystick button looks always pressed after the first time第一次后,Arduino 模拟操纵杆按钮看起来总是被按下
【发布时间】:2018-10-24 22:59:19
【问题描述】:

我的 BrainTest() 函数有问题 当我加载它时,液晶显示屏上没有出现起始文本,它直接进入:

如果(按钮 == 0){

lcd.clear();

lcd.setCursor(0,0);

lcd.print("让我们开始吧");

延迟(1400);

lcd.clear();

lcd.setCursor(0,0);

lcd.print("测试");

}

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

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

#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() 中;功能第一个问题不起作用。 我无法在答案之间切换...

这是新代码

#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);
      }
    }
  }
}

【问题讨论】:

  • 这里为什么有for (;;) { ... ; break; }?无论如何,您不了解它的速度有多快,并且您必须使用一些循环来进行开关检测。也可能去抖动。

标签: button arduino-ide lcd joystick


【解决方案1】:

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

我建议从超级简单的程序开始:计算按钮被按下的次数并将其发送到串口。

在您可以使这个简单的按钮按下计数器正常工作之前,您无法创建任何您想要的东西。

【讨论】:

  • 谢谢,我会试试这个,我会回复你
  • 我试图延迟(200);在调用 selectapp() 之后;在循环函数中,但它仍在发生
  • 你建议我把延迟放在哪里
  • 我建议你等到按钮真正释放后再进入子程序选择。
  • 你会怎么做?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-06
相关资源
最近更新 更多