Sei sulla pagina 1di 15

ME-EE 106 Fundamentals of Mechatronics

Name: Chanh Le Date: 3/1/2018

SID: 011048232

Arduino Programming and Power Interfacing

Summary
What was learned in the assignment:
This homework was more difficult than all the previous ones. I learned to construct a schematic diagram for a
custom LED and make an interface for it connecting to the Arduino Pin 12. There are many free circuit schematic
makers on internet that’s beyond helpful for students. I used https://easyeda.com/editor to draw my schematic.
While doing question 1, I realized how important is the transistor in controlling the current through my LED. By
turning a small input current into a large output current, the transistor acts like an amplifier. But it also acts like
a switch at the same time. When there is no current to the base, little or no current flows between the collector
and the emitter.
Problem 2 was interested enough that kept my brain fried for the past two days. I got the LED 2 and 9 flashed
after multiple tries from last homework. However, I can’t turn verify whether pin 12 Is working properly. I don’t
have the LED in given problem.

Chanh Le Arduino Programming and Power Interfacing 3/1/2018 Page 1 of 15


ME-EE 106 Fundamentals of Mechatronics

1. (10 pts) Design an interface between the Arduino pin 12 and a Fairchild QED233 IR LED. Assume that
you have a 12 V power supply available to power the LED, and that you need to supply at least 80
mA to drive the LED for a particular application. Document your interface with a schematic diagram
that shows all the components involved in the interface, their specific values, and all the
connections including the Arduino. You are welcome to draw your schematic by hand, but you might
find it helpful to get practice using some software tools that facilitate drawing schematics. There are
a host of readily available tools to draw schematics. On the ‘just graphics’ end of the spectrum, you
can use Microsoft Office drawing tools, or Microsoft Visio (which has Electrical Engineering
symbols). There are more specific schematic capture programs such as Multisim (available in the
Mechatronics lab or from ni.com), DipTrace, Eagle, etc. that you might want to explore. One simple
freeware system is Circuit Maker, which you can download from:

Spec 3N3904

𝑉𝑏𝑒 = 0.7𝑉

ℎ𝑓𝑒 = 30

𝐼𝑐 = 200𝑚𝐴

𝑉𝐶𝐸 = 40𝑉 (𝑀𝐴𝑋)

𝑖𝑐 = ℎ𝑓𝑒 ∗ 𝑖𝑏
100𝑒 3
𝑖𝑏 = = 3.3𝑚𝐴
30
𝑉𝐴𝑟𝑑𝑢𝑖𝑛𝑜 − 𝑉𝑁𝑃𝑁
𝑅𝐵 =
𝑖𝐵

(5-0.7)/3.3𝑒 3 =1303 Ω

Choose 910Ω for safety.

Chanh Le Arduino Programming and Power Interfacing 3/1/2018 Page 2 of 15


ME-EE 106 Fundamentals of Mechatronics

2. (10 pts) Revisit Problem 2 from HW 4 (Cylon Eyes with buttons), but revise it in the following ways:
a. Blink the IR LED from Problem 1 above instead of blinking the LED segments 2 and 9 when
both buttons SW11 and SW10 are pressed.
b. Write your revised Cylon Eyes with buttons program using a state machine approach as
outlined in lecture. Conceptualize your state machine with at least three states:
1) ‘Cylon-ing’ 2) Blinking the IR LED on 3) Blinking the IR LED off. Your state machine should
respond to events of the button presses and a millis()-based ‘timer’ expiration to blink the IR
LED on and off at 2 Hz.
You can add more states or modify the suggested conceptualization of the state machine if
another configuration makes more sense to you. For example, maybe you prefer to
conceptualize the single state, ‘Cylon-ing’ as two states: Cylon-ing_RIGHT and Cylon-
ing_LEFT where transition between these two states occur on reaching the max or min value
of a counter for the LED segments. The state machine and Events-Services approach is a way
to help you organize, design, and test your software before you even start typing it in. There
is no single, right way to conceptualize your software using this approach.

Include in your submission a figure showing your state diagram with events labeled. Make
sure you also comment your Arduino code.

#define SW11 11 //set up switches

#define SW10 10

#define PIN12 12 //set up LED pins

#define PIN9 9

#define PIN8 8

#define PIN7 7

#define PIN6 6

#define PIN5 5

#define PIN4 4

#define PIN3 3

#define PIN2 2

#define PIN1 1

#define PIN0 0

Chanh Le Arduino Programming and Power Interfacing 3/1/2018 Page 3 of 15


ME-EE 106 Fundamentals of Mechatronics

#define switchState_1 0 //SWITCH11_OFF_10_OFF

#define switchState_2 1 //SWITCH11_OFF_10_ON

