Arduino + Processing Serial Port DATA TRANSFER

This is my control center which displays the buttons for each room. I am wanting to turn on the lights in one room and have my light sensor transfer the data from Arduino’s serial port into Processing to display the readings right next to the buttons.

PROCESSING CODE:

import controlP5.*; //import ControlPS library
import processing.serial.*;

Serial port;

ControlP5 cp5; //create ControlPS object
PFont font;


void setup(){
  size(600,800); //window size, (width, height)
  port = new Serial(this,"COM3", 9600); //COM3 depends on your port
  
  
  //adding buttons
  
  cp5 = new ControlP5(this);
  font = createFont("calibri light", 14); //fonts for buttons and title
  
  
  cp5.addToggle("Room 1")
    .setPosition(60, 50) //x and y coordinates of upper left corner of button
    .setSize(125, 100) //(width, height)
    .setMode(ControlP5.SWITCH)
    .setFont (font)
  ;
  cp5.addToggle("Room 2")
    .setPosition(60, 200)
    .setSize(125, 100)
    .setMode(ControlP5.SWITCH)
    .setFont (font)
  ;
    cp5.addToggle("Room 3")
    .setPosition(60, 350)
    .setSize(125, 100)
    .setMode(ControlP5.SWITCH)
    .setFont (font)
  ;
    cp5.addButton("SENSOR READING 1")
    .setPosition(230, 50)
    .setSize(300, 100)
    .setFont (font)
  ;
    cp5.addButton("SENSOR READING 2")
    .setPosition(230, 200)
    .setSize(300, 100)
    .setFont (font)
  ;

  cp5.addButton("SENSOR READING 3")
    .setPosition(230, 350)
    .setSize(300, 100)
    .setFont (font)
  ;  
  cp5.addButton("Errors")
    .setPosition(60, 500)
    .setSize(125, 100)
    .setFont (font)
  ;
  cp5.addButton("Alarms")
    .setPosition(60, 650)
    .setSize(125, 100)
    .setFont (font)
  ;
}
void draw(){ 
  background(0, 0, 0); //background color of window 
  
  //text
  fill(0, 255, 0); // (r, g, b) or (0 to 255)
  text("CONTROL CENTER", 250, 30);


}

void Room1(){
  port.write('a');
}

void Room2(){
  port.write('b');
}

void Room3(){
  port.write('c');
}

ARDUINO SENSOR CODE:

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_TSL2591.h"

Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591);

//Configures the gain and integration time for the TSL2591
void configureSensor(void)
{
  tsl.setGain(TSL2591_GAIN_MED);
  tsl.setTiming(TSL2591_INTEGRATIONTIME_300MS);
 
  Serial.println(F("------------------------------------"));
  Serial.print  (F("Gain:       "));
  tsl2591Gain_t gain = tsl.getGain();
  switch(gain)
  {
  case TSL2591_GAIN_LOW:
    Serial.println(F("1x (Low)"));
    break;
  case TSL2591_GAIN_MED:
    Serial.println(F("25x (Medium)"));
    break;
  case TSL2591_GAIN_HIGH:
    Serial.println(F("428x (High)"));
    break;
  case TSL2591_GAIN_MAX:
    Serial.println(F("9876x (Max)"));
    break;
  }
  Serial.print  (F("Timing:     "));
  Serial.print((tsl.getTiming() + 1) * 100, DEC);
  Serial.println(F(" ms"));
  Serial.println(F("------------------------------------"));
}
void setup(void)
{
  Serial.begin(9600);
  Serial.println(F("Starting Adafruit TSL2591..."));
 
  if (tsl.begin())
  {
  Serial.println(F("Found a TSL2591 sensor"));
  }
  else
  {
  Serial.println(F("No sensor found ..."));
  while (1);
  }
  configureSensor();
}
// The first "flag" for checking the Lux levels. If the Lux level is less than 500 then luxCheck will be 0.
// This is the yellow light
int luxCheck;

// Reads IR and full spectrum then converts to Lux and checks for less than 500 Lux value
void advancedRead(void)
{
  sensors_event_t event;
  tsl.getEvent(&event);
  uint32_t lum = tsl.getFullLuminosity();
  uint16_t ir, full;
  ir = lum >> 16;
  full = lum & 0xFFFF;
  if ((event.light < 500))
  {
    luxCheck = 0;
    Serial.println(F("WARNING: LUX NOT WITHIN DESIRED RANGE!"));
    Serial.print(F("Lux: ")); Serial.println(tsl.calculateLux(full, ir), 2);
    Serial.println(F("------------------------------------"));
  }
  else
    {
      luxCheck = 1;
      Serial.print(F("Lux: ")); Serial.println(tsl.calculateLux(full, ir), 2);
      Serial.println(F("------------------------------------"));
    }
}
// Arduino loop function
void loop(void)
{
  advancedRead();
  delay(2000);
}
1 Like

please format your code posting by pasting it into the

</> code button

of the editor header menu ( context name: Preformatted text )
it looks like
```
type or paste code here
```

