Arduino Nano crashs randomly

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

Does it improve if you declare this variables outside (before) the if?

Also, are you allowing scrollPosition to get a negative value?

Are you updating the screen too fast?

Posting an annotated schematic showing exactly how you wired it will be a big help. I have a feeling it is a hardware problem.

hallo

i have connected the oled to A4 A5 5V and GND
VIN is at stable 12V

the buttons with 1o K pull-up resistors

??
Are you updating the screen too fast?

where can is et this, a friend told me this old could make ram problems or so

sorry i am a total newbie

and just confused why it is running sometimes 1 hour and then after 2 minutes it freezes without anything

HI,
It sounds like you have to much hardware on the 5V supply and are loading the linear regulator into shutdown as it gets hot.

If you run everything of 5V does the problem occur?

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

What is the 12V PSU?

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

its a little bit chaos but working

A 12 V external impuls will switch the optocouplers and trigger the buttons , they have pull up resistors 10K

display at 5V gnd and A4 A5

1 reed relay is the only output

thank you

it crashes also sometimes when no inputs are pressed already at IDLE

if Kredit value is 0 the crolling text will start
if kredit is more then 0 it will shown at trhe display

Hi,
Thanks for the schematic.
Do you have a DMM?

If so measure the 5V supply from the Nano as see what it reads when your problem occurs.

I highly recommend you get an external 5V supply and run your project of that, or at least all the hardware, including the Nano, from it.

Do you understand that the linear 5V regulator on the Nano can only supply current for the Nano and "some " low power peripherals?

Do you have current limit resistors on all those diode inputs on the opto-couplers?

What is K1, ae you driving a relay directly from D12?

What are A1 and A2?
Where is the 12V supply?

Please some pictures of your project?
So we can see your component layout.

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

If so measure the 5V supply from the Nano as see what it reads when your problem occurs.
could test this tonight

Do you understand that the linear 5V regulator on the Nano can only supply current for the Nano and "some " low power peripherals?
the only output is a reed relay 5V

Do you have current limit resistors on all those diode inputs on the opto-couplers?
the opto couplers have all 330K resistors at the nano side

What is K1, ae you driving a relay directly from D12?
a reed relay 5V

freeze hapens also when in IDLE so no relay problem for me

A1 and A2 are only solder points for an external switch at the pcb

Where is the 12V supply?
what do you mean it is a arcade power supply and connected to regulated 12V

image

Hi,
It would be good to have a complete schematic, including all lights and buttons and what ever C1 to C6 are.

A tip, when you have a lot of gnd and 5V and 12V wiring, simplify your schematic with symbols representing these interconnections.

I notice in your schematic, you do not have a back EMF diode on the relay coil.

Tom.. :grinning: :+1: :coffee: :australia:

I notice in your schematic, you do not have a back EMF diode on the relay coil

the reed relay only sends a credit impulse and i think it has a build in diode

but
the freeze never happens when therelay is switching

C1-C6 are the inpout signals from a 12V coin acceptor

the pcb will regognize the credits, save it and display it

and when i press a button the credit will transfered to the output

Where on your schematic?

Have you monitored the 5V supply.

What are the specs of your reed relay?

What supply does the display run from.

Where are the resistors on your schematic?

Tom... :grinning: :+1: :coffee: :australia:

here are this resistors

its a network resistor
image

this is the relay
https://www.reichelt.at/at/de/reedrelais-5v-1-schliesser-1a-ri-500-ohm-sil-7271-l-5v-p27669.html?PROVID=2807&gclid=CjwKCAiA0syqBhBxEiwAeNx9N6aETICRlk37tLQg13_TL9dysyHt4bSvBxltmZQ4-nXTWJ0mDMV1HBoCcdUQAvD_BwE

Where is the 12V supply?
the 12v comes from a testpoint or the 10 pin connector

Well show them and annotate your diagram use part numbers.
What are the odd red crosshatches with C or CH annotations?

Tom.. :grinning: :+1: :coffee: :coffee: :coffee: :australia:

What are the odd red crosshatches with C or CH annotations?

this are just tespoints or a jumper to connect the 1 output to a pin at the 10-pin connector at the outside

could it be a problem of the oled display?
i also tested with a 7 segment modul before some days and this never happened

i also use a 120 cm long 4 pin cable now for the OLED `?

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