Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

mBlock

Plow
Do you know what
a plow is?

It's an agricultural instrument composed of


one or more pieces of iron with a pointed
end.

It is used to open up furrows in the soil and


remove the soil before sowing.

Plowing increases the porosity of the soil


which favors plant growth.

PROGRAMMING - MAKER
SMART PLOW
Do you think there is an
alternative to replace
manual plowing?

The power of technology is growing


exponentially and thanks to assisted
engineering and sensors, there has been
an increase in the amount of data
available concerning soil, crops, and
climatic conditions.

At present, work continues to reduce costs


by optimizing resources, and minimizing
occupational safety issues.

PROGRAMMING - MAKER
SMART PLOW
In this occasion, you will practice:
"Maker Skills",
Programming, and
Mathematical Reasoning.

PROGRAMMING - MAKER
SMART PLOW
"C"
Language

STAGE 2
C
LANGUAGE
SENSORS

PROGRAMMING - MAKER
SMART PLOW
Your code
Structure
Segments you
will paste.

Libraries Segments you will


learn from zero.
Variables

Functions

ISR Encoder
Move
Delay
Setup

TCCR-ENCODERS
ATTACHED INTERRUPTED
LIGHT SENSOR
MOVEMENTS AND SENSORS
CODE
PROGRAMMING - MAKER
SMART PLOW
Language
"C"

It is your robot's brain,

Me Auriga it has integrated sensors,


ports for motors, and
external sensors.

PROGRAMMING - MAKER
SMART PLOW
Language
"C" Information
Inputs

1
Imagine that you want
to activate the
"FORWARD"
movemente of your
plow with a SOUND
SENSOR.

2
With the
"INFRARED
SENSOR " you want
it to detect an
obstacle and to
"STOP".

3
You want to manually
"ACTIVATE"
the DC MOTOR
with the "DIMMER"
to control
the seed dispenser.
MILLONES

MILLONES

3
PROGRAMMING - MAKER
SMART PLOW
"C"
Language

Move your plow forward with the help of your


sound sensor (internal).

Program the ultraSonic Sensor (external) to


detect an obstacle and to stop.

Finally, with a "Dimmer" activate the DC motor


which is your seed dispenser.

Program it in "C" language.


Check that the libraries are saved.
1 You dont́ need to do it again, use
your previous code.

Libra
ries a
re
piece
#include <Arduino.h> of co
de m
s
third ade b
#include <Wire.h> parti
es us
y
to fa ed
#include <SoftwareSerial.h> progr
cilita
te
amm
#include <MeAuriga.h> ing.

PROGRAMMING - MAKER
SMART PLOW
Variables

Declare the VARIABLES, inputs and outputs of


2 information. That is, your sensors and motors
with their respective ports.
You almost have everything, you just need to
add the SENSORS and the aditional MOTOR.

MeLightSensor lightsensor_12(12);
MeSoundSensor soundsensor_14(14);

MeEncoderOnBoard Encoder_1(SLOT1);
MeEncoderOnBoard Encoder_2(SLOT2);

MeUltrasonicSensor ultrasonic_6(6);
MePotentiometer potentiometer_10(10);

MeDCMotor motor_1(1);

External
DC
Sensors
Motor

PROGRAMMING - MAKER
SMART PLOW
Language
"C"

The mBot Ranger has a complete library of functions that


allows to program it directly from the arduino IDE.

However, the syntax is a bit cumbersome for someone who


is taking their first steps in programming with the
arduino IDE (and for many who have been around for a
long time).

THIS IS THE REASON


WHY
WE COPY AND PASTE
SOME PIECES OF
CODE.

CUMBERSONE:That it is unclear and includes superfluous


and messy things or ideas that make it confusing and
heavy.

PROGRAMMING - MAKER
SMART PLOW
REVIEW

To build your code and sensor


movement, make sure you have these
functions:
ISR ENCODER
MOVE
DELAY

PROGRAMMING - MAKER
SMART PLOW
"C"
Language

Verify
the
syntax
of
your code

This is how
your code
should look
so far!

PART 1/ 3
PROGRAMMING - MAKER
SMART PLOW
"C"
Language

Verify
the
syntax
of
your code

This is how
your code
should look
so far!

PART 2/ 3
PROGRAMMING - MAKER
SMART PLOW
"C"
Language

Verify
the
syntax
of
your code

This is how
your
void delay and
void setup codes
should look !

PART 3/ 3
PROGRAMMING - MAKER
SMART PLOW
WHILE
Create an infinite cycle

1y0 These are


Boolean
true false values

while(1) = (true)
While the robot
is on.

e.g.
While it is raining, we
will not go swimming.

Can y
ou
give m
ore
exam
ples?

PROGRAMMING - MAKER
SMART PLOW
Sound Sensor
The c
aptu
red s
As long as the sound sensor will b
e com ound
refer p ared
detects a sound intensity it exc
ence
v alue
to a
eeds and if
greater than 240 then move actio
it, th
en an
n wil
the encoders forward. exec l be
uted
.

