Buzz wire game extended

Hi guys,

I’m trying to make a buzz wire game to play with up to 4 different people.
So i have 4 different “circuits” shaped each as a different letter.
And i have at each of this ciruits a contact point “reset” at the beginning and a contact point “succes” at the end. The idea is that all 4 circuits should reach the end/succes before having 4 errors/contacts.

So i’m a real noob to, so this is probably not the fastest way to accomplish the code, but anyway.
The problem i’m having is, that at a certain moment (when blinkled() is active, the blinking leds stop and i can continue to play without even having to reset. Which is not the point.
In the serial monitor at the moment it happens i see a reversed question mark.
Is this an error or a bug or…

Anyone who can help me with this?

int ERR1_LED = 11;
int ERR2_LED = 13;
int ERR3_LED = 12;
int SUC_LED = 1;
int RESET1 = 2;
int RESET2 = 4;
int RESET3 = 6;
int RESET4 = 8;
int WIRE = 10;
int SUCCES1 = 3;
int SUCCES2 = 5;
int SUCCES3 = 7;
int SUCCES4 = 9;

int COUNT = 0;
int COUNTS1 = 0;
int COUNTS2 = 0;
int COUNTS3 = 0;
int COUNTS4 = 0;
int COUNTR1 = 1;
int COUNTR2 = 1;
int COUNTR3 = 1;
int COUNTR4 = 1;

void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600);
  pinMode(ERR1_LED, OUTPUT);
  pinMode(ERR2_LED, OUTPUT);
  pinMode(ERR3_LED, OUTPUT);
  pinMode(SUC_LED, OUTPUT);
  pinMode(RESET1, INPUT);
  pinMode(RESET2, INPUT);
  pinMode(RESET3, INPUT);
  pinMode(RESET4, INPUT);
  pinMode(WIRE, INPUT);
  pinMode(SUCCES1, INPUT);
  pinMode(SUCCES2, INPUT);
  pinMode(SUCCES3, INPUT);
  pinMode(SUCCES4, INPUT);
}

void loop() {

  // contact + reset

  if (digitalRead(WIRE) == HIGH && COUNT < 4) {
    COUNT++;
    delay (500);
  }

  if (COUNT == 1) {
    digitalWrite(ERR1_LED, HIGH);
  }

  if (COUNT == 2) {
    digitalWrite(ERR1_LED, HIGH);
    digitalWrite(ERR2_LED, HIGH);
  }

  if (COUNT == 3) {
    digitalWrite(ERR1_LED, HIGH);
    digitalWrite(ERR2_LED, HIGH);
    digitalWrite(ERR3_LED, HIGH);
  }

  if (COUNT == 4) {
    blinkled();
  }

  if (digitalRead(RESET1) == HIGH) {
    COUNTR1 = 0;
  }
  else {
    COUNTR1 = 1;
  }
  if (digitalRead(RESET2) == HIGH) {
    COUNTR2 = 0;
  }
  else {
    COUNTR2 = 1;
  }
  if (digitalRead(RESET3) == HIGH) {
    COUNTR3 = 0;
  }
  else {
    COUNTR3 = 1;
  }
  if (digitalRead(RESET4) == HIGH) {
    COUNTR4 = 0;
  }
  else {
    COUNTR4 = 1;
  }

  if (COUNTR1 == 0 && COUNTR2 == 0 && COUNTR3 == 0 && COUNTR4 == 0) {
    reset();
  }

  // succes

  if (digitalRead(SUCCES1) == HIGH) {
    COUNTS1 = 1;
  }
  if (digitalRead(SUCCES2) == HIGH) {
    COUNTS2 = 1;
  }
  if (digitalRead(SUCCES3) == HIGH) {
    COUNTS3 = 1;
  }
  if (digitalRead(SUCCES4) == HIGH) {
    COUNTS4 = 1;
  }

  if (COUNTS1 + COUNTS2 + COUNTS3 + COUNTS4 == 4 && COUNT < 4) {
    action();
  }

  Serial.println("Count = ");
  Serial.print(COUNT);
  Serial.print("- reset ");
  Serial.print(COUNTR1);
  Serial.print(COUNTR2);
  Serial.print(COUNTR3);
  Serial.print(COUNTR4);
  Serial.print("- wire ");
  Serial.print(WIRE);
  Serial.print("- succes ");
  Serial.print(COUNTS1);
  Serial.print(COUNTS2);
  Serial.print(COUNTS3);
  Serial.print(COUNTS4);

}

void action() {
  digitalWrite (ERR1_LED, LOW);
  digitalWrite (ERR2_LED, LOW);
  digitalWrite (ERR3_LED, LOW);
  digitalWrite(SUC_LED, HIGH);
  while (285) { }
}

