Pages: [1]   Go Down
Author Topic: Arduino to Processing Serial Communication to text file  (Read 2045 times)
0 Members and 2 Guests are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:
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
Code:
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
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

}

« Last Edit: April 20, 2013, 06:26:20 am by Katherz » Logged

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It would be helpful to know what symptoms you are seeing.  Does the Processing program compile and run?  What gets printed?  Is a file created? 

-br

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In the first attempt, the file gets written but has no data written whatsoever. In the second attempt, a file gets written but it's only printing 0,0,0 which are not the values in the serial monitor on the Arudino side
Logged

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So, it's time for you to do some debugging.  I would start with example 2 since it's at least making a file. 

This doesn't look right to me:

Code:
String myString = mySerial.readStringUntil('\n');
if (mySerial.available() > 0) {

I'm not clear why would you test for more characters available after your readStringUntil has completed.

Instrumenting your program with more Serial.print() of critical values will help you debug the problem.


-br
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
The Arduino can not read a text file on the PC.

Code:
    sample = analogRead(1);
    if (sample < 1024) // toss out spurious readings
    {
That is a silly test. The analogRead() function does NOT return values greater than 1023.

Code:
  newSample = map(sample, 0.0, 3.20,0,255);
The map function expects longs. Why are you calling it with floats?

Code:
  unsigned char r = char(newTempReading);
  unsigned char g = char(newphotoReading);
  unsigned char b = char(newSample);
A char is a signed value. Theoretically, half your values won't be in the range of a char.

Why are you casting the ints to char, then storing in an unsigned char?

Code:
Serial.println (r);
  Serial.println (",");
  Serial.println(g);
  Serial.println (",");
  Serial.println (b);
  Serial.println("\n");
This will send something like "120<cr><lf>,30<cr><lf>,72<cr><lf><cr><cr><lf>". Why?

You need to understand what print() and println() do, and how they are different. The Processing code is going to read and buffer until the carriage return arrives. Sending 4 of them seems silly.
Logged

Pages: [1]   Go Up
Jump to: