How to read the value of each sensor from the Arduino

Hi, I’m new to Arduino and Processing.py.

I am using Processing and Arduino to complete my graduation project.(English is not my native language, sometimes I speak it’s very strange.)
I want to implement the Serial library so that Processing can read different values of different sensors on arduino.I tried to create an ever-changing list of sensor values using split().
I tried to use split() to separate the incoming values, but I kept getting an error. And I don’t know how to use bufferUntil() properly. :sob: :sob:

Here is my Arduino and Processing code.Can someone help me?


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:

  int val0 = analogRead(A0);   //
  int val1 = analogRead(A1);
  int val2 = analogRead(A2);
  int val3 = analogRead(A3);
  int val4 = analogRead(A4);
  int val5 = analogRead(A5);

  Serial.print(val0, DEC);
  Serial.print(',');
  Serial.print(val1, DEC);
  Serial.print(',');
  Serial.print(val2, DEC);
  Serial.print(',');
  Serial.print(val3, DEC);
  Serial.print(',');
  Serial.print(val4, DEC);
  Serial.print(',');
  Serial.print(val5, DEC);
  Serial.print('\n');
  //    Serial.println();
  delay(1000);

}
add_library("serial")
myPort = Serial(this,"COM3",9600)

def setup():
    size(200,200)
    myPort.bufferUntil('\n') 
    
def draw():
   
    if myPort.available()>0:
        P_dates = myPort.readString()
        string01 = P_dates
        TRY = string01.split(",")
        C = list()
        for i in TRY:
            C.append(int(i))
        println(C)
        
    delay(1000)
    
def serialEvent(Serial):
    inString = p.readString()

These are some of the mistakes I’ve encountered

TypeError: bufferUntil(): 1st arg can't be coerced to int

If I delete bufferUnitl ()

ValueError: invalid literal for int() with base 10: '77 51'

When I changed the program, the output list became strange

if myPort.available()>0:
        P_dates = myPort.readString()
        string01 = P_dates
        TRY = string01.split(",")
        C = list()
        for i in TRY:
            C.append((i))
        println(TRY)
        

I don’t understand why the output value is like this.The values on the serial monitor are correct.
The console values are as follows

[u'49', u'56', u'53', u'53', u'123', u'80\n']
[u'48', u'56', u'53', u'53', u'89', u'60\n']
[u'49', u'55', u'53', u'53', u'94', u'60\n']
[u'49', u'56', u'53', u'53', u'110', u'64\n']
[u'48', u'55', u'52', u'52', u'103', u'60\n']
[u'47', u'55', u'53', u'53', u'91', u'57\n']
1 Like

Cheers @The-NUll !

I’m afraid I can’t test/explore your code right now, but I’ve been using Firmata to read the values from Arduino, maybe this project can be of use for you:

Beware that on some platforms the Serial library is not working with Processing.py :frowning:

1 Like

Thank you very much! You gave me a new idea to try! I hope it helps me. :smiling_face_with_three_hearts:

1 Like

Hello,

I am new to Python as well.

I tried this and it worked:

add_library("serial")
myPort = Serial(this,"COM5",9600)

def setup():
    size(200,200)
    myPort.bufferUntil(0x0A) 
    frameRate(10);
    
def draw():
    background(255, 0, 0); // I needed something here
    
def serialEvent(Serial):
    inString = myPort.readString()
    println(inString)
    inString = trim(inString)
    arr = inString.split(",")
    for i in arr:
        print(i)

:)

1 Like

You can replace literal 0x0A w/ constant ENTER:
myPort.bufferUntil(ENTER)

BtW, I recommend taking a look at my Java Mode sketch example too:

2 Likes

Just realized I’ve already had a Python Mode example for the Serial library, but for a single value: :snake:

2 Likes

Thank you. I solve my problem, now it works well :smiley: :smiley:

2 Likes

I tried this and responded here:

:)

What was the solution?

1 Like