Professional Documents
Culture Documents
ENG Sensors Arado Makeblock
ENG Sensors Arado Makeblock
Plow
Do you know what
a plow is?
PROGRAMMING - MAKER
SMART PLOW
Do you think there is an
alternative to replace
manual plowing?
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.
Functions
ISR Encoder
Move
Delay
Setup
TCCR-ENCODERS
ATTACHED INTERRUPTED
LIGHT SENSOR
MOVEMENTS AND SENSORS
CODE
PROGRAMMING - MAKER
SMART PLOW
Language
"C"
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
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
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"
PROGRAMMING - MAKER
SMART PLOW
REVIEW
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
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
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);
PROGRAMMING - MAKER
SMART PLOW
Sound
Sensor
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
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
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.
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.
1023
< A 500
If it is > a 500, it moves.
ó
If not, it stops.
> A 500
PROGRAMMING - MAKER
SMART PLOW
Potentiometer
Inside While
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