Two sensors trigger same part of code

edited March 2017 in Arduino

I am using two pressure sensors with arduino to receive two sets of values so I can trigger different things on the screen, but the two sensors keep only triggering the same part of code, I think it's the port problem but couldn't figure it out.

import processing.sound.*;
import processing.serial.*;
import cc.arduino.*;

int linefeed;
int count;
int sensors[];
int a,b,c;

int lf0 = 10;
int lf1 = 10;

String myString0 = null;
String myString1 = null;
Serial myPort0;
Serial myPort1;
float pressureSession0num, pressureSession1num;

void setup() {
  myPort0 = new Serial(this, Serial.list()[0], 9600);
  myPort1 = new Serial(this, Serial.list()[1], 9600);
  myPort0.clear();
  myPort1.clear();
  fullScreen();
  smooth();
}

void draw() {
  background(0);
  while (myPort0.available() > 0) {
    myString0 = myPort0.readStringUntil(lf0);
    if (myString0 != null) {
      pressureSession0num=float(myString0);  
      println("1P"+pressureSession0num);
    }
  }
  myPort0.clear();

  while (myPort1.available() > 0) {  
    myString1 = myPort1.readStringUntil(lf1);
    if (myString1 != null) {
      pressureSession1num=float(myString1);  
      println("2P"+pressureSession1num);
    }
  }
  myPort1.clear();
}
  void counter() {
    if (pressureSession0num>500) {
      a += 1;
    }
    if (pressureSession1num>500) {
      b += 1;
    }
  }

Arduino code

int analogPin0 = 0;
int analogPin1 = 1;

float temp0=0;
float temp1=0;

void setup() { 
  Serial.begin(9600);
} 

void loop() {
  temp0=analogRead(analogPin0);
  temp1=analogRead(analogPin1);

  Serial.println(temp0); // println add Linefeed to my float
  Serial.println(temp1); // println add Linefeed to my float

  delay(250);
}  