void reset() {
  digitalWrite (ERR1_LED, LOW);
  digitalWrite (ERR2_LED, LOW);
  digitalWrite (ERR3_LED, LOW);
  digitalWrite (SUC_LED, LOW);
  COUNT = 0;
  COUNTS1 = 0;
  COUNTS2 = 0;
  COUNTS3 = 0;
  COUNTS4 = 0;
  COUNTR1 = 1;
  COUNTR2 = 1;
  COUNTR3 = 1;
  COUNTR4 = 1;
}

void blinkled() {
  digitalWrite(ERR1_LED, HIGH);
  digitalWrite(ERR2_LED, HIGH);
  digitalWrite(ERR3_LED, HIGH);
  delay(100);
  digitalWrite(ERR1_LED, LOW);
  digitalWrite(ERR2_LED, LOW);
  digitalWrite(ERR3_LED, LOW);
  delay(100);
}

You should probably not use pin 1 (and 0)as they are used for Serial communication

What’s this supposed to do?while (285) { }

The code is truncated

What happened to the rest of your code ?

Also: When you are tempted to suffix variable names with numbers, it is time to learn about arrays.

@J-M-L: I thought so, but the serial messages for the monitor are only for evaluation purposes since something went wrong and i could not figure out what. Once i know the problem, i will remove the serial messages and then i think i can use ports 1 and 0. No?

@J-M-L: the while is an empty loop that prevents other things from happening once everything is succesfull. I have to reset afterwards to restart the game. Couldn’t figure out how to do this different. As i said, i’m a noob. This is my first attempt.

@UKHeliBob: I edited the code, thanks for noticing this.

@vaj4088: i never learned to code, never learned about arrays, i think i understand a bit how it is supposed to work. But could you show me perhaps how you see this?

Thanks allready for the answers, but still no clue what’s going wrong there…

Pin1 is Tx... that’s what is sending data to your PC... when you print something, it modifies the HIGH/LOW status of that pin... so your SUC_LED will behave weirdly. You have more pins, don’t use 0 and 1...and when you light the led you actually mess with the Serial line, hence the quest mark probably

Ok for the infinite loop. Usually one writes while (true); instead of a weird number and best is to try to not to lock the microcontroler and handle next round gracefully and keep the loop spinning waiting for some input.

@J-M-L: Thanks, i assume this will be the problem. Although the error happened when SUC_LED was not supposed to change from LOW to HIGH. I will remove the Serial lines, so i can use the pins 1 or 0, since i have no other pins left. (I tried the analog pins, but it did not seem to work... ) This should do the trick no?

for the analog pin I replaced the "int SUC_LED = 1;" to "int SUC_LED = A0;" guess this was not enough :)

Rx/Tx was not the problem, since i removed the serial lines from the code.
I even removed the connection to pin 1. so pin 0 and pin 1 were not in use…

The problem still persisted.

No one an idea what could cause this?

I have a hard time following how this actually "works"

The problem i'm having is, that at a certain moment (when blinkled() is active, the blinking leds stop and i can continue to play without even having to reset. Which is not the point.

when blinkled() is active, you have reached COUNT == 4 but all the other tests on the RESET*n* input are still running. if you see something weird, it's likely that you are entering that condition:

  if (COUNTR1 == 0 && COUNTR2 == 0 && COUNTR3 == 0 && COUNTR4 == 0) {
    reset();
  }

which performs the reset(). That would happen if all the RESET*n* lines are HIGH. You are not using INPUT_PULLUP, do you have pull down?

you have one arduino connected to the different letters ? can you post a picture or drawing of how things are setup ?

How are the inputs wired ? Do you have pulldown resistors in place or are the inputs floating at an unknown voltage ?

I would be interested to see a diagram of your wiring, hand drawn and photographed is OK

I will try to upload the drawing/pictures tonight.
No pullup, no pulldown, no resistors in place…
So floating i gues.

Changed pin SUC_LED from 1 to 11 (since 1 should not be used)
Changed pin ERR1_LED to A2 pin
Changed pin ERR2_LED to A3 pin
Changed pin ERR3_LED to A4 pin

So i believe we can rule out TX/RX issues.

It will be issues regarding floating, pullup

indeed if your pins are floating then all bets are off…

use the built in pull-ups on your arduino pins →   pinMode(RESETxx, [b][color=red]INPUT_PULLUP[/color][/b]);.

that will change the logic, LOW will be equivalent to button being pressed and HIGH as standby mode.

but of course I’m not sure how things are connected so would need to see the circuit indeed

Sorry, i did use resistors for each input.

and the problem is only happening when blinkled() is active…

error.png

Your image:

5d03ea1aacf79307530a5350a106c6bf714dd9d1.png
(best to post text as text…)

→ could you fix your carriage return to see exactly where the question mark arrives.

  Serial.print("Count = "); // NOT PRINTLN
  Serial.println(COUNT); // HERE PRINTLN TO GO TO NEXT LINE

Seems it’s the COUNT variable that could be corrupted (don’t thing so) or something played with the Tx line…

is this without using pin 1? can you post new code?