Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
37 views

Programming Concepts (Part B) : Engr 10 Introduction To Engineering

SetDigitalOutput(3, 1); This indicates: - Port number 3 is used - A value of 1 is sent, turning on the device connected to port 3 So in summary, this function allows sending digital signals (0 or 1) to control devices like LEDs.

Uploaded by

Shashank Patil
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Programming Concepts (Part B) : Engr 10 Introduction To Engineering

SetDigitalOutput(3, 1); This indicates: - Port number 3 is used - A value of 1 is sent, turning on the device connected to port 3 So in summary, this function allows sending digital signals (0 or 1) to control devices like LEDs.

Uploaded by

Shashank Patil
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 53

Programming

Concepts
(Part B)

ENGR 10
Introduction to Engineering
Review Question: What are the
values of A and B at end of this
A = 10 ;
program?
B = 12 ;
if ((A + B) < 22)
{ B=A; (A) A=12, B=22
A = B; }
else (B) A=12, B=10
{ A = B; (C) A= 10, B=10
B = A + B; }
(D) A=12, B=24
(E) A=10, B=22
Functions
• A function is a way to organize the
program so that:
– frequently used sets of instructions or
– a set of instructions that has a specific purpose
are efficiently represented in the main
program.

• For example, the factorial routine can be


represented by a single instruction (a
function).

8! result = factorial(8);
Functions
Q) What is a function?
• A function is just a name given to represent a set
of instructions (routine).
• Whenever we want to use that routine, we just
use the name instead of repeating the entire set
of instructions.

• The following example shows how to write a


function and how to call that function
– MAX_Three  find the maximum of 3 numbers
Sequential execution of main program
which calls the defined function

Function
mar gor P ni a M
Introduction to Local
Variable
• The MAX_Three function finds the greatest of the
three numbers A, B and C.

• These variables were defined as global variables.

• Before this function is called, your program must


have copied the values into these three
variables.

• This operation can be done more efficiently by


using local variables.
Program using
Function with
passing variables
What is a Local variable ?
• The value of a local variable is accessible only to the
function within which it is defined.
• The main function (main program) passes the values
into the function and these values are copied into the
local variables of the function.
• The function doesn’t care what are the names of the
variables defined in the main function.
• The function does what it should do and then returns
the result to the main function.
• The values of these local variables are lost after the
execution of the function is completed.
Global Variable Concept
Main Function

Defined Function

A Task of
B the Z
function
C
Local Variable Concept
Main Function

Defined Function

A x Task of
B b the h Z
function
C k
Review Question #2
A local variable is known only to the
function in which it is defined:
A. True
B. False
Interfacing

Interface
Motor
• Robot movement made possible by the use
of motors.
• The robot has two motors, one controlling
each side of the robot.
• This combination is enough to perform any
of the basic movements.
• The motors can be rotated either clockwise
or counter-clockwise, controlling the overall
motion of the robot.
This chart
describes the
combination of
the motor
movement
required to
perform each
maneuver
How to move a motor?
• Use the following statement to move
a motor

SetMotor ( 2 , 255 );

This number This number sets


indicates that the the direction and
motor is speed of the
connected to motion of the
motor port 2 of the motor
total available 8
Motor Speed & Direction
Value Motor Direction
255 Clockwise high speed
254

128 Clockwise low speed


127 Stop
126 Counter Clockwise low
speed

1
0
Counter Clockwise high
speed
How to connect the motor
to the Controller?
• On the VEX controller you have several ports
available so that you can connect not only
motors but also other devices and sensors to
it.
PORT:
• A port is a specific place on the board where
we can connect other external devices, so
that the two devices in connection can
exchange information or data.
Program to move the robot
forward
• Now let us write a program to make the
robot move forward for 2 seconds and
then stop.

Assume:
Left Motor – connected to motor
port 3
Right Motor – connected to motor
port 2
Program to make the motor
move forward for 2 seconds
and then stop
Review Question #3
What value of x will cause the motor to
be stopped: SetMotor(2, x)?
A. 255
B. 127
C. 0
D. -127
E. -255
Servos
• Servo units are similar to motors except that
they control position rather than speed.
• Servo units have limited range of motion
(about 120o)
• A value between 0 and 255 sets the
direction of the servo.
0 – Extreme clockwise direction
127 – No movement
255 – Extreme counter-clockwise
direction
Graphical representation of
the direction of rotation of
the Servo
0o clo
er- e c kw
t
oun e 12 is
C
 kwis 7
c
cl o
- 60
o
60o 0
25
5
Servo
• Similar to the motor the following
statement is used to set the servo to
turn to a direction.
SetServo ( 5 , 64 );

Indicates the This value sets the


motor port direction of the servo
number to which
the servo is
connected.
SetServo (5 , 64)

se clo
w i ck
l ock 0o
wi
e r-c se
t
oun 12
30 
C 7
o

 64

- 60
0
60o 0
25
5
SetServo (5 , 191)

i s e
k w clo
oc c
r- cl 0o  kw
u nte is e
Co -
12
 30o
7
191

- 60
o
60o 0
25
5
Program showing how to
move the servo
continuously
Output of the above
program
i s e clo
w
-cl o ck 0o ck
wi
er se
u nt
Co -
127 30 
 30o o
191 6
4
- 60
o
60o 0
255
Introduction to Logic Signals
• A logic signal can take on either 0v or 5v

→ 0v represents “Low state” or “Logic 0”


→ 5v represents “High state” or “Logic
1”

A logic signal can also be called a


“digital signal”
Bumper Switch

• A Bumper Switch is a digital sensor.


