Why void setup() is called repeatedly?

hi all,

normally i never had problems with void setup() to be called once - like in examples -
and then do several jobs in void loop() on arduino mega 2560

but whith this code setup() seems to be called mutiple :
<
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>
// declare the lcd object for auto i2c address location
hd44780_I2Cexp lcd;

// LCD geometry
const unsigned int LCD_COLS = 20;
const unsigned int LCD_ROWS = 4;
int lcd_status = 0;

/* Substitutes HIGH/LOW for Analog-Reading
#define A_LOW 0
#define A_HIGH 255
*/

#define SHOW_DELAY 6000
#define LOOP_DELAY 20

#define COUNT_ANALOG_PINS 4
#define COUNT_ANALOG_PINS_ALL 16
#define COUNT_DIGITAL_PINS 4
#define COUNT_DIGITAL_PINS_ALL 6

const unsigned int arr_outpins_digital[COUNT_DIGITAL_PINS_ALL] = {25,27,29,
31,33,35};//- pins for Raodways to switch on/off

const String arr_outpins_texte[COUNT_DIGITAL_PINS_ALL] = {"LED FS ST1 rd","LED FS ST2 g","LED FS St2 rd",
"LED FS ST1 g","LED FS Sekt B","LED Sekt A"};

unsigned int max_pins = COUNT_DIGITAL_PINS_ALL;

void setup()
{
//Serial.begin(38400);

int lcd_status = lcd.begin(LCD_COLS, LCD_ROWS);
if (lcd_status) // non zero status means it was unsuccessful
{
// begin() failed so blink error code using the onboard LED if possible
//hd44780::fatalError(status); // does not return
}

//- Print a message to the LCD
lcd.clear();
lcd.print("led_ampel3 for mega");

delay(1000);

//-LED's init u. testen

/*
max_pins = COUNT_ANALOG_PINS_ALL;
for (unsigned int i = 0; i < max_pins; i++)
{
pinMode(arr_outpins_analog[i] , OUTPUT); // sets the digital pin [i] as output
}
*/

max_pins = COUNT_DIGITAL_PINS_ALL;
for (unsigned int i = 0; i < COUNT_DIGITAL_PINS_ALL; i++)
{
pinMode(arr_outpins_digital[i] , OUTPUT); // sets the digital pin [i] as output
}

lcd.clear();
lcd.print("Setup ready");
delay(1000);
}

void loop()
{
max_pins = COUNT_ANALOG_PINS_ALL;
for (unsigned int i = 0; i < max_pins; i++)
{
//analogWrite(arr_outpins_analog[i], A_HIGH);
digitalWrite(arr_outpins_digital[i],HIGH);
delay(LOOP_DELAY); //- Zeit zum Schreiben geben

lcd.clear();
//lcd.print("i : "+String(i)+" PIN "+String(arr_outpins_analog[i])+" "+arr_outpins_texte[i]);
lcd.print("i : "+String(i)+" PIN "+String(arr_outpins_digital[i])+" "+arr_outpins_texte[i]);

delay(SHOW_DELAY);	//- Zeit zum Lesen geben
//analogWrite(arr_outpins_analog[i], A_LOW);
digitalWrite(arr_outpins_digital[i],LOW);

}
delay(LOOP_DELAY);
}
/>
the goal of the sketch is to test the function of several LED-stripes on a model-railway-tracking-desktop

all messages are shown on LCD-Display because Ardiuno per default is NOT connected to pc and IDE , but gets 7,85V from a reguklated supply.

to check what is done the sketch shows on lcd in setup first its name then the message "Setup ready" after initialisations are done
after this the loop() repeats writing to the pins and shows messages on lcd

at this moment - i was really surprised ! - the "Setup ready" appears while running
the loop !

wonder...do i have "tomatoes on the eyes" not seeing eraneous code ?

and I wonder repeatedly, that enclosing code whith backtick odr braces does Not work here !

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Running the setup function repeatedly is often caused by interference being picked up by the processor and causing a reset.

This happens often when there are motors or relays in the system and you have not taken sufficient measures to suppress this interference at source.

