user19475: I am currently making a robot with Arduino Uno, D6T omron thermal sensor (i2c sensor) and some other sensors. My thermal sensor will always update the data in array form. It will display 4 data arrays every 1 second. 
I wish to have my Arduino send out a string "found" over serial when it meet a condition, and thermal sensor will stop updating my data. Thermal sensor and Arduino will just have to wait until the user sends a selection, jump to the selected function, and thermal sensor will start updating data again after that. I am new to coding, any suggestion on how to make my arduino and sensor to wait? Thank you guys! =) 
Attached below is my code. (Arduino Sketch):
#include <Wire.h>
#include <WireExt.h>

#define D6T_addr 0x0A
#define D6T_cmd 0x4C

int rbuf[35];
float tdata[16];
float t_PTAT;

const int S_DATATRANSFER = 0;
const int S_WAIT = 1;
const int S_STOPTRANSFER = 2;
const int S_CONFIRMATION = 3;
const int S_ALLSTOP = 4;

float left_sum, right_sum, center_sum;

//-----------------------------------------------------------------------------

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  Serial.flush();

}

void loop()
{

static int state = S_DATATRANSFER;


    switch(state)
    {

    case S_DATATRANSFER:
        //Calculate temp
        //serialprint temp
        int i;

        if (Serial.available() > 0) 
        {     
                      char inByte = Serial.read();     
                      if (inByte == '1') 
            {  //processing will send one byte to arduino if ready     

                Wire.beginTransmission(D6T_addr);
                Wire.write(D6T_cmd);
                Wire.endTransmission();

            if (WireExt.beginReception(D6T_addr) >= 0) 
                {
                    i = 0;
                    for (i = 0; i < 35; i++) 
                    {
                        rbuf[i] = WireExt.get_byte();
                    }
                    WireExt.endReception();

                    t_PTAT = (rbuf[0]+(rbuf[1]<<8))*0.1;
                    for (i = 0; i < 16; i++) 
                    {
                        tdata[i]=(rbuf[(i*2+2)]+(rbuf[(i*2+3)]<<8))*0.1;
                    }
                }

                right_sum= tdata[0]+ tdata[4] + tdata[8] + tdata[12];
                left_sum= tdata[3] + tdata[7] + tdata[11] + tdata[15];
                center_sum= tdata[5] + tdata[6] + tdata[9] + tdata[10];            
                output_csv();  

               }
                      }
        state = S_WAIT;
        break;

    case S_WAIT:
        if (center_sum > 35*4)
        {
        state = S_STOPTRANSFER;
        }
        else
        {
        state = S_DATATRANSFER;
        }
        break;

    case S_STOPTRANSFER:

        Serial.println("F");
                delay(300);

        state = S_CONFIRMATION;

        break;

    case S_CONFIRMATION:
        while (Serial.available())
        {
         char val = Serial.read();
            if (val == '2')
            {
                state = S_ALLSTOP;
            }
            else if (val == '3')
            {
                state = S_DATATRANSFER; 
                }  
                        else if (val == '1')
                        {
                                state = S_DATATRANSFER;
                        }
                }   

        break;

        case S_ALLSTOP:

        break;


    }
}

void output_csv() 
{
  for (int i = 0; i < 16; i++) 
  {
    Serial.print(tdata[i]);
    if (i < 15) 
    {
      Serial.print(",");
    } 
    else 
    {
      Serial.println();
    }
  }
}

Attached below is my code in processing:
import processing.serial.*;
import controlP5.*;
import javax.swing.JOptionPane.*;
import com.onlylemi.processing.android.capture.*;
ControlP5 cp5;
javax.swing.JOptionPane JOptionPane;

AndroidCamera ac;
PImage img;
Textlabel myTextlabelA, myTextlabelB;
boolean canStart = true;
boolean canInput = false;
boolean Start = false;
boolean Start_camera = false;
String typedText = "Comments: ";
PFont font;
String command = "";
String confirm;
String found;

PImage carImage;
float[] tdata = new float[17]; 
float tptat; // PTAT
String portName; 
//int serialport = 0; 

String buf; 
float colourGreen; //change green in relation to temperature 

int lbTemp = 25; //lowest relative temperature
int hbTemp = 35; //highest relative temperature 
Serial myPort;  // Create object from Serial class


