variable declaration

Hello! I'm having troubles with my code...it's happening something I can't understand. the code runs correctly, but when I declare variables at the top of my sketch as 0, I find them at the end of setup changed in value. for example: int x = 0; void setup{ serial begin serial print x}

on my serial monitor x = 255. this is my problem... any hints are welcome! thanks!

Are you certain that example compiles?

here is a part of the the code
it compiles and show the error.(probably mine)

#include <EEPROM.h>
#include "LedControl.h"
unsigned long refreshtimevar = 0;
unsigned long refreshtime = 500;
int var = 0;
byte thousands = 0;
byte hundreds = 0;
byte tens = 0;
byte ones = 0;
LedControl lc=LedControl(12,11,10,1);
volatile int rg = 0;
volatile int zcountkm = 0;
volatile int x = 0;
volatile int z = 0;
volatile int zt = 0;
volatile int zg = 0;
volatile int r = 0;
volatile int rt = 0;
unsigned long speedtime = 0;
unsigned long speedtime2 = 0;
float rwz = 0;
float flcirc = 0;
float flzfront = 0;
float flzrear = 0;
float flnhall = 0;
int intrwz = 0;
float kmh = 0;
byte zrear = 43;
byte zfront = 15;
byte nhall = 4;
int circ = 2000;
byte thoucirc = 0;
byte hundcirc = 0;
byte tenscirc = 0;
byte onescirc = 0;
int nz = 1;
unsigned long debounce = 0;
long nr = 1;
unsigned long rpmtime = 0;
unsigned long rpmtime2 = 0;
unsigned long rpmtime3 = 0;
int rpm = 0;
byte spc = 0;
int rpmled = 3500;
byte tensthousandsled = 0;
byte thousandsled = 0;
byte hundredsled = 0;
byte tensled = 0;
byte onesled = 0;
int volt = 0;
const int numReadings = 50;
int readings[numReadings];
int index = 0;
unsigned long total = 0;
int average = 0;
int totalhmtrip = 0;
int totalkmtrip = 0;
int deckmtrip = 0;
int centkmtrip = 0;
int setmode = 0;
byte runmode = 1;
int spdpos = 0;
int spdkind = 0;
int previousrunmode = 0;
unsigned long showtime = 0;
unsigned long startime1 = 0;
int val1 = 0;
int oldval1 = 0;
unsigned long startime2 =0;
int val2 = 0;
int oldval2 = 0;
unsigned long startime3 = 0;
int val3 = 0;
int oldval3 = 0;
int menu = 0;
int oldmenu = 0;
int val1s = 0;
int oldval1s = 0;
unsigned long startime1s = 0;
int saved = 0;
int savedled = 0;
int stepgear = 0;
int gear = 0;
int lastgear = 0;
unsigned int ratio = 0;
int neutral = 0;
int setgear = 0;
unsigned int previousratio = 0;
int n = 0;
unsigned long showtime2 = 0;
int iwr = 0;
unsigned long learntime = 0;
int testedgear = 1;
int lock = 0;
unsigned long endtime = 0;
int previousstep = 2;
int blk = 0;
int arrayreset = 0;
int reset = 0;
int indrat = 0;
unsigned int minratio[8];
unsigned int maxratio[8];
int mintensthousandsratio[8];
int minthousandsratio[8];
int minhundredsratio[8];
int mintensratio[8];
int minonesratio[8];
int maxtensthousandsratio[8];
int maxthousandsratio[8];
int maxhundredsratio[8];
int maxtensratio[8];
int maxonesratio[8];
int disable = 0;
float hm = 1000;
int inthm = 1000;
int resettrip = 0;
int save = 1;
unsigned long cuttime = 20;
byte bytecuttime = 20;
unsigned long shiftdelay = 0;
unsigned long voidshift = 0;
int pedalval = 0;
unsigned long shiftime = 20;
int cut = 0;
int oldpedalval = 0;
#define rwpin 2
#define rpmpin 3
#define button1 4
#define button2 5
#define cutpin 6
#define led 7
#define button3 8
#define neutralpin 9
void setup()
{ for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0;
  attachInterrupt(0,int1,FALLING);
  attachInterrupt(1,int2,RISING);
  lc.shutdown(0,false);
  lc.setIntensity(0,15);
  lc.clearDisplay(0);
pinMode (rpmpin,INPUT);
pinMode (rwpin,INPUT);
pinMode (button1,INPUT);
pinMode (button2,INPUT);
pinMode (neutralpin,INPUT);
pinMode (button3,INPUT);
pinMode (led,OUTPUT);
pinMode (cutpin,OUTPUT);
totalhmtrip = EEPROM.read(0);
totalkmtrip = EEPROM.read(1);
deckmtrip = EEPROM.read(2);
centkmtrip = EEPROM.read(3);
zrear = EEPROM.read(4);
runmode = EEPROM.read(5);
thoucirc = EEPROM.read(6);
hundcirc = EEPROM.read(7);
tenscirc = EEPROM.read(8);
onescirc = EEPROM.read(9);
spc = EEPROM.read(10);
tensthousandsled = EEPROM.read(11);
thousandsled = EEPROM.read(12);
hundredsled = EEPROM.read(13);
tensled = EEPROM.read(14);
onesled = EEPROM.read(15);
bytecuttime = EEPROM.read(16);
zfront = EEPROM.read(17);
nhall = EEPROM.read(18);
lastgear = EEPROM.read(19);
spdkind = EEPROM.read(20);
spdpos = EEPROM.read(21);
for (n = 0;n < 9;n++){
mintensthousandsratio[n] = EEPROM.read(22 + n);
minthousandsratio[n] = EEPROM.read(30 + n);
minhundredsratio[n] = EEPROM.read(38 + n);
mintensratio[n] = EEPROM.read(46 + n);
minonesratio[n] = EEPROM.read(54 + n);
maxtensthousandsratio[n] = EEPROM.read(62 + n);
maxthousandsratio[n] = EEPROM.read(70 + n);
maxhundredsratio[n] = EEPROM.read(78 + n);
maxtensratio[n] = EEPROM.read(86 + n);
maxonesratio[n] = EEPROM.read(94 + n);
minratio[n] = mintensthousandsratio[n]*10000+minthousandsratio[n]*1000+
minhundredsratio[n]*100+mintensratio[n]*10+minonesratio[n];
maxratio[n] = maxtensthousandsratio[n]*10000+maxthousandsratio[n]*1000+
maxhundredsratio[n]*100+maxtensratio[n]*10+maxonesratio[n];}
rpmled = ((tensthousandsled*10000)+(thousandsled*1000)+(hundredsled*100)+(tensled*10)+(ones));
circ = ((thoucirc * 1000)+(hundcirc * 100)+(tenscirc * 10)+(onescirc));
flcirc = float(circ);
flzrear = float(zrear);
flzfront = float(zfront);
flnhall = float(nhall);
rwz = flcirc / ((flzrear/flzfront)*flnhall);
hm = 100000 / rwz;
inthm = (int)hm;
cuttime = (unsigned long)bytecuttime;
intrwz = (int)rwz;
iwr = circ / intrwz;
  Serial.begin(9600);
    Serial.println(disable);}
    void loop(){}
    void int1(){}
    void int2(){}

here is a part of the the code it compiles

If it is part of the code, it doesn't compile. It doesn't show the error, because you haven't shown us the error. My guess is you're running off the end of an array, but until you post the results, I can't be bothered to look.

There's a useful tool in the IDE called "Auto format" (ctrl-T) - please use it before posting code.

Board?

for (n = 0;n < 9;n++){

Indices go 0…8 in an array declared with 8 elements.

Great that was the problem! thank you very much for solving it!