0

Arduino tečaj: osnove

1. lesson

Ovo je dio cjelovitog tečaja Arduino-a.
  1. Štampajte dijelove koristeći ovaj Sketchup model RescueLine.zip.
  2. Sastavite robota koristeći upute za sastavljanje.
  3. Spojite kablove koristeći plan spajanja.
  4. Programirajte robota.
    1. Osnove (ova stranica).
    2. Praćenje crte.
    3. Naredbe.
    4. LIDARi.
    5. Prekidač, IMU, 8x8 LED.
    6. Praćenje zida.
    7. Srebro.
    8. 8x8 LED programiranje.
    9. Robotska ruka.
    10. Raspberry Pi.
    11. Prepoznavanje kamerom, OpenCV.
    12. Arduino i RPI surađuju.

Arduino

  • Download and install Arduino IDE.
  • Download and install Teensyduino.
  • Download all the ML-R libraries (or at least Commands, Displays, LEDS, Motors, PID, Reflectance sensors, Switches, TimeMeasure, and VL53L0X) using https://www.github.com/PribaNosati/MRMS. Download zip files, start Arduino IDE and add libraries one by one, selecting Sketch - Include Library - Add .ZIP Library.

C++

You will have to learn it, at least basics. Learning is beyond scope of these lessons. The part of C++ You will need here is C. C is a subset of C++. If You learn basics of C, that will be OK for the beginning: variables, loops (for, while), branching (if), functions. Try http://www.learn-c.org/ or some other web. Lessons for C++ tend to be too complicated for a beginner and Arduino programming doesn't demand too many advanced topics to get the job done.

First program

Enter this code in the main window of Arduino IDE:
void setup(){
}

void loop(){
}

Here we have 2 functions: setup and loop. When Arduino microcontroller is powered up, setup is run once. After that loop is is executed an endless number of times.

Power on Teensy board by switching on regulator board using the switch. Connect Teensy to Your PC using a USB cable.

Go to Tools - Board and choose Teensy 3.2.

Go to Tools - Port and anc choose COMXX where XX matches the port number where Teensy is connected.

Find Nike button (first on the left, "Verify") and click it. The sketch (program You made) will be compiled. Message "Done compiling" reports success. If You get an error message, You will have to correct the program.

Second program

Previous program doesn't do anything so we will extend it. Watch for // parts of the code in the next example. Arduino ignores everything You type after //, in that line. In this way we add comments.

void setup() {
  Serial.begin(115200);    // Serial (USB) communication to our computer is established. You call function begin of the object Serial with 1 argument: 115200.
  delay(1000);              // Do nothing for 1000 ms (1 sec.) - wait for the USB connection.
  Serial.println("Start"); // Display "Start" on PC's screen. Function println() prints the argument ("Start") and jumps to a new line.
}

void loop() {
}

Adjust speed of Your serial monitor to match the speed in function Serial.begin(): 115200. Power up Teensy, connect it to Your PC and Use top-left button ("Serial monitor"). Select 115200 from the botoom-right drop-down list.

Keep the monitor window open. Press second button (arrow to left, "Upload"). The program will be compiled, uploaded to Teensy and will start immediately. Program will print "Start":

Motors

We will now include a library for motor control. Extend the previous code with orange parts:

#include <Motors.h> //Library Motors will be used. It contain motor-control functions.

Motors motors; //motors is an C++ object of class Motors. If You do not understand this, never mind. Just always use motors with this line.

void setup() {
  Serial.begin(115200);    // Serial (USB) communication to our computer is established. You call function begin of the object Serial with 1 argument: 115200.
  delay(1000);             // Do nothing for 1000 ms (1 sec.) - wait for the USB connection.
  Serial.println("Start"); // Display "Start" on PC's screen. Function println() prints the argument ("Start") and jumps to a new line.

  //In the following 4 lines we will add 4 motors. First 2 parameters, numbers (like 3, 5), are pins that are connected
  //to motor controller. Third parameter (true or false) is true when it is a left motor, otherwise false. There is an optional fourth parameter in the
  //first 2 lines and there can be even more of them. Optional (default) parameters do not have to be mentioned.
  motors.add(3, 5, true, true);
  motors.add(23, 4, false);
  
  motors.test(); //With all the motors connected, we can test them.
}