void setup() {
  size(1366,768);
  //background(#FFFFFF);
  background(ControlP5.BLACK);
  //println(Serial.list());
  //portName = Serial.list()[serialport];
  myPort = new Serial(this, "COM15", 9600);
  myPort.clear();
  cp5 = new ControlP5(this);
  ac = new AndroidCamera(720, 480, 30);
  ac.start();

  myTextlabelA = cp5.addTextlabel("label")
                    .setText("GUI of Alive Human Detection System.")
                    .setPosition(120,10)
                    .setColorValue(#FFFAF0)
                    .setFont(createFont("Georgia",20))
                    ;

  cp5.addButton("Start")
     .setPosition(115, 670)
     .setSize(100,30)
     ;

  cp5.addButton("Stop")
     .setPosition(375, 670)
     .setSize(100,30)
     ;
  cp5.addButton("Start Camera")
     .setPosition(799, 670)
     .setSize(100,30)
     ;
}

public void Start(int theValue) {
  println("Initialising...");
  println("Connected.");
  //start displaying
 // carImage = loadImage(".png");
  Start = !Start;
}

public void Stop(int theValue) {
  println("Shuting Down...");
  //finish = loadImage("robot.png");
  //image(finish,30,64);
  myPort.write(0x02);
  Start = !Start;
  //strokeWeight(0);
}

public void Start_camera(int theValue) {
  println("Initialising...");
  println("Connected.");
  //start displaying
 // carImage = loadImage(".png");
  Start_camera = !Start_camera;
}

void draw() 
{
  //if (Start_camera){
  //img = ac.getCameraImage();
  //image(img, 610, 0);
  //}
  if (Start){

    myPort.write('1'); 
    delay(100);
    while (myPort.available() > 0) { 
    delay(300);  
    buf = myPort.readStringUntil('\n');
    if (buf.equals("F")){
    int dialogResult = JOptionPane.showConfirmDialog (null, "Human found? Please Confirm!!", "Alert!", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
    if(dialogResult == JOptionPane.YES_OPTION)
    {
    fill(255,0,0);
    rect(100,670,50,30);
    myPort.write('2');
    }
    else if(dialogResult == JOptionPane.NO_OPTION)
    {
    fill(0,255,0);
    rect(100,670,50,30);
    myPort.write('3');
    }
    }
    else{
    myPort.clear();
    tdata = float(split(buf, ','));  
    for (int i = 0; i <16; i++) { 
    colourGreen = map(tdata[i],lbTemp,hbTemp,255,0);
    fill (255,colourGreen,0);

      if (i == 0)
    { rect(450,64,150,150);}
      else if (i == 1)
    { rect(300,64,150,150);}
      else if (i == 2)
    { rect(150,64,150,150);}
      else if (i == 3)
    { rect(0,64,150,150);}
      else if (i == 4)
    { rect(450,214,150,150);}
      else if (i == 5)
    { rect(300,214,150,150);}
      else if (i == 6)
    { rect(150,214,150,150);}
      else if (i == 7)
    { rect(0,214,150,150);}
      else if (i == 8)
    { rect(450,364,150,150);}
      else if (i == 9)
    { rect(300,364,150,150);}
      else if (i == 10)
    { rect(150,364,150,150);}
      else if (i == 11)
    { rect(0,364,150,150);}
      else if (i == 12)
    { rect(450,514,150,150);}
      else if (i == 13)
    { rect(300,514,150,150);}
      else if (i == 14)
    { rect(150,514,150,150);}
      else if (i == 15)
    { rect(0,514,150,150); }

    if (tdata[i]<5) {fill(255);} else {fill(0);}
      if (i ==0)
      {
      textAlign(CENTER, CENTER);
      textSize(20);
      text(str(tdata[i]),525, 139);}
      else if (i == 1)
      {
      textAlign(CENTER, CENTER);
      textSize(20);
      text(str(tdata[i]),375, 139);
      }
      else if (i == 2)
      {
      textAlign(CENTER, CENTER);
      textSize(20);
      text(str(tdata[i]), 225, 139);
      }
      else if (i == 3)
      {
      textAlign(CENTER, CENTER);
      textSize(20);
      text(str(tdata[i]),75, 139); 
      }
      else if (i == 4)
      {
      textAlign(CENTER, CENTER);
      textSize(20);
      text(str(tdata[i]), 525, 289); 
      }
      else if (i == 5)
      {
      textAlign(CENTER, CENTER);
      textSize(20);
      text(str(tdata[i]), 375, 289);  
      }
      else if (i == 6)
      {
      textAlign(CENTER, CENTER);
      textSize(20);
      text(str(tdata[i]), 225, 289);  
      }
      else if (i == 7)
      {
      textAlign(CENTER, CENTER);
      textSize(20);
      text(str(tdata[i]), 75, 289); 
      }
      else if (i == 8)
      {
      textAlign(CENTER, CENTER);
      textSize(20);
      text(str(tdata[i]), 525, 439);
      }
      else if (i == 9)
      {
      textAlign(CENTER, CENTER);
      textSize(20);
      text(str(tdata[i]), 375, 439);  
      }
      else if (i == 10)
      {
      textAlign(CENTER, CENTER);
      textSize(20);
      text(str(tdata[i]), 225, 439);
      }
      else if (i == 11)
      {
      textAlign(CENTER, CENTER);
      textSize(20);
      text(str(tdata[i]), 75, 439);
      }
      else if (i == 12)
      {
      textAlign(CENTER, CENTER);
      textSize(20);
      text(str(tdata[i]), 525, 592);  
      }
      else if (i == 13)
      {
      textAlign(CENTER, CENTER);
      textSize(20);
      text(str(tdata[i]), 375, 592); 
      }
      else if (i == 14)
      {
      textAlign(CENTER, CENTER);
      textSize(20);
      text(str(tdata[i]), 225, 592);
      }
      else if (i == 15)
      {
      textAlign(CENTER, CENTER);
      textSize(20);
      text(str(tdata[i]), 75, 592);  
      }
    }
    delay(350);  
  }
    }
    }
}


user20245: Implement a state machine first in your code for each state:
1) Sending i2c data.
2) Not sending i2c data.
And identify the conditions necessary to switch states:
1 to 2 when condition meet.
2 to 1 when command received.
Then drop your code into the state machine's code.
Read about state machines here and here.

