Array changes value by itself

Hi guys,
I'm trying to send an array with six integers between two arduinos.
The masters sending part of the code looks like this:

          Wire.beginTransmission(9);
          Wire.write(werte[0]);
          Wire.endTransmission();
          Wire.beginTransmission(9);
          Wire.write(werte[1]);
          Wire.endTransmission();
          Wire.beginTransmission(9);
          Wire.write(werte[2]);
          Wire.endTransmission();
          Wire.beginTransmission(9);
          Wire.write(werte[3]);
          Wire.endTransmission();
          Wire.beginTransmission(9);
          Wire.write(werte[4]);
          Wire.endTransmission();
          Wire.beginTransmission(9);
          Wire.write(werte[5]);
          Wire.endTransmission();

The slaves part for receiving looks like this and I first thought everything works fine because I get the right values printed on the serial monitor. But if I want to use these values of the werte array in the loop they have changed.

int werte[6] = {0,0,0,0,0,0};

void setup() {
 Wire.begin(9);
 Wire.onReceive(receiveEvent); //function who gets called every time something is send
 Serial.begin(9600);

}

void receiveEvent(int bytes){


  werte[x] = Wire.read();
  if (x==0){
    Serial.println(werte[0]);
  }
  if (x==1){
     Serial.println(werte[1]);
  }
  if (x==2){
     Serial.println(werte[2]);
  }
  if (x==3){
     Serial.println(werte[3]);
  }
  if (x==4){
     Serial.println(werte[4]);
  }
  if (x==5){
     Serial.println(werte[5]);
  }

Isn't the value stored global because of the way the function is called? Or am I doing something else entirely wrong?

It is impossible to know what is happening from your snippets. Post complete programs. Also tell us exactly what data is sent and what is received.

...R

werte[x] = Wire.read();
  if (x==0){
    Serial.println(werte[0]);
  }
  if (x==1){
     Serial.println(werte[1]);
  }
  if (x==2){
     Serial.println(werte[2]);
  }
  if (x==3){
     Serial.println(werte[3]);
  }
  if (x==4){
     Serial.println(werte[4]);
  }
  if (x==5){
     Serial.println(werte[5]);
  }

Are you being paid per line?

Well all the code of the Slave:

#include <AFMotor.h> 
#include <Wire.h>
float a,b,c,d,e,f;
int x=0;
int werte[6] = {0,0,0,0,0,0};
AF_Stepper motor(48, 1);
AF_Stepper motor2(48, 2); 

void setup() {
 Wire.begin(9);
 Wire.onReceive(receiveEvent);
 Serial.begin(9600);

motor.setSpeed(100);
motor2.setSpeed(10);
}
void receiveEvent(int bytes){
 // Serial.println(bytes);

  werte[x] = Wire.read();
  if (x==0){
    Serial.println(werte[0]);  //the correct value is printed
  }
  if (x==1){
     Serial.println(werte[1]);  //the correct value is printed
  }
  if (x==2){
     Serial.println(werte[2]);  //the correct value is printed
  }
  if (x==3){
     Serial.println(werte[3]); //the correct value isprinted
  }
  if (x==4){
     Serial.println(werte[4]); //the correct value is printed
  }
  if (x==5){
     Serial.println(werte[5]); //the correct value is printed
  }
 
  
  x++;
  if (x==5){
    x=0;
  }
  }
void loop() {

 
 if((werte[0]+werte[1]+werte[2]+werte[3]+werte[4]+werte[5])>0) {
 
 if (werte[0]>0){
  motor.step(300, BACKWARD, DOUBLE);
  a=(werte[0] - 1) * 100;         //should calculate a delaytime dependent of the value of werte[0]
  Serial.println(a);                    // gives some seemingly random value if the werte[5] held a value,  
                                              // if werte[5] = 0 it prints nothing (does not get into the if condition)        
  Serial.println(werte[0]);         //gives werte [5] if it held a value, if werte[5] = 0 it prints nothin 
  if (a<0) {
    a=0.001;
    }
  delay(a); 
  motor.step(300, FORWARD, DOUBLE);
  }

  motor2.step(200/6, FORWARD, SINGLE);
 if (werte[1]>0){
  motor.step(300, BACKWARD, DOUBLE);
  b=(werte[1]-1)*100;
  if (b<0) {
    b=0.001;
    }
  delay(b); 
  motor.step(300, FORWARD, DOUBLE);
  }

  motor2.step(200/6, FORWARD, SINGLE);
 if (werte[2]>0){
  motor.step(300, BACKWARD, DOUBLE);
  c=(werte[2]-1)*100;
  if (c<0) {
    c=0.001;
    }
  delay(c); 
  motor.step(300, FORWARD, DOUBLE);
  }

  motor2.step(200/6, FORWARD, SINGLE);
 if (werte[3]>0){
  motor.step(300, BACKWARD, DOUBLE);
  d=(werte[3]-1)*100;
  if (d<0) {
    d=0.001;
    }
  delay(d); 
  motor.step(300, FORWARD, DOUBLE);
  }
 
  motor2.step(200/6, FORWARD, SINGLE);
 if (werte[4]>0){
  motor.step(300, BACKWARD, DOUBLE);
  e=(werte[4]-1)*100;
  if (e<0) {
    e=0.001;
    }
  delay(e); 
  motor.step(300, FORWARD, DOUBLE);
  }

  motor2.step(200/6, FORWARD, SINGLE);
 if (werte[5]>0){
  motor.step(300, BACKWARD, DOUBLE);
  f=(werte[5]-1)*100;
  if (f<0) {
    f=0.001;
    }
  delay(f); 
  motor.step(300, FORWARD, DOUBLE);
 }
  motor2.step(200/6, FORWARD, SINGLE);


 werte[0] = 0;
 werte[1] = 0;
 werte[2] = 0;
 werte[3] = 0;
 werte[4] = 0;
 werte[5] = 0;
 }
  
}

The send value of the array are Integers from 0 to 100. The rest of the mastercode ist quite long, and there, the sent werte indexes have the right values.

AWOL:

werte[x] = Wire.read();

if (x==0){
   Serial.println(werte[0]);
 }
 if (x==1){
    Serial.println(werte[1]);
 }
 if (x==2){
    Serial.println(werte[2]);
 }
 if (x==3){
    Serial.println(werte[3]);
 }
 if (x==4){
    Serial.println(werte[4]);
 }
 if (x==5){
    Serial.println(werte[5]);
 }



Are you being paid per line?

Well... no, but as I wrote this I thought like this I could check for ervery value if it was send correctly. (I now im a beginner)

You are aware, aren't you, that Wire.write() expects, and processes, a byte?

PaulS:
You are aware, aren't you, that Wire.write() expects, and processes, a byte?

Yes (at least I think so), but integers from 0 to 100 can be sent as single bytes or not?

Yes (at least I think so), but integers from 0 to 100 can be sent as single bytes or not?

Yes, but why would you use an int (array) to store bytes? Use the most appropriate type, to eliminate issues with using the wrong type.

Another thing Paul was pointing out is the inefficiency of:

werte[x] = Wire.read();
  if (x==0){
    Serial.println(werte[0]);
  }
  if (x==1){
     Serial.println(werte[1]);
  }
  if (x==2){
     Serial.println(werte[2]);
  }
  if (x==3){
     Serial.println(werte[3]);
  }
  if (x==4){
     Serial.println(werte[4]);
  }
  if (x==5){
     Serial.println(werte[5]);
  }

Suppose x does equal 0. Your code then proceeds to do 5 impossible checks on x even though you know it is 0. One way to fix this is a cascading if-else statement block. But a little more thought and you'll see something like:

  werte[x] = Wire.read();
  if (x < (sizeof(werte) / sizeof(werte[0])){  // Is x within the confines of the array?
    Serial.println(werte[x]);                  // Yep, so print it.
  }

can replace the if statement blocks.

Thank you very much Paul and econjack, everything works great now!