Beginner help please


#1

Hi,
I’m a teacher and have recently begun teaching coding with 11-13 year old kids and find block coding is great for teaching them the basics of coding. I’m wanting to extend things further next year and am looking into purchasing some arduino kits and making some robotic “cars” with them.
I’m really wanting some help getting to grips with things and wondering how do I get a DC motor running on Makeblock. I have an arduino UNO (similar, not the real deal) with a motor shield.
I’ve plugged the DC motor into the M1 port, attached a battery. I managed to get it going 1 time (by a fluke I think)
Any ideas about what the code should look like for running a DC motor in this way?
Mblock 3.4.11
Thanks in advance,

billk


#2

Hi,

What kits are you using (mBot, Starter, Ranger)?

I’m working on the second volume in my mBot series that focuses on Arduino programming (in my not-so-copious spare time). The first thing I’d observe is that the version of Arduino that ships with mBlock 3.4.11 is a bit out-of-date. It works, but the latest version has a lot of improvements. If you are focusing on Arduino programming, then you may want to consider installing the current version of the Arduino programming environment and installing the Makeblock library into the sketchbook. Just my opinion and your mileage may vary. :smiley:

The mBlock environment lets you see what Arduino code is generated by the blocks by selecting the Arduino mode from the Edit menu. A lot of folks start there so that they can see working examples. For example, the following code blocks:

MoveForwardExample

generates the following code (seen in the Arduino mode editor pane):

#include <Arduino.h>
#include <Wire.h>
#include <SoftwareSerial.h>

#include <MeMCore.h>

MeDCMotor motor_9(9);
MeDCMotor motor_10(10);
void move(int direction, int speed)
{
      int leftSpeed = 0;
      int rightSpeed = 0;
      if(direction == 1){
          leftSpeed = speed;
          rightSpeed = speed;
      }else if(direction == 2){
          leftSpeed = -speed;
          rightSpeed = -speed;
      }else if(direction == 3){
          leftSpeed = -speed;
          rightSpeed = speed;
      }else if(direction == 4){
          leftSpeed = speed;
          rightSpeed = -speed;
      }
      motor_9.run((9)==M1?-(leftSpeed):(leftSpeed));
      motor_10.run((10)==M1?-(rightSpeed):(rightSpeed));
}
double angle_rad = PI/180.0;
double angle_deg = 180.0/PI;

void setup(){
    move(1,150);
    _delay(1);
    move(1,0);
}

void loop(){
    _loop();
}

void _delay(float seconds){
    long endTime = millis() + seconds * 1000;
    while(millis() < endTime)_loop();
}

void _loop(){
}

There is a lot of boilerplate code which it typical of a code generator. The code could be simplified into:

#include <MeMCore.h>

// declare motor objects
MeDCMotor left_motor(9);
MeDCMotor right_motor(10);

// move forward method
void move_forward(int speed)
{
    // The motors must spin in opposite directions
    // to move the robot in a forward motion
    left_motor.run(-speed);
    right_motor.run(speed);
}

// setup method - runs once
void setup(){
    // move forware at speed of 150
    move_forward(150);

    // wait 1 second
    _delay(1);
    
    // stop moving (speed 0)
    move_forward(0);
}

// wait until the time has expired
void _delay(float seconds) {
    // The delay() method is a blocking call which is fine for 
    // this example. A blocking call makes everything else wait
    // until it completes. This will not affect the motors because
    // they have been told to run at a certain speed until
    // further notice.
    //
    // the code generate by mBlock uses a non-blocking call so
    // that any operations in the loop_() method (not shown in 
    // this snippet) will continue to be executed.
    delay(seconds * 1000);
}
// loop method runs continuously after setup method completes
void loop(){
}

Hope this helps. :slight_smile:

Chuck


#3

Thanks Chuck. It is a definite starting point. I’m really just starting out and trying to get my head around it. So from this I gather that the mBot programming blocks can also be used with all arduino kits and is not limited to mbots.


#4

Yes, but you need to be aware that there are different kinds of Arduino. The mCore board is based on the Atmel328P, the Starter Kit uses the Orion board (also an Atmel 328P but with slightly different ports), and the Ranger uses the Auriga (Atmel 2560). The header used in Arduino programming will either be:

#include <MeMCore.h> --- mBot

#include <MeOrion.h> --- Starter Kit / Ultimate Robotics Kit v1.0

#include <MeAuriga.h> --- mBot Ranger

I should note that the following files:

#include <Arduino.h>
#include <Wire.h>
#include <SoftwareSerial.h>

are included in each of the header files mentioned above. It doesn’t hurt to have them there, but it’s really not needed.

Regards,

Chuck


#5

Okay, (see how much of a beginner I am). So is this how some things are called Arduino UNO and others are different or do things differ even between the UNO? Sorry for the very basic questions.