void setup always run.

hi guys i’m ikhsan from indonesia. so guys i have a problem about my code. the problem is the void setup running more than once ? after the second ‘for it should be serial.print(’’") and the rest. but afte the second ‘for’ the void setup run again. before this problem my program working well. and right know i can’t find out what the mistake is. can you guys help me ? thank you very much i really appreciate your help.

#include <Array.h>
int led = 9;
int ir = 8;
int sensor = A0;
float kadarhb, kadarspo2;
int arrayled[200];
int arrayir[200];

void setup() {
  // put your setup code here, to run once:
  pinMode(led, OUTPUT);  // put your setup code here, to run once:
pinMode(ir, OUTPUT);
pinMode(sensor, INPUT);
Serial.begin(9600);
Serial.println("5");
delay(1000);
Serial.println("4");
delay(1000);
Serial.println("3");
delay(1000);
Serial.println("2");
delay(1000);
Serial.println("1");
delay(1000);
}

void loop() {
  // put your main code here, to run repeatedly:
int  z,z2=0;
  int k=120;
  int k2=50;
  
  for ( int i=0; i<500; i++)
{
  digitalWrite(ir,HIGH);
 digitalWrite(led,LOW);
  delay(10);
if(i>300)
 {
  arrayir[i] = analogRead(sensor);
  Serial.println(arrayir[i]);

  if((arrayir[i] >z) && (arrayir[i]<120))
  {
  z = arrayir[i];
  }
if ((arrayir[i] <= k) && (arrayir[i] > 0))
   {
    k = arrayir[i]; 
   }
  }
  digitalWrite(ir,LOW);
  digitalWrite(led,LOW);
  delay(10);
  }

  for (int j=0; j<500; j++)
{
  digitalWrite(ir,LOW);
  digitalWrite(led,HIGH);
  delay(10);

  if(j>300)
  {
  arrayled[j] = analogRead(sensor); 
 Serial.println(arrayled[j]);
  if((arrayled[j] >z2) && (arrayled[j]<100))
  {
  z2 = arrayled[j];
  }

   if ((arrayled[j] < k2) && (arrayled[j] > 0))
   {
    k2 = arrayled[j]; 
   }
  }
  digitalWrite(ir,LOW);
  digitalWrite(led,LOW);
  delay(10);
}
Serial.println("");
Serial.println("");
Serial.print("nilai maks LED merah = ");
Serial.println(z2);
Serial.print("nilai min LED merah = ");
Serial.println(k2);
Serial.print("nilai maks LED infrared = ");
Serial.println(z);
Serial.print("nilai min LED infrared = ");
Serial.println(k);

float led= z2/k2;
float infra = z/k;


float rasio = led/infra;

if(rasio > 1)
{
  rasio =1;
}

if (rasio < 0.4)
{
rasio=rasio*2;
}

kadarspo2 = 110 -(25*rasio);
Serial.print("rasio =");
Serial.println(rasio);
Serial.print("kadar spo2 =");
Serial.println(kadarspo2);

delay(5000);

}

You're writing off the end of an array, don't do that:

arrayir is 200 elements:

int arrayir[200];

Then you do this:

if(i>300)
 {
  arrayir[i] = analogRead(sensor);

If i>300, arrayir[ i] is referencing something way past the end of the array, hence scribbling over some other block of memory.

Frequently, writing off the end of an array will result in the sketch resetting (or hanging).

DrAzzy:
You're writing off the end of an array, don't do that:

arrayir is 200 elements:

int arrayir[200];

Then you do this:

if(i>300)

{
  arrayir[i] = analogRead(sensor);



If i>300, arrayir[ i] is referencing something way past the end of the array, hence scribbling over some other block of memory. 


Frequently, writing off the end of an array will result in the sketch resetting (or hanging).

thank you very much sir !! hats off ! really appreciate it