19475: <p>I am currently making a robot with Arduino Uno, D6T omron thermal sensor (i2c sensor) and some other sensors. My thermal sensor will always update the data in array form. It will display 4 data arrays every 1 second. </p>

<p>I wish to have my Arduino send out a string "found" over serial when it meet a condition, and thermal sensor will stop updating my data. Thermal sensor and Arduino will just have to wait until the user sends a selection, jump to the selected function, and thermal sensor will start updating data again after that. I am new to coding, any suggestion on how to make my arduino and sensor to wait? Thank you guys! =) </p>

<p>Attached below is my code. (Arduino Sketch):</p>

<pre><code>#include &lt;Wire.h&gt;
#include &lt;WireExt.h&gt;

#define D6T_addr 0x0A
#define D6T_cmd 0x4C

int rbuf[35];
float tdata[16];
float t_PTAT;

const int S_DATATRANSFER = 0;
const int S_WAIT = 1;
const int S_STOPTRANSFER = 2;
const int S_CONFIRMATION = 3;
const int S_ALLSTOP = 4;

float left_sum, right_sum, center_sum;

//-----------------------------------------------------------------------------

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  Serial.flush();

}

void loop()
{

static int state = S_DATATRANSFER;


    switch(state)
    {

    case S_DATATRANSFER:
        //Calculate temp
        //serialprint temp
        int i;

        if (Serial.available() &gt; 0) 
        {     
                      char inByte = Serial.read();     
                      if (inByte == '1') 
            {  //processing will send one byte to arduino if ready     

                Wire.beginTransmission(D6T_addr);
                Wire.write(D6T_cmd);
                Wire.endTransmission();

            if (WireExt.beginReception(D6T_addr) &gt;= 0) 
                {
                    i = 0;
                    for (i = 0; i &lt; 35; i++) 
                    {
                        rbuf[i] = WireExt.get_byte();
                    }
                    WireExt.endReception();

                    t_PTAT = (rbuf[0]+(rbuf[1]&lt;&lt;8))*0.1;
                    for (i = 0; i &lt; 16; i++) 
                    {
                        tdata[i]=(rbuf[(i*2+2)]+(rbuf[(i*2+3)]&lt;&lt;8))*0.1;
                    }
                }

                right_sum= tdata[0]+ tdata[4] + tdata[8] + tdata[12];
                left_sum= tdata[3] + tdata[7] + tdata[11] + tdata[15];
                center_sum= tdata[5] + tdata[6] + tdata[9] + tdata[10];            
                output_csv();  

               }
                      }
        state = S_WAIT;
        break;

    case S_WAIT:
        if (center_sum &gt; 35*4)
        {
        state = S_STOPTRANSFER;
        }
        else
        {
        state = S_DATATRANSFER;
        }
        break;

    case S_STOPTRANSFER:

        Serial.println("F");
                delay(300);

        state = S_CONFIRMATION;

        break;

    case S_CONFIRMATION:
        while (Serial.available())
        {
         char val = Serial.read();
            if (val == '2')
            {
                state = S_ALLSTOP;
            }
            else if (val == '3')
            {
                state = S_DATATRANSFER; 
                }  
                        else if (val == '1')
                        {
                                state = S_DATATRANSFER;
                        }
                }   

        break;

        case S_ALLSTOP:

        break;


    }
}