also can use the ``` manually above and below your code.

thank you.


so please repair above 2 code postings ( processing & arduino )


your header / topic ask for
SECONDARY WINDOW
what does that mean and how is that related to your code.


you want do that via processing ?
there is no code about communication from processing to arduino


you have arduino code what send a whole story to serial port

( please provide a printout of that ( while connecting the arduino IDE monitor )
also posted like code )

but looks like in processing you want use that as DATA…
so please send DATA
in the form of one CSV line ( every 2 sec ok )

1023,1023,...

as in processing that

  • is easy to catch and
  • convert to number and
  • map to your button text

see also basics

1 Like

Some previous posts related to retrieving data via serial in Processing:

Take the one that is the closest related to your task and try to understand it. Then modify your code and show your attempt. If you have specific questions, just ask them. There are are also also online tutorial with videos and I find they are excellent resources to get started in this kind of projects.

Kf

2 Likes

This is a screenshot of the serial monitor’s output data that I am trying to transfer over into Processing.

This is a screenshot of Processing’s sketch. I am trying to find a way to get the information from Arduino’s serial monitor port into the sensor reading option. If anyone has any ideas, please share them with me.

1 Like

as you not follow my recommendation of
using a dataline CSV style
from arduino to Processing via USB

you must find your own way to use the string line you send.


you could try to separate the lux data line
in 'lux: ’ and ‘number’
try convert the number string part to float …

I’m still not able to get it running properly…

that is bad,

-a- did you try to change the arduino program ?

-b- did you try to catch the data from the unchanged arduino code sending text lines?
split the line, and if first part contains “Lux:” then second part contains a number?
could you convert in that case the number string to a float

See also my sample code:

Have a look at the print or println statements in your arduino sketch.
As long as you are using the sample code you will not have success.

Check were the data for the LUX value is coming from. See the Adafruits sample code for the detector (see: Adafruit TSL 2591 Library )

void advancedRead(void)
{
  // More advanced data read example. Read 32 bits with top 16 bits IR, bottom 16 bits full spectrum
  // That way you can do whatever math and comparisons you want!
  uint32_t lum = tsl.getFullLuminosity();
  uint16_t ir, full;
  ir = lum >> 16;
  full = lum & 0xFFFF;
  Serial.print(F("[ ")); Serial.print(millis()); Serial.print(F(" ms ] "));
  Serial.print(F("IR: ")); Serial.print(ir);  Serial.print(F("  "));
  Serial.print(F("Full: ")); Serial.print(full); Serial.print(F("  "));
  Serial.print(F("Visible: ")); Serial.print(full - ir); Serial.print(F("  "));
  Serial.print(F("Lux: ")); Serial.println(tsl.calculateLux(full, ir), 6);
}

and modify it accordingly:

void advancedRead(void)
{
  // More advanced data read example. Read 32 bits with top 16 bits IR, bottom 16 bits full spectrum
  // That way you can do whatever math and comparisons you want!
  uint32_t lum = tsl.getFullLuminosity();
  uint16_t ir, full;
  ir = lum >> 16;
  full = lum & 0xFFFF;
  Serial.print(millis()); 
  Serial.print(",");
  Serial.print(ir);
  Serial.print(",");
  Serial.print(full);  
  Serial.print(",");
  Serial.print(full - ir); 
  Serial.print((",");
  Serial.println(tsl.calculateLux(full, ir), 6);
}

Not tested, but you now get a csv seperated line and next step is to learn how to read the data in Processing.

And for the calculated LUX value just:

void advancedRead(void)
{
  // More advanced data read example. Read 32 bits with top 16 bits IR, bottom 16 bits full spectrum
  // That way you can do whatever math and comparisons you want!
  uint32_t lum = tsl.getFullLuminosity();
  uint16_t ir, full;
  ir = lum >> 16;
  full = lum & 0xFFFF;
  Serial.println(tsl.calculateLux(full, ir), 6);
}

And your code should look like:

if ((event.light > 500))
  {
    luxCheck = 1;
    Serial.println(tsl.calculateLux(full, ir), 2);
  
  }
1 Like

Thank you for the arduino info. I have modified my code to show the lux calculations but now my main concern is how to apply that to Processing to show the serial monitor’s output data.

I have adjusted my arduino code to split the lines and now it shows the number then the second part contains "Lux: ". I am concerned about the Processing code because I am not able to show the output serial monitor data into the Processing app.

Hello,

Are you able to get this to work?

Arduino Code
float angle;
int count;

void setup() 
  {
  Serial.begin(9600);
  delay(1000);
  }
  
void loop() 
  {
  float x = sin(angle);
  float y = cos(angle);
  Serial.print(count);
  Serial.print(',');
  Serial.print(x);
  Serial.print(',');
  Serial.print(y);
  Serial.print('\n'); //same as Serial.println()
  delay(100);

  angle += TWO_PI/100;
  count++;
  }
Processing Code
import processing.serial.*; 
 
Serial myPort;    // The serial port
String inString;  // Input string from serial port
int lf = 10;      // ASCII linefeed 
 
void setup()
  { 
  size(400,200); 
  textSize(18);
  
  // List all the available serial ports: 
  printArray(Serial.list()); 
  
  // Open whatever port is the one you're using. 
  myPort = new Serial(this, Serial.list()[2], 9600); 
  myPort.bufferUntil(lf);
  delay(1000);
  } 
 
void draw() 
  { 
  background(0); 
  text("received: " + inString, 10,50); 
  } 
 
void serialEvent(Serial p) 
  { 
  inString = p.readString(); 
  } 

Processing code is modified version of this:
https://processing.org/reference/libraries/serial/serialEvent_.html

:slight_smile:

1 Like

I am getting back “received: null”
When it tries to pull all the ports, I’m not sure if it is getting the right port. Maybe that is the problem? I am not quite sure at this point. Any ideas?

Hello,

The code works:
image

Processing must be connected to the Arduino serial port that is sending data; you will have to modify code for your COM port.

Do some research.

I would get a measurement for a brief second and then it continues to show ----- right after until another value is measured. Do you know how i’m able to fix that?

It is what is programmed on the Arduino; you can edit the code to suit your project.

Suggestions:

  • Modify your Arduino code to send meaningful strings (CSV or otherwise) that you can receive and display. My code example does this.
  • trim() the received string.
  • split() the trimmed, received string into a String array.
  • convert the string elements in the array into a float (or datatype(s) that you were sending) for use in your main code.
1 Like

Thank you, everything is running properly once I adjusted my Arduino code!