Arduino in one port and Processing out the other port

Hi,
I'm new in Arduino and all things electrical so excuse my lack of knowledge and ways to express myself.
I have a project where I am controlling the brightness of a light through Processing (reading a csv file). That goes out one port.
On the other hand, and port, I have an arduino attached with a light sensor and an SD card so that it can record the light it is receiving. This is recording the light being manipulated through Processing. Hence both ports being used.
My problem is that as soon as I turn the Processing sketch to start manipulating the light, the SD card on the arduino stops recording.
I have tried taking the light recorder off the laptop and plugged in through a converter but the light readings are erratic, often spiking. So I am left having to plug them both in to my laptop.
Is there any way round this?
thanks

Would you show the code or prefer to fix it telepathically?

The Arduino may be reset whenever its USB port is opened or closed. Check your board for an jumper, that allows to disable that function. After such a modification you may have to press the reset button, whenever you want to upload a sketch.

My apologies. I didn’t think it was a coding problem and hence I did not post it.

Arduino code:

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561_U.h>
#include <DS3231.h> //RTC library
#define SD_CS_PIN SS
#include <SPI.h>
#include <SdFat.h>

int LED = 13;

SdFat SD;
File myFile;
DS3231  rtc(SDA, SCL);
const int chipSelect = 8;
   
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);

void displaySensorDetails(void)
{
  sensor_t sensor;
  tsl.getSensor(&sensor);
  Serial.println("------------------------------------");
  Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" lux");
  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" lux");
  Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" lux");  
  Serial.println("------------------------------------");
  Serial.println("");
  delay(500);
}


void configureSensor(void)
{
  /* You can also manually set the gain or enable auto-gain support */
  // tsl.setGain(TSL2561_GAIN_1X);      /* No gain ... use in bright light to avoid sensor saturation */
   tsl.setGain(TSL2561_GAIN_16X);     /* 16x gain ... use in low light to boost sensitivity */
  //tsl.enableAutoRange(true);            /* Auto-gain ... switches automatically between 1x and 16x */
  
  /* Changing the integration time gives you better sensor resolution (402ms = 16-bit data) */
  //tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);      /* fast but low resolution */
   tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS);  /* medium resolution and speed   */
  // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS);  /* 16-bit data but slowest conversions */

  /* Update these values depending on what you've set above! */  
  Serial.println("------------------------------------");
  Serial.print  ("Gain:         "); Serial.println("16X");
  Serial.print  ("Timing:       "); Serial.println("101 ms");
  Serial.println("------------------------------------");
}

/**************************************************************************/
/*
    Arduino setup function (automatically called at startup)
*/
/**************************************************************************/
void setup(void) 
{
  Serial.begin(9600);
  Serial.println("Light Sensor Test"); 
  Serial.println("");

 
  /* Initialise the sensor */
  if(!tsl.begin())
  {
    /* There was a problem detecting the ADXL345 ... check your connections */
    Serial.print("Ooops, no TSL2561 detected ... Check your wiring or I2C ADDR!");
    while(1);
  }
 
  
  /* Display some basic information on this sensor */
  rtc.begin(); // intialize clock

  displaySensorDetails();
  /* Setup the sensor gain and integration time */
  configureSensor();
  pinMode(8, OUTPUT);
  pinMode(LED, LOW);

  Serial.println("Initializing SD card...");
  }

void loop(void) 
{  
  /* Get a new sensor event */ 
  sensors_event_t event;
  tsl.getEvent(&event);
  
 if (!SD.begin(SPI_HALF_SPEED, chipSelect)) {
    Serial.println("initialization failed!");
    digitalWrite(LED, HIGH);
    delay(1000);
    digitalWrite(LED, LOW);
    return;
    
  }
  
  myFile = SD.open("empty.csv", FILE_WRITE);
  if (!myFile) {
    Serial.println ("File failed to open");
     return;
    }
  
  /* Display the results (light is measured in lux) */
  if (event.light) {

    //  SERIAL MONITOR CHECK
   Serial.print(rtc.getDOWStr());
   Serial.print(" ");
   Serial.print(rtc.getDateStr());
   Serial.print(" ");
   Serial.println(rtc.getTimeStr());
   Serial.print(event.light); 
   Serial.println(" lux");

    //print to file
   myFile.print(rtc.getDateStr());
   myFile.print(",");
   myFile.print(rtc.getTimeStr());
   myFile.print(",");
   myFile.println(event.light);

     delay(1000);
  }
    myFile.close();//close file in order to save it?
}

And the Processing code:

import processing.serial.*;
Serial port;
Table table;
TableRow row;
float fData;

PFont font;
int index = 0;
int i = 0;
int mapMin = 0;
int mapMax = 4105;


