# Processing program using OSC messages

**URL:** <https://discourse.processing.org/t/processing-program-using-osc-messages/6147>\
**Category:** Libraries\
**Created:** [December 1, 2018, 5:16pm UTC](https://discourse.processing.org/t/processing-program-using-osc-messages/6147 "2018-12-01T17:16:41Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![justin](https://avatars.discourse-cdn.com/v4/letter/j/3ab097/32.png) [@justin](https://discourse.processing.org/u/justin)\
**Post date:** [December 1, 2018, 5:16pm UTC](https://discourse.processing.org/t/processing-program-using-osc-messages/6147/1 "2018-12-01T17:16:41Z")

</div>

Hello friends,  
I am beginner in this programming platform.Can anybody help me with a code to convert the below described program with OSC message using accelerometer data from an android phone.It is a simple program which displays two arms and they move according to mouse movement.I am trying to modify this program using OSC messages.Thanks in advance.

```auto
float x, y;
float angle1 = 0.0;
float angle2 = 0.0;
float segLength = 100;

void setup() {
  size(640, 360);
  strokeWeight(30);
  stroke(255, 160);
  
  x = width * 0.3;
  y = height * 0.5;
}

void draw() {
  background(0);
  
  angle1 = (mouseX/float(width) - 0.5) * -PI;
  angle2 = (mouseY/float(height) - 0.5) * PI;
  
  pushMatrix();
  segment(x, y, angle1); 
  segment(segLength, 0, angle2);
  popMatrix();
}

void segment(float x, float y, float a) {
  translate(x, y);
  rotate(a);
  line(0, 0, segLength, 0);
}

```

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [December 2, 2018, 10:27am UTC](https://discourse.processing.org/t/processing-program-using-osc-messages/6147/2 "2018-12-02T10:27:49Z")

</div>

Hi there 🙂 Your question is rather vague and the answer is probably “yes” (someone can help you). You didn’t mention what you tried and what failed to work. Here some great ideas on how to get help faster: [Guidelines—Tips on Asking Questions](https://discourse.processing.org/t/guidelines-tips-on-asking-questions/2147) 🙂

---

<div class="post-metadata">

**Author:** ![justin](https://avatars.discourse-cdn.com/v4/letter/j/3ab097/32.png) [@justin](https://discourse.processing.org/u/justin)\
**Post date:** [December 2, 2018, 1:31pm UTC](https://discourse.processing.org/t/processing-program-using-osc-messages/6147/3 "2018-12-02T13:31:52Z")

</div>

Thanks for your advice.I made the following changes to the program.I used accelerometer data.The arm is shaking according to accelerometer data but its not showing a fix position as the accelerometer data is also fluctuation.what changes should i make to stabilize the arm?

```auto
float p, q;
float angle1 = 0.0;
float angle2 = 0.0;
float segLength = 100;
import oscP5.*;
import netP5.*;
OscP5 osc;

void setup() {
  
  osc = new OscP5(this, 10000);
  osc.plug(this, "shake", "/accelerometer");
  
  
  size(640, 360);
  strokeWeight(30);
  stroke(255, 160);
  
  p = width * 0.3;
  q = height * 0.5;
}

void draw() {
}
void shake(float x, float y, float z){
  background(0);

  println(x+"," + y+","+ z);
  angle1 = 0;
  angle2 = (x);
  
  pushMatrix();
  segment(p, q, angle1); 
  segment(segLength, 0, angle2);
  popMatrix();
}

void segment(float p, float q, float a) {
  translate(p, q);
  rotate(a);
  line(0, 0, segLength, 0);
}

```

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [December 2, 2018, 2:26pm UTC](https://discourse.processing.org/t/processing-program-using-osc-messages/6147/4 "2018-12-02T14:26:54Z")

</div>

Great question 🙂

A very simple approach to smooth out a noisy `y` variable would be to replace something like this:

```
// make x equal y NOW
x = y; // x is as noisy as y

```

with this

```
// move x 9% towards y
x = lerp(x, y, 0.09); // x is less noisy than y

```

so even if y is jumping up and down, x will slowly drift towards y. One issue of this approach is that it adds some latency, so x follows y after some frames or seconds depending on how small the third parameter is. Latency and smoothing increases as that third parameter decreases.

If you need a better smoothing formula you could use this library instead:

> **[SableRaf/signalfilter](https://github.com/SableRaf/signalfilter)**
>
> A signal filtering library for Processing. Contribute to SableRaf/signalfilter development by creating an account on GitHub.

Unrelated tips:

- Please don’t delete things you have written before in the forum because it makes the thread harder to read. Think that when you ask a question is not only for you, but for everyone to learn from, so it’s useful to see how you improved your question above 🙂
- You can format your code in the forum nicely with syntax highlighting by editing your post, selecting the code, and pressing the `</>` button in the post editor.

Welcome to the new forum! 🙂

---

<div class="post-metadata">

**Author:** ![justin](https://avatars.discourse-cdn.com/v4/letter/j/3ab097/32.png) [@justin](https://discourse.processing.org/u/justin)\
**Post date:** [December 4, 2018, 7:20pm UTC](https://discourse.processing.org/t/processing-program-using-osc-messages/6147/5 "2018-12-04T19:20:53Z")

</div>

I added the codes to the program. Its working fine. Even though it is not 100% stable.My idea is to rotate the arm according to the angle created by the phone during tilting.can i do it with the accelerometer data?I tried by using rotateX(x) command.But i think it is not the right idea.

```auto
float p, q;
float angle1 = 0.0;
float angle2 = 0.0;
float segLength = 100;
import oscP5.*;
import netP5.*;
OscP5 osc;

void setup() {
  
  osc = new OscP5(this, 10000);
  osc.plug(this, "shake", "/accelerometer");
  
  
  size(640, 360);
  strokeWeight(30);
  stroke(255, 160);
  
  p = width * 0.3;
  q = height * 0.5;
}

void draw() {
}

void shake(float x, float y, float z){
  background(x*2.5,y*2.5,z*2.5);

  println(x+"," + y+","+ z);
  x = y;
  x = lerp(x, y, 0.09);
  angle1 = 0;
  angle2 = (x);
  
  pushMatrix();
  segment(p, q, angle1); 
  segment(segLength, 0, angle2);
  popMatrix();
}

void segment(float p, float q, float a) {
  translate(p, q);
  rotate(a);
  line(0, 0, segLength, 0);
}

```

---

<div class="post-metadata">

**Author:** ![justin](https://avatars.discourse-cdn.com/v4/letter/j/3ab097/32.png) [@justin](https://discourse.processing.org/u/justin)\
**Post date:** [December 19, 2018, 9:11pm UTC](https://discourse.processing.org/t/processing-program-using-osc-messages/6147/6 "2018-12-19T21:11:49Z")

</div>

I used an application called “control” to send accelerometer data from phone  
which i got from a processing tutorial video.  
[https://funprogramming.org/102-Control-Processing-tilting-your-phone.html](https://funprogramming.org/102-Control-Processing-tilting-your-phone.html)

The problem is that, I am getting false accelerometer data.Instead of showing 0 to 9.8 it is showing big values 0 to 120.  
My aim is to get the tilt angle of the phone according to the accelerometer data.Any suggestions will be a great help.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [December 19, 2018, 10:47pm UTC](https://discourse.processing.org/t/processing-program-using-osc-messages/6147/7 "2018-12-19T22:47:44Z")

</div>

> [@justin](#):
>
> I am getting false accelerometer data

Is the data bad, or is the range just not what you expect and something that needs to be scaled?

That is, does it react more to big movements than little ones, and does it give 0 for no movement?

---

<div class="post-metadata">

**Author:** ![justin](https://avatars.discourse-cdn.com/v4/letter/j/3ab097/32.png) [@justin](https://discourse.processing.org/u/justin)\
**Post date:** [December 20, 2018, 11:45am UTC](https://discourse.processing.org/t/processing-program-using-osc-messages/6147/8 "2018-12-20T11:45:03Z")

</div>

The range of accelerometer data is not the one that i expected. It is of the of the range 0 to 125. I need the value in range of -9.8 to + 9.8. So that I can get the tilt angle by equation;  
Theta=Tan^-1(x/y)

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [December 20, 2018, 5:13pm UTC](https://discourse.processing.org/t/processing-program-using-osc-messages/6147/9 "2018-12-20T17:13:28Z")

</div>

> [@justin](#):
>
> It is of the of the range 0 to 125. I need the value in range of -9.8 to + 9.8.

Use `map`: [map() / Reference / Processing.org](https://processing.org/reference/map_.html)

> map(value, start1, stop1, start2, stop2)

so:

```auto
mappedVal = map(rawVal, 0, 125, -9.8, 9.8);

```

---

<div class="post-metadata">

**Author:** ![justin](https://avatars.discourse-cdn.com/v4/letter/j/3ab097/32.png) [@justin](https://discourse.processing.org/u/justin)\
**Post date:** [December 21, 2018, 8:10pm UTC](https://discourse.processing.org/t/processing-program-using-osc-messages/6147/10 "2018-12-21T20:10:53Z")

</div>

Thanks for your tips. I got the the filtered accelerometer data value. In our example, we tied an android smart phone on the lower arm in order to get the movement of arm and we are trying to interpret the movement in a 2D model. Their is a small problem in our model.In one quadrant the model is not moving according to the actual movement of arm.  
Video link:

[![](https://img.youtube.com/vi/jUpG0gELnI4/hqdefault.jpg "WhatsApp Video 2018 12 21 at 8 18 27 PM 1") ](https://www.youtube.com/watch?v=jUpG0gELnI4)

I am attaching three photos, which showing the the three different positions.

1. At no movement ![AIO](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/0/08707c28946c30300af7b829a7089af280e2ed25.png)

2)Vertical position

1. Tilting backward from vertical position
