mercredi 28 avril 2021

C++ Add secondary integer for couting values

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:

/*
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);

      }

    }
  }
}

In another forum a user wrote to me that I needed to make a sub-function with arrays, although this is fairly new to me I came up with something like

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

Would anyone happen to share their thoughts on whether or not I am close to figuring this one out?

Your time and help are very much appreciated.

Aucun commentaire:

Enregistrer un commentaire