How are the arduino coded boards supposed to operate when the button is pushed ?

I have an Adafruit ESP8266 Feather Huzzah with a complicated and lengthy sketch that I have been given to make work.
It might be my imagination but the sketch seems to work properly when I do an Update.
However when I press the reset button it does crazy things.

So my question is:
Does pressing the rst button act in the same way as an upload ?

thanks

you should post the code -- the "" top left

the difference between a power up and reset is at power up ram is random, but after a reset, uninitialized variables will maintain their values from the previous execution of the program result in different behavior

One possible difference, but you have to check the board to see if the ESP reset line is shared with Arduino reset line. In that case, with the reset, the ESP is restarted... with an upload, it is not restarted - it will be in whatever state it was before the upload.

So, if I press the rst button where does the sketch start over ? Re posting the sketch here, it is much too large and exceeds the 9000 max.

How do I guarantee that all the variables are set to what's expected at sketch startup ? IE, as if it were a new board. Will the sketch start at setup(); ??

You can always be confident that everything in setup() will run at boot time, no matter what the cause of the boot. The processor is set up to reset on power up, or on application of a ground to the reset pin. There is no difference between those two, as far as what gets executed at boot time. In the Arduino environment, setup() is always called at boot time, after the hidden function init(), which prepares some system things like registers, timers and so on.

It's perfectly okay to post a sketch here, over 9000 characters. Just add it as an attachment. But it's highly unlikely that anything in your sketch is causing the problem.

if a variable is statically initialized to a value

int a = 33;

the .elf file contains a section (.data) that contains all the initialized variables and their values. how are those values initialized after a reset?

aarg: It's perfectly okay to post a sketch here, over 9000 characters. Just add it as an attachment. But it's highly unlikely that anything in your sketch is causing the problem.

It's not the main sketch, it's the 15 or so .cpp and .h files that are the issue. I can provide a link, and have done so, to the original code.

the .elf file contains a section (.data) that contains all the initialized variables and their values. how are those values initialized after a reset?

Why would you expect (or want) it to be initialised to anything but 33 ?

hextejas: It's not the main sketch, it's the 15 or so .cpp and .h files that are the issue. I can provide a link, and have done so, to the original code.

Interested in this as I had no clue what an .elf file is.

@hextejas, if you select File:Preferences, and turn on Verbose outputs, you can see the files that get created:

Compiling core...
Using precompiled core
Linking everything together...
"C:\Arduino 1.8.2\hardware\tools\avr/bin/avr-gcc" -w -Os -g -flto -fuse-linker-plugin -Wl,--gc-sections -mmcu=atmega1284p  -o "C:\Users\CROSSR~1.CRO\AppData\Local\Temp\arduino_build_839433/Avi_45_digit_code_R1.ino.elf" 

