Duvidas em código projecto placar desportivo

boas. sou novo nestas andanças, por isso ainda me estou a ambientar a este mundo que recentemente descobri.~
O objetivo é fazer um placar desportivo com o arduino uno, usando leds e sensor contador por IR e lcd 16x02.
apos alguma pesquisa encontrei um codigo para esse fim, no entanto nao estou conseguindo polo em pratica.
assim gostaria que alguem mais expert o conseguisse verificar para perceber se o codigo esta correcto. agradeco desde ja a vossa disponiblidade para me ajudar. Obrigada
deixo aqui o mesmo

/*
  LiquidCrystal Library - Hello World

 Demonstrates the use a 16x2 LCD display.  The LiquidCrystal
 library works with all LCD displays that are compatible with the
 Hitachi HD44780 driver. There are many of them out there, and you
 can usually tell them by the 16-pin interface.

 This sketch prints "Hello World!" to the LCD
 and shows the time.

  The circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * LCD VSS pin to ground
 * LCD VCC pin to 5V
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)

 Library originally added 18 Apr 2008
 by David A. Mellis
 library modified 5 Jul 2009
 by Limor Fried (http://www.ladyada.net)
 example added 9 Jul 2009
 by Tom Igoe
 modified 22 Nov 2010
 by Tom Igoe
 modified 7 Nov 2016
 by Arturo Guadalupi

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/LiquidCrystalHelloWorld

*/

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <FastLED.h>

#define LEDPIN 13
#define HOME_SENSORPIN 7
#define AWAY_SENSORPIN 4

// LEDS
#define BRIGHTNESS  80
// lado do moedeiro
#define FIRST_STRIP_NUM_LEDS 35
#define FIRST_STRIP_PIN 10
CRGB first_leds[FIRST_STRIP_NUM_LEDS];
// lado oposto moedeiro, tem mais 1 led
#define SECOND_STRIP_NUM_LEDS 36
#define SECOND_STRIP_PIN 11
CRGB second_leds[SECOND_STRIP_NUM_LEDS];

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);

const char HOME_TEAM[] = "BEIRA MAR  ";
const char AWAY_TEAM[] = "ACADEMICA  ";

int home_goals = 0;
int away_goals = 0;

int home_wins = 0;
int away_wins = 0;

// variables will change:
int homeSensorState = 0, homeLastState=0;         // variable for reading the pushbutton status
int awaySensorState = 0, awayLastState=0;         // variable for reading the pushbutton status

void flashYellow() {
  int i = 0;
  for (i = 0; i < FIRST_STRIP_NUM_LEDS; i++) {
    first_leds[i] = CRGB::Yellow;
  }
  for (i = 0; i < SECOND_STRIP_NUM_LEDS; i++) {
    second_leds[i] = CRGB::Yellow;
  }
  FastLED.show();
}

void flashGray() {
  int i = 0;
  for (i = 0; i < FIRST_STRIP_NUM_LEDS; i++) {
    first_leds[i] = CRGB::Gray;
  }
  for (i = 0; i < SECOND_STRIP_NUM_LEDS; i++) {
    second_leds[i] = CRGB::Gray;
  }
  FastLED.show();
}

void flashBlack() {
  int i = 0;
  for (i = 0; i < FIRST_STRIP_NUM_LEDS; i++) {
    first_leds[i] = CRGB::Black;
  }
  for (i = 0; i < SECOND_STRIP_NUM_LEDS; i++) {
    second_leds[i] = CRGB::Black;
  }
  FastLED.show();
}

void printScore() {
  // Write home info
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(HOME_TEAM);
  if (home_goals < 10) {
    lcd.print(" ");
  }
  lcd.print(home_goals);
  if (home_wins < 10) {
    lcd.print(" ");
  }
  // separate between info
  lcd.print(" ");
  lcd.print(home_wins);

  // Write away info
  lcd.setCursor(0, 1);
  lcd.print(AWAY_TEAM);
  if (away_goals < 10) {
    lcd.print(" ");
  }
  lcd.print(away_goals);
  if (away_wins < 10) {
    lcd.print(" ");
  }
  // separate between info
  lcd.print(" ");
  lcd.print(away_wins);
}