Most such measures involve the liberal use of 0.1uF ceramic capacitors and flyback suppression diodes.

I have not looked at your code because it was not posted according to the rules of this forum using code tags.

Have you read the getting the best out of this forum sticky post?

As i wrote, this did Not work !
May be, commenting tags destroy the function of code tags ?

I'll ommit these then send code again..

now,
here is uncommented code in code tags

#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>

hd44780_I2Cexp lcd;

const unsigned int LCD_COLS = 20;
const unsigned int LCD_ROWS = 4;
int lcd_status = 0;

#define SHOW_DELAY 6000
#define LOOP_DELAY 20

#define COUNT_ANALOG_PINS 4
#define COUNT_ANALOG_PINS_ALL 16
#define COUNT_DIGITAL_PINS 4
#define COUNT_DIGITAL_PINS_ALL 6

const unsigned int arr_outpins_digital[COUNT_DIGITAL_PINS_ALL] = {25,27,29,
                                                                  31,33,35};

const String arr_outpins_texte[COUNT_DIGITAL_PINS_ALL] = {"LED FS ST1 rd","LED FS ST2 g","LED FS St2 rd",
													                              	"LED FS ST1 g","LED FS Sekt B","LED Sekt A"};

unsigned int max_pins = COUNT_DIGITAL_PINS_ALL;

void setup() 
{
  int lcd_status = lcd.begin(LCD_COLS, LCD_ROWS);
  if (lcd_status) 
  {
    
    
  }

  
  lcd.clear();
  lcd.print("led_ampel3 for mega");
  
  delay(1000);
  
  max_pins = COUNT_DIGITAL_PINS_ALL;  
  for (unsigned int i = 0; i < COUNT_DIGITAL_PINS_ALL; i++)
  {
    pinMode(arr_outpins_digital[i] , OUTPUT);    // sets the digital pin [i] as output
  }
  
  lcd.clear();
  lcd.print("Setup ready");
  delay(1000);
}

void loop()
{
  max_pins = COUNT_ANALOG_PINS_ALL;
  for (unsigned int i = 0; i < max_pins; i++)
  {
    digitalWrite(arr_outpins_digital[i],HIGH);
    delay(LOOP_DELAY);	//- Zeit zum Schreiben geben

    lcd.clear();
    lcd.print("i : "+String(i)+" PIN "+String(arr_outpins_digital[i])+" "+arr_outpins_texte[i]);
    
    delay(SHOW_DELAY);	//- Zeit zum Lesen geben
    digitalWrite(arr_outpins_digital[i],LOW);
  }  
  delay(LOOP_DELAY);
}

seems that works
but then what is comment for ?

in the posted sketch there's nothing of motor or relays

in other of my sketches setup behaves per default whereas servos and motorbridges are connected..

question : i have often used flatband-cable with 8 - 10 wires bound together over 5-10cm..could this generate the problems ?

Hi,
Do you get anything posted in the LCD display?

But is there any hardware connected?
Please post a schematic of your project?
Please post some images of your project so we can see your component layout?

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

hi tom,

component layout ?

ok, there's a lot of wires coming from pins 22 to 52 of arduino mega board
soldered very near together by the sockets..from there flatband cables are wired to the
leds on desktop..

all messages are send to LCD on desktop as to the in the posted code..

i'll loose the the paricular wires of the flatband cable from each other
then see the result..

in a different thread one told that the particular wires of flatband in a pc
are separated by grounded wires..that could be an additional idea in my case..

I'm sorry..as i have to do my newspaper-job on this weekend i'll continue
next week..

wish a nice weekend to all

Words are not seeing!.

A comment is to tell other people and yourself in six months time why you wrote that line. Note this is a totally different thing than a description of what the line does.
Example of a bad comment

