#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 ?