C ++添加用于计数值的辅助整数

C++ Add secondary integer for counting values

我是 Alexander,我是 Arduino UNO 和 C++ 初学者。

我一直在尝试添加一个辅助整数来计算值。我怀疑问题出在循环中的第一个 if 语句中。

我有两个整数,一个代表主场,一个代表客场。有人会碰巧指出我如何在这个 if 语句中或至少作为这个 if 语句的成员添加我的辅助整数 (Away) 吗?

 if (currentHomeState != previousHomeState) {
previousHomeState = currentHomeState;

if (currentHomeState == On) {
  if ( (msec - msecLst) > Interval)  {
    msecLst = msec;
    numberOfGoals++; 
}

完整程序:

/*
Program for the 1960s Panco Mini-Match table top soccer game.
*/

#include <SSD1320_OLED.h>

// Initialize the display with the follow pin connections.
SSD1320 flexibleOLED(10, 9); //10 = CS, 9 = RES

// Define constants for home and away team.
const int Home = A1;
const int Away = A2;

// Define constants for LED pins.
const int LED_1 = 7;    // Pin 7 connected to a LED, turns HIGH when away team score.
const int LED_2 = 8;  // Pin 8 connected to a LED, turns HIGH when home team score.

enum { Off = HIGH, On = LOW };

#define Interval  1000

// Initalize and define states for the goal sensors.
int currentHomeState = 0, previousHomeState = 0;
int currentAwayState = 0, previousAwayState = 0;

int numberOfGoals = 0;

unsigned long msecLst = 0;

void setup() {
  //Run once

  //Initilize the display
  flexibleOLED.begin(160, 32);  //Display is 160 wide, 32 high
  flexibleOLED.clearDisplay();  //Clear display and buffer

  //Display Home score
  flexibleOLED.setContrast(255);
  flexibleOLED.setFontType(2); //7-segment display style characters, 10x16-pixels each.
  flexibleOLED.setCursor(45, 7);
  flexibleOLED.print(numberOfGoals);

  //Display Away score
  flexibleOLED.setContrast(255);
  flexibleOLED.setFontType(2); //7-segment display style characters, 10x16-pixels each.
  flexibleOLED.setCursor(95, 7);
  flexibleOLED.print(numberOfGoals);
  flexibleOLED.display();
  pinMode(Home, INPUT_PULLUP);
  pinMode(LED_1, OUTPUT);

  Serial.begin(9600);
}

void loop() {
  unsigned long msec = millis ();
  //Run repeatedly
  currentHomeState = digitalRead(Home);
  currentAwayState = digitalRead(Away);

  if (currentHomeState != previousHomeState) {
    previousHomeState = currentHomeState;

    if (currentHomeState == On) {
      if ( (msec - msecLst) > Interval)  {
        msecLst = msec;
        numberOfGoals++;
        Serial.println (numberOfGoals);

        flexibleOLED.setContrast(255);
        flexibleOLED.setFontType(2); //7-segment display style characters, 10x16-pixels each.
        flexibleOLED.setCursor(45, 6);
        flexibleOLED.print(numberOfGoals);
        flexibleOLED.display();

        digitalWrite(LED_1, HIGH);
        delay(250);
        digitalWrite(LED_1, LOW);
      }

      if (numberOfGoals == 5) {
        digitalWrite(LED_1, HIGH);
        delay(250);
        digitalWrite(LED_1, LOW);
        delay(250);
        digitalWrite(LED_1, HIGH);
        delay(250);
        digitalWrite(LED_1, LOW);
        delay(250);
        digitalWrite(LED_1, HIGH);
        delay(250);
        digitalWrite(LED_1, LOW);
        delay(250);
        digitalWrite(LED_1, HIGH);
        delay(250);
        digitalWrite(LED_1, LOW);
        delay(250);
        digitalWrite(LED_1, HIGH);

      }

      if (numberOfGoals == 5) {
        numberOfGoals = 0;
        delay(250);
        digitalWrite(LED_1, LOW);

      }

      if (numberOfGoals == 0) {

        flexibleOLED.clearDisplay(); // Clear display and buffer
        flexibleOLED.setContrast(255);
        flexibleOLED.setFontType(2); //7-segment display style characters, 10x16-pixels each.
        flexibleOLED.setCursor(45, 6);
        flexibleOLED.print(numberOfGoals);
        flexibleOLED.display();
        Serial.println (0);

      }

    }
  }
}

在另一个论坛上,一位用户写信给我说我需要用数组创建一个子函数,虽然这对我来说是相当新的,但我想出了类似的东西

int main(int argc, char** argv) {
string teams[2] = {"Home", "Away"};
int numbers[1];
for(int i = 0;i<1;i++) 
{
// not yet sure what to add here.
} 
for(int i = 1;i<1;i++)
{
// not yet sure what to add here.
}
return 0;
}

有人会碰巧分享他们对我是否接近解决这个问题的想法吗?

非常感谢您的宝贵时间和帮助。

原则上,您可以通过引入一个团队 class 并实例化两个对象来使您对团队的处理具有可扩展性。但由于我认为不会有超过 2 支球队,一个直接的解决方案就是为客队做同样的事情,就像你已经为主队做的那样:

#include <SSD1320_OLED.h>

// Initialize the display with the follow pin connections.
SSD1320 flexibleOLED(10, 9); //10 = CS, 9 = RES

// Define constants for home and away team.
const int Home = A1;
const int Away = A2;

// Define constants for LED pins.
const int LED_Away = 7;    // Pin 7 connected to a LED, turns HIGH when away team score.
const int LED_Home = 8;  // Pin 8 connected to a LED, turns HIGH when home team score.

enum { Off = HIGH, On = LOW };

#define Interval  1000

// Initalize and define states for the goal sensors.
int currentHomeState = 0, previousHomeState = 0;
int currentAwayState = 0, previousAwayState = 0;

int homeGoals = 0;  // introduce seperate variables for home and away goals
int awayGoals = 0;

//int numberOfGoals = 0;

unsigned long msecLst = 0;

void setup() {
  //Run once

  //Initilize the display
  flexibleOLED.begin(160, 32);  //Display is 160 wide, 32 high
  flexibleOLED.clearDisplay();  //Clear display and buffer

  //Display Home score
  flexibleOLED.setContrast(255);
  flexibleOLED.setFontType(2); //7-segment display style characters, 10x16-pixels each.
  flexibleOLED.setCursor(45, 7);
  flexibleOLED.print(homeGoals);

  //Display Away score
  flexibleOLED.setContrast(255);
  flexibleOLED.setFontType(2); //7-segment display style characters, 10x16-pixels each.
  flexibleOLED.setCursor(95, 7);
  flexibleOLED.print(awayGoals);
  flexibleOLED.display();
  pinMode(Home, INPUT_PULLUP);
  pinMode(Away, INPUT_PULLUP);
  pinMode(LED_home, OUTPUT);
  pinMode(LED_away, OUTPUT);

  Serial.begin(9600);
}

void loop() {
  unsigned long msec = millis ();
  //Run repeatedly
  currentHomeState = digitalRead(Home);
  currentAwayState = digitalRead(Away);
//-------------handle home --------------------------------
  if (currentHomeState != previousHomeState) {
    previousHomeState = currentHomeState;

    if (currentHomeState == On) {
      if ( (msec - msecLst) > Interval)  {
        msecLst = msec;
        homeGoals++;
        Serial.println (homeGoals);

        flexibleOLED.setContrast(255);
        flexibleOLED.setFontType(2); //7-segment display style characters, 10x16-pixels each.
        flexibleOLED.setCursor(45, 6);
        flexibleOLED.print(homeGoals);
        flexibleOLED.display();

        digitalWrite(LED_1, HIGH);
        delay(250);
        digitalWrite(LED_1, LOW);
      }

      if (homeGoals == 5) {
        digitalWrite(LED_home, HIGH);
        delay(250);
        digitalWrite(LED_home, LOW);
        delay(250);
        digitalWrite(LED_home, HIGH);
        delay(250);
        digitalWrite(LED_home, LOW);
        delay(250);
        digitalWrite(LED_home, HIGH);
        delay(250);
        digitalWrite(LED_home, LOW);
        delay(250);
        digitalWrite(LED_home, HIGH);
        delay(250);
        digitalWrite(LED_home, LOW);
        delay(250);
        digitalWrite(LED_home, HIGH);

      }

      if (homeGoals == 5) {
        homeGoals = 0;
        awayGoals = 0;
        delay(250);
        digitalWrite(LED_home, LOW);

      }

      if (homeGoals == 0) {

        flexibleOLED.clearDisplay(); // Clear display and buffer
        flexibleOLED.setContrast(255);
        flexibleOLED.setFontType(2); //7-segment display style characters, 10x16-pixels each.
        flexibleOLED.setCursor(45, 6);
        flexibleOLED.print(homeGoals);
        flexibleOLED.display();
        Serial.println (0);

      }
//----------------handle away
  if (currentAwayState != previousAwayState) {
    previousAwayState = currentAwayState;

    if (currentAwayState == On) {
      if ( (msec - msecLst) > Interval)  {
        msecLst = msec;
        awayGoals++;
        Serial.println (awayGoals);

        flexibleOLED.setContrast(255);
        flexibleOLED.setFontType(2); //7-segment display style characters, 10x16-pixels each.
        flexibleOLED.setCursor(45, 6);
        flexibleOLED.print(awayGoals);
        flexibleOLED.display();

        digitalWrite(LED_away, HIGH);
        delay(250);
        digitalWrite(LED_away, LOW);
      }

      if (awayGoals == 5) {
        digitalWrite(LED_away, HIGH);
        delay(250);
        digitalWrite(LED_away, LOW);
        delay(250);
        digitalWrite(LED_away, HIGH);
        delay(250);
        digitalWrite(LED_away, LOW);
        delay(250);
        digitalWrite(LED_away, HIGH);
        delay(250);
        digitalWrite(LED_away, LOW);
        delay(250);
        digitalWrite(LED_away, HIGH);
        delay(250);
        digitalWrite(LED_away, LOW);
        delay(250);
        digitalWrite(LED_away, HIGH);

      }

      if (awayGoals == 5) {
        homeGoals = 0;
        awayGoals = 0;
        delay(250);
        digitalWrite(LED_away, LOW);

      }

      if (awayGoals == 0) {

        flexibleOLED.clearDisplay(); // Clear display and buffer
        flexibleOLED.setContrast(255);
        flexibleOLED.setFontType(2); //7-segment display style characters, 10x16-pixels each.
        flexibleOLED.setCursor(45, 6);
        flexibleOLED.print(awayGoals);
        flexibleOLED.display();
        Serial.println (0);

      }
    }
  }
}

请注意,我没有检查代码是否有错误。另外一般来说,您应该考虑编写单独的函数以使代码更易于阅读。