Add secondary integer for couting values

Hi, I'm Alexander, I'm an Arduino UNO and C++ beginner.

I'm stuck trying to add a secondary integer for counting values. I suspect that the issue is within the first if statement in the loop.

I have two integers, one for Home and one for Away. Would anyone happen to pinpoint how I can add my secondary integer (Away) within or at least as a member of this if statement?

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

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

The complete program below:

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

#include <SSD1320_OLED.h>

// Initialize the display with the following 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);

      }

    }
  }
}

Now, I might be totally wrong after all, that would be good to know as well, but any help or guidance would be highly appreciated :slightly_smiling_face:

seems like you need a sub-function that can process home or away given some state (use arrays)

1 Like

@gcjr thanks a lot. I will read more about sub-functions and Arrays to see if this can solve the matter :slightly_smiling_face:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.