I've been searching around trying to figure out how to do this but I really cannot figure it out! I am trying to send data from Arduino to processing and have processing spit out a text file that I will then have Arduino read. I have 8 RGB LEDS with 3 shift registers, a DHT Temp Sensor, a photoresistor, and an electret microphone. The input from the 3 sensors are controlling the color of the RGB LEDS. My code on the Arduino side works but I can't get processing to read the data and save it to a text file. Here are my code attempts so far (it's probably all wrong, I'm new to programming)
Attempt 1
import processing.serial.*;
Serial myPort;
PrintWriter output;
int r;
int g;
int b;
void setup() {
output = createWriter("data/data.txt");
myPort = new Serial(this, "/dev/tty.usbmodemfa121", 9600);
myPort.bufferUntil('\n');
}
void serialEvent(Serial myPort){
String myString = myPort.readStringUntil('\n');
int data [] = int(split(myString, ','));
if (data.length > 1) {
r = data[0];
g = data[1];
b = data[2];
println("Printing RGB Vals to File");
output.println(r+","+g+","+b);
output.flush();
}
}
void stop(){
output.close();
super.stop();
}
Attempt 2
import processing.serial.*;
Serial mySerial;
PrintWriter output;
int r;
int g;
int b;
void setup() {
output = createWriter("data/data.txt");
mySerial = new Serial(this, "/dev/tty.usbmodemfa131", 9600);
mySerial.bufferUntil('\n');
}
void draw() {
write();
println("Printing RGB Vals to File");
}
void serialEvent(Serial mySerial) {
String myString = mySerial.readStringUntil('\n');
if (mySerial.available() > 0) {
int data [] = int(split(myString, ','));
if (myString !=null) {
r = data[0];
g = data [1];
b = data [2];
}
}
mySerial.write(65);
}
void write() {
output.println(r+","+g+","+b);
output.flush();
}
void stop() {
output.close();
super.stop();
}
Arduino Code
//+++++++++++++++DHT TEMP SENSOR+++++++++++++
#include "DHT.h" // DHT library
#define DHTPIN 2 // what pin DHT is connected too
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
//+++++++++TEMP+++++++++
float fahTemp = 0; //temperature to convert
int NewTempint = 0; // int to convert fahtemp float into an int
int newTempReading; // Final temp reading with mapping
//++++PHOTORESISTOR++++++++++++
int photocellPin = A0; //photoresistor A0
int photocellReading; //photoresistor reading before mapping
int newphotoReading; //new reading for photoresistor after mapping
//+++++++++++NOISE++++++++++++++
const int sampleWindow = 50;
unsigned int sample;
int newSample;
//++++++++++RGB LED+++++++++
const int ShiftPWM_latchPin=8;
#define SHIFTPWM_NOSPI
const int ShiftPWM_dataPin = 11;
const int ShiftPWM_clockPin = 12;
const bool ShiftPWM_invertOutputs = false;
const bool ShiftPWM_balanceLoad = false;
#include <ShiftPWM.h>
unsigned char maxBrightness = 255;
unsigned char pwmFrequency = 100;
int numRegisters = 3;
int numRGBleds = numRegisters*8/3;
// +++++++++++++++++SETUP++++++++++++++++++++
void setup() {
Serial.begin(9600);
dht.begin(); //begin reading temp
ShiftPWM.SetAmountOfRegisters(numRegisters);
ShiftPWM.SetPinGrouping(4);
ShiftPWM.Start(pwmFrequency,maxBrightness);
}
//++++++++++++++++++LOOP+++++++++++++++++++++++++++++++++
void loop() {
//+++++++++TEMP++++++++
float t = dht.readTemperature(); //read temp in *C
fahTemp = (t * 1.8) +32; //convert *C to *F
NewTempint = (int(fahTemp)); //converting the *F temp into an int
newTempReading = map(NewTempint, 70,89, 0,255); //mapping the temp to 0-255
//+++++++PHOTORESISTOR+++++++++
photocellReading = analogRead(photocellPin); //reading the values from the photoresistor
photocellReading = 1023 - photocellReading; //inverting values from 0-1023 to 1023-0
newphotoReading = map(photocellReading, 0, 1023,0,255); //mapping photoresistor values to 0-255
//++++++++++++NOISE+++++++++++++
unsigned long startMillis= millis(); // Start of sample window
unsigned int peakToPeak = 0; // peak-to-peak level
unsigned int signalMax = 0;
unsigned int signalMin = 1024;
//collect data for 50 mS
while (millis() - startMillis < sampleWindow)
{
sample = analogRead(1);
if (sample < 1024) // toss out spurious readings
{
if (sample > signalMax)
{
signalMax = sample; // save just the max levels
}
else if (sample < signalMin)
{
signalMin = sample; // save just the min levels
}
}
}
peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
double volts = (peakToPeak * 3.3) / 1024; // convert to volts
newSample = map(sample, 0.0, 3.20,0,255);
//++++++++++++++RGB LEDS+++++++++++++++++++++++++++++++++
int led1 = 0;
int led2= 1;
int led3 = 2;
int led4 = 3;
int led5 = 4;
int led6 = 5;
int led7 = 6;
int led8 = 7;
unsigned char r = char(newTempReading);
unsigned char g = char(newphotoReading);
unsigned char b = char(newSample);
ShiftPWM.SetRGB(led1,r,g,b);
ShiftPWM.SetRGB(led2,r,g,b);
ShiftPWM.SetRGB(led3,r,g,b);
ShiftPWM.SetRGB(led4,r,g,b);
ShiftPWM.SetRGB(led5,r,g,b);
ShiftPWM.SetRGB(led6,r,g,b);
ShiftPWM.SetRGB(led7,r,g,b);
ShiftPWM.SetRGB(led8,r,g,b);
Serial.println (r);
Serial.println (",");
Serial.println(g);
Serial.println (",");
Serial.println (b);
Serial.println("\n");
//++++++++++PRINT TO SERIAL MONITOR++++++++++
delay (500); //delay after reading
}