while(1) {
if(soundsensor_14.strength() > 240)
{
Añadir texto

negative Total power

Encoder_1.setTarPWM(-1 * 50/100.0*255);
Mathematic
operations
1 X 50 = 50
50/100 = 0.5
0.5 X 255 = 127.5

Encoder_2.setTarPWM(1 * 50/100.0*255);

} You are just


using 50% of
the power.

PROGRAMMING - MAKER
SMART PLOW
Sound
Sensor

Create the "while" and the conditional of


1 the sensor that you will use, in this case
"sound sensor".

Verify which range


works for you, you
can start with 100.
while(1)
{
if(soundsensor_14.strength() > 240)
{

Encoder_1.setTarPWM(-1 * 50/100.0*255);

Encoder_2.setTarPWM(1 * 50/100.0*255);

}
Notice that
the "while"
keys
are still open.

PROGRAMMING - MAKER
SMART PLOW
infinite_loop

Creating an infinite loop allows


2 you to repeat a line of code
forever.

void _loop()
{
Encoder_1.loop();
Encoder_2.loop();
}
void loop()
{
_loop();
In these lines, add your
} "void_loop" and within
the keys, another "loop"
so that it always stays in
the cycle.

PROGRAMMING - MAKER
SMART PLOW
Sound
Sensor

Close the"while" and test how the


3 program works so far.
Verify which range
works for you. You
can start with 500.

Ready!
Download and try your program out.
PROGRAMMING - MAKER
SMART PLOW
Ultrasonic
Sensor

Inside While
1 Create the "if" conditional of the sensor that you
will use, in this case"ultrasonic".
Set the distance in centimeters at which you want
it to detect the proximity of the object and the
movements to be executed. Less than:
if the obstacle is
within 15cm of range,
then "The plow" will
not move.

if(ultrasonic_6.distanceCm() < 15)


{
Encoder_1.setTarPWM(1 * 0/100.0*255);

Encoder_2.setTarPWM(-1 * 0/100.0*255);

} ¡Listo!
The power is
} zero because Descarga y
you want
Close the your robot prueba
to stop.
"while" tu programa.
key
PROGRAMMING - MAKER
SMART PLOW
if then-else
if then else, works to decide between
two options, or choose between two
paths.

That is, IF the condition is met and what


to do if the condition is NOT met.
Añadir texto

Total If it's cold, I wear a sweater.


potentiometer
value: If I do not wear a sweater,

1023

< A 500
If it is > a 500, it moves.

ó
If not, it stops.

> A 500

PROGRAMMING - MAKER
SMART PLOW
Potentiometer

Inside While

Create the "if" conditional of the input you are


going to use, in this case the "potentiometer".

Determine that if it is at 500 or more, the motor


stops.

If it is at 500 or less, the motor activates.

if(potentiometer_10.read() < 500)


{

motor_1.run(255); Activate
}
else
{

motor_1.stop(); Stop
}
Ready!
Download and try your program out.
PROGRAMMING - MAKER
SMART PLOW
#include <Arduino.h>
#include <Wire.h>
#include <SoftwareSerial.h>
#include <MeAuriga.h>

MeLightSensor lightsensor_12(12);
MeSoundSensor soundsensor_14(14);
MeEncoderOnBoard Encoder_1(SLOT1);
MeEncoderOnBoard Encoder_2(SLOT2);
MeUltrasonicSensor ultrasonic_6(6);
MePotentiometer potentiometer_10(10);
MeDCMotor motor_1(1);

void isr_process_encoder1(void)
{
if(digitalRead(Encoder_1.getPortB()) == Verify
0)
{
Encoder_1.pulsePosMinus();
the
}
else syntax
{

}
Encoder_1.pulsePosPlus();
of your
code
}
void isr_process_encoder2(void)
{
if(digitalRead(Encoder_2.getPortB()) ==
0)
{
Encoder_2.pulsePosMinus();
}
else
{
Encoder_2.pulsePosPlus();
}
}
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;
}

PART 1 / 2
Encoder_1.setTarPWM(leftSpeed);
Encoder_2.setTarPWM(rightSpeed);
}
PROGRAMMING - MAKER
SMART PLOW
void _delay(float seconds)
{
if(seconds < 0.0)
{
seconds = 0.0;
}
long endTime = millis() + seconds * 1000;
while(millis() < endTime) _loop();
}

void setup()
{
randomSeed((unsigned long)(lightsensor_12.read() * 123456));
TCCR1A = _BV(WGM10);
TCCR1B = _BV(CS11) | _BV(WGM12);
TCCR2A = _BV(WGM21) | _BV(WGM20);
Verify
TCCR2B = _BV(CS21);
attachInterrupt(Encoder_1.getIntNum(), isr_process_encoder1, RISING);
attachInterrupt(Encoder_2.getIntNum(), isr_process_encoder2, RISING);
the
while(1)
{ syntax
if(soundsensor_14.strength() > 240)
{
of your
Encoder_1.setTarPWM(-1 * 50/100.0*255);

Encoder_2.setTarPWM(1 * 50/100.0*255);
code
}
if(ultrasonic_6.distanceCm() < 15)
{

Encoder_1.setTarPWM(-1 * 0/100.0*255);

Encoder_2.setTarPWM(1 * 0/100.0*255);

}
if(potentiometer_10.read() < 500)
{

motor_1.run(255);

}
else
{

motor_1.stop();

_loop();
}

} Ready!
Download and
void _loop()
{
Encoder_1.loop();
Encoder_2.loop();
}

void loop()
try your program out.
{

}
_loop();
PART 2 / 2
PROGRAMMING - MAKER
SMART PLOW
U
A L AT
T
R
G

IO
C ON

STAGE #2 NS !

"C"
Language
SENSORS

COMPLETED

PROGRAMMING - MAKER
SMART PLOW

You might also like