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

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

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

1 Like