#define switchState_3 2 //SWITCH11_ON_10_OFF

#define switchState_4 3 //SWITCH11_ON_10_ON

#define switchTime_1 0 //When time not yet passing

#define switchTime_2 1 //When time already passed

float blinkTime = 100; // delay between each blink in millisecond

int dir = 1; // dir = direction, variable used to control direction of LED lights

unsigned long currentMillis = 0; // initializing the current time checker

int LED_current = 0; // initializing the value for current LED

int BUTTON_STATUS = 0; // to be used to control the switch statement of the button reading

int TIME_STATUS = 0;

int ledState = LOW;

// reads current status of the switch

int switch_read(int currentSwitch)

return digitalRead(currentSwitch);

// reverses voltage of selected LED

void led_reverse(int LED_current)

digitalWrite(LED_current,!digitalRead(LED_current));

Chanh Le Arduino Programming and Power Interfacing 3/1/2018 Page 4 of 15


ME-EE 106 Fundamentals of Mechatronics

void led_high(int LED_current)

digitalWrite(LED_current,HIGH); //turn on

// sets desired led to low

void led_low(int LED_current)

digitalWrite(LED_current,LOW);

void both_led_pushed() // if both switches are pushed, blink the IR LED at the desired ra
te

int switchTime = 0;

while (switch_read(SW11) == LOW && switch_read(SW11) == LOW)

if ((millis() - switchTime)>= 250) // if more than 250 milliseconds have passed

switchTime = millis(); // set time tracker to the current time

