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