Final Arduino Code

On this post I will write the final Arduino code. It is set up to receive byte from Processing divided into three range of numbers. Probably is not the cleanest of the solutions, but unfortunately I did not manage to send strings of byte or to set up a protocol to read the three bands of signals coming from Processing. Below the code, that I will try to explain bit by bit.

First of all I declare the “incomingByte” variable, which will be the variable that will store perhaps the incoming bytes from the Processing console. I declare also the variable “tuning” that will serve as a multiplier to fine tune the output from Arduino to the fans. Finally, I initialize the serial port and set the baud rate to the maximum.

byte incomingByte;
int tuning = 6;
void setup()
{
Serial.begin(115200);
}

I set the Arduino board on a listening status. I start the conditional that act as a switch. If serial data arrives, than go to the next section.

void loop()
{
if (Serial.available())
{

While receiving the serial data (the function Serial.available() gives me the response about the reception of serial data), I read the serial port and store the first byte coming into the “incomingByte” variable. I then, transform it into an integer, ready to be written on the PWM pin of Arduino.

while (Serial.available())
{
incomingByte = Serial.read();
// Transform the byte into a numeric value
int pointer=int(incomingByte);

Here I start the sub selection. As I said, because I did not find out a way to send multiple bytes from Processing all at once, I set Processing in a way that will output the signal for the bass on a range of numbers that go from 32 to 107, the medium from 108 to 182, and the high frequency from 183 to 255. I omitted bytes values from 0 to 32 because they are special characters, and often gave my errors during the reading. So, if Arduino receive a byte for the bass (between 32 and 107), with a proportion, I calculate the percentage of a range between 0 and 255, which is the output available for the analogWrite function, related to the range that I set in Processing which goes from 0 to 74. I used the proportion on this way: 255:x=y:74. Because I know the value of y, that is the numeric value of the incomingByte variable, then is easy to find the value of x, which is given by the following operation: 255/74*x. I added, at the end, the tuning variable, which is the multiplier of the result given by the operation, just in case values where too small to make the fans turn around. Once I get the final value, this number is sent to the desired pin using the Arduino function analogWrite(number_of_pin, value_between_0_and_255) that output the signal using the technique of the Pulse Width Modulation, used by digital devices to simulate analog output. More information about PWM can be found here: Link.

// Range for bass
if (pointer > 32 && pointer < 107) {
int incomingInt = int(incomingByte)-33;
int bass = (255/74*incomingInt) * tuning;
analogWrite(9,bass);
}

// Range for medium
if (pointer > 108 && pointer < 182) {
int incomingInt = int(incomingByte)-107;
int medium = (255/74*incomingInt)* tuning;
analogWrite(10,medium);
}

// Range for high
if (pointer > 183 && pointer < 255) {
int incomingInt = int(incomingByte)-183;
int high = (255/74*incomingInt)*tuning;
analogWrite(11,high);
}
}
}
}

Finish! I close the conditional statements, and the loop status and upload everything into Arduino. Let’s get to the Processing code now: Link!

No Comments

No comments yet.

Comments RSS

Sorry, the comment form is closed at this time.