DESIGNED & ENGINEERED IN LONDON, UK

© 2019 Creoqode

Before starting this chapter, please make sure you have paid attention to position the servo motors to 90 degrees angle by connecting them to Nova Servo Shield during the assembly as told in the assembly booklet. You can refer to the page 32 and 33 of the assembly booklet which explains how to set the angle of the servo motors shafts to 90 degrees by connecting them to pin 46 one by one. Additionally, please also make sure that you have put all the casing parts together as shown in the illustrations in terms of their relative angular positioning. 

This is essential before continuing and starting to work with Nova, because the way Nova looks in the below photo is when all 5 of the servo motors are positioned at 90 degrees angle. This will be used as a reference for the movements of Nova during the rest of this educational guide.

Any mistakes during this stage will force Nova to move outside of the predefined motion area, which might easily damage the servo motors and the casing parts which are exposed to high stress levels.

After double checking the positioning of the servo motors and the casing parts, we can now connect Nova to the PC with the mini USB cable provided in the kit, to start programming to calibrate it. However, do not plug Nova to the power socket yet. The main purpose of this chapter is learning how to move Nova, which functions to use, and to understand the motion boundaries of Nova to prevent any potential damage in the future while coding your own applications.

First of all, let's connect the servo motors to the correct pin numbers on the Nova Servo Shield. Please find below the illustration and use it as a reference for your connections. Also make sure not to mix VCC, GND and I/O pins. I/O pins should match with orange wires, VCC with red wires, and GND with brown wires.

Now, we will open Arduino Software (IDE) and look at the libraries and functions that will be used to control servo motors and to move Nova.

PROGRAMMING NOVA WITH ARDUINO SOFTWARE (IDE)

First of all, we need to select the correct board, processor and communication port options from the "Tools" menu. You have to select "Arduino/Genuino Mega or Mega 2560" for the board, "ATmega2560 (Mega 2560)" for the processor, and the relevant Port option provided for your PC depending on your operating system. You can find all the details about this section in the previous chapter about Arduino Software (IDE).

After selecting the correct hardware options from the Tools menu, let's continue by creating our first sketch. To be able to communicate with the servo motors with the ease of a function, we need to use the built-in Servo Library provided with the Arduino Software (IDE).

This library allows Creoqode Mini Mega or an Arduino board to control servo motors. Servos have integrated gears and a shaft that can be precisely controlled. Servo motors allows the shaft to be positioned at various angles, between 0 and 180 degrees in Nova's case. The Servo library supports up to 48 servos on Creoqode Mini Mega and Arduino Mega. If you want to customise Nova in the future by adding new movement capabilities, remember that you have spare pins for 43 more servo motors! Note that servos draw considerable power, so if you need to drive more than one or two, you'll need to power them from a separate supply which is Nova Servo Shield in our case that is connected to a power socket.

To be able to use the servo functions while programming Nova, we have to include the library in our sketch by adding the below line on top:

#include <Servo.h>

Now that the Servo Library is included in our sketch, we can use the functions that are based on this library. First of all, let's start by creating and defining the servo motors of Nova. We are doing this step so that Creoqode Mini Mega knows to which of its pins the servo motors are connected to and where to send the required signal. The function we will be using to create servo motors is provided below. As Nova has 5 servo motors, we will create 5 servos:

Servo NovaServo_1;
Servo NovaServo_2;
Servo NovaServo_3;
Servo NovaServo_4;
Servo NovaServo_5;

We have created and defined 5 servo motors in our sketch, but we have not mentioned yet to which pins they are connected to on Nova Servo Shield. To assign a pin to each one of the servo motors we have created, the below function will be used:

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

Note that these functions will be placed inside the "void setup()" function that runs for once every time Creoqode Mini Mega is powered up or reset. Everytime you power up Nova, servo motors will be assigned to the required pins before going into the main body of your sketch, which is "void loop()" function. You can delete the comments (the lines of text after "//") as they just explain the role of setup() and loop() functions and do not contribute to the structure of the sketch.

While coding, it is always a good habit to put comments in your sketch that will remind you anything you think is important and essential. Not only for yourself, but also it makes it easier to understand your code for others who are reading it. You explain the role of the functions, the structure of the sketch, certain variables etc.

We will now write comments next to the servo motors we created, to remind us which servo is responsible for which movement. You can always use these comments as reference when programming different movements for Nova. It can be frustrating and time consuming to trace the wire of the servo everytime and check which pin it is connected to. As an example, we will be adding the below comments in our sketch, but you can write whatever you want unless it makes it easy for you to see the role of each servo.

Never forget to put "//" at the beginning of your comment. Otherwise, Arduino Software (IDE) will see your comment as command, and will generate an error message.

We will now use a function to position the servos in a specific angle. The function we will use for this purpose is below:

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

This function writes a value to the servo, controlling the shaft accordingly. For the servos of Nova, this will set the angle of the shaft (in degrees), moving the shaft to that orientation. With the above lines, we will be programming Creoqode Mini Mega to set the angles of all 5 servos of Nova to 90 degrees.

Please note that the above code lines can go into the "void loop()" function in order to continuously send position signals to the servos. Alternatively, if you want to use the above code lines to initiate Nova before going into the main body of your sketch, you can place them in the setup() function as well. 

Now, your first sketch is ready to be uploaded to Nova. This sketch will program Creoqode Mini Mega to send signals to all 5 servos and to set them all in 90 degrees angle. As you have already assembled Nova with servos oriented at 90 degrees, this sketch will put Nova in its stationery position which is seen in the illustration at the beginning of this chapter.