void output_csv() 
{
  for (int i = 0; i &lt; 16; i++) 
  {
    Serial.print(tdata[i]);
    if (i &lt; 15) 
    {
      Serial.print(",");
    } 
    else 
    {
      Serial.println();
    }
  }
}
</code></pre>

<p>Attached below is my code in processing:</p>

<pre><code>import processing.serial.*;
import controlP5.*;
import javax.swing.JOptionPane.*;
import com.onlylemi.processing.android.capture.*;
ControlP5 cp5;
javax.swing.JOptionPane JOptionPane;

AndroidCamera ac;
PImage img;
Textlabel myTextlabelA, myTextlabelB;
boolean canStart = true;
boolean canInput = false;
boolean Start = false;
boolean Start_camera = false;
String typedText = "Comments: ";
PFont font;
String command = "";
String confirm;
String found;

PImage carImage;
float[] tdata = new float[17]; 
float tptat; // PTAT
String portName; 
//int serialport = 0; 

String buf; 
float colourGreen; //change green in relation to temperature 

int lbTemp = 25; //lowest relative temperature
int hbTemp = 35; //highest relative temperature 
Serial myPort;  // Create object from Serial class


void setup() {
  size(1366,768);
  //background(#FFFFFF);
  background(ControlP5.BLACK);
  //println(Serial.list());
  //portName = Serial.list()[serialport];
  myPort = new Serial(this, "COM15", 9600);
  myPort.clear();
  cp5 = new ControlP5(this);
  ac = new AndroidCamera(720, 480, 30);
  ac.start();

  myTextlabelA = cp5.addTextlabel("label")
                    .setText("GUI of Alive Human Detection System.")
                    .setPosition(120,10)
                    .setColorValue(#FFFAF0)
                    .setFont(createFont("Georgia",20))
                    ;

  cp5.addButton("Start")
     .setPosition(115, 670)
     .setSize(100,30)
     ;

  cp5.addButton("Stop")
     .setPosition(375, 670)
     .setSize(100,30)
     ;
  cp5.addButton("Start Camera")
     .setPosition(799, 670)
     .setSize(100,30)
     ;
}

public void Start(int theValue) {
  println("Initialising...");
  println("Connected.");
  //start displaying
 // carImage = loadImage(".png");
  Start = !Start;
}

public void Stop(int theValue) {
  println("Shuting Down...");
  //finish = loadImage("robot.png");
  //image(finish,30,64);
  myPort.write(0x02);
  Start = !Start;
  //strokeWeight(0);
}

public void Start_camera(int theValue) {
  println("Initialising...");
  println("Connected.");
  //start displaying
 // carImage = loadImage(".png");
  Start_camera = !Start_camera;
}

void draw() 
{
  //if (Start_camera){
  //img = ac.getCameraImage();
  //image(img, 610, 0);
  //}
  if (Start){

    myPort.write('1'); 
    delay(100);
    while (myPort.available() &gt; 0) { 
    delay(300);  
    buf = myPort.readStringUntil('\n');
    if (buf.equals("F")){
    int dialogResult = JOptionPane.showConfirmDialog (null, "Human found? Please Confirm!!", "Alert!", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
    if(dialogResult == JOptionPane.YES_OPTION)
    {
    fill(255,0,0);
    rect(100,670,50,30);
    myPort.write('2');
    }
    else if(dialogResult == JOptionPane.NO_OPTION)
    {
    fill(0,255,0);
    rect(100,670,50,30);
    myPort.write('3');
    }
    }
    else{
    myPort.clear();
    tdata = float(split(buf, ','));  
    for (int i = 0; i &lt;16; i++) { 
    colourGreen = map(tdata[i],lbTemp,hbTemp,255,0);
    fill (255,colourGreen,0);

      if (i == 0)
    { rect(450,64,150,150);}
      else if (i == 1)
    { rect(300,64,150,150);}
      else if (i == 2)
    { rect(150,64,150,150);}
      else if (i == 3)
    { rect(0,64,150,150);}
      else if (i == 4)
    { rect(450,214,150,150);}
      else if (i == 5)
    { rect(300,214,150,150);}
      else if (i == 6)
    { rect(150,214,150,150);}
      else if (i == 7)
    { rect(0,214,150,150);}
      else if (i == 8)
    { rect(450,364,150,150);}
      else if (i == 9)
    { rect(300,364,150,150);}
      else if (i == 10)
    { rect(150,364,150,150);}
      else if (i == 11)
    { rect(0,364,150,150);}
      else if (i == 12)
    { rect(450,514,150,150);}
      else if (i == 13)
    { rect(300,514,150,150);}
      else if (i == 14)
    { rect(150,514,150,150);}
      else if (i == 15)
    { rect(0,514,150,150); }

    if (tdata[i]&lt;5) {fill(255);} else {fill(0);}
      if (i ==0)
      {
      textAlign(CENTER, CENTER);
      textSize(20);
      text(str(tdata[i]),525, 139);}
      else if (i == 1)
      {
      textAlign(CENTER, CENTER);
      textSize(20);
      text(str(tdata[i]),375, 139);
      }
      else if (i == 2)
      {
      textAlign(CENTER, CENTER);
      textSize(20);
      text(str(tdata[i]), 225, 139);
      }
      else if (i == 3)
      {
      textAlign(CENTER, CENTER);
      textSize(20);
      text(str(tdata[i]),75, 139); 
      }
      else if (i == 4)
      {
      textAlign(CENTER, CENTER);
      textSize(20);
      text(str(tdata[i]), 525, 289); 
      }
      else if (i == 5)
      {
      textAlign(CENTER, CENTER);
      textSize(20);
      text(str(tdata[i]), 375, 289);  
      }
      else if (i == 6)
      {
      textAlign(CENTER, CENTER);
      textSize(20);
      text(str(tdata[i]), 225, 289);  
      }
      else if (i == 7)
      {
      textAlign(CENTER, CENTER);
      textSize(20);
      text(str(tdata[i]), 75, 289); 
      }
      else if (i == 8)
      {
      textAlign(CENTER, CENTER);
      textSize(20);
      text(str(tdata[i]), 525, 439);
      }
      else if (i == 9)
      {
      textAlign(CENTER, CENTER);
      textSize(20);
      text(str(tdata[i]), 375, 439);  
      }
      else if (i == 10)
      {
      textAlign(CENTER, CENTER);
      textSize(20);
      text(str(tdata[i]), 225, 439);
      }
      else if (i == 11)
      {
      textAlign(CENTER, CENTER);
      textSize(20);
      text(str(tdata[i]), 75, 439);
      }
      else if (i == 12)
      {
      textAlign(CENTER, CENTER);
      textSize(20);
      text(str(tdata[i]), 525, 592);  
      }
      else if (i == 13)
      {
      textAlign(CENTER, CENTER);
      textSize(20);
      text(str(tdata[i]), 375, 592); 
      }
      else if (i == 14)
      {
      textAlign(CENTER, CENTER);
      textSize(20);
      text(str(tdata[i]), 225, 592);
      }
      else if (i == 15)
      {
      textAlign(CENTER, CENTER);
      textSize(20);
      text(str(tdata[i]), 75, 592);  
      }
    }
    delay(350);  
  }
    }
    }
}
</code></pre>
 20245: <p>Implement a state machine first in your code for each state:</p>

<p>1) Sending i2c data.</p>

<p>2) Not sending i2c data.</p>

<p>And identify the conditions necessary to switch states:</p>

<p>1 to 2 when condition meet.</p>

<p>2 to 1 when command received.</p>

<p>Then drop your code into the state machine's code.</p>

<p>Read about state machines <a href="https://en.wikipedia.org/wiki/Finite-state_machine" rel="nofollow">here</a> and <a href="http://teachmetomake.com/wordpress/arduino-tutorial-state-machine" rel="nofollow">here</a>.</p>