Answers

  • edited March 2017

    And is "port" and analog in the same thing? confusing...

  • Answer ✓

    @Julo

    I have few questions. In your processing code, you are not using your counter() function. You need to revise your code.

    Stepping back for one second, what do you want it to do? You talk about triggering. Based on your code, you want to keep track of how many times each sensor is trigger that are above certain threshold?

    You are collecting data from two sensors using a single board based on your arduino code. However, your processing code is collecting data base on the concept that you are accessing two arduino boards. You will need to modify the code and you need to study the following posts and focus on @GoToLoop's contributions:

    1. https://forum.processing.org/two/discussion/comment/88115/#Comment_88115 (handling multiple sensors)

    2. https://forum.processing.org/two/discussion/16618/processing-with-arduino-void-serialevent#Item_1 (A recommend way to manage data arrival in Processing - not the only way but a very successful practice)

    For this example to be useful to you, you must implement the next change in your arduino code. Replace lines 15 and 16 with

      Serial.print(temp0); 
      Serial.print(','); 
      Serial.println(temp1);
    


    Another available code that you could use is the following:
    https://forum.processing.org/two/discussion/comment/89905/#Comment_89905

    For this to work for you, you need to change some values but the main code should work as it is. Focus in the last two posts dated Feb 27, 2017. One post has the Processing code and the other one has the Arduino code. However, before you check this previous post, it is important that you get your code working and understand the basic interaction with sensors via Arduino. I will even advise to simplify your code to work with a single sensor at a time. This will ensure that you your sensor-arduino-PC are all happy working together and you know how the triggering works. It will be a very important step for debugging as well.


    For the "port" and "analog" question... you will need to put those words in context to ensure we are working with their proper concepts. In general, when you have a connection between arduino and a PC, you interact through a port referring to a serial connection. Port could also referred to one of the physical connections in your arduino board (yes, the one you use to establish a serial connection). A computer can also have ports, many of them. They can be serial or USB ports. There are software ports....

    Analog refers to the nature of the signal that your arduino is managing. Digital signals is another type of signal an Arduino board could manage. There are lots of info in the web talking about this. If I google "arduino analog vs. digital" I get lots of great hits. For example:
    https://learn.sparkfun.com/tutorials/analog-vs-digital
    http://www.societyofrobots.com/robotforum/index.php?topic=6857.0

    Kf

  • edited March 2017

    Thanks Kfrajer, those are great info. 1. I was using the counter() function to trigger animation on screen, I didn't put that part of the code in since I know the problem is not there. 2. Ok, 1 arduino board is all I need, got that part clear, thanks. 3. I eventually got my code working, I change the arduino code to the following

    Serial.print(pressure1); Serial.write('\t'); Serial.print(pressure2); Serial.write('\t'); Serial.println(pressure3);

    can you explain a bit about what this code does? Serial.write('\t'); 4. I rewrite the processing code to the following from the references provided, am clear now values will come from [1],[2],[3] analog in. The last part serialEvent I check the processing documentation and your reference, but still a bit confuse about what it does, can you help explain a bit on this?

              import processing.sound.*;
              import processing.serial.*;
              import cc.arduino.*;
    
              int[] vals = new int[3];
    
              void setup() {
    
              final String[] ports = Serial.list();
              printArray(ports);
              new Serial(this, ports[1], 9600).bufferUntil(ENTER);
            }
    
            void draw() {
              background(0);
              println(vals[0], TAB, vals[1], TAB, vals[2]);
            }
    
            void serialEvent(final Serial s) {
              vals = int(splitTokens(s.readString()));
              redraw = true;
            }
    

    5. Actually after the above code revised, my two sensors still trigger the same values, and turn out it was the circuit problem, two sensors seem to share the same ground resistor, separate them, problem solved.

  • Answer ✓

    Please check this:

    https://www.arduino.cc/en/Serial/Print
    https://www.arduino.cc/en/serial/println
    https://www.arduino.cc/en/Serial/write

    The next post could clarify more: http://forum.arduino.cc/index.php?topic=42603.0

    In your case, I will stick to print() instead of write() although it might work fine in this case, it is clear sticking to the same function.

    One thing you are missing in your previous example is noLoop() in your setup function. This is how it works. The fxn noLoop() disables the loop nature of draw(). Normally draw executes 30 fps. Calling the function in setup cases draw to be executed only once and then the sketch stops running. When a serial event happens as in the arrival of new data, then serialEvent() gets executed an redraw() triggers the execution of draw() once. Moe info about this here: https://processing.org/reference/draw_.html

    For splitTokens, check this https://processing.org/reference/splitTokens_.html

    Kf

  • Thanks for the information. Let's say if I want something in the background looping even when I am not receiving data, then noLoop() and redraw() you mentioned don't apply right?

  • It depends on the nature of those operations but as a guess, I will say that this approach would not be convenient for you. To clarify, this approach is not mandatory. You can also have the draw function running at all times and check for proper data in the callback function using conditionals.

    Kf

  • edited March 2017

    If you've got other dynamic stuff to do in draw() while awaiting for data[] to arrive, logically you can't use the noLoop() + redraw() approach. :-\"

    But if at the same time you don't wanna print() the same data[] @ 60 FPS, but only when new data[] arrives, just declare some boolean for it. *-:)

    Set it to true within serialEvent(). And after checking it out in draw(), set it back to false: B-)

    // forum.Processing.org/two/discussion/21447/
    // two-sensors-trigger-same-part-of-code#Item_7
    
    // GoToLoop (2017-Mar-21)
    
    import processing.serial.Serial; 
    
    static final int VALUES = 3, PORT_IDX = 1, BAUDS = 9600;
    int[] vals = new int[VALUES];
    boolean dataArrived;
    
    void setup() {
      //noLoop();
      final String[] ports = Serial.list();
      printArray(ports);
      new Serial(this, ports[PORT_IDX], BAUDS).bufferUntil(ENTER);
    }
    
    void draw() {
      if (dataArrived) {
        dataArrived = false;
        for (final int v : vals)  print(v, TAB);
        println();
      }
    }
    
    void serialEvent(final Serial s) {
      vals = int(splitTokens(s.readString()));
      dataArrived = true;
      //redraw = true;
    }
    
Sign In or Register to comment.