Hi
Sorry my english is not the best i tried to built a input output interface for an arcade game to handle coin impulses
8 button ins normally pulled up
1 output to an reed reelay
and 6 optocoupler in
1 old display 128 x 32
sometimes the system freeze also when nothing happened no pressed buttons
it is connected to 12V PSU
thank you tom
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <EEPROM.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int kredit = 0;
const int testkreditTasterPin = 11;
const int d2TasterPin = 2;
const int d3TasterPin = 3;
const int d4TasterPin = 4;
const int d5TasterPin = 5;
const int d6TasterPin = 6;
const int d7TasterPin = 7;
const int buttonPin10 = 10;
const int outputPin = 12;
unsigned long testkreditTasterPressTime = 0;
unsigned long d2TasterPressTime = 0;
unsigned long d3TasterPressTime = 0;
unsigned long d4TasterPressTime = 0;
unsigned long d5TasterPressTime = 0;
unsigned long d6TasterPressTime = 0;
unsigned long d7TasterPressTime = 0;
bool increaseCredit = false;
bool resetCredit = false;
const unsigned long debounceInterval = 60; // Entprellungsintervall für alle Taster (in Millisekunden) vorher 80
const unsigned long resetDuration = 5000; // Dauer für den Reset nach Neustart (in Millisekunden)
bool button10Pressed = false;
unsigned long outputStartTime = 0;
bool d3LastState = HIGH;
unsigned long lastD3ChangeTime = 0;
bool d2LastState = HIGH; // Hält den vorherigen Zustand von D2.
bool d4LastState = HIGH;
bool d5LastState = HIGH;
bool d6LastState = HIGH;
bool d7LastState = HIGH;
// Blink-Funktion für die interne LED
void blinkInternalLED() {
digitalWrite(LED_BUILTIN, HIGH);
delay(10);
digitalWrite(LED_BUILTIN, LOW);
}
void writeKreditToEEPROM() {
EEPROM.update(0, highByte(kredit));
EEPROM.update(1, lowByte(kredit));
}
void readKreditFromEEPROM() {
byte highByteValue = EEPROM.read(0);
byte lowByteValue = EEPROM.read(1);
kredit = word(highByteValue, lowByteValue);
}
void setup() {
Wire.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextSize(4);
display.setTextColor(SSD1306_WHITE);
pinMode(outputPin, OUTPUT);
pinMode(testkreditTasterPin, INPUT_PULLUP);
pinMode(d2TasterPin, INPUT_PULLUP);
pinMode(d3TasterPin, INPUT_PULLUP);
pinMode(d4TasterPin, INPUT_PULLUP);
pinMode(d5TasterPin, INPUT_PULLUP);
pinMode(d6TasterPin, INPUT_PULLUP);
pinMode(d7TasterPin, INPUT_PULLUP);
pinMode(buttonPin10, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT); // Pin für die interne LED
display.display();
delay(2000);
kredit = 0;
delay(500);
readKreditFromEEPROM();
delay(500);
}
void loop() {
int testkreditTasterState = digitalRead(testkreditTasterPin);
int d2TasterState = digitalRead(d2TasterPin);
int d3TasterState = digitalRead(d3TasterPin);
int d4TasterState = digitalRead(d4TasterPin);
int d5TasterState = digitalRead(d5TasterPin);
int d6TasterState = digitalRead(d6TasterPin);
int d7TasterState = digitalRead(d7TasterPin);
int buttonState10 = digitalRead(buttonPin10);
unsigned long currentTime = millis();
display.clearDisplay();
if (kredit == 0) {
static int scrollPosition = SCREEN_WIDTH;
static unsigned long scrollTime = currentTime;
if (currentTime - scrollTime >= 1) {
scrollPosition--;
scrollTime = currentTime;
}
if (scrollPosition < -(15 * 26)) {
scrollPosition = SCREEN_WIDTH;
}
display.setCursor(scrollPosition, 0);
display.setTextSize(4);
display.setTextColor(SSD1306_WHITE);
display.print("Muenzen einwerfen ");
} else {
display.setTextSize(4);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 5);
display.print("$ ");
display.print(kredit);
}
display.display();
if (testkreditTasterState == LOW) {
if (testkreditTasterPressTime == 0) {
testkreditTasterPressTime = currentTime;
}
if (currentTime - testkreditTasterPressTime >= resetDuration) {
if (!resetCredit) {
kredit = 0;
resetCredit = true;
increaseCredit = false;
writeKreditToEEPROM();
}
} else {
increaseCredit = true;
}
} else {
if (testkreditTasterPressTime != 0) {
if (currentTime - testkreditTasterPressTime >= 50 && increaseCredit) {
kredit++;
writeKreditToEEPROM();
blinkInternalLED(); // Blinken nach jedem erfolgreichen Impuls
}
testkreditTasterPressTime = 0;
increaseCredit = false;
resetCredit = false;
}
}
if (d2TasterState == LOW && d2TasterState != d2LastState) {
if (currentTime - d2TasterPressTime >= debounceInterval) {
kredit += 1; // CH1
writeKreditToEEPROM();
blinkInternalLED();
d2TasterPressTime = 0;
}
}
if (d3TasterState == LOW && d3TasterState != d3LastState) {
if (currentTime - d3TasterPressTime >= debounceInterval) {
kredit += 1; // CH2
writeKreditToEEPROM();
blinkInternalLED();
d3TasterPressTime = 0;
}
}
if (d4TasterState == LOW && d4TasterState != d4LastState) {
if (currentTime - d4TasterPressTime >= debounceInterval) {
kredit += 1; // CH3
writeKreditToEEPROM();
blinkInternalLED();
d4TasterPressTime = 0;
}
}
if (d5TasterState == LOW && d5TasterState != d5LastState) {
if (currentTime - d5TasterPressTime >= debounceInterval) {
kredit += 2; // CH4
writeKreditToEEPROM();
blinkInternalLED();
d5TasterPressTime = 0;
}
}
if (d6TasterState == LOW && d6TasterState != d6LastState) {
if (currentTime - d6TasterPressTime >= debounceInterval) {
kredit += 4; // CH5
writeKreditToEEPROM();
blinkInternalLED();
d6TasterPressTime = 0;
}
}
if (d7TasterState == LOW && d7TasterState != d7LastState) {
if (currentTime - d7TasterPressTime >= debounceInterval) {
kredit += 1; // CH6
writeKreditToEEPROM();
blinkInternalLED();
d7TasterPressTime = 0;
}
}
d2LastState = d2TasterState;
d3LastState = d3TasterState;
d4LastState = d4TasterState;
d5LastState = d5TasterState;
d6LastState = d6TasterState;
d7LastState = d7TasterState;
if (buttonState10 == LOW) {
if (!button10Pressed) {
button10Pressed = true;
}
} else {
if (button10Pressed) {
if (kredit > 0) {
kredit--;
digitalWrite(outputPin, HIGH);
outputStartTime = currentTime;
writeKreditToEEPROM();
blinkInternalLED();
}
button10Pressed = false;
}
}
if (currentTime - outputStartTime >= 100) {
digitalWrite(outputPin, LOW);
}
}