"C:\Users\CROSSR~1.CRO\AppData\Local\Temp\arduino_build_839433\sketch\Avi_45_digit_code_R1.ino.cpp.o" "C:\Users\CROSSR~1.CRO\AppData\Local\Temp\arduino_build_839433\libraries\SPI\SPI.cpp.o" "C:\Users\CROSSR~1.CRO\AppData\Local\Temp\arduino_build_839433\libraries\Keypad\Keypad.cpp.o" "C:\Users\CROSSR~1.CRO\AppData\Local\Temp\arduino_build_839433\libraries\Keypad\utility\Key.cpp.o" "C:\Users\CROSSR~1.CRO\AppData\Local\Temp\arduino_build_839433/..\arduino_cache_520795\core\core_mighty-1284p-1.6.3_avr_bobuino_9d6a88e538e53c6c477754e194653841.a" "-LC:\Users\CROSSR~1.CRO\AppData\Local\Temp\arduino_build_839433" -lm
"C:\Arduino 1.8.2\hardware\tools\avr/bin/avr-objcopy" -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0  "C:\Users\CROSSR~1.CRO\AppData\Local\Temp\arduino_build_839433/Avi_45_digit_code_R1.ino.elf" "C:\Users\CROSSR~1.CRO\AppData\Local\Temp\arduino_build_839433/Avi_45_digit_code_R1.ino.eep"
"C:\Arduino 1.8.2\hardware\tools\avr/bin/avr-objcopy" -O ihex -R .eeprom  "C:\Users\CROSSR~1.CRO\AppData\Local\Temp\arduino_build_839433/Avi_45_digit_code_R1.ino.elf" "C:\Users\CROSSR~1.CRO\AppData\Local\Temp\arduino_build_839433/Avi_45_digit_code_R1.ino.hex"
Using library SPI at version 1.0 in folder: C:\Arduino 1.8.2\hardware\arduino\avr\libraries\SPI 
Using library Keypad in folder: C:\Arduino 1.8.2\libraries\Keypad (legacy)
Sketch uses 13096 bytes (10%) of program storage space. Maximum is 130048 bytes.
Global variables use 676 bytes (4%) of dynamic memory, leaving 15708 bytes for local variables. Maximum is 16384 bytes.
C:\Arduino 1.8.2\hardware\tools\avr/bin/avrdude -CC:\Arduino 1.8.2\hardware\tools\avr/etc/avrdude.conf -v -patmega1284p -carduino -PCOM48 -b115200 -D -Uflash:w:C:\Users\CROSSR~1.CRO\AppData\Local\Temp\arduino_build_839433/Avi_45_digit_code_R1.ino.hex:i

In this example, you can see this file at the end of the first line
“C:\Users\CROSSR~1.CRO\AppData\Local\Temp\arduino_build_839433/Avi_45_digit_code_R1.ino.elf”
(it’s actually the middle of a line, I added a blank line after it to make it easier to see)
(the folder might be Hidden, you’ll have to unhide it.
Or if you go into File:Preferences, and click on the path for your Preferences, you can navigate from there to get to that Temp folder)
Here are the first 3 lines from the file above, I opened the file with Notepad++, it’snot all that helpful that I can see

ELF          S        4   hD 3   4    (      ”           š2  š2           .3   € š2  Ž   Ž            ¼3  Ž€ Ž€             ”  ”Ê ”Ê ”Ê ”Ê ”Ê ”Ê ”Ê ”Ê ”Ê ”Ê ”Ê ”Ê ”Ê ”Ê ”Ê ”Ê ”Ê ”K”Ê ”;””Ê ”Ê ”Ê ”Ê ”Ê ”Ê ”甽”Ê ”Ê ”Ê ”Ê ”Ê è
ÃpÊw$Ñ~*ׄ1ÞŒÆ
a

And part of what it looks like in Notepad++

Thank you Crossroads, I am going to see if I can read it. I am always leery of relying on files in a /tmp folder or with a .tmp extension. To me they are temporary work files and likely to get deleted.

UKHeliBob: Why would you expect (or want) it to be initialised to anything but 33 ?

if the memory location corresponding the the variable is initialized to the value when the .data section is loaded, after a reset, the .data section is not re-loaded, code just executes from the beginning.

is this different in C++ with constructors than with C?

is this different in C++ with constructors than with C?

Pressing the reset button causes a hardware reset irrespective of the program running at the time or the language that it was written in.

If I restart my program then that is what I expect it to do rather than leaving possibly junk values in memory.

UKHeliBob: Pressing the reset button causes a hardware reset irrespective of the program running at the time or the language that it was written in.

of course. a reset restarts the program written in any language

UKHeliBob: If I restart my program then that is what I expect it to do rather than leaving possibly junk values in memory.

is a statically initialized variable initialized by the program or the load process?

gcjr: is a statically initialized variable initialized by the program or the load process?

It's initialized by the hidden init() function that runs when the C program environment is launched, before setup().

aarg:
It’s initialized by the hidden init() function that runs when the C program environment is launched, before setup().

since a developer on unix/windows/laptop writes his own main(), would the developer be required to write code to initialize static variables?

here’s a sketch that has two global variables, cnt and cpy. cnt is incremented with each button press and both the values of cpy and cnt printed. cpy is never initialized.

// recognize multiple button presses; tgl LED when pressed

byte butPin = A1;
byte ledPin = 10;

byte butLst;

int  cnt = 123;
int  cpy;

// -----------------------------------------------------------------------------
void setup (void)
{
    Serial.begin (115200);
    Serial.println ("static init");

    digitalWrite (ledPin, HIGH);
    pinMode      (ledPin, OUTPUT);

    pinMode      (butPin, INPUT_PULLUP);
    butLst      = digitalRead (butPin);
}

// -----------------------------------------------------------------------------
void loop (void)
{
    byte but = digitalRead (butPin);

    if (butLst != but)  {
        butLst = but;

        if (LOW == but)  {     // button pressed
            digitalWrite (ledPin, ! digitalRead (ledPin));

            Serial.print (" cpy ");
            Serial.print (cpy);
            Serial.print (", cnt ");
            Serial.println (cnt++);
        }

        delay (10);         // debounce
    }
}

and here’s a main() copied and modified from Arduino/hardware/arduino/avr/cores/arduino/main.cpp that copies the value of cnt to cpy on the 1st line of main() before init() is called

The value of cpy is 0 is that line is commented out. presumably uninitialized data is initialized to zero by the same mechanism statically initialized values are set.

/*
  main.cpp - Main loop for Arduino sketches
  Copyright (c) 2005-2013 Arduino Team.  All right reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <Arduino.h>

// Declared weak in Arduino.h to allow user redefinitions.
int atexit(void (* /*func*/ )()) { return 0; }

