C 在继续之前等待输入-Arduino Uno
我正在使用Arduino Uno制作一个基本的体育比赛记分器(壁球/球拍球) 目标是,2个按钮,1名玩家各有一个按钮,他们可以使用该按钮增加分数。C 在继续之前等待输入-Arduino Uno,c,open-source,arduino,arduino-uno,C,Open Source,Arduino,Arduino Uno,我正在使用Arduino Uno制作一个基本的体育比赛记分器(壁球/球拍球) 目标是,2个按钮,1名玩家各有一个按钮,他们可以使用该按钮增加分数。 这很简单,但是如果&当两个玩家同时获得10分时,他们需要选择13分或15分 我的问题是,它正在侦听调用scoreDecise()func之前按下的最后一个按钮 例如,player1在9(左键),player2在10(右键) player1按下左键,最多10次,scoreDecision()被自动调用,因为它是10/10[score] player2决
这很简单,但是如果&当两个玩家同时获得10分时,他们需要选择13分或15分 我的问题是,它正在侦听调用
scoreDecise()
func之前按下的最后一个按钮
例如,player1在9(左键),player2在10(右键)
player1按下左键,最多10次,scoreDecision()
被自动调用,因为它是10/10[score]
player2决定按右键,选择15作为分数,但实际上它已经选择了左键值,因为这是调用scoreDecise()
func之前最后一次按的值,这意味着13实际上被设置为target
,按右键实际上增加了player2的分数
我的代码如下,到目前为止我已经花了8个小时,但我对Arduino完全是新手,没有运气
// Squash Scorer, by @MrWigster
// Squash scoring is fairly easy for the most part, however at 10-10 the first player to have reached 10 must decide whether to play until 13 or 15, as the winner must always have a 2 point lead.
// For this reason we need to include logic to let the players choose what score to play until.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//Setting up Switches, 1 for each player to press.
const int switchPlayer1 = 7;
const int switchPlayer2 = 6;
//Setting up LEDs, 1 representing each player.
const int led1 = 9;
const int led2 = 10;
//Players names
String player1 = "Bob";
String player2 = "Alex";
//Sets up initial values for switches
int switchStatePlayer1 = 0;
int switchStatePlayer2 = 0;
int prevSwitchState1 = 0;
int prevSwitchState2 = 0;
int p1score = 0;
int p2score = 0;
int winner;
// Initial Target score to get to
int target = 10;
void setup() {
lcd.begin(16,2);
pinMode(switchPlayer1,INPUT);
pinMode(switchPlayer2,INPUT);
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
lcd.print(player1);
lcd.print(" Vs ");
lcd.print(player2);
lcd.setCursor(0,1);
lcd.print("SquashScorer 0.1");
}
void loop() {
// If LED is still on, turn it off.
if((digitalRead(led1) == HIGH) || (digitalRead(led2) == HIGH)){
delay(200);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
}
// If players scores are the same, and it's at the target score to change
if ((p1score == p2score) && (p1score == target)){
// Where we call the new target deciding function
delay(50); // Trying to give time for button to reset???
target = scoreDecide(); // Main code that isn't working
}
if ((target == 15) || (target == 13) || (target == 10)) {
switchStatePlayer1 = digitalRead(switchPlayer1);
if (switchStatePlayer1 != prevSwitchState1){
if(switchStatePlayer1 == HIGH){
digitalWrite(led1, HIGH);
p1score = p1score++;
//Deciding if this was winning point
if (
(p1score == target +1) || (p2score == target +1)
&& (p1score != p2score)
) {
winner = whoWins(player1);
}
// Writes the new score on the board
newScore();
}
}
prevSwitchState1 = switchStatePlayer1;
switchStatePlayer2 = digitalRead(switchPlayer2);
if (switchStatePlayer2 != prevSwitchState2){
if(switchStatePlayer2 == HIGH){
digitalWrite(led2, HIGH);
p2score = p2score++;
//Deciding if this was winning point
if (
(p1score == target +1) || (p2score == target +1)
&& (p1score != p2score)
) {
winner = whoWins(player2);
}
// Writes the new score on the board
newScore();
}
}
prevSwitchState2 = switchStatePlayer2;
}
}
// Let the player(s) decide which score to go up to, either 13 or 15.
int scoreDecide() { // This function doesn't seem to work, its already getting a value before the player gets to choose, based on whatever player last reached TARGET.
lcd.clear();
lcd.setCursor(0,0);
// Give the players the info about which score they can choose
lcd.print(target + 3);
lcd.print(" OR ");
lcd.print(target + 5);
switchStatePlayer1 = digitalRead(switchPlayer1);
if (switchStatePlayer1 != prevSwitchState1){
if(switchStatePlayer1 == LOW){
target = target + 3;
delay(1000);
return target;
}
}
prevSwitchState1 = switchStatePlayer1;
switchStatePlayer2 = digitalRead(switchPlayer2);
if (switchStatePlayer2 != prevSwitchState2){
if(switchStatePlayer2 == LOW){
target = target + 5;
delay(1000);
return target;
}
}
prevSwitchState2 = switchStatePlayer2;
}
void newScore(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Bob Alex");
lcd.setCursor(0,1);
lcd.print(p1score);
lcd.print(" Target:");
lcd.print(target);
lcd.print(" ");
lcd.print(p2score);
}
void ledflash(){
for (int i=0;i<5;i++){
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
delay(500);
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
delay(500);
}
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
}
int whoWins(String player){
lcd.clear();
lcd.setCursor(0,0);
lcd.print(player);
lcd.setCursor(0,1);
lcd.print("is the Winner!");
ledflash();ledflash();
delay(10000);
p1score = 0;
p2score = 0;
target = 10;
}
//壁球记分员,作者@MrWigster
//壁球得分在大多数情况下是相当容易的,但是在10-10时,第一个达到10分的球员必须决定是打到13分还是15分,因为胜利者必须始终领先2分。
//出于这个原因,我们需要包括逻辑,让球员选择什么分数发挥,直到。
#包括
液晶显示器(12,11,5,4,3,2);
//设置开关,每个播放器按一个。
const int switchPlayer1=7;
const int switchPlayer2=6;
//设置LED,1个代表每个玩家。
常数1=9;
常数int led2=10;
//球员姓名
字符串播放器1=“鲍勃”;
字符串播放器2=“Alex”;
//设置开关的初始值
int switchStatePlayer1=0;
int switchStatePlayer2=0;
int-prevSwitchState1=0;
int-prevSwitchState2=0;
int p1得分=0;
积分p2=0;
int优胜者;
//要达到的初始目标分数
int目标=10;
无效设置(){
lcd.begin(16,2);
pinMode(开关播放器1,输入);
pinMode(开关播放器2,输入);
引脚模式(led1,输出);
引脚模式(led2,输出);
lcd.打印(播放器1);
lcd.打印(“Vs”);
lcd.打印(播放器2);
lcd.setCursor(0,1);
lcd.打印(“挤压记分器0.1”);
}
void循环(){
//如果LED仍亮起,请将其关闭。
如果((数字读取(led1)=高)| |(数字读取(led2)=高)){
延迟(200);
数字写入(led1,低电平);
数字写入(led2,低电平);
}
//如果球员的得分相同,那么就在目标得分处改变
如果((p1score==p2score)和&(p1score==target)){
//我们称之为新的目标决定函数
延迟(50);//试图给按钮重置时间???
target=scoreDecise();//不起作用的主代码
}
如果((目标==15)| |(目标==13)| |(目标==10)){
switchStatePlayer1=数字读取(switchPlayer1);
if(switchStatePlayer1!=prevSwitchState1){
如果(switchStatePlayer1==高){
数字写入(led1,高);
p1score=p1score++;
//决定这是否是一个胜利点
如果(
(p1score==目标+1)| |(p2score==目标+1)
&&(p1score!=p2score)
) {
赢家=谁赢(玩家1);
}
//将新分数写在黑板上
新闻核心();
}
}
prevSwitchState1=switchStatePlayer1;
switchStatePlayer2=数字读取(switchPlayer2);
if(switchStatePlayer2!=prevSwitchState2){
如果(switchStatePlayer2==高){
数字写入(led2,高);
p2score=p2score++;
//决定这是否是一个胜利点
如果(
(p1score==目标+1)| |(p2score==目标+1)
&&(p1score!=p2score)
) {
赢家=谁赢(玩家2);
}
//将新分数写在黑板上
新闻核心();
}
}
prevSwitchState2=switchStatePlayer2;
}
}
//让球员决定得分,13分或15分。
int scoreDecise(){//这个函数似乎不起作用,它在玩家选择之前已经得到了一个值,基于玩家最后到达目标的位置。
lcd.clear();
lcd.setCursor(0,0);
//给玩家提供他们可以选择的分数信息
lcd.打印(目标+3);
lcd.打印(“或”);
lcd.打印(目标+5);
switchStatePlayer1=数字读取(switchPlayer1);
if(switchStatePlayer1!=prevSwitchState1){
如果(switchStatePlayer1==低){
目标=目标+3;
延迟(1000);
回报目标;
}
}
prevSwitchState1=switchStatePlayer1;
switchStatePlayer2=数字读取(switchPlayer2);
if(switchStatePlayer2!=prevSwitchState2){
如果(switchStatePlayer2==低){
// Squash Scorer, by @MrWigster
// Squash scoring is fairly easy for the most part, however at 10-10 the first player to have reached 10 must decide whether to play until 13 or 15, as the winner must always have a 2 point lead.
// For this reason we need to include logic to let the players choose what score to play until, always in either incremenets of 2 || 4. EG: 13 || 15, 17 || 19 etc.
//To Do :
// * Set/Games Counter
// Function for score incrementing to help respect DRY coding
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//Setting up Switches, 1 for each player to press.
const int player1switch = 7;
const int player2switch = 6;
//Setting up LEDs, 1 representing each player.
const int led1 = 9;
const int led2 = 10;
//Players names
String player1 = "Hraday";
String player2 = "Alex";
int switchStatePlayer1 = 0;
int switchStatePlayer2 = 0;
int targetChosen = 1;
int decideTarget = 1;
int p1score = 0;
int p2score = 0;
int winner;
// Initial Target score to get to
int target = 11;
void setup() {
Serial.begin(9600);
lcd.begin(16,2);
pinMode(player1switch,INPUT);
pinMode(player2switch,INPUT);
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
lcd.print(player1);
lcd.print(" Vs ");
lcd.print(player2);
lcd.setCursor(0,1);
lcd.print("SquashScorer 0.1");
}
void loop() {
// If LED is still on, turn it off.
if((digitalRead(led1) == HIGH) || (digitalRead(led2) == HIGH)){
delay(200);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
}
// If players scores are the same, and it's at the target score to change
if (targetChosen == 0){
lcd.clear();
lcd.setCursor(0,0);
// Give the players the info about which score they can choose
lcd.print(target + 2);
lcd.print(" OR ");
lcd.print(target + 4);
targetChosen = 1;
decideTarget = 0;
}
// While new target score hasn't been decided, keep checking.
while(decideTarget == 0) {
newTarget();
}
if((targetChosen == 1) && (decideTarget == 1)) {
switchStatePlayer1 = digitalRead(player1switch);
if(switchStatePlayer1 == HIGH){
digitalWrite(led1, HIGH);
p1score = p1score++;
if ((p1score == target - 1) && (p2score == target - 1)) { targetChosen = 0; }
//Deciding if this was winning point
if ((p1score == target) || (p2score == target) && (p1score != p2score)) {
winner = whoWins(player1);
}
Serial.println("New Score__________");
Serial.println(p1score);
newScore();
}
switchStatePlayer2 = digitalRead(player2switch);
if(switchStatePlayer2 == HIGH){
digitalWrite(led2, HIGH);
p2score = p2score++;
if ((p1score == target - 1) && (p2score == target - 1)) { targetChosen = 0; }
//Deciding if this was winning point
if ((p1score == target) || (p2score == target) && (p1score != p2score)) {
winner = whoWins(player2);
}
Serial.println("New Score__________");
Serial.println(p2score);
newScore();
}
}
}
int newTarget(){
Serial.println(decideTarget);
Serial.println("Waiting for input...");
if(digitalRead(player1switch) == HIGH){
target = target + 2;
newScore();
decideTarget = 1;
delay(250);
}
if(digitalRead(player2switch) == HIGH){
target = target + 4;
newScore();
decideTarget = 1;
delay(250);
}
}
void newScore(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print(player1);
lcd.print(" ");
lcd.print(player2);
lcd.setCursor(0,1);
lcd.print(p1score);
lcd.print(" Target:");
lcd.print(target);
lcd.print(" ");
lcd.print(p2score);
Serial.println("NewScore() Called");
}
void ledflash(){
for (int i=0;i<4;i++){
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
delay(500);
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
delay(500);
}
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
}
int whoWins(String player){
lcd.clear();
lcd.setCursor(0,0);
lcd.print(player);
lcd.setCursor(0,1);
lcd.print("is the Winner!");
ledflash();
delay(3000);
p1score = 0;
p2score = 0;
target = 10;
}