• It can distinguish between two
states: pressed or not pressed.
• When the switch is not pressed, the
robot interprets this value as a 1
(high state).
• When the switch is pressed the robot
interprets the value as a 0 (low
state).
Bumper Switch
• Use of the following statement in the
program to read the bumper switch.

Bumper = GetDigitalInput
( 2 );
A variable into This indicates the port
which the status number to which the
of the bumper bumper switch is
switch will be connected
stored.
Limit Switch

• A limit switch behaves just like a


bumper switch
• When switch is pressed: digital signal
=0
• When switch is open (not pressed):
digital signal = 1
• Program the same way as bumper
Program to make the robot halt,
turn and move forward when there
is an obstacle
stores the state of the bumper
switch into the variable
“bumper”.
checks if there is a contact
with an obstacle (switch will
be pressed if there is a
contact)
motor moves forward as long
as there is no obstacle
(bumper ==1)

if the switch is pressed


(bumper==0), these
statements make the robot
halt for 1 second
Then the robot turns left
for 2 seconds
Digital Output
• The “Digital output” function can be used
to control the state of a digital output.
• By using this function you can send digital
signals (either 0 or 1) to the devices which
can accept digital signals.
• For example, by using this function you
can turn off or on an LED.
1 => 5v => light is on
0 => 0v => light is off
Digital Output
• This statement is used in the
program to control the devices i.e.,
send digital signals.
SetDigitalOutput ( 6 , 1 );

Indicates the port The digital value to


number (here port be sent (either 0 or
6) to which the 1)
device to be
controlled is
connected
Program to make an
LED blink
continuously
Review Question #4

What voltage level corresponds to logic


level 1?
A. 0 V
B. 10 V
C. 5 V
D. 255
E. 127
Analog Signals
• For the VEX controller, an analog signal
can take any value between 0v and 5v.
5 1023
→ 0v is represented by a value 0 v

→ 5v is represented by a value 1023

• All other voltage levels between 2.5v 51


1
0v and 5v are represented by the
numbers between 0 and 1023.
0 0
v
Analog Input
• An analog input function block can be used to
receive analog signals (any value between 0v
& 5v) from analog sensors.

• For our VEX controller, the range 0v-5v is


represented by the range of numbers 0-1023.

• But in general the range used to represent


0v-5v depends on the controller.
Light Sensor
• A light sensor is an example of a analog
sensor.
• A light sensor reads the intensity of light
of an area, translates the reading into a
single numeric value, and sends that
value to the robot.
• The range of values used by this sensor is
0-1023
• The higher the value, the darker will be
the area and vice versa.
Light sensor
• The following statement is used to
read the analog signal from a light
sensor.

light = GetAnalogInput ( 1 ) ;
A variable named Indicates the port
“light” into which number (here port 1) to
the reading (0- which the light sensor is
1023) from the light connected
sensor is stored
Program to make the robot move
towards the moving light source

If light intensity is more on


the left side, the robot
turns toward the left side

If light intensity is more on


right side, the robot turns
toward the right side

If light intensity is the same


on both sides, the robot
moves forward
Ultrasonic Sensor

• The Ultrasonic Sensor is also an analog sensor.


• The Ultrasonic Sensor uses high-frequency sound
waves to detect objects.
• It emits a sound wave, measures how long it takes
the sound wave to bounce back, and then
calculates how far away the object is.
• The measurement is translated into a numeric
value from 2 - 100.
• Lower signal value corresponds to a closer detected
object and vice versa.
Ultrasonic Sensor
Connections

Digital
Input
Output port

Interrupt
Output
port

VEX Controller Ultrasonic Sensor


Ultrasonic Sensor
• The following statement starts an
ultrasonic sensor i.e., the sensor starts
sending and recording the sound signals.

StartUltrasonic ( 1, 8 ) ;

Sensor connected Sensor connected to


to interrupt port 1 output port 8 through
of the total which it receives digital
available 6 ports. control signals from
the controller.
Ultrasonic Sensor
• The following statement is used to
read a value from the sensor

Range = GetUltrasonic ( 1
, 8 );
Interrupt port #1 The sensor is
Variable into
to which the connected to
which the
sensor is output port #
translated
connected 8 on the
value is stored
controller
Program to stop the forward
moving robot when a nearing
object is detected
Setting the robot to
move forward

Start the ultrasonic


detector connected to
interrupt port # 8
and output port # 1
Store the translated
signal into the variable
Range
Checking if the obstacle is
near or far

Making the robot to stop


if an obstacle is nearby
Optical Shaft Encoder
• The optical shaft encoder is a digital sensor.
• It is used to measure rotational movement
• The encoder basically has a disc with 90
equally spaced slots on its outer edge.
• As the disc rotates, an infrared light sensor is
used to count the number of slots passed.
• A count of 90 makes one revolution.
Optical Shaft Encoder
Optical Shaft Encoder
• The following statement is used to
start the encoder counting.

StartEncoder ( 2 ) ;

This number indicates that the


encoder is connected to interrupt
port # 2 on the VEX controller
Optical Shaft Encoder
• The following statement is used to
get the present count value from the
encoder.

Count = GetEncoder ( 2 ) ;
A variable named “count” Interrupt port # 2 to
into which the present which the Encoder is
count value will be stored connected
PresetEncoder ( 2, 25) ;
This statement is used to make the encoder
start counting from a given number (here 25)
instead of from zero.

Example
Write a program to make the robot move
for 1 meter and then stop.
Assume 5 turns of the wheel = 1 meter
Program to make the robot
move forward for 1 meter

Presetting the encoder to 20.


Count value starts from 20.

if count reaches 470 the


robot stops.
470 = 450 (count for 5 turns)
+ 20 (initial value)

You might also like