Before uploading the sketch to Nova, as a last warning, make sure you have set the servos to 90 degrees angle during the assembly and fitted the parts together with the same relative positions as shown in the assembly booklet. If not, do the assembly correctly in order to avoid any damages to Nova.

If everything is okay, click the upload symbol on the top left corner of Arduino Software (IDE) and your code should be uploaded. Now, plug Nova to a power socket with the power adapter provided in the kit. Nova should be positioned with all its servos set at 90 degrees. If you hear some noise coming from servos or see some servo jittering, it is completely normal. Congratulations! You have uploaded your first sketch to Nova. Now, it is time to play around with these functions in order to move Nova as we wish.

Firstly, let's start by changing the angle of Nova's face. For this purpose, we will need the servo which is connected to pin 34 (NovaServo_2). We need to comment all the other servo motors by putting "//" in front of them in the loop() function, so there is no need to set a specific angle to other servos apart from the one we will be working with.

//NovaServo_1.write(90);
NovaServo_2.write(90);
//NovaServo_3.write(90);
//NovaServo_4.write(90);
//NovaServo_5.write(90);

When changing the angle, always start with small increments or decrements. As your Nova is currently positioned at 90 degrees, you can start by changing the angle of the NovaServo_2 to 85 or 95 degrees angle. Change the function as below and upload it to Nova again by clicking the upload symbol on the top left corner of Arduino Software (IDE).

NovaServo_2.write(85);

Repeat the same procedure with 5 or 10 degree increments to see how Nova moves and also to see if there is any parts that collide in a specific position of the servo motors. Never forget that you are programming Nova from scratch and that there are no built-in collision detections unless you create one. This is why you are starting with small angle changes to get to know Nova and to prevent any damages.

IMPORTANT: Note that for NovaServo_2, NovaServo_3 and NovaServo_4, the angle range you can set for these servos are quite wide. Most of the times, these servos allow you to use 0 - 180 degrees range without any collisions or causing high stress levels for the casing parts or servos. However, this is not the same case for NovaServo_1 and NovaServo_5. The angle range you can use for these two servos are narrower because of the design. Be extra careful when you are trying to find the angle limitations for these two, and never exceed 5 degrees range when incrementing the angle until you feel confident about using Nova.

By uncommenting each servo individually (by deleting and adding "//" in the beginning of the relevant write() functions), discover the angle range you can use for each one of the servos. Getting to know the angle range that can be used for each servo and feeling confident about the movement characteristics of Nova will help a lot during the upcoming chapters.

CONTINUOUS MOTION WITH FOR-LOOP

Now, we will be changing our sketch slightly by adding a couple of more lines so Nova repeats a set of pre-defined motion commands, instead of positioning to a fixed angle that is set in the sketch.

The for-loop statement is used to repeat a block of statements enclosed in curly braces. An increment counter is usually used to increment and terminate the loop. The for statement is very useful for any repetitive operation, and we will be using it a lot while working with Nova. The syntax of for-loop can be seen below:

for (initialization; condition; increment) {
 //statement(s);
}

Simply, the working mechanism for for-loop can be explained in a couple of sentences. The initialization happens first and exactly once. Each time through the loop, the condition is tested; if it’s true, the statement block, and the increment is executed, then the condition is tested again. When the condition becomes false, the loop ends. This will make much more sense with an actual example on Nova.

Let's create a for-loop so that NovaServo_2 continuously increments its shaft angle from 40 degrees to 140 degrees, and then decrements it from 140 degrees to 40 degrees with 30 milliseconds between each angle.

int x = 1;
for (int i = 50; i > 49; i = i + x){
      NovaServo_2.write(i);
      if (i == 130) x = -1;             // switch direction at peak
      delay(30);
}

Make sure you place these code lines in the loop() function, and do not forget to comment the previous servo write() functions. Please see below as reference. When you upload it, Nova should continuously repeat the movement that was explained in the previous paragraph.

This sketch provides a repeated motion for Nova with a constant angular speed of 1 degree per 30 milliseconds. Let's add a couple of more lines in the sketch so that the movement becomes smoother. Within the for-loop, we need to also increment and decrement the milliseconds in the delay() function, so that movement is the fastest at 90 degrees position, and slowest at 50 and 130 degrees position. Let's change the for-loop as below:  

int x = 1;
int timer = 45;
for (int i = 50; i > 49; i = i + x){
      NovaServo_2.write(i);
      if (i < 90) timer = timer - x;
      if (i > 90) timer = timer + x;
      if (i == 130) x = -1;      // switch direction at peak
      delay(timer);
}

We have firstly created a new variable in the loop() function which is called "timer". As an initial value, it has been equaled to 45 milliseconds. At each iteration of the for-loop, value of "timer" is decremented by 1 until the shaft position of the servo is 90 degrees. When the shaft position exceeds 90 degrees, "timer" is incremented by 1 until the rotation direction switches at 130 degrees.

As you will notice, the variable "timer" is placed in the delay() function. The fact that "timer" is decremented by 1 at each iteration means the duration between each angular position change shortens. In simpler terms, as "timer" decreases, angular speed of the servo increases. As "timer" increases, angular speed of the servo decreases.

You can change the value of the "timer", the angular motion range, towards which position of the shaft the angular speed increases or decreases etc. Play with the values, use the same technique to move other servos as well.

Congratulations, you can now use Nova's servo motors to their full movement capabilities. To fully understand and feel confident about this chapter, practice what you have learned by programming Nova for a series of movements that takes at least 30 seconds to complete, by using all the servos.