void splashScreen() {
  int i = 0;
  int j = 0;
  for (i = 0; i < 2; i++) {
    for (j = 0; j < 16; j++) {
      //lcd.clear();
      lcd.setCursor(j, i);
      lcd.print("/");
      delay(25);
    }
  }
}

void blinkHome() {
  Serial.println("blinkHome");

  flashBlack();
  delay(150);
  flashYellow();
  delay(150);
  flashBlack();
}

void blinkAway() {
  Serial.println("blinkAway");

  flashBlack();
  delay(150);
  flashGray();
  delay(150);
  flashBlack();
}

void printGoal() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("  GOLO  GOLO  GOLO!!");
  lcd.setCursor(0, 1);
  lcd.print("GOLO  GOLO  GOLO !");
  delay(200);
  
//  lcd.clear();
//  delay(100);
//
//  lcd.clear();
//  lcd.setCursor(0, 0);
//  lcd.print("GOLO  GOLO  GOLO!!");
//  lcd.setCursor(0, 1);
//  lcd.print("  GOLO  GOLO  GOLO !");
//  delay(200);
}

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin();
  lcd.backlight();
  // Print a message to the LCD.
  //lcd.print("hello, world!");

  splashScreen();
  
  printScore();

  // initialize the LED pin as an output:
  pinMode(LEDPIN, OUTPUT);  

  // initialize the sensor pin as an input:
  pinMode(HOME_SENSORPIN, INPUT);     
  digitalWrite(HOME_SENSORPIN, HIGH); // turn on the pullup

  pinMode(AWAY_SENSORPIN, INPUT);     
  digitalWrite(AWAY_SENSORPIN, HIGH); // turn on the pullup

  FastLED.addLeds<NEOPIXEL, FIRST_STRIP_PIN>(first_leds, FIRST_STRIP_NUM_LEDS);
  FastLED.addLeds<NEOPIXEL, SECOND_STRIP_PIN>(second_leds, SECOND_STRIP_NUM_LEDS);
  FastLED.setBrightness(BRIGHTNESS);

  flashGray();
  
  Serial.begin(9600);
}

void loop() {
  // Turn the LED on, then pause
//  first_leds[0] = CRGB::Gray;
//  first_leds[1] = CRGB::Yellow;
//  first_leds[2] = CRGB::Green;
//  FastLED.show();
//  delay(500);
//  // Now turn the LED off, then pause
//  first_leds[0] = CRGB::Black;
//  first_leds[1] = CRGB::Black;
//  first_leds[2] = CRGB::Black;
//  FastLED.show();
//  delay(500);

  
  //delay(2000);
  //home_goals++;
  //printScore();

  // read the state of the pushbutton value:
  homeSensorState = digitalRead(HOME_SENSORPIN);
  awaySensorState = digitalRead(AWAY_SENSORPIN);
 
  // check if the sensor beam is broken
  // if it is, the sensorState is LOW:
  if (homeSensorState == LOW) {     
    // turn LED on:
    digitalWrite(LEDPIN, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(LEDPIN, LOW); 
  }

  if (awaySensorState == LOW) {     
    // turn LED on:
    digitalWrite(LEDPIN, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(LEDPIN, LOW); 
  }
  
  if (homeSensorState && !homeLastState) {
    Serial.println("home Unbroken");
  } 
  if (!homeSensorState && homeLastState) {
    Serial.println("home Broken");

    printGoal();
    blinkHome();
    blinkHome();
    blinkHome();
    blinkHome();
    blinkHome();
    blinkHome();

    flashGray();
    
    home_goals++;
    printScore();
    if (home_goals + away_goals == 9) {
      if (home_goals > away_goals) {
        home_wins++;
      }
      else {
        away_wins++;
      }
      home_goals = 0;
      away_goals = 0;
      delay(2000);
      printScore();
    }
  }

  if (awaySensorState && !awayLastState) {
    Serial.println("away Unbroken");
  } 
  if (!awaySensorState && awayLastState) {
    Serial.println("away Broken");

    printGoal();
    blinkAway();
    blinkAway();
    blinkAway();
    blinkAway();
    blinkAway();
    blinkAway();

    flashGray();
    
    away_goals++;
    printScore();
    if (home_goals + away_goals == 9) {
      if (away_goals > home_goals) {
        away_wins++;
      }
      else {
        home_wins++;
      }
      away_goals = 0;
      home_goals = 0;
      delay(2000);
      printScore();
    }
  }
  
  homeLastState = homeSensorState;
  awayLastState = awaySensorState;
}

OK, voce postou corretamente seu código. Isto é bom.
Mas para que possamos dar uma boa ajuda precisamos conhecer o seu projeto físico..
Então poste um esquemático do seu projeto.

PS: Explique como deve funciona seu placar e conte como está funcionado.
Aqui tem ele simulado.

Boas. Desde já obrigada pela resposta, pois assim já sei que o código está certo. Assim que receba o material, irei fazer a montagem e colocarei aqui. O problema que tenho tido, deve-se ao facto de este código estar a dar erros no simulador que estou a usar. É como sou novo nisto, preferi partilhar o código com o intuito de verificar se o código era bom. Assim, vou trabalhar com este código e proceder às alterações que pretendo.
Basicamente apenas pretendo mudar botões para sensor ir de movimento, pois os sensores vão ler o movimento da bola após o golo. De resto apenas alterar nomes é cores. Mas logo que o material chegue, posto aqui a montagem.
Desde já o meu obrigada pela disponibilidade de resposta

Existem alguns "erros".
O primeiro acho que depende da biblioteca LiquidCrystal_I2C.h usada.
Tive que corrigir a linha de " lcd.begin(); "para "lcd.init();" para que a biblioteca usada pelo simulador não desse erro na compilação.

Outro "erro" que percebi foi que ao apertar o botão "HOME_SENSORPIN" o "LEDPIN" só dá uma pequena piscada, mas ao apertar o botão "AWAY_SENSORPIN" o "LEDPIN" fica aceso enquanto os strips piscam.
Este é um erro de logica e não de sintaxe.

Tb reparei isso no led pin.
Já fiz algumas alterações para chegar ao resultado pretendido.
No entanto, queria colocar um botão para cada equipa com a finalidade de fazer um back.
Basicamente a equipa home marca um golo, e o resultado passa a 4 - 3, no entanto o golo é invalidado. Esse botão serve para retornar o resultado 3 - 3.
Estou a ter algum dificuldade com isso, no entanto pergunto, é possível? Obrigada

boas. deixo aqui o projeto quase concluído. No esquema estao dois botoes, cada um deles terá a funçao de decrementar o valor para o anterior em cada equipa. no entanto estou com alguma dificuldade, será que me podes ajudar ruiviana?

Explique sua dificuldade.

eu ainda percebo muito pouco disto. no entanto o code já esta complexo. estive a estudar o incremento e decremento, no entanto toda a informação que vi, eram codes simples, fáceis de entender. Mas aqui com tantas variáveis, esta dificil.

  • defini a porta digital 8 para btndechome e porta 2 para btndecaway
  • defini a variavel leituradechome = 0 e leituradecaway = 0 para guardar os valor dos botoes de decremento
  • depois no void setup configurei o pinMode de ambos os botoes para input e digitalwrite high.
  • em void loop
    leituradec = digitalRead(decremento);

até aqui penso que está tudo bem, no entanto a partir daqui tudo o que fiz, ou entrava em conflito ou nao fazia nada.
Reconheco que este codigo já está demasiado complexo para o meu nivel de conhecimento, mas gostava de conseguir colocar mais esta funcao de decremento.
Pois no jogo de matraquilhos, acontece muitas vezes ao lançar a bola, esta entrar nas balizas, e estes golos nao contam, e por isso esta funcao de decremento para retornar ao valor inicial.

Muito obrigada, mas finalmente já consegui decrementar resultado. Agora é esperar pelo material e se tiver alguma dúvida logo entro em contacto. mais uma vez obrigada

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