Whack-A-Mole change of code from analog button reading to digital

Hi Arduino Community,

I am working on a gift where I want to built a two player whack a mole table. My base is this:

Here an analog reading is being used to figure out which button is pressed. Since I had quite a lot of issues with it and I am using an Arduino Mega with enough digital pins, I would like to change this code to digitalRead.

Can someone point out to me how I can achieve this? My fiddling around so far only lead to an impressive amount of compiling errors. :wink:

// IMPORTANT!
// DOWNLOAD the MD_Parola library
// https://github.com/MajicDesigns/MD_Parola


#include <stdio.h>
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
//#include "Parola_Fonts_data.h"
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 8
#define CLK_PIN   29
#define DATA_PIN  45
#define CS_PIN    37
MD_Parola P = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);



long randNumber;
int step_counter = 0;
//int button_values[] = {913, 429, 267, 179, 110};
//int btn_tol = 20;
int bValue = 0;
int pin_p1[5] = {20,21,22,23,24};
int pin_p2[5] = {30,31,32,33,34};
int leds_cnt = 5;
int p1_leds[5] = {2,3,4,5,6};
int p2_leds[5] = {42,43,44,45,41};
int p1_score = 0;
int p2_score = 0;
int action_speed = 2000;
int action_speed_min = 250;

void setup()
{
  Serial.begin(9600);

  randomSeed(analogRead(A7));
  
  pinMode(pin_p1, INPUT);
  pinMode(pin_p2, INPUT);

  for (int i = 0; leds_cnt > i; i++) {
    pinMode(p1_leds[i], OUTPUT);
  }
  for (int i = 0; leds_cnt > i; i++) {
    pinMode(p2_leds[i], OUTPUT);
  }  

  P.begin(2);
  P.setZone(0,0,3);
  P.setZone(1,4,7);
  P.displayZoneText(0, "abc", PA_CENTER, 0, 0, PA_PRINT, PA_NO_EFFECT);
  P.setZoneEffect(0, true, PA_FLIP_UD);
  P.displayZoneText(1, "abcd", PA_CENTER, 0, 0, PA_PRINT, PA_NO_EFFECT);
  P.setZoneEffect(1, true, PA_FLIP_UD);
  P.displayAnimate();

}

void loop()
{
  if(p1_score < 100 and p2_score < 100) {
    
    step_counter++;
    bool step_action = false;
    if (step_counter > action_speed) {
      step_counter = 0;
      step_action = true;  
      action_speed = action_speed - round(action_speed/50);
      if (action_speed < action_speed_min) {
        action_speed = action_speed_min;
      }
      Serial.println(action_speed);
    }
  
    if (step_action) {
      int pin_light = random(0,5);
      digitalWrite(p1_leds[pin_light], HIGH);
      digitalWrite(p2_leds[pin_light], HIGH);
      
    }
  
    bValue = digitalRead(pin_p1);
    for (int i = 0; leds_cnt > i; i++) {
      if ( pin_light < bValue[i] and pin_light > bValue[i] ){
        if(digitalRead(p1_leds[i]) == HIGH){
          digitalWrite(p1_leds[i], LOW);
          p1_score++;
        }
      }
    }
  
    bValue = digitalRead(pin_p2);
    for (int i = 0; leds_cnt > i; i++) {
      if ( pin_light < bValue[i] and pin_light > bValue[i] ){
        if(digitalRead(p2_leds[i]) == HIGH){
          digitalWrite(p2_leds[i], LOW);
          p2_score++;
        }
      }
    }
  
    if ( step_counter % 100 == 0){
      char Score1[80];
      sprintf(Score1, "%d", p1_score);
      char *chrdisp[] = {Score1};
  
      char Score2[80];
      sprintf(Score2, "%d", p2_score);
      char *chrdisp2[] = {Score2};
  
      P.displayZoneText(0, chrdisp[0], PA_CENTER, 0, 0, PA_PRINT, PA_NO_EFFECT);
      P.displayZoneText(1, chrdisp2[0], PA_CENTER, 0, 0, PA_PRINT, PA_NO_EFFECT);
      P.displayAnimate();
    }
  } else {
    if (p1_score > p2_score) {
      P.displayZoneText(0, "Winner", PA_CENTER, 0, 0, PA_PRINT, PA_NO_EFFECT);
      P.displayZoneText(1, "Looser", PA_CENTER, 0, 0, PA_PRINT, PA_NO_EFFECT);
    } else {
      P.displayZoneText(0, "Looser", PA_CENTER, 0, 0, PA_PRINT, PA_NO_EFFECT);
      P.displayZoneText(1, "Winner", PA_CENTER, 0, 0, PA_PRINT, PA_NO_EFFECT);
    }
    P.displayAnimate();  
    delay(500);
      char Score1[80];
      sprintf(Score1, "%d", p1_score);
      char *chrdisp[] = {Score1};
  
      char Score2[80];
      sprintf(Score2, "%d", p2_score);
      char *chrdisp2[] = {Score2};
    P.displayZoneText(0, chrdisp[0], PA_CENTER, 0, 0, PA_PRINT, PA_NO_EFFECT);
    P.displayZoneText(1, chrdisp2[0], PA_CENTER, 0, 0, PA_PRINT, PA_NO_EFFECT);    
    P.displayAnimate();
    delay(500);        
  }
}

My fiddling around so far only lead to an impressive amount of compiling errors.

Please post the full error messages copied from the IDE using the "Copy error messages" button and posted here in code tags

This line

bValue = digitalRead(pin_p1);

is a problem. Are you trying to read the entire array? pin_p1 is an array of 5 pins so you will need a loop to read them all.

Ah, good point! I will try to do this. Thank you!

And to not leave UKHeliBob's request unanswered:

whack_digi_V2.ino:95:32: warning: invalid conversion from 'int*' to 'uint8_t {aka unsigned char}' [-fpermissive]
     bValue = digitalRead(pin_p2);
                                ^
In file included from sketch/whack_digi_V2.ino.cpp:1:0:
/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Arduino.h:136:5: note:   initializing argument 1 of 'int digitalRead(uint8_t)'
 int digitalRead(uint8_t pin);
     ^~~~~~~~~~~
whack_digi_V2:97:12: error: 'pin_light' was not declared in this scope
       if ( pin_light < bValue[i] and pin_light > bValue[i] ){
            ^~~~~~~~~
whack_digi_V2:97:32: error: invalid types 'int[int]' for array subscript
       if ( pin_light < bValue[i] and pin_light > bValue[i] ){
                                ^
whack_digi_V2:97:58: error: invalid types 'int[int]' for array subscript
       if ( pin_light < bValue[i] and pin_light > bValue[i] ){
                                                          ^
exit status 1
'pin_light' was not declared in this scope

Which sketch did that list of errors come from ?

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