top of page

CHAPTER

10

CONTROLLING NOVA WITH

MOUSE & KEYBOARD

In this chapter, we will be learning how to control Nova with the mouse and keyboard of your PC using Processing IDE.

 

First, we need to program Nova in order to make it receive data from our PC and act accordingly. Let's start by opening Arduino IDE and adding the below code lines to our sketch.

#include <Servo.h>

Servo NovaServo_1;      //Head Movement - Front and Back
Servo NovaServo_2;      //Head Rotation - Clockwise and Anticlockwise
Servo NovaServo_3;      //Head Rotation - Up and Down
Servo NovaServo_4;      //Whole Body Rotation - Z axis
Servo NovaServo_5;      //Head Movement - Up and Down

int serialCount = 0;
int serialInArray[4];

 

001.JPG

We are creating an array of 4 elements (serialInArray[4]) to store the data received from our PC. The reason there are 4 elements in the array is because 4 of the servos will be used for this tutorial. If you decide to use the 5th one as well, you can create an array of 5 elements.

serialCount is the variable we will be using to assign each data received from our PC to a different element of the array we created.

Let's continue with the setup() function of our sketch.

Serial.begin(9600);


Serial.setTimeout(10);

 

NovaServo_1.attach(32);
NovaServo_2.attach(34);
NovaServo_3.attach(36);
NovaServo_4.attach(38);

//NovaServo_5.attach(40);

 

NovaServo_1.write(90);
NovaServo_2.write(90);
NovaServo_3.write(110);
NovaServo_4.write(90);

//NovaServo_5.write(90);

 

002.JPG

Now, add the below lines to your loop() function.

while(Serial.available() == 0);

serialInArray[serialCount] = Serial.read();
 

serialCount++;

 

if (serialCount > 3){

 

NovaServo_4.write(serialInArray[0]);
NovaServo_3.write(serialInArray[1]);
NovaServo_2.write(serialInArray[2]);
NovaServo_1.write(serialInArray[3]);

 

serialCount = 0;

}

The above lines first check if there is any data coming from PC. If data is coming, it saves each data to a different element of the array. If the variable serialCount exceeds 3 (when the array is full), each element of the array is sent to a different servo motor as we define.

That's it! You can now upload this sketch to Nova.

003.JPG

Let's now open Processing IDE and start creating our sketch so that we can send the data from the mouse and keyboard of the PC. 

Start by adding the below lines before the setup function.

import processing.serial.*;

Serial myPort;

int default1 = 90;
int default2 = 110;
int default3 = 90;
int default4 = 90;

Then, in the setup() function, we will be creating a window of size 360 pixels by 360 pixels and defining the communication port through which the data is going to be transferred. You need to change the "COM4" to the port you are using to program Nova on Arduino IDE.

 size(360, 360);
 myPort = new Serial(this, "COM4", 9600);
 myPort.bufferUntil('\n');

 

004.JPG

For the draw() function, which is the equivalent of loop() function on Arduino IDE, we will start by defining the background colour of the window we are creating.

 

Then, we will set an if statement: If the left click of the mouse is pressed while the cursor is on the window we created, Processing IDE will send the position of the mouse (according to the pixel coordinates of the window) to Nova. Which means you will be able to control Nova by moving the mouse on the window created while pressing left click.

 

Let's have a look below:

background(0,0,0);
 
if(mousePressed && (mouseButton == LEFT)) {
   
default1 = mouseX/2;
default2 = mouseY/2;

 

myPort.write(mouseX/2);
myPort.write(mouseY/2);
myPort.write(default3);
myPort.write(default4);

 

}

The reason we are taking the half value of mouse coordinates (mouseX/2, mouseY/2) is because the window created has 360 pixels in x axis, and 360 pixels in y axis. In this case, centre of the window has coordinates (180, 180). By dividing this value by 2, we get (90, 90) which can directly be sent to the servo motors, as the range of the movement will be between 0 to 180 degrees.

 

005.JPG

We are now going to do the same if statement with the right click this time. Then, we will create another if statement that updates the "default1" variable depending on the pressed keys of keyboard.

 

if(mousePressed && (mouseButton == RIGHT)){
   
default3 = mouseX/2;
default4 = mouseY/2;
 
myPort.write(default1);
myPort.write(default2); 
myPort.write(default3);
myPort.write(default4);
}
 
if(keyPressed && (key == 'a')){
myPort.write(default1);
myPort.write(default2);
myPort.write(default3);
myPort.write(default4);
}

 

if(keyPressed && (key == 'd')){
myPort.write(default1);
myPort.write(default2);
myPort.write(default3);
myPort.write(default4);
  }
}

 

 

006.JPG

Finally, we need to define the function "keyPressed" in order to tell tell our PC what to do in case of keys "a" or "d" is pressed. Make sure to add the lines below out of draw() function.

 

void keyPressed(){


 switch(key){


   case 'a':
   default1 = default1 + 2;
   break;
  
   case 'd':
   default1 = default1 - 2;
   break;


}
}

You can simply create as many cases as you wish like above. When "a" is pressed, angle of the servo shaft will increase, and when "d" is pressed it will decrease.

 

 

 

007.JPG

That's it! You can now run your program on Processing IDE. When the window with black background appears, gently move your cursor on it while pressing the left or right click. Alternatively, you can use "a" and "d" keys of your keyboard as well to control Nova.

 

 

 

 

bottom of page