Placeholder Image

字幕列表 影片播放

  • Hi my name is Massimo Banzi

  • and I like to make stuff.

  • Today we are here

  • to look at another project

  • from our Arduino Starter Kit.

  • Today

  • we are making this spaceship interface.

  • This is a simple project

  • designed to teach you about

  • simple inputs and outputs with Arduino.

  • This circuit is going to show you

  • how simple it is to connect

  • a small button

  • and a set of LEDs

  • to the Arduino board,

  • and how you can control

  • the LEDs through the button.

  • First I want to explain

  • a few concepts about Arduino.

  • First of all Arduino

  • is the size of a credit card

  • as you can see here.

  • We can program

  • using the Arduino development environment

  • that I am going to show you in a few minutes.

  • The idea is that we write instructions

  • in the development environment,

  • then we press the button

  • that turns the instructions into a program

  • and get downloaded into the Arduino.

  • Then the Arduino can interface

  • with the outside world

  • to implement any crazy

  • projects that you can come up with.

  • So this project is very simple.

  • We have a button connected to

  • pin number 2 on the Arduino,

  • and then we have three LEDs connected to

  • pin 5, 4 and 3

  • of the Arduino.

  • When I press the button,

  • these two LEDs that are now

  • blinking will stop blinking,

  • and the yellow LED will turn on.

  • If I release the button

  • the two LEDs will keep blinking.

  • So,

  • this is a very simple

  • circuit that allows us to

  • control the behavior of these 3 LEDs

  • from this button.

  • Let's have a look to the code

  • that we need

  • to implement this behavior.

  • At the beginning of the code we have

  • this line that says

  • const int redLed1 = 5,

  • so this creates a constant

  • called redLed1 that contains the value 5.

  • This is actually a very clever technique

  • that allows us to give a meaningful name

  • to pin numbers.

  • So, throughout the code

  • I don't have to use the numbers

  • but I can use redLed1

  • to remember that particular pin

  • is associated with the first red LED.

  • And if you look, there are another

  • couple of lines

  • where you defined constant

  • for redLed2 and greenLed.

  • Then, later on we have another

  • constant called switchPin = 2.

  • This specifies that a switch

  • or a button that we are using

  • is connected to pin number 2.

  • Then let's look at the setup.

  • The setup() is that part in your

  • Arduino code that gets executed once

  • when the board is powered on or reset,

  • This means also right after

  • you upload some code into the Arduino.

  • So, as I said, this gets executed only once.

  • We see the instruction pinMode().

  • pinMode() basically tells Arduino

  • that we want to make sure that

  • pins, redLed1, redLed2 and greenLed

  • are all configured as outputs,

  • because the input and the output pins

  • of the Arduino can be configured

  • to assume both configuration.

  • Then we have an instruction called

  • pinMode(switchPin, INPUT),

  • which is used to specify that

  • the pin number 2 is connected to a switch.

  • And then we want to make sure

  • that is an input so we can read from it.

  • Now let's have a look of the loop section.

  • The loop section in your code

  • gets executed over and over

  • as long as the board is powered on.

  • We created a variable

  • called switchState and then we say

  • switchState = digitalRead(switchPin)

  • Basically what this does

  • is to read the state of the pin

  • connected to the pushButton

  • and it returns a value HIGH or LOW

  • depending on the fact that the button

  • is pressed - HIGH - or released - LOW

  • Then we are going to use

  • a clever statement called if,

  • which is very important

  • whenever you write some code

  • because this "if" statement allows us

  • to take decisions.

  • In this particular case

  • we basically ask Arduino

  • if (the switchState == LOW)

  • do something.

  • So in this case, we used

  • curly brackets to group

  • lines of code together.

  • You can see that

  • this

  • "if" statement

  • is followed by a question,

  • a condition that needs to be verified.

  • and then the curly brackets

  • specify which lines of code

  • need to be executed

  • when the condition is true.

  • In this case, switchState == LOW;

  • basically says if the button

  • is not pressed,

  • and then we follow that

  • with a series of digitalWrite()

  • statements that are used to

  • turn on and off the LEDs

  • and to implement this particular

  • blinking behavior.

  • After that we have a

  • delay of 250 milliseconds

  • followed by a short blink cycle

  • that happens on the other red LEDs.

  • The instructions that you see

  • in this section

  • of the "if" statement

  • are used to implement this

  • blinking behavior that you see here.

  • Afterwards,

  • there is a statement called "else".

  • Else is a statement that allows

  • you to basically create a fork

  • in the road in your Arduino code.

  • With "if" you can say

  • if something is true, execute this

  • piece of code. And else says

  • if that condition is

  • not true, then execute

  • this other piece of code.

  • So you can have two different

  • paths of your code that get executed

  • depending on the condition,

  • if that condition is true or false.

  • In this case, when the button is pressed

  • then we use two digitalWrite()

  • to turn off the red LEDs.

  • And we use one

  • digitalWrite(greenLed, HIGH)

  • to turn on the green or yellow LED

  • like we have here.

  • So if I press the button, the LED

  • turns on. If I release the button,

  • the LEDs are blinking.

  • So this is all the code

  • that we need in order to

  • implement this behavior.

  • I want to remind you that the code

  • that's inside the loop

  • statement will be executed over and over.

  • so as you can see

  • the blinking pattern is executed.

  • Then

  • Arduino reads the input,

  • checks if it's true or false.

  • So depending on that it decides which

  • behavior to implement

  • and then loops back

  • from the beginning.

  • That's all for today.

  • I hope you enjoyed the tutorial.

  • And remember: build it,

  • hack it, share it,

  • because Arduino is you!

Hi my name is Massimo Banzi

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

B1 中級

Arduino視頻教程02:宇宙飛船的接口 (Arduino Video Tutorial 02: Spaceship Interface)

  • 280 1
    Chuan Zhe Lin 發佈於 2021 年 01 月 14 日
影片單字