Activity 2 - Digital Input MTS-100
Activity 2 - Digital Input MTS-100
1. Objective(s):
The activity aims to covers the discussions on Arduino digital input. It also
includes the discussions on topic about Arduino looping and control statements.
Digital Input
Digital input is one of the most fundamental physical connections for any
microcontroller. The pins to which you connect the circuits shown in the activity are
called General Purpose Input-Output, or GPIO, pins. Even if a given project doesn’t
use digital in and out, you’ll often use LEDs and switches during the development for
testing whether everything’s working.
Properties of Pins Configured as INPUT
This also means however, that pins configured as pinMode(pin, INPUT) with
nothing connected to them, or with wires connected to them that are not
connected to other circuits, will report seemingly random changes in pin state,
picking up electrical noise from the environment, or capacitively coupling the
state of a nearby pin.
The pullup resistors provide enough current to dimly light an LED connected to
a pin that has been configured as an input. If LEDs in a project seem to be
working, but very dimly, this is likely what is going on.
The pullup resistors are controlled by the same registers (internal chip memory
locations) that control whether a pin is HIGH or LOW. Consequently, a pin that is
configured to have pullup resistors turned on when the pin is an INPUT, will have
the pin configured as HIGH if the pin is then switched to an OUTPUT with
pinMode(). This works in the other direction as well, and an output pin that is left in
a HIGH state will have the pullup resistors set if switched to an input with
pinMode().
Digital Read()
Description
Reads the value from a specified digital pin, either HIGH or LOW.
Syntax
digitalRead(pin)
Parameters
pin: the number of the digital pin you want to read (int)
Returns
HIGH or LOW
No matter what type programming languages you are using, e.g. C/C++,
Visual Basic, PHP and etc, there are always some logic paths you want your
software to decide how it should react based on certain criteria. This is where
control structure needed.
if and if…else… statements
if and if…else… are the most straight forward condition checking control
statement. The syntax of the if statement should be as below:
if (myVar> 0)
{
// do something here if myVar is greater than 0
}
Sometime you may need an if…else… control statement for more than 1
condition.
if (myVar< 10)
{
// do Thing A
}
else if (myVar>= 100)
{
}
else
{
for statement
// do Thing B
Another useful control structure is the for loop. It is used to repeat a block of
statements between its curly braces. An increment/decrement counter is usually
used to increment/decrement and terminate the loop. Normally the for statement is
used in combination with arrays to operate on collections of data/pins in Arduino.
The above for loop Arduino programming example fades a LED up slowly from
off to full brightness.
Switch…case… statement
Another control statement for Arduino programming that quite similar
toif…else… is switch…case… statement. Like if statements, switch…case…
controls the flow of programs by allowing programmers to specify code that
should be executed in various conditions. Below is the syntax example go the
Arduino programming switch…case… statement.
switch (MyVar) {
case 1:
//do something when MyVar equals 1
break;
case 2:
//do something when MyVar equ
als 2
break;
default:
// if nothing else matches, do the default
// default is optional
}
You can see the above switch…case… Arduino codes are very similar to the
if…else…
statement.
myVar = 0;
while(var < 50){
// do something repetitive 50 times
myVar=myVar + 1;
}
{
delay(50); // wait for sensors to stabilize
sensorVal = readSensors(); // check the sensors
} while (sensorVal< 100);
The main different between while loop and do…while… loop is while loop
check the condition before executes the action, butdo…while… loop is executing
the action at least once before exiting the loop by meeting the exit condition.
MTS-100 Keypad/Switch
MTS-100 is equipped with configurable Keypad. The Keypad is can function as
individual switch or a keypad itself.
As individual switch below is the configuration:
5. Procedures
In your setup() method, you need to set the three pins you’re using as inputs or
outputs,
appropriately.
voidsetup() {
pinMode(2, OUTPUT); // set the yellow LED pin to be an output
pinMode(3, OUTPUT); // set the yellow LED pin to be an output
pinMode(8, INPUT); // set the switch pin to be an input
}
In the main loop, first you need an if-then-else statement to read the switch.
voidloop() {
// read the switch input:
if(digitalRead(8) == LOW) { //Check if Button1 is pressed
// if the switch is closed: digitalWrite(2,
HIGH); // turn on 1STLED
digitalWrite(3, LOW); // turn off 2NDLED
}
else{
// if the switch is open:
digitalWrite(2, LOW); // turn off 1ST LED
digitalWrite(3, HIGH); // turn on the 2ND LED
}
}
6.Supplemental Activity
1. Create a program that will output the sequence 8-bits LED below whenever
the corresponding switch is being pressed.
Arduino Switch No. Outpu
Pin t
8 1 Continuous Blinking lights
9 2 Continuous Running Lights (Back & Forth)
10 3 Continuous Alternate low and high nibble
SUPPLEMENTAL ACTIVITY 1
2. Create a program that simulates a car counter on a parking area. The program
should count all the cars that comes in and comes out on the parking area. 2
switches should represents the sensor for sensing the incoming car and
outgoing car. Seven segments should be the output. For reference refer to the
figure below MTS-100 Seven Segment connection.
MICROPROCESSOR-ACTIVITY 2
3. In a certain car, there are two lights to light the interior and one light for the
trunk. The first (LED0) is in front of the car, the second (LED1) is in the back of
the car, and the third (LED2) is in the trunk. There are five switches that control
these three lights: SW0-SW3 is the switches that indicate if one of the four doors
is opened or not. SW4 indicates if the trunk is opened or not. Let's suppose that
the switches are closed if the doors are open. When one of the four car doors is
opened, both lights in the interior needs to light up. When the trunk is opened,
only the trunk-light needs to light up. (Assuming the LEDs on the car needs 4.8
volts to light up). If the switches are connected to pin 8-12 respectively (pin8 to
SW0, pin 9 to SW1 …) and the LEDs are connected to pin3 and pin4 (LED0
connected to pin 3 and LED1 connected to pin 4) of the Arduino, create a
complete C code for Arduino that will simulate the given machine problem.
7. Conclusion: