0

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);  
  }
    }
    }
}

migrated from electronics.stackexchange.com Apr 16 '16 at 16:46

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

2

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.

  • Thanks a lot for the tips! I shall go and try it out. @st2000 – lim lagoon Apr 17 '16 at 5:03
  • Hi there, I successfully asked my arduino and sensor to wait when it meet the condition by implementing FSM into my code. Thank you very much. However, I faced another problem here whereby my processing doesn't seems to read my string sending from Arduino when condition meet. My GUI in processing freeze when program jump into this state case S_STOPTRANSFER: . Any idea how to solve this? Latest coding is edited in the post. Thanks a lot in advance! – lim lagoon Apr 19 '16 at 10:35
  • There might a logic problem in your code. I'm going to leave that up to you to find. However, I will suggest you approach the problem in parts. First, determine if you can send a found message to your "GUI". Do this unconditionally when you start the Arduino. If that does not work, perhaps your Arduino logic is good but your "GUI" can not understand the message. If it does work, then, second, start eliminating some of the conditions in the Arduino code which prevents sending the message to the "GUI". Perhaps the logic of one of the conditions is incorrect. – st2000 Apr 19 '16 at 13:56
  • Thank you very much for all the suggestions given! It's working now! =D i found the mistake in my coding. I should be using Serial.print to print my string in Arduino instead of Serial.println because Serial.println will return in \r\n instead of \n. Manually adding \n after my text in both Arduino and Processing coding. In Arduino, Serial.print("FOUND\n"); In Processing, buf = myPort.readStringUntil('\n'); if (buf.equals("FOUND\n")) Program in processing is now able to detect my string from Arduino. Thanks again! =) – lim lagoon Apr 19 '16 at 16:08
0

Create a flow chart of the entire process you want to happen. Once you proof your flow chart to cover all possible functions, you can create the logic, control, and subroutines that are needed. If you have a good book on Arduino programming, and how I2C works, you will know that the I2c display will hold whatever data you wish until you change it. Also, you probably know that you can chain a whole bunch of I2c devices, no matter if they are displays, or anything else, so each device will hold its own data independent of the others. Any updating is under your control in programming. You might also look into using interrupts for some of your program control. Sensors don't push data, the processor reads the data, and your program is in control of how often and when the data is read, and what it does with the data. The exception to your program being in control, is when you use interrupts. You should be storing the data so you will always have access to it for program examination and possible decision making. Lastly, to increase the abilities of your system, consider using one master Arduino, and several slave Arduino's. Some of the slaves, depending on what you want them to do, can be very small. Look at the tiny85. Some of the boards with the Tiny85 are the size of a postage stamp. I hope some of these suggestions are helpful to you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.