// Weak empty variant initialization function.
// May be redefined by variant files.
void initVariant() __attribute__((weak));
void initVariant() { }

void setupUSB() __attribute__((weak));
void setupUSB() { }

extern int cnt;
extern int cpy;

int main(void)
{
    cpy = cnt;
 init();

 initVariant();

#if defined(USBCON)
 USBDevice.attach();
#endif
 
 setup();
    
 for (;;) {
 loop();
 if (serialEventRun) serialEventRun();
 }
        
 return 0;
}

these are the results. “static init” is printed in setup(). after a reset, “static init” is printed again, but the value of cnt must have been already reinitialized at that time, before main() was invoked

static init
 cpy 123, cnt 123
 cpy 123, cnt 124
 cpy 123, cnt 125
 cpy 123, cnt 126
 cpy 123, cnt 127
static init
 cpy 123, cnt 123
 cpy 123, cnt 124
 cpy 123, cnt 125
 cpy 123, cnt 126
 cpy 123, cnt 127

C/C++ demands that all uninitialized global variables be set to 0. All global variables must be initialized before main() is called. How else could it be done?

If look at the schematic for the Adafruit ESP8266 Feather Huzzah, you'll see that the RESET line that's actuated by the reset button is also actuated by the SiLabs (USB <--> Serial) Chip. So, it's the same line that gets asserted by the download tool after new code is loaded.

Thus, a reset after download is probably indistinguishable from a reset due to button press. I'm guessing OP's problem has nothing to do with reset source and everything to do with flaky code.

is a statically initialized variable initialized by the program or the load process?

It’s initialized by the hidden init() function that runs when the C program environment is launched, before setup().

since a developer on unix/windows/laptop writes his own main(), would the developer be required to write code to initialize static variables?

Initialized variables are initialized, and uninitialized variables are zeroed, but the C startup code that executes BEFORE main gets called. (not “init()”, which is an Arduino Core function that initializes the peripherals appropriate to the Arduino environment.)
It’s possible to omit the standard C startup code, requiring that the programmer explicitly do the initialization themselves, but that usually requires pretty extraordinary efforts, and should NOT be the case for Arduino sketches.

The RESET switch should cause all the same initialization that happen at power-up.

westfw: Initialized variables are initialized, and uninitialized variables are zeroed, but the C startup code that executes BEFORE main gets called.

what is that code (or function name)? i couldn't find it under the arduino build directory. it could be a generic function driven by a table

it must be program specific because of the initialized values.

here's the list of symbols (nm) for the code I posted above. is that initialization function "__init"

000000ee t .do_clear_bss_loop
000000f0 t .do_clear_bss_start
0080013a b Serial
00000690 t _GLOBAL__sub_I___vector_18
         w _Z11serialEventv
000002f2 t _Z14serialEventRunv
000001ca t _Z17Serial0_availablev
000001de t _ZN14HardwareSerial17_tx_udr_empty_irqEv
00000196 t _ZN14HardwareSerial4peekEv
0000016e t _ZN14HardwareSerial4readEv
000002ae t _ZN14HardwareSerial5flushEv
00000220 t _ZN14HardwareSerial5writeEh
000001b2 t _ZN14HardwareSerial9availableEv
00000478 t _ZN5Print11printNumberEmh.constprop.9
000004fa t _ZN5Print5printEli.part.2.constprop.7
00000454 t _ZN5Print5writeEPKc.constprop.13
00000118 t _ZN5Print5writeEPKhj
00800102 d _ZTV14HardwareSerial
00000003 W __FUSE_REGION_LENGTH__
0000003e a __SP_H__
0000003d a __SP_L__
0000003f a __SREG__
00000114 T __bad_interrupt
008001d7 B __bss_end
0080012e B __bss_start
000000c4 T __ctors_end
000000c2 T __ctors_start
00000548 t __cxa_pure_virtual
0080012e D __data_end
000009f0 A __data_load_end
000009c2 A __data_load_start
00800100 D __data_start
000000e6 T __do_clear_bss
000000d0 T __do_copy_data
000000f6 T __do_global_ctors
000000c4 T __dtors_end
000000c4 T __dtors_start
00810000 N __eeprom_end
00000000 W __heap_end
000000c4 W __init
000008ff W __stack
000009c0 t __stop_program
000009a8 T __tablejump2__
00000000 a __tmp_reg__
00000068 T __trampolines_end
00000068 T __trampolines_start
00000964 T __udivmodsi4
0000098a t __udivmodsi4_ep
00000970 t __udivmodsi4_loop
00000114 W __vector_1
00000114 W __vector_10
00000114 W __vector_11
00000114 W __vector_12
00000114 W __vector_13
00000114 W __vector_14
00000114 W __vector_15
000005fc T __vector_16
00000114 W __vector_17
00000598 T __vector_18
0000054c T __vector_19
00000114 W __vector_2
00000114 W __vector_20
00000114 W __vector_21
00000114 W __vector_22
00000114 W __vector_23
00000114 W __vector_24
00000114 W __vector_25
00000114 W __vector_3
00000114 W __vector_4
00000114 W __vector_5
00000114 W __vector_6
00000114 W __vector_7
00000114 W __vector_8
00000114 W __vector_9
00000000 W __vector_default
00000000 T __vectors
00000001 a __zero_reg__
0080012e D _edata
008001d7 N _end
000009c2 T _etext
000009be T _exit
000009b4 T abort
0080012e b butLst
00800100 d cnt
0080012f b cpy
00000358 t digitalRead
000003aa t digitalWrite
0000009a t digital_pin_to_bit_mask_PGM
00000086 t digital_pin_to_port_PGM
000000ae t digital_pin_to_timer_PGM
000009be W exit
000006ea T main
0000040a t micros
0000007c t port_to_input_PGM
00000068 t port_to_mode_PGM
00000072 t port_to_output_PGM
00800131 b timer0_fract
00800132 b timer0_millis
00800136 b timer0_overflow_count
00000306 t turnOffPWM