# Midterm: relaxing punching bag

Today we’re presenting our midterm, yey! The project was made with Oscar and Rafa

The idea of this post is to document the process, and explain some of the code.

First, a video showing what’s about:

We have a ‘punching bag’ (right now just a backpack), that when it’s hit it triggers relaxing music.

Concept documentation

The concept didn’t change much from the previous step, so reading that post may help.

Workflow

```1. Punching bag moves
2. Accelerometer attached in bag detects acceleration
3. Arduino receives accelerometer values
4. Arduino sends serial data
5. Pure data receives serial data
6. Pure data triggers MIDI notes based on data
7. Ableton plays music based on the midi notes received```

Arduino code

```void setup()
{
// initialize the serial communications:
Serial.begin(9600);
int i;
for(i = 0; i < 100; i++){
avgX += analogRead(A0);
avgY += analogRead(A1);
}
avgX = avgX / i;
avgY = avgY / i;
}```

As explained in another post, we do a calibration for the accelerometer in the setup() phase. This way, avgX and avgY end up storing the still values for the speed components.

```void loop()
{

vx = map(analogRead(A0) - avgX, minv, maxv, 0, 255);
vy = map(analogRead(A1) - avgY, minv, maxv, 0, 255);

if(abs(vx - oldx) > threshold || abs(vy - oldy) > threshold) {
vx = vx != 127 ? vx : 128; //we avoid submitting 127
vy = vy != 127 ? vy : 128; //we avoid submitting 127
Serial.write(vx);
Serial.write(vy);
oldx = vx;
oldy = vy;
delay(200);
}

}```

analogRead() – avg gives the calibrated value.
Then, we scale that value between 0 and 255 to fit in a byte.
maxv and minv are the maximum and minimum values obtained by the accelerometer, after calibration. These values were obtained empirically. For the accelerometer used, they are 550 and -550 respectively.

We only write data into serial if the new values from the accelerometer are different enough from the old values sent (the if condition:
abs(vx – oldx) > threshold || abs(vy – oldy) > threshold). This is to avoid playing music sounds because of oscillation, and also to achieve that each punch should only trigger few times a new sound. If we don’t do this, the noteout command from PD overwrites the previous one. That clearly cannot happen at every loop

We never submit 127 because that is 0 speed, and in our PD patch we need the speeds to be always != 0.

Lastly, we update the old values with the ones just sent.

Pure data (PD)

The comport command listens to serial communication on the port specified on the open command above it.

Then, we unpack the two values (vx and vy), and we substract 127, because a value received is between 0 and 255 but speed can go both ways, so actually the middle value is the no speed (that’s why in arduino we never send 127).

With these values, then, we will generate the 8 possible chords played. The chord played is based on the relation between vx and vy: vx / vy. With this (and based on if they are positive or negative), we divided the plane in 8 regions. All the right part of the sketch is arithmetic to determine which chord to play.

The amount of speed is used to set the velocity of the note (MIME protocol). The amount of speed is determined by sqrt(vxˆ2 + vy^2). We divide this value by 2 to make it fit in a byte.

Lastly, the noteout command sends the note to the channel specified.