Pages: [1]   Go Down
Author Topic: Processing to arduino serial question?  (Read 1011 times)
0 Members and 1 Guest are viewing this topic.
Cork, Ireland
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a message coming into processing from pure data via OSC data I'm just wondering could I use the processing arduino library to trigger a function I have in my arduino program to reset the serial data back to zero. I currently have this working with a switch but I need to change it to when I swap fiduscial markers in a pure data patch I'm making. I am unable to use pduino for this as far as I know. I just want to know if it's possible before I waste any more time attempting it. (The OSC messages can carry integer,floats etc.)
Logged

Cork, Ireland
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I know I could incorporate a button to solve this but presentation is counting a lot for this project I'm working on and the current design for this would make a button look fairly obvious so I'm looking for a software solution instead.
Logged

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

Quote
I use the processing arduino library to trigger a function I have in my arduino program to reset the serial data back to zero.
To reset what serial data back to 0? Why does the Arduino need to be involved?
Logged

Cork, Ireland
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have an encoder running an ISR in Arduino that needs to be reset when the fiduscial markers are changed in PD. I've generally just been having trouble working with the encoder and this was just a workaround for me because I'm not very comfortable coding in arduino with the encoder. I kind of got thrown in the deep end with having to use it for my project.
« Last Edit: April 19, 2013, 06:07:19 am by spillaney07 » Logged

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

Quote
I have an encoder running an ISR
Not likely. You may have an encoder triggering an interrupt, but the ISR does not run on the encoder.

Quote
that needs to be reset when the fiduscial markers are changed in PD.
What, exactly, needs to be reset? Some count that the ISR is incrementing?

Quote
I've generally just been having trouble working with the encoder and this was just a workaround for me because I'm not very comfortable coding in arduino with the encoder.
You also seem to be having trouble expressing yourself. I don't see where having an ISR handle input from an encoder is any kind of "workaround". That is how an encoder is generally used.

Perhaps if you used your fingers when programming the Arduino, you'd have an easier time than using the encoder to program the Arduino.

Quote
I kind of got thrown in the deep end with having to use it for my project.
Which you haven't described.
Logged

Cork, Ireland
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Not likely. You may have an encoder triggering an interrupt, but the ISR does not run on the encoder.
My mistake, yes you are correct it's using interrupts.

Quote
What, exactly, needs to be reset? Some count that the ISR is incrementing?
Basically I have audio tracks in PD that are chosen by fiduscial markers. I then have a hand crank attached to the encoder that effects the speed of the track. So when a track is changed by replacing the fiduscial the encoder values need to be reset back to zero so the process can once again repeat itself.

Quote
You also seem to be having trouble expressing yourself. I don't see where having an ISR handle input from an encoder is any kind of "workaround". That is how an encoder is generally used.

I'm having trouble sending out secondary values along with the pulse data that I can use in the other programs and I can't find any examples that I can work with that would enable me to do so or if its possible (we only begun working with Arduino last semester). The fiduscial markers are the workaround I'm referring to, I was originally going to handle choosing the tracks to play in pd by using the arduino to do so (e.g button presses, rfid).   

Quote
Which you haven't described.
I hope to eventually work with the encoder using the speed at which the crank is spun at rather than the pulses, by counting the pulses per second. This will speed in the audio in PD until users find a "sweet spot" at which the track will maintain it's normal rate of play. I need to sort out communication between the 3 programs first so I can then focus on the encoder as I already have it communicating to PD through processing but not back the other way and I am currently using the pulse count to effect song speed. I then plan on having this drive a motor according to the speed also. This motor will then spin a record and will be placed inside an old gramophone to act as the user interface and hopefully drive the motor in the opposite direction using a h-bridge and the negative pulses. I didn't want to post in the wrong forum that's why I didn't fully describe my project and the concept of the project had to be developed before we even got a chance to investigate whether it was feasible or not, hence my lack of technical knowledge of the encoder.


Logged

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

I wouldn't understand the PD side, but seeing the Arduino code might provide some clues as to how to help you.

Quote
I then have a hand crank attached to the encoder that effects the speed of the track.
How? Does the Arduino push data or does PD pull data?

Quote
So when a track is changed by replacing the fiduscial the encoder values need to be reset back to zero so the process can once again repeat itself.
How to do this depends on whether the Arduino is pushing data or PD is pulling data. In any case, it is not the serial data that needs to be be reset. It is the value that is sent as serial data that needs to be reset.

Quote
I'm having trouble sending out secondary values along with the pulse data that I can use in the other programs and I can't find any examples that I can work with that would enable me to do so or if its possible
See line 1.

Quote
The fiduscial markers are the workaround I'm referring to, I was originally going to handle choosing the tracks to play in pd by using the arduino to do so (e.g button presses, rfid).   
What happened to that idea? Seems more logical to me.

Quote
I need to sort out communication between the 3 programs first so I can then focus on the encoder as I already have it communicating to PD through processing but not back the other way and I am currently using the pulse count to effect song speed.
Show the Processing sketch, too.

