Use the machine learning program Wekinator to play different noises through buzzers connected to an Arduino UNO.
In this article, part 7 of a larger machine learning series, you are going to learn about modulating and playing different noises from Arduino using machine learning through the Wekinator platform. We will give input to the Wekinator from processing, and after training the Wekinator, we will play the noises from Arduino using the output of Wekinator.
Catch up on the rest of the Wekinator Machine Learning series here:
Circuit Diagram
Connect the positive of one of the buzzers to pin 9 of Arduino and the positive of the other buzzer to pin 10 of Arduino. Then connect the negatives of both buzzers to the ground of Arduino.
How to Run the Program
First of all, paste the code given for the Arduino in the end of this post in the Arduino IDE and upload the code.
Then you will need to download the sketch from the quick walkthrough page of Wekinator.
Download the on-screen mouse control example. Unzip it and run the sketch in processing. This sketch will give the input to the Wekinator. You will need another sketch for the output from the Wekinator. The code for that sketch is at the end of this post. Paste that into processing and run it. Both the processing output windows will look like shown below.
Now open Wekinator and make the settings as shown in the figure below. Set the inputs and the output to 2. Set the type to custom and click on “configure”. You can also check out the attached video below to see the process
When you click on “configure”, a new window will open up, as shown below. Set the settings in that window as shown in the figure below.
Now drag the green box in the processing window to the bottom left corner and click on “randomize”. Start the recording for half a second.
Drag the green box in the processing window to the center top and click on “randomize”. Start the recording for half a second.
Drag the green box in the processing window to the bottom right corner and click “randomize”. After that, start the recording for half a second.
Then click on “Train” and then “Run”. Now when you drag the green box in the processing window, the Arduino will make noises according to that.
Try experimenting with different interfaces or even attempt making music using this graphical interface synthesizer.
Processing Code (Output From Wekinator)
import vsync.*; // Importing the library that will help us in sending and receiving the values from the Arduino
import processing.serial.*; // Importing the serial library
// Below libraries will connect and send, receive the values from wekinator
import oscP5.*;
import netP5.*;
// Creating the instances
OscP5 oscP5;
NetAddress dest;
ValueSender sender;
// These variables will be syncronized with the Arduino and they should be same on the Arduino side.
public int output;
public int output1;
void setup()
{
// Starting the serial communication, the baudrate and the com port should be same as on the Arduino side.
Serial serial = new Serial(this, "COM10", 19200);
sender = new ValueSender(this, serial);
// Synchronizing the variables as on the Arduino side. The order should be same.
sender.observe("output");
sender.observe("output1");
// Starting the communication with wekinator. listen on port 12000, return messages on port 6448
oscP5 = new OscP5(this, 12000);
dest = new NetAddress("127.0.0.1", 6448);
}
// Recieve OSC messages from Wekinator
void oscEvent(OscMessage theOscMessage) {
if (theOscMessage.checkAddrPattern("/wek/outputs") == true) {
// Receiving the output from wekinator
float value = theOscMessage.get(0).floatValue(); // First output
float val = theOscMessage.get(1).floatValue(); // Second output
// Converting the output to int type
output = int(value);
output1 = int(val);
}
}
void draw()
{
// Nothing to be drawn for this example
}
Arduino Code
#include <VSync.h> // Including the library that will help us in receiving and sending the values from processing
ValueReceiver<2> receiver; /*Creating the receiver that will receive up to 2 values.
Put the number of values to synchronize in the brackets */
/* The below two variables will be synchronized in the processing
and they should be same on both sides. */
int output;
int output1;
// Pin connected to buzzer
int buzzer = 9;
int buzzer1 = 10;
int i,j;
void setup()
{
/* Starting the serial communication because we are communicating with the
Arduino through serial. The baudrate should be same as on the processing side. */
Serial.begin(19200);
// Synchronizing the variables with the processing. The variables must be int type.
receiver.observe(output);
receiver.observe(output1);
// Defines the Buzzer pins as output
pinMode(buzzer,OUTPUT);
pinMode(buzzer1,OUTPUT);
}
void loop()
{
// Receiving the output from the processing.
receiver.sync();
// Making the buzzer to beep according to the output from the processing
tone(buzzer1, output);
delay(5);
noTone(buzzer1);
tone(buzzer,output1);
delay(5);
noTone(buzzer);
}