my code is faulty

#include <Servo.h>

Servo servo1;
Servo servo2;

int pos1=0;
int pos2=0;
int i=0;
int j=0;
int k=0;
int temp;
int temp2=0;
int count=0;
int m=0;

int pos1Array[100];
int pos2Array[100];
int sensorArray[100];
int siraArray[100];

void setup()
{
  Serial.begin(9600);
  servo1.attach(9);
  servo2.attach(10);
}

void loop()
{
  for( pos1=20 ; pos1<=200 ; pos1=pos1+20 ) //1.servo moving a step
  {
    servo1.write(pos1); // pos1, 1.servo'nun pozisyon de?erini tutar
    delay(200);
   
      for(pos2=20 ; pos2<180 ; pos2=pos2+20 ) // 2.servo moving a step 
    {
      servo2.write(pos2); 
      delay(200);
      
      int sensorVal=analogRead(A0); 
      delay(200);
      
        if(sensorVal<50) 
        {
          Serial.println(sensorVal);
          Serial.println("fire detected"); 
         
           pos1Array[k]=pos1;
           pos2Array[k]=pos2;
           sensorArray[k]=sensorVal; 
           k=k+1;
          
        }
    }
   }    
	
	for(i=0;i<k;i++){
		temp=1025;
		for(j=0;j<100;j++){
			if(sensorArray[j]>temp2&&sensorArray[j]<=temp){
				temp=sensorArray[j];
			    count=j;
			}
		}
		temp2=temp;
		siraArray[i]=count;
	}
	for(int i=0;i<100;i++){
		Serial.print(siraArray[i]); 
                Serial.print(" ");
        }
        Serial.println();
        delay(10000);
	
         
         for(i=0;i<100;i++){
         
         m=siraArray[i];
         if(m!=0){
           
           pos1=pos1Array[m];
           pos2=pos2Array[m];
         
         servo1.write(pos1);
         Serial.println(pos1);
         delay(2000);
         servo2.write(pos2);
         Serial.println(pos2);
         delay(2000);
         }
      
         
       }
   memset(pos1Array,0,sizeof(pos1Array));
   memset(pos2Array,0,sizeof(pos2Array));
   memset(sensorArray,0,sizeof(sensorArray));
   memset(siraArray,0,sizeof(siraArray)); 
}

(code tags added by moderator)

my project is fire detected , i use fire sensor and two servo motors .

but my code is not work . Arduino when first try is working correctly . But code does not work correctly when trying a second time . Array values is faulty.

How can i fix it ?

Start by clicking the MODIFY button and highlight your code and click the "#" CODE TAGS toolbutton in the second row just to the left of the quote button and then click SAVE.

Hello cgdsdkc

One place to start looking is this part of your code, and what values k takes on as the program runs.

           pos1Array[k]=pos1;
           pos2Array[k]=pos2;
           sensorArray[k]=sensorVal; 
           k=k+1;

In particular, each time you go round the main loop(), what value do you want k to start at? Where in your program are you setting it to this value? How do you make sure that k will not get bigger than the size of the arrays?

Regards

Ray

int pos1=0;
int pos2=0;
int i=0;
int j=0;
int k=0;
int temp;
int temp2=0;
int count=0;
int m=0;

i fixed this problem.

the problem was solved when you define these parameters in loop() function .

k parameter values ??can not be more positions value.

#include <Servo.h>

Servo servo1;
Servo servo2;

int pos1=0;
int pos2=0;
int i=0;
int j=0;
int k=1;
int temp;
int temp2=0;
int count=0;
int m=0;

int pos1Array[100];
int pos2Array[100];
int sensorArray[100];
int siraArray[100];

void setup()
{
  Serial.begin(9600);
  servo1.attach(9);
  servo2.attach(10);
}

void loop()
{ 
  pos1=0;
  pos2=0;
  i=0;
  j=0;
  k=1;
  temp2=0;
  count=0;
  m=0;

  for( pos1=20 ; pos1<=160 ; pos1=pos1+20 ) 
  {  
    servo1.write(pos1); 
    delay(200);
   
      for(pos2=40 ; pos2<140 ; pos2=pos2+20 ) 
    {
      servo2.write(pos2); 
      delay(200);
      
      int sensorVal=analogRead(A0); 
      delay(200);
      
        if(sensorVal<50) 
        {
          Serial.println(sensorVal);
          Serial.println("ates bulundu");
         
           pos1Array[k]=pos1;
           pos2Array[k]=pos2;
           sensorArray[k]=sensorVal; 
           k=k+1;
        }
    }
   }    
	
	for(i=1;i<k;i++){
		temp=1025;
		for(j=1;j<k;j++){
			if(sensorArray[j]>temp2&&sensorArray[j]<=temp){
				temp=sensorArray[j];
			    count=j;
			}
		}
		temp2=temp;
		siraArray[i]=count;
	}
	for(int i=1;i<k;i++){
		Serial.print(siraArray[i]); 
                Serial.print(" ");
        }
        Serial.println();
        delay(3000);
	
         
         for(i=0;i<100;i++){
         
         m=siraArray[i];
         if(m!=0){
           
           pos1=pos1Array[m];
           pos2=pos2Array[m];
         
         servo1.write(pos1);
         Serial.println(pos1);
         delay(2000);
         servo2.write(pos2);
         Serial.println(pos2);
         delay(2000);
         }
       
         
       }
   memset(pos1Array,0,sizeof(pos1Array));
   memset(pos2Array,0,sizeof(pos2Array));
   memset(sensorArray,0,sizeof(sensorArray));
   memset(siraArray,0,sizeof(siraArray)); 
}

this is true code , it work correctly .

if(sensorArray[j]>temp2&&sensorArray[j]<=temp){

Isyourspacekeybroken?