if (ledState == LOW) {

ledState = HIGH;

} else {

Chanh Le Arduino Programming and Power Interfacing 3/1/2018 Page 5 of 15


ME-EE 106 Fundamentals of Mechatronics

ledState = LOW;

digitalWrite(PIN12,ledState);

// digitalWrite(PIN12,LOW); // reverse the current voltage value of digital pin 12

digitalWrite(PIN12,ledState);

int button_status() // reads status of buttons pressed

return !switch_read(SW10) + 2*!switch_read(SW11);

// find out if the time that has passed has reached the threshold

bool time_pass(unsigned long currentMillis)

return (millis() - currentMillis >= blinkTime);

void setup()

Chanh Le Arduino Programming and Power Interfacing 3/1/2018 Page 6 of 15


ME-EE 106 Fundamentals of Mechatronics

//Serial.begin(9600);

byte i;

for (i=0; i < 10; i++) { // iterates through pins 0 - 9

pinMode(i, OUTPUT); // sets pins 0 - 9 as output

pinMode(SW11,INPUT); // initialize switch

pinMode(SW10,INPUT); //

pinMode(SW11,INPUT_PULLUP); // making switch a pull-up

pinMode(SW10,INPUT_PULLUP);

void loop() {

//currentMillis = millis(); // set the current time to now

// checking if time threshold has passed

BUTTON_STATUS = button_status(); // Reading the status of the buttons

// state for button pressed

switch (BUTTON_STATUS)

case (switchState_4):

led_reverse(LED_current); // reverse current LED

Chanh Le Arduino Programming and Power Interfacing 3/1/2018 Page 7 of 15


ME-EE 106 Fundamentals of Mechatronics

both_led_pushed(); // set state to cycle LED PIN 12

led_reverse(LED_current); // reverse current LED

break;

case (switchState_3): // if switch 11 is on and switch 10 is not

blinkTime+=.0005; // make the program run slower

break;

case (switchState_2): // if switch 10 is on and switch 11 is not

blinkTime-=.0005; // make the program run faster

break;

case (switchState_1): // if both switches are off

blinkTime = 100; // reset the program speed

break;}

TIME_STATUS = time_pass(currentMillis); // check if it is time to move the LEDs

switch (TIME_STATUS)

case (switchTime_1):{

break; } // if time has not passed, do nothing

case (switchTime_2):{ // if time has passed

currentMillis = millis(); // set current time to millis()

led_low(LED_current); // set current led off

Chanh Le Arduino Programming and Power Interfacing 3/1/2018 Page 8 of 15


ME-EE 106 Fundamentals of Mechatronics

led_high(LED_current+dir); // turn th enext led on

LED_current = LED_current+dir; // make the next led in series the current led

if ((LED_current)== 0 || (LED_current == 9)) dir = -


dir; // if the current LED is 0 or 9, switch travel directions

break;

break;

#define SW11 11

#define SW10 10

#define PIN12 12

#define PIN9 9

#define PIN8 8

#define PIN7 7

#define PIN6 6

#define PIN5 5

#define PIN4 4

#define PIN3 3

#define PIN2 2

#define PIN1 1

#define PIN0 0

Chanh Le Arduino Programming and Power Interfacing 3/1/2018 Page 9 of 15


ME-EE 106 Fundamentals of Mechatronics

#define switchState_1 0 //SWITCH11_OFF_10_OFF

#define switchState_2 1 //SWITCH11_OFF_10_ON

#define switchState_3 2 //SWITCH11_ON_10_OFF

#define switchState_4 3 //SWITCH11_ON_10_ON

#define switchTime_1 0 //When time not yet passing

#define switchTime_2 1 //When time already passed

float blinkTime = 100; // delay between each blink in millisecond

int dir = 1; // dir = direction, variable used to control direction of LED lights

unsigned long currentMillis = 0; // initializing the current time checker

int LED_current = 0; // initializing the value for current LED

int BUTTON_STATUS = 0; // to be used to control the switch statement of the button reading

int TIME_STATUS = 0;

int ledState = LOW;

// reads current status of the switch

int switch_read(int currentSwitch)

return digitalRead(currentSwitch);

// reverses voltage of selected LED

void led_reverse(int LED_current)

digitalWrite(LED_current,!digitalRead(LED_current));

Chanh Le Arduino Programming and Power Interfacing 3/1/2018 Page 10 of 15


ME-EE 106 Fundamentals of Mechatronics

void led_high(int LED_current)

digitalWrite(LED_current,HIGH); //turn on

// sets desired led to low

void led_low(int LED_current)

digitalWrite(LED_current,LOW);

void both_led_pushed() // if both switches are pushed, blink the IR LED at the desired ra
te

int switchTime = 0;

while (switch_read(SW11) == LOW && switch_read(SW11) == LOW)

if ((millis() - switchTime)>= 250) // if more than 250 milliseconds have passed

switchTime = millis(); // set time tracker to the current time

if (ledState == LOW) {

ledState = HIGH;

} else {

Chanh Le Arduino Programming and Power Interfacing 3/1/2018 Page 11 of 15


ME-EE 106 Fundamentals of Mechatronics

ledState = LOW;

digitalWrite(PIN12,ledState);

// digitalWrite(PIN12,LOW); // reverse the current voltage value of digital pin 12

digitalWrite(PIN12,ledState);

int button_status() // reads status of buttons pressed

return !switch_read(SW10) + 2*!switch_read(SW11);

// find out if the time that has passed has reached the threshold

bool time_pass(unsigned long currentMillis)

return (millis() - currentMillis >= blinkTime);

void setup()

Chanh Le Arduino Programming and Power Interfacing 3/1/2018 Page 12 of 15


ME-EE 106 Fundamentals of Mechatronics

//Serial.begin(9600);

byte i;

for (i=0; i < 10; i++) { // iterates through pins 0 - 9

pinMode(i, OUTPUT); // sets pins 0 - 9 as output

pinMode(SW11,INPUT); // initialize switch

pinMode(SW10,INPUT); //

pinMode(SW11,INPUT_PULLUP); // making switch a pull-up

pinMode(SW10,INPUT_PULLUP);

void loop() {

//currentMillis = millis(); // set the current time to now

// checking if time threshold has passed

BUTTON_STATUS = button_status(); // Reading the status of the buttons

// state for button pressed

switch (BUTTON_STATUS)

case (switchState_4):

led_reverse(LED_current); // reverse current LED

Chanh Le Arduino Programming and Power Interfacing 3/1/2018 Page 13 of 15


ME-EE 106 Fundamentals of Mechatronics

both_led_pushed(); // set state to cycle LED PIN 12

led_reverse(LED_current); // reverse current LED

break;

case (switchState_3): // if switch 11 is on and switch 10 is not

blinkTime+=.0005; // make the program run slower

break;

case (switchState_2): // if switch 10 is on and switch 11 is not

blinkTime-=.0005; // make the program run faster

break;

case (switchState_1): // if both switches are off

blinkTime = 100; // reset the program speed

break;}

TIME_STATUS = time_pass(currentMillis); // check if it is time to move the LEDs

switch (TIME_STATUS)

case (switchTime_1):{

break; } // if time has not passed, do nothing

case (switchTime_2):{ // if time has passed

currentMillis = millis(); // set current time to millis()

led_low(LED_current); // set current led off

Chanh Le Arduino Programming and Power Interfacing 3/1/2018 Page 14 of 15


ME-EE 106 Fundamentals of Mechatronics

led_high(LED_current+dir); // turn th enext led on

LED_current = LED_current+dir; // make the next led in series the current led

if ((LED_current)== 0 || (LED_current == 9)) dir = -


dir; // if the current LED is 0 or 9, switch travel directions

break;

break;

Chanh Le Arduino Programming and Power Interfacing 3/1/2018 Page 15 of 15

Potrebbero piacerti anche