float lerpFdata;
int timer = 0;
int timerDelay = 1000; // how often the next row of data is loaded
float lerpEase = 0.01; // lerp easing factor
byte timePast;
float fPast;


 void setup() {
 size(500, 100);
 background(255);
  font = createFont("Arial", 14, true);
  table = loadTable("01.07.2016 20m am.csv", "header");
  frameRate(60); 
  println("Available serial ports:");
 // if using Processing 2.1 or later, use Serial.printArray()
 
 printArray(Serial.list());
 port = new Serial(this,  Serial.list()[3], 9600);
 
 }

 void draw() {
  background(0);
  // delay update of new data and reset timer
  if (millis() >= timer) {
    timer = millis() + timerDelay;
    row = table.getRow(i); // get a new row of data
    fData = map(row.getInt("lux"), mapMin, mapMax, 0, 255); // get new data from row
  
    //  check to see if at end of data
    if (i == table.getRowCount()) {
      i = 0;//if so, loop back to first row
    } 
   
 // increment row counter
    i++;
 }
  
 // fData = map(row.getInt("lux"), mapMin, mapMax, 0, 255);
  
  //lerpFdata = lerp(lerpFdata, fData, lerpEase);
  fPast = map(fData,0, 255, 0,255);
  timePast = byte(fPast);
  port.write(timePast);
  println(timePast);
  // draw text
  fill(255);
  textFont(font);
  textAlign(LEFT);
  text(fPast, 10, height - 70);
  text("Day: "+(row.getString("date")) + " Time: "+row.getString("time") + " Lux: "+row.getInt("lux"), 10, height - 50); 

 }

DrDiettrich:
The Arduino may be reset whenever its USB port is opened or closed. Check your board for an jumper, that allows to disable that function. After such a modification you may have to press the reset button, whenever you want to upload a sketch.

I'm sorry, a jumper? I'm new at this and have no idea what you refer to.
By what you say, through disabling the USB port opening or closing I should be able to connect both boards, one through Processing, and the other through Arduino, in the same laptop and have them working at the same time?
The serial port does not need to be open in Arduino, just as well as Processing is taking it up, so I'm not sure how USB communication is interfering here except in powering the Arduino (with the sensor and SD card)?

Most probably you use the USB cable to upload a sketch, and to communicate with the Arduino. Whenever a program attaches to that USB port of your PC, the Arduino is reset. See the description of your board, whether and where there are jumpers (solder bridges...) which you can open and close yourself.

Your PC can have open many USB ports at the same time, and even a program can have multiple ports open. But as already mentioned, whenever a program opens such a port, the connected device may reset. Even if you only want to power the board from the PC, using an USB cable, a program can connect to that port. No such problem can arise when the board is powered from a separate power supply, maybe to the USB connector or power jack.

OK, that's set me off on where the problem might lie. I will try some things out and come back.
Thank you.

Do you have the same problem when opening usb com port in Arduino IDE's Serial Monitor?

Your one port and the other port description is very confusing. Give them names so we can understand.

I am not in favour of tampering with the Arduino hardware to solve the reset problem because you will want it to reset for uploading another program.

IF the problem is due to your Processing program causing the Arduino to reset the simple thing is to start the Processing program first.

IF you just want a system in which you can occasionally connect a PC to a running Arduino I suggest you get a USB-TTL cable - that won't reset the Arduino if you only connect TX, Rx and GND.

...R

It can be read as

I have a pc with two (USB? Serial? other?) ports. On one port a Processing App sends commands to some device that controls light intensity.
on a second USB/Serial port I have Arduino IDE+Serial monitor where I can see what my Arduino (UNO? Leonardo? Mega?) is sending over serial line. The Arduino reads in light sensor data, logs it on a SD card and also sends read sensor values to the Serial Monitor.

Arduino works as expected on the second port until the light-controlling Processing App opens and sends data through the first port.

Is this the case?

DrDiettrich:
Most probably you use the USB cable to upload a sketch, and to communicate with the Arduino. Whenever a program attaches to that USB port of your PC, the Arduino is reset. See the description of your board, whether and where there are jumpers (solder bridges...) which you can open and close yourself.

Your PC can have open many USB ports at the same time, and even a program can have multiple ports open. But as already mentioned, whenever a program opens such a port, the connected device may reset. Even if you only want to power the board from the PC, using an USB cable, a program can connect to that port. No such problem can arise when the board is powered from a separate power supply, maybe to the USB connector or power jack.

Sorry for taken so long to reply. My SD card messed up and had to get a new one before I continued testing.
Yes, this has solved the problem. The ports were being reset and hence one cancelling the other out.
It is fixed by connecting and starting the Arduino port first, double checking ports and correcting this in the Processing port.
Many thanks.
Connecting the sensor/arduino/sd card device through an alternative power supply is malfunctioning (it didn't use to and worked a treat, but that's another problem I would like to solve at some point). Therefore my need to have it connected to the laptop as well.
Thanks again.