for (int i=0 ; i<5; i++) { // a for loop to run five times

Example of a good comment

for (int i=0 ; i<5; i++) { // turn off all the LEDs

No they don't.

That doesn't stop an Arduino from being reset by interference. EMI (Electro Magnetic Interference) can get into your circuits by two mechanism.

  1. Conduction - where there is a physical connection between your interference source and your Arduino.
  2. Radiation - where the interference is transmitted exactly like a radio through the air.

No this is often a cure for radiation especially if one of more of the signals carried is a ground.

There is nothing in your code that calls setup.
You could be running out of memory which is causing the Arduino to crash and start again.

What is the memory report when you have compiled this?

Put in some print statements in your code to see if you can find a point where it crashes.

Could it be a power issue? How are you powering the LED strips? Could you be exceeding the total output of the Mega?

ATmega 2560 has an absolute limit of 40mA on each pin, and 200mA in total for all pins. Power issues usually result in a reset due to voltage drop when current is exceeded.

Hi,

My thoughts exactly, hence the request for images and schematic.

At this point if @lupus51 does not have a schematic, then it is time he/she drew one.

Tom... :smiley: :+1: :coffee: :australia:

hi ToddL,

sorry, as i am on job most time this weekend have no time to draw..

you are right supposing this, but i saw this in advance :
the stripes are not directly connected to arduino's pin but
powered over a 10k-resistor and npn-transistor like a booster,
which is powered by extra source..

only some pairs of leds (red and green) are - by 1k-resistor - directly connected
with arduino-pins.. as they show the status of a railway-switch, only one led is on
at any time, this is done by sketch..

the servos at the pwm-pins will be driven only once at a time by sketch
if a roadway is selected, all commands come sequently, never more than one item needs power at a time..
so i guess arduino cant be overdriven..

the over-all current is 30-50ma inclusive lcd without any ttp223-sensor is on.

with these sensors touched the over-all current rises up..
that is a point i'll have to deal with..

at now the output-pins are connected direcly with arduino-pins..
may be thats wrong ? guess i should insert resistors - may be 470 Ohm ? - there..

Without a schematic you are lost and so are we... might as well randomly connect things.

How can you find time to do 1/2 a job and expect working results.

Take the time and draw out a schematic... even a napkin would be better than nothing.

Good luck

:smiley_cat:

1 Like

If you're powering servos from the Arduino they will be pulling current all the time, not just when moving.

this is Not the case, as you see in this schema

hope, you understand the schema

servos are connected to arduino ONLY by their control-wire

maybe, the 5V-switching-regulator has too much to provide ?

the current measuered was under 30mA without touching sensors,
otherwise current rises up for milliseconds not easy to estimate..

I hope you have the servo power supply gnd connected to the mega gnd?
This where a proper circuit diagram would help.
Forget a bout graphics app, draw by hand and post an image.
Show power supplies, component names and pin labels.

Tom..... :smiley: :+1: :coffee: :australia:

all drawed lines mean 2 wires as electrical connection, and all external components
are connected on Minus wires..

at least by drawing this it helped me to overthink and you were helpful,
thanks a lot !

hi,
proceeding analyzing setup-repeat ..

analyzing power supply with oszillograph and several modules to get 15V DC
for further step-down regulators to 8V for Arduino mega and 5,1V for LCD and several LED's
found one module which deliveres flat line on oszi, no scatter or other stuff

setup repeats anyways

reducing code to nearly no function
setup Not repeated !

so i guess the code and used space are too much for arduino mega !?
at compiling the sketch scpaces seem enough values at 45-50% shown
what then is the problem ?

now have reduced sketch in a detailed way to have a small set of funtion
(removing managing of power for roadways, only managing sensors , led's and
code-functions for switches)

if this will succeed, the first solution will be to switch the power for roadways manually,ok at first..
else ?

have i to move solving with another type of arduino - which has more space with same or more amount of pins - or even rasperry or anything else ?

further i thought on EEPROM, but from the themes here it seems for me EEPROM - exchange is more like backup, one can't use values directly from it without putting them back in arduinos space ?

meanwhile after testing with reduced but functional code ..
setup repeats anyways :frowning_face:

So max_pins = 16. Then in the for loop...

BUT...

this array only has 6 elements.

You are reading beyond the bounds of the array so likely that is causing the Arduino to reset.

You don't need the max_pins variable... just use the already defined constant.