Hello,
I have some code that uses 2 temperature sensors. It works great on the arduino and is able to output to the serial monitor. My problem comes when I attempt to send the data to Processing and create a .csv file with the data. Arduino and Processing are both attempting to use COM3, and of course they can't at the same time. The Processing creates a .csv file, but does not have any data in it. Could someone point out my issue? I have tried everything I can think of at this point. My code is below:
Arduino:
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2 /*-(Connect to Pin 2 )-*/
#define bus2 4
OneWire ourWire(ONE_WIRE_BUS);
OneWire ourWire2(bus2);
DallasTemperature sensors(&ourWire);
DallasTemperature sensors2(&ourWire2);
void setup()
{
Serial.begin(9600);
delay(1000);
sensors.begin();
}
void loop()
Serial.println();
sensors.requestTemperatures();
Serial.print(sensors.getTempCByIndex(0));
Serial.print(", ");
sensors2.requestTemperatures();
Serial.print(sensors2.getTempCByIndex(0));
delay(1000);
}
Processing:
import processing.serial.*;
PrintWriter output;
DateFormat fnameFormat= new SimpleDateFormat("yyMMdd_HHmm");
DateFormat timeFormat = new SimpleDateFormat("hh:mm:ss");
String fileName;
Serial myPort;
short portIndex = 0;
char HEADER = 'H';
void setup()
{
size(200, 200);
String portName = Serial.list()[portIndex];
println(Serial.list());
println(" Connecting to -> " + Serial.list()[portIndex]);
myPort = new Serial(this, portName, 9600);
Date now = new Date();
fileName = fnameFormat.format(now);
output = createWriter(fileName + ".csv");
}
void draw()
{
int val;
String time;
if ( myPort.available() >= 15)
{
if( myPort.read() == HEADER)
{
String timeString = timeFormat.format(new Date());
println("Message received at " + timeString);
output.println(timeString);
val = readArduinoInt();
// print the value of each bit
for(int pin=2, bit=1; pin <= 13; pin++){
print("digital pin " + pin + " = " );
output.print("digital pin " + pin + " = " );
int isSet = (val & bit);
if( isSet == 0){
println("0");
output.println("0");
}
else {
println("1");
output.println("0");
}
bit = bit * 2;
}
// print the six analog values
for(int i=0; i < 6; i ++){
val = readArduinoInt();
println("analog port " + i + "= " + val);
output.println("analog port " + i + "= " + val);
}
println("----");
output.println("----");
}
}
}
void keyPressed() {
output.flush();
output.close();
exit();
}
int readArduinoInt()
{
int val;
val = myPort.read();
val = myPort.read() * 256 + val;
return val;
}
Thank you in advance!!!