void loop() {
}

/**All the ML-R libraries invoke this function when something goes wrong. Here we stop the motors, display the error
message which enters the function as the only argument ("message") and then enter an endless loop. while(1) never ends as the condition
(1) is always true. The only command that is in the loop is ";" (empty command - does nothing).
@param message*/
void error(String message){
  motors.go(0, 0);         //stop the motors to avoid harm
  Serial.println(message); //display error message
  while(1)                 //never stop
    ;                      //do nothing
}

Before running this program, we will save it. Press save button (arrow pointing downwards, "Save"):



A message might pop up. Press OK:



Name Your sketch "RescueLine" and press Save:



From now on You can open the sketch using "Open" button (arrow pointing upwards):



Names of Your programs are listed below "libraries". Choose "RescueLine":



Now run the program as the last one, but first allow the wheels to spin freely, not touching any surface. Monitor should display this:



The motors should be spinning according to the display. If the direction of a motor is wrong, correct it by changing (or adding) the fourth parameter (direction - true to false or false to true). Run the program again.

With motors set up correctly, You are ready to move Your robot. Amend the program by deleting the motors.test() line and adding a new code:

#include <Motors.h> //Library Motors will be used. It contain motor-control functions.

Motors motors; //motors is an C++ object of class Motors. If You do not understand this, never mind. Just always use motors with this line.

void setup() {
  Serial.begin(115200);    // Serial (USB) communication to our computer is established. You call function begin of the object Serial with 1 argument: 115200.
  delay(1000);             // Do nothing for 1000 ms (1 sec.) - wait for the USB connection.
  Serial.println("Start"); // Display "Start" on PC's screen. Function println() prints the argument ("Start") and jumps to a new line.

  //In the following 4 lines we will add 4 motors. First 2 parameters, numbers (like 3, 5), are pins that are connected
  //to motor controller. Third parameter (true or false) is true when it is a left motor, otherwise false. There is an optional fourth parameter in the
  //first 2 lines and there can be even more of them. Optional (default) parameters do not have to be mentioned.
  motors.add(3, 5, true, true);
  motors.add(25, 21, true, true);
  motors.add(23, 4, false);
  motors.add(6, 22, false);
  
  //The following 3 lines will move the robot forwards and stop after 1 sec.
  motors.go(30, 30); //Turn the left and right motors using the same speed: 30 (100 is maximum).
  delay(1000);       //Wait 1 sec. (while the motors are running, so the robot goes forwards).
  motors.go(0, 0);   //Stop the motors.
}

void loop() {
}

/**All the ML-R libraries invoke this function when something goes wrong. Here we stop the motors, display the error
message which enters the function as the only argument ("message") and then enter an endless loop. while(1) never ends as the condition
(1) is always true. The only command that is in the loop is ";" (empty command - does nothing).
@param message*/
void error(String message){
  motors.go(0, 0);         //stop the motors to avoid harm
  Serial.println(message); //display error message
  while(1)                 //never stop
    ;                      //do nothing
}
That's it, this example is over. However, a curious user might ask himself what other functions of Motors library exist or what other parameters of the used functions (go, add) are possible. And yes, there are other functions and other parameters. In Arduino IDE they are not visible. You can use Visual Studio together with Visual Micro to get a much powerful IDE (Integrated Development Environment). If not, view the library's files in an editor (Notepad++ is excellent). Arduino saves sketches and libraries in "Documents\Arduino" folder:



View Motors.h file to learn all the possibilities. You can also change Motors.cpp if You are experienced enough.

Previous lesson
Next lesson