Pages: [1]   Go Down
Author Topic: arduino to processing problem  (Read 1744 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i'm trying to get a simple processing sketch up and based on on what i read on the arduino page [the one about processing and arduino]

but it's not working and things are not explained. I really don't think there should be any code inside
check it:

Code:
import processing.serial.*;
import cc.arduino.*;

Arduino arduino;

void setup(){
  size(400, 400);
  arduino = new Arduino(this, Arduino.list()[0], 57600);
}

void draw(){
  background(arduino.analogRead(0));
}

i really feel this page, http://www.arduino.cc/playground/Interfacing/Processing  poorly explains what the library downloaded can possibly do for me,

for instance, it doesn't explain how this works, only that it does:
  arduino = new Arduino(this, Arduino.list()[0], 57600);

any help would be appreciated. if there's a well explained guide to the arduino-processing library i'd appreciate that as well.

Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 111
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That...odd.  I've got a sketch communicating to Processing, and it doesn't call an "Arduino" library at all.

Code:
import processing.serial.*;

Serial port;  // Create object from Serial class
int val;      // Data received from the serial port
int newval = 0;

void setup()
  {
  size(800, 600);  
  frameRate(100);
  println(Serial.list());      // this line creates a list serial devices currently connected
  port = new Serial(this, Serial.list()[2], 9600); // the third port in the list is where my Arduino was
  }

void draw()
  {
  background(255);   // setting the new window's background color first
  if (0 < port.available())
    {
    val = port.read();
    port.clear();     // so when I poll the port next time, I don't get old data off it    
    }

  if (val != newval)  // for this particular routine, change box color when data changes
    {
    switch (val - 50) // hacking; sending int from Arduino, and "1" sent = "51" received.
      {
      case 0:             // using switch-case to set four different box colors
        fill(#F62817);
        println(val);    // just for debugging purposes; prints to the Processing environment only
        newval = val;  
        val = 0;          
        break;
      case 1:            
        fill(#F76521);
        println(val);  
        newval = val;  
        val = 0;
        break;
     case 2:            
        fill(#FDD017);
        println(val);
        newval = val;  
        val = 0;
        break;
      case 3:            
        fill(#347C2C);
        println(val);
        newval = val;  
        val = 0;
        break;
      }
   }
  rect(25, 25, 750, 550);
}
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks, i'll be looking over your code and will be focusing on the serial library provided by processing instead of the lib by arduino. thanks
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yay i got it figured.
so what i have here is my arduino plugged into my laptop through usb. on the arduino is thing that detects the proximity of an object by using infrared light and an infrared sensor. it has a power, ground, and data wire, the last through which it returns numbers relevant to the distance of an object on top of the sensor. the data wire goes in analog read pin 0. there is no code in the arduino.

this is the processing code:
Code:
import processing.serial.*;

Serial myPort;
byte[] inBuffer = new byte[5];

float float_data;
int int_data;
int ir_data;
int data;

int last_val_data = 0;

void setup(){
  size (300, 300);
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 9600);
}

void draw (){
  inBuffer = myPort.readBytes();
  myPort.readBytes(inBuffer);
  if (inBuffer != null) {
    String myString = new String(inBuffer);
    String raw_data = myString ;
    
    if (raw_data != null)
    float_data = float(raw_data);
  }
  
    int_data = int(float_data);
    
    if (int_data != 0){
      ir_data = int_data;
    }

    data = smoother(ir_data, last_val_data, 10);
    last_val_data = data;
  
    background(data);
    println(data);
  
}


int smoother (int last_output, int input, int factor){
  return last_output + (input - last_output) / factor;
}

any one is free to improve on my code and share changes!
« Last Edit: May 22, 2008, 12:05:28 am by Ramy » Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 111
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm suspect of that
  • there in the MyPort.  I believe your USB keyboard/mouse is probably first on the list of identified ports, with the Arduino somewhere further down the list.

Make an ultra-simple program, like a counter or a button-press, and see if you can pick up serial data from the Arduino.  Then work outward from there.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

nah, i'm using my laptop, so there's nothing plugged in except for arduino. i'll post the Serial.list later today.
Logged

Central Florida, USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 59
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
there is no code in the arduino.

Um, if there is no code in the Arduino, how does it know to read analog(0) and send the data out its serial port to Processing running on the laptop?

To avoid creating Arduino SW, don't you at least have to have Firmata loaded into the Arduino?
« Last Edit: May 26, 2008, 12:03:51 pm by kBit » Logged

Pages: [1]   Go Up
Jump to: