Coin counter with 8 tilt switches (ky031) and a re-set button

I'm creating a coin counter with 8 tilt switches (ky031) and a re-set button. When I use the simple code below (first) the sensor works well. When I use the second code that includes all 8 switches it does not work well. Practically, this means that the sensor needs more power to detect a signal and as such it is not performing as it should.

Any suggestions would be much appreciated!
Thomas

const int buttonPin = 2; // Change this to match your setup

// Define a variable to store the state of the button
int buttonState = 0;
void setup()
{
  // Initialize serial communication
  Serial.begin(9600);

  // Set the button pin as an input
  pinMode(buttonPin, INPUT);
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
  
  buttonState = digitalRead(buttonPin);

  // Check if the button is pressed
  if (buttonState == LOW) {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000); // Wait for 1000 millisecond(s)
   digitalWrite(LED_BUILTIN, LOW);
  delay(1000); // Wait for 1000 millisecond(s) 
  }
}```

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
 
// Define instance for LCD I2C display
LiquidCrystal_I2C lcd(0x27, 16, 2);
 
// Change 0x27 to 0x3F and retry if LCD screen does not show anything
// Change 16,2 to 20,4 if you are using LCD2004


// Define the pins for buttons and LED display
const int twoEuroButtonPin = 2;
const int oneEuroButtonPin = 3;
const int fiftyCentButtonPin = 4;
const int twentyCentButtonPin = 5;
const int tenCentButtonPin = 6;
const int ledPin = 13;
const int resetButtonPin = 10; // Pin for the reset button
const int fiveCentButtonPin = 7;
const int twoCentButtonPin = 8;
const int oneCentButtonPin = 9;
// Define the coin values
const int twoEuroValue = 200;
const int oneEuroValue = 100;
const int fiftyCentValue = 50;
const int twentyCentValue = 20;
const int tenCentValue = 10;
const int fiveCentValue = 5;
const int twoCentValue = 2;
const int oneCentValue = 1;


// Variable to hold total euro value
int twoEuroCount = 0;
int oneEuroCount = 0;
int fiftyCentCount = 0;
int twentyCentCount = 0;
int tenCentCount = 0;
int fiveCentCount = 0;
int twoCentCount = 0;
int oneCentCount = 0;

float totalEuroValue = 0.0;

void setup() {
  // Initialize button pins as inputs
  pinMode(twoEuroButtonPin, INPUT);
  pinMode(oneEuroButtonPin, INPUT);
  pinMode(fiftyCentButtonPin, INPUT);
  pinMode(twentyCentButtonPin, INPUT);
  pinMode(tenCentButtonPin, INPUT);
  pinMode(resetButtonPin, INPUT);
  pinMode(fiveCentButtonPin, INPUT);
  pinMode(twoCentButtonPin, INPUT);
  pinMode(oneCentButtonPin, INPUT);
  
  // Initialize LED pin as output
  pinMode(ledPin, OUTPUT);
  // Initialize serial communication for debugging
  Serial.begin(9600);
  
  lcd.init();       // Initialize the LCD display screen
  lcd.backlight();  // Turn on backlight of LCD display screen.
  
  lcd.setCursor(0, 0);          // Go to position column 2 & row 1
  lcd.print("Welcome to our");   // Print "Hello, World!"
  lcd.setCursor(0, 1);          // Go to position column 1 & row 2
  lcd.print("channel:Tomstell)");
  delay(2000);
  lcd.clear();
  
  lcd.setCursor(0, 0);          // Go to position column 2 & row 1
  lcd.print("like and share");   // Print "Hello, World!"
  lcd.setCursor(0, 1);          // Go to position column 1 & row 2
  lcd.print("and hit the");
  delay(1000);
  lcd.clear();
  
  lcd.setCursor(0, 0);          // Go to position column 2 & row 1
  lcd.print("subscribe button");   // Print "Hello, World!"
  lcd.setCursor(0, 1);          // Go to position column 1 & row 2
  lcd.print("Enjoy it....");
  delay(1000);
  lcd.clear();
  
  lcd.setCursor(0, 0);          // Go to position column 2 & row 1
  lcd.print("Let's get ready");   // Print "Hello, World!"
  lcd.setCursor(0, 1);          // Go to position column 1 & row 2
  lcd.print("to sort & count");
  delay(1000);
  lcd.clear();
  
  
  
}

void loop() {
  // Read the button states
  int twoEuroButtonState = digitalRead(twoEuroButtonPin);
  int oneEuroButtonState = digitalRead(oneEuroButtonPin);
  int fiftyCentButtonState = digitalRead(fiftyCentButtonPin);
  int twentyCentButtonState = digitalRead(twentyCentButtonPin);
  int tenCentButtonState = digitalRead(tenCentButtonPin);
  int resetButtonState = digitalRead(resetButtonPin);
  int fiveCentButtonState = digitalRead(fiveCentButtonPin);
  int twoCentButtonState = digitalRead(twoCentButtonPin);
  int oneCentButtonState = digitalRead(oneCentButtonPin);
  
   if (resetButtonState == HIGH) {
    // Reset all coin counts to 0
    twoEuroCount = 0;
    oneEuroCount = 0;
    fiftyCentCount = 0;
    twentyCentCount = 0;
    tenCentCount = 0;
    fiveCentCount = 0; 
    twoCentCount = 0;
    oneCentCount = 0;
    // Print reset message
    Serial.println("Counts reset to 0");
     lcd.clear();
    // Display reset message on LCD
    lcd.setCursor(0, 0);
    lcd.print("Counts");
     lcd.setCursor(0, 1);
    lcd.print("reset to 0");
     
    delay(3000); // Delay for button debounce
     lcd.clear();
     
    
   }
  // Check if any of the buttons are pressed
  if (twoEuroButtonState == LOW) {
    delay(100);
    twoEuroCount++;
    coinDetected();
    
    // Calculate total euro value
  totalEuroValue = (twoEuroCount * 2.0) + (oneEuroCount * 1.0) + (fiftyCentCount * 0.5) + (twentyCentCount * 0.2) + (tenCentCount * 0.1) + (fiveCentCount * 0.05) + (twoCentCount * 0.02) + (oneCentCount * 0.01); 
  
  // Print total euro value for debugging
  Serial.print("Total Euro Value: ");
  Serial.println(totalEuroValue, 2); // Print with 2 decimal places
  Serial.println();
  
  }
  if (oneEuroButtonState == LOW) {
    delay(100);
    oneEuroCount++;
    
    coinDetected();
    // Calculate total euro value
 totalEuroValue = (twoEuroCount * 2.0) + (oneEuroCount * 1.0) + (fiftyCentCount * 0.5) + (twentyCentCount * 0.2) + (tenCentCount * 0.1) + (fiveCentCount * 0.05) + (twoCentCount * 0.02) + (oneCentCount * 0.01);  
  // Print total euro value for debugging
  Serial.print("Total Euro Value: ");
  Serial.println(totalEuroValue, 2); // Print with 2 decimal places
  Serial.println();
  
  }
  if (fiftyCentButtonState == LOW) {
    delay(100);
    fiftyCentCount++;
    coinDetected();
    // Calculate total euro value
 totalEuroValue = (twoEuroCount * 2.0) + (oneEuroCount * 1.0) + (fiftyCentCount * 0.5) + (twentyCentCount * 0.2) + (tenCentCount * 0.1) + (fiveCentCount * 0.05) + (twoCentCount * 0.02) + (oneCentCount * 0.01);  
  // Print total euro value for debugging
  Serial.print("Total Euro Value: ");
  Serial.println(totalEuroValue, 2); // Print with 2 decimal places
  Serial.println();
  }
  if (twentyCentButtonState == LOW) {
    delay(100);
    twentyCentCount++;
    coinDetected();
    // Calculate total euro value
 totalEuroValue = (twoEuroCount * 2.0) + (oneEuroCount * 1.0) + (fiftyCentCount * 0.5) + (twentyCentCount * 0.2) + (tenCentCount * 0.1) + (fiveCentCount * 0.05) + (twoCentCount * 0.02) + (oneCentCount * 0.01);  
  // Print total euro value for debugging
  Serial.print("Total Euro Value: ");
  Serial.println(totalEuroValue, 2); // Print with 2 decimal places
  Serial.println();
  }
  if (tenCentButtonState == LOW) {
    delay(100);
    tenCentCount++;
    coinDetected();
    // Calculate total euro value
 totalEuroValue = (twoEuroCount * 2.0) + (oneEuroCount * 1.0) + (fiftyCentCount * 0.5) + (twentyCentCount * 0.2) + (tenCentCount * 0.1) + (fiveCentCount * 0.05) + (twoCentCount * 0.02) + (oneCentCount * 0.01);  
  // Print total euro value for debugging
  Serial.print("Total Euro Value: ");
  Serial.println(totalEuroValue, 2); // Print with 2 decimal places
  Serial.println();
  
  }
  
  if (fiveCentButtonState == LOW) {
    delay(100);
    fiveCentCount++;
    coinDetected();
    // Calculate total euro value
 totalEuroValue = (twoEuroCount * 2.0) + (oneEuroCount * 1.0) + (fiftyCentCount * 0.5) + (twentyCentCount * 0.2) + (tenCentCount * 0.1) + (fiveCentCount * 0.05) + (twoCentCount * 0.02) + (oneCentCount * 0.01);  
  // Print total euro value for debugging
  Serial.print("Total Euro Value: ");
  Serial.println(totalEuroValue, 2); // Print with 2 decimal places
  Serial.println();
  
  }
  if (twoCentButtonState == LOW) {
    delay(100);
    twoCentCount++;
    coinDetected();
    // Calculate total euro value
 totalEuroValue = (twoEuroCount * 2.0) + (oneEuroCount * 1.0) + (fiftyCentCount * 0.5) + (twentyCentCount * 0.2) + (tenCentCount * 0.1) + (fiveCentCount * 0.05) + (twoCentCount * 0.02) + (oneCentCount * 0.01);  
  // Print total euro value for debugging
  Serial.print("Total Euro Value: ");
  Serial.println(totalEuroValue, 2); // Print with 2 decimal places
  Serial.println();
  }
  
  if (oneCentButtonState == LOW) {
    delay(100);
    oneCentCount++;
    coinDetected();
    // Calculate total euro value
 totalEuroValue = (twoEuroCount * 2.0) + (oneEuroCount * 1.0) + (fiftyCentCount * 0.5) + (twentyCentCount * 0.2) + (tenCentCount * 0.1) + (fiveCentCount * 0.05) + (twoCentCount * 0.02) + (oneCentCount * 0.01);  
  // Print total euro value for debugging
  Serial.print("Total Euro Value: ");
  Serial.println(totalEuroValue, 2); // Print with 2 decimal places
  Serial.println();
  }
  
  lcd.setCursor(0, 0);          // Go to position column 2 & row 1
  lcd.print("Total euro:");   // Print "Hello, World!"
  lcd.setCursor(0, 1);          // Go to position column 1 & row 2
  lcd.print(totalEuroValue);
  
}

// Function to indicate coin detected
void coinDetected() {
  // Turn on LED to indicate coin detected
  digitalWrite(ledPin, HIGH);
  delay(100); // Delay to debounce the button
  digitalWrite(ledPin, LOW);
  
  
}



Hello tomstell

Consider:

//https://forum.arduino.cc/t/coin-counter-with-8-tilt-switches-ky031-and-a-re-set-button/1238886
//https://europe1.discourse-cdn.com/arduino/original/4X/7/e/0/7e0ee1e51f1df32e30893550c85f0dd33244fb0e.jpeg
#define ProjectName "Coin counter with 8 tilt switches (ky031) and a re-set button"
#define NotesOnRelease "Arduino MEGA tested"
// make names
enum TimerEvent {NotExpired, Expired};
enum TimerControl {Halt, Run};
enum ButtonNames {Reset, One, Two, Five, Ten, Twenty, Fifty, Hundred, TwoHundred};
enum ButtonStates {Released, Pressed};
// make variables
uint32_t currentMillis = millis();
constexpr uint8_t ButtonPins[] {A0, A1, A2, A3, A4, A5, A6, A7, A8};
constexpr uint8_t Coins[] {0, 1, 2, 5, 10, 20, 50, 100, 200};
uint16_t total;
// make structures
struct TIMER
{
  uint32_t interval;
  uint8_t control;
  uint32_t now;
  uint8_t expired(uint32_t currentMillis)
  {
    uint8_t timerEvent = currentMillis - now >= interval and control;
    if (timerEvent == Expired) now = currentMillis;
    return timerEvent;
  }
};
struct BUTTON2COINS
{
  uint8_t name;
  uint8_t pin;
  uint8_t stateOld;
  TIMER debounce;
  void make(uint8_t name_, uint8_t pin_)
  {
    name = name_;
    pin = pin_;
    pinMode(pin,INPUT_PULLUP);
    stateOld = digitalRead(pin) ? LOW : HIGH;
    debounce.interval = 20;
    debounce.control = Run;
    debounce.now = currentMillis;
  }
  uint8_t run()
  {
    uint8_t reTurn = Released;
    if (debounce.expired(currentMillis) == Expired)
    {
      uint8_t stateNew = digitalRead(pin) ? LOW : HIGH;
      if (stateNew != stateOld)
      {
        stateOld = stateNew;
        if (stateNew == Pressed)
        {
          reTurn = Pressed;
          (name == Reset) ? total = Coins[name] : total = total + Coins[name];
        }
      }
    }
    return reTurn;
  }
} button2Coins[sizeof(ButtonPins)];
// make support
void heartBeat(const uint8_t LedPin, uint32_t currentMillis)
{
  static bool setUp = false;
  if (setUp == false) pinMode (LedPin, OUTPUT), setUp = true;
  digitalWrite(LedPin, (currentMillis / 500) % 2);
}
// make application
void setup()
{
  Serial.begin(115200);
  Serial.print("Source: "), Serial.println(__FILE__);
  Serial.print(ProjectName), Serial.print(" - "), Serial.println(NotesOnRelease);
  uint8_t element = 0;
  for (auto &button2Coin : button2Coins)
  {
    button2Coin.make(element, ButtonPins[element]);
    element ++;
  }
  Serial.println(" =-> and off we go\n");
}
void loop()
{
  currentMillis = millis();
  
  heartBeat(LED_BUILTIN, currentMillis);
  for (auto &button2Coin : button2Coins) if (button2Coin.run() != Released) Serial.println((float)total/100.0,2);
}

Have a nice day and enjoy coding in C++.

Thanks for the code! I use arduino uno not mega. Are A0-A8 analog input? I tried to use it and an error appeared saying A8 was not declared in the scope. I use digital input for ky031.

You can use any digital input pin as you like.

p.s. except pin0 and pin1.

I have tried using your code but I can't get it to work. Is there any way we can fix my initial code?