Processing, Firmata, and sensors question

Hello!

So, currently I have a Processing sketch that listens to Arduino for inputs via two potentiometers. Processing takes the two pot. values and moves a circle around the screen (and changes the color of the circle).

This part works fine. Now, I am thinking to use a PING distance sensor (HC-SR04) to set the color instead. Note I’m using StandardFirmata uploaded to Arduino, and am using Processing to directly receive the values.

I won’t include the StandardFirmata code, but here’s the gitHub link.

Here’s my Processing sketch:

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

Arduino arduino;

int myLED = 3;
int myPot = 0;
int myPot2 = 1;
int trigPin = 3;
int echoPin = 4;
float potValue1, potValue2;
float circleColor = 255;
float circleAutoColor = 0;

void setup(){
  colorMode(HSB);
  size(600,600);
  arduino = new Arduino(this, "COM5", 57600);
  arduino.pinMode(myLED, Arduino.OUTPUT);
  arduino.pinMode(myPot, Arduino.INPUT);
  background(0);
}

void getPotValue(){
  potValue1 = map(arduino.analogRead(0),0,1023,0 ,height);  // get the value from arduino via StandardFirmata, without Serial Monitor
  potValue2 = map(arduino.analogRead(1),0,1023,0, width);
  circleColor = map(potValue2,0,1023,0,255);
 // circleAutoColor = map(potValue2,0,1023,0,255);
  print(potValue1);
  print(", ");
  print(potValue2);
  print(", ");
  println(circleAutoColor);
}

void moveCircle(){
  getPotValue();
  if (mousePressed){
      if (circleAutoColor >= 255){
         circleAutoColor = 0;
      } else {circleAutoColor ++;}
    fill(circleAutoColor,255,255);
  } else {
  if (circleColor >=255){
    circleColor = 0;
    } 
  fill(circleColor,255,255);
  }
  
  
  ellipse(potValue1,potValue2,potValue2,potValue2);
}

void blinkLED(){
  arduino.digitalWrite(myLED, Arduino.HIGH);
}

void draw(){
 text("hi",20,20);
  if (key == ENTER){  // before pressing enter, you'll have a trail where the circle was. pressing 'enter' then starts refreshing the bg
    background(0);  // so it's just the circle
  }
  moveCircle();
 
  }

As far as I have been able to tell (see this thread), I don’t think I can use the Ping sensor in the same way as the potentiometers. Since the Arduino sketches using this sensor require the #include <NewPing.h> library, I can’t just put that in Processing and proceed like I have with the Potentiometers, can I?

In other words, is there a command (in the Processing sketch) that would make this work (note: psuedocode obviously):

import cc.arduino.NewPing.*; // import the NewPing library so I can use it later
int TRIGGER_PIN = 10
int ECHO_PIN = 9
int MAX_DISTANCE = 500

void getDistance(){
 int cm = Arduino.sonar.ping_cm(); // using the sonar.ping_cm() function from the NewPing library
}

SO - I will be needing to get rid of Firmata, and upload an Arduino sketch that writes the two Pot values and the distance value to the Serial monitor. Then, in Processing, read the data from the Serial monitor. However, my question is how can I, in Processing, assign different variables from the same line in the Serial monitor?

Would I need to use some character to separate the three values in the Serial Monitor, and in Processing, somehow read the first, second, and third value after that character?

…this is getting kind of convoluted, so please let me know if I can clarify. Thank you very much for any guidance/help.

TLDR: Is StandardFirmata best used for simple Arduino stuff, and if you want to use multiple sensors in Processing, use a sketch uploaded to the arduino that receives the multiple values, sends to Serial Monitor, then use a processing sketch to read the different values and assign variables that way.

So, currently I have a Processing sketch that listens to Arduino for inputs via two potentiometers.

No, it does not. That statement implies that the Arduino is responsible for sending the data whenever it deems appropriate. By loading Firmata on the Arduino, you’ve made it into an idiot savant.

Now, I am thinking to use a PING distance sensor (HC-SR04) to set the color instead. Note I’m using StandardFirmata uploaded to Arduino, and am using Processing to directly receive the values.

The Processing sketch has to do ALL the work, since you lobotomized the Arduino.

As far as I have been able to tell (see this thread), I don’t think I can use the Ping sensor in the same way as the potentiometers.

Sure you can.

Since the Arduino sketches using this sensor require the #include <NewPing.h> library

No, they do not. That is only ONE way to read a ping sensor.

I can’t just put that in Processing and proceed like I have with the Potentiometers, can I?

Of course not.

In other words, is there a command (in the Processing sketch) that would make this work

No.

SO - I will be needing to get rid of Firmata

That’s one way.

upload an Arduino sketch that writes the two Pot values and the distance value to the Serial monitor. Then, in Processing, read the data from the Serial monitor

You can not read the data that the Serial Monitor has already read. You could read from the serial port, in Processing, instead of using the Serial Monitor.

Would I need to use some character to separate the three values

Unless you install the mind reader module, you will.

and in Processing, somehow read the first, second, and third value after that character?

Processing makes that trivial. It can split a String at any delimiter AND convert the substrings to ints or floats in the process.

Is StandardFirmata best used for simple Arduino stuff

No. StandardFirmata is best just laughed at.