The Arduino is not talking to PD and Processing, is it? It only has one serial port that is connected to the PC via the USB cable. It can only talk to one application on the PC end.
Logged

Cork, Ireland
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The Arduino is not talking to PD and Processing, is it? It only has one serial port that is connected to the PC via the USB cable. It can only talk to one application on the PC end.
No I have Processing acting as a middleman of sorts to communicate between the two. PD is pulling data from processing and processing is using the processing serial to work with arduino.


Quote
I wouldn't understand the PD side, but seeing the Arduino code might provide some clues as to how to help you.
I have the PD side mostly finished but the arduino code I am using is based on a hardware debounce but I am now using an optical encoder for more stability and I have yet to rectify the code. I still must take out unnecessary attaches and detaches as well as using CHANGE instead of RISE and FALL.

The Arduino sketch:
Code:
int pulses, A_SIG=0, B_SIG=1;
int digitalInput5 = 5; 
int value5 = '0'; // switch setup to reset encoder values



void setup(){
  attachInterrupt(0, A_RISE, RISING);
  attachInterrupt(1, B_RISE, RISING);
  pinMode(digitalInput5,OUTPUT);

  Serial.begin(9600);
}//setup


void loop(){
  value5 = digitalRead(5);
   if (value5 ){
pulses=0; //pulses reset to zero
}
 
 
}

void A_RISE(){
 detachInterrupt(0);
 A_SIG=1;
 
 if(B_SIG==0)
 pulses++;//moving forward
 if(B_SIG==1)
 pulses--;//moving reverse
 Serial.println(pulses);
 attachInterrupt(0, A_FALL, FALLING);
}

void A_FALL(){
  detachInterrupt(0);
 A_SIG=0;
 
 if(B_SIG==1)
 pulses++;//moving forward
 if(B_SIG==0)
 pulses--;//moving reverse
 Serial.println(pulses);
 attachInterrupt(0, A_RISE, RISING); 
}

void B_RISE(){
 detachInterrupt(1);
 B_SIG=1;
 
 if(A_SIG==1)
 pulses++;//moving forward
 if(A_SIG==0)
 pulses--;//moving reverse
 Serial.println(pulses);
 attachInterrupt(1, B_FALL, FALLING);
}

void B_FALL(){
 detachInterrupt(1);
 B_SIG=0;
 
 if(A_SIG==0)
 pulses++;//moving forward
 if(A_SIG==1)
 pulses--;//moving reverse
 Serial.println(pulses);
 attachInterrupt(1, B_RISE, RISING);
}

The processing  then sketch trims off unnecessary white space and sends the data to PD as floats using the OSC messages and finally Pd recieves this data using a localhost connection.

The Processing sketch:
Code:
import processing.serial.*;
 
import oscP5.*;
import netP5.*;
// The serial port:
Serial myPort;

OscP5 oscP5;
NetAddress myRemoteLocation;


 
void setup() {
   size(400,400);
  frameRate(25);
  // List all the available serial ports:
  println(Serial.list());
 
  myPort = new Serial(this, Serial.list()[0], 9600);
 
    oscP5 = new OscP5(this,12000);
 
  myRemoteLocation = new NetAddress("127.0.0.1",12000);
     
}

 
void draw() {
      background(0); 

      String pulses_raw = myPort.readStringUntil('\n'); //read until the new line, like print ln
 
  if (pulses_raw != null) {
   
    // trim off any whitespace:
    pulses_raw = trim(pulses_raw);
   
    float pulses = float(pulses_raw);
 

println(pulses);

  // preparing message to send to PD   
 OscMessage myMessage = new OscMessage("/hello");
  //Attaches pulse values to send
  myMessage.add(pulses);
  //sends pulse data to PD
    oscP5.send(myMessage, myRemoteLocation);
   
  }
}

Quote
What happened to that idea? Seems more logical to me.
That was the original plan but I have been having a really hard time figuring how to use the encoder along with another sensor to print to serial so I can use the different values in PD. The answer may be staring me straight in the face and I may have needlessly complicated things for myself but I cannot seem to figure it out.
Logged

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

You can't do Serial.print() in an ISR. Get over it.

That renders most of your Arduino sketch rather useless. What you need to do is periodically, determine how many pulses have occurred, and divide that number by the time over which they arrived, and send that, as speed, to the Processing application.

Code:
      String pulses_raw = myPort.readStringUntil('\n'); //read until the new line, like print ln
You should not be doing serial input in draw. Add the serialEvent() callback, and do your serial input in serialEvent().

I'm not sure why you aren't sending something like "speed=someValue" and "lightLevel=someSensorReading" and "thisOtherValue=thatValue" to Processing, so you have a clue what the string you get represents.
Logged

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

you might find the examples in our OSC for Arduino library helpful.
https://github.com/CNMAT/OSC
Logged

Pages: [1]   Go Up
Jump to: