Electronics Beginner Arduino Projects Electronic Technology Basic Arduino Coding
Electronics Beginner Arduino Projects Electronic Technology Basic Arduino Coding
www.bookbenefits.com
Arduino Catenary
What is a microcontroller?
A large Arduino family was introduced
About hardware prototyping
Arduino software properties
Beginner Arduino
Intermediate Arduino: Inputs and Outputs
Project 01- IoT Fidget
Project 02 - 3 LED With Arduino 101
Project 03 - Ultrasonic Distance Sensor in
Arduino
Project 04 - Flowing LED Lights With
Arduino Uno R3
Project 05 - Light Sensor With Arduino in
Tinkercad
Project 06 - DIY | 3x3x3 LED Cube for
www.bookbenefits.com
Arduino Nano+
Project 07 - Ultrasonic Sensor (HC-SR04)
Project 08 - How to Use an RGB LED
Project 09 - PIR Motion Sensor
Project 10 - DIY Arduino Obstacle Avoiding
Car at Home
What is a microcontroller?
www.bookbenefits.com
after we learned more about microcontrollers in general and in particular
a large and wonderful Arduino family. This chapter will teach you how to be
completely perfect
ready to enter code, phone, and check things with your new hardware friend.
Yes, this will do
it happened quickly, very quickly; now let's go inside!
What is a microcontroller?
A microcontroller is an integrated circuit (IC) that contains all the main
components of a standard
Computer, the following:
• Processor
• Memories
• Edges
• Inputs and outputs
The brain processor, the part where all the decisions are made and what
he can count.
Memories are often the two spaces where both the internal system and the
user
elements are active (commonly called Read Only Memory (ROM) and
Random
Access Memory (RAM)).
I describe in detail the artificial materials contained in the global board; these
are
a very diverse range of integrated circuits with a major purpose: to support
processor and extend its power.
Inputs and outputs are means of international communication (worldwide
microcontroller) and the microcontroller itself.
The first single-chip processor was developed and developed by Intel
Corporation in
In 1971 under the name Intel 4004. It was a 4-bit central processing unit
(CPU).
Since the 70s, things have changed a lot and we have a lot of processors
around us.
Look around, you will see your phone, your computer, and your screen.
Processors or
microprocessors drive almost everything.
Compared to microprocessors, microcontrollers provide a way to reduce
power
usage, size, and cost. Indeed, microprocessors, albeit faster than ever
processors embedded in microcontroller, requiring multiple interruptions in
order to operate
work. The high level of integration provided by the microcontroller makes it
friendly
of embedded programs that control the engine of the car, the remote control
of your TV,
desktop equipment including your beautiful printer, household items,
children's games,
cell phones, and I can go on…
www.bookbenefits.com
the use of electronics in building collaborative projects for university
students.
It is based on the Atmel AVR processor and offers many inputs and effects
on only one hardware item.
The project was launched in Italy in 2005 by founders Massimo Banzi and
David
Cuartielles. Today it is one of the best examples of open source
concept, brought to the world of hardware and is often used only in the file
world software.
We are talking about the Arduino family because today we can count about
15 boards
‘Arduino-based’, which is a funny meta-term to describe a different type of
board
designs everything made using the Atmel AVR processor. The main
difference between
www.bookbenefits.com
From our point of view to our final offer, we usually have to follow these
steps.
If we want to do that LED flashing, we have to define a few flashing features
For example. It will help to accurately describe the project, which is the key
to success.
After that we will have to draw a scheme with our Arduino board and our
LED; will do
catch the question, "How are they connected together?"
A firmware program that uses the C language can be started directly after we
have it
underline the circuit because, as we will see later, it is directly related to the
hardware.
This is one of the strongest forces of Arduino development. Do You
Remember? Board
design is only designed to make us think about our project and not to confuse
us with invisible pieces of reading that are at a very low level.
The upload step is very important. It can give us many details
especially if it happens to continue to solve the problem. We will learn that
this step is not necessary
more than a few clicks when the board is properly installed on our computer.
After that, testing and adjustment of the subcycle will take place. We will
learn by doing, by testing, and by
it means failure again. It is an important part of the process and will teach you
the
a lot. I have to admit something important here: when I first started my career
bonome project, RGB monome clone tool,
I spent two hours repairing the LED matrix with wires back. Now, I know
them very well
because I failed one day.
The last step is the coldest. I say that because we have to keep it in mind
the ultimate goal, the one that will bring us happiness in the end; it is the
secret to success.
www.bookbenefits.com
Beginner Arduino
works:
On each side of the loaf board, two rows of holes are connected throughout
the length of the board (pictures 1 and 2 above). Typically, you will connect
these long "rails" to 0V (also called "ground") and any gas power you used
(in this class we will use 5V from Arduino), so that those connections are
available everywhere on board. In this case, the first thing you want to do is
plug in these links to your Arduino as shown in Figure 4, be careful how I
connect the line to the "-" line and 5V to the line marked "+", your
breadboard or it may not be written. Note: sometimes these side strands will
only stretch half the length of the bread board, using the strings to complete
the connection (Figure 5).
All the other holes in the breadboard are arranged in five rows in the middle
of the bread board (Figure 3). This is where you will connect electrical
appliances to each other and form circuits.
This works, and we can leave it like that and everything will work fine, but it
is not the most effective way to write our code. Instead, we will use a
structure called a loop to rotate the LEDs. For loops it helps to repeat a piece
of code over and over again. In the above case we repeat the lines:
digitalWrite (led4Pin, HIGH);
delay (1000);
digitalWrite (led4Pin, LOW);
delay (1000);
Here's how to write a loop:
of (int ledPin = 4; ledPin <8; ledPin ++) {
digitalWrite (ledPin, HIGH);
delay (1000);
digitalWrite (ledPin, LOW);
delay (1000);
}
www.bookbenefits.com
In the first row we start the "ledPin" variable as 4 and tell Arduino that we
would like to rotate it with the starting variable values by 4, up to 7 (ledPin
<8). LedPin ++ tells Arduino to increase ledPin value by 1 each time we
repeat the loop. After that we make lines inside the loop using the flexible
ledPin. So first ledPin = 4, and pin 4 is turned on and off, then ledPin is
raised to 5 and then the loop starts again, this time the pin 5 is opened and
closed, and so on ... The result is exactly the same as the diagram of verbose
more, where we repeat digitalWrite commands and frequent delays. Here is
the full diagram:
//Multi LED Blink
int led1Pin = 4;
int led2Pin = 5;
int led3Pin = 6;
int led4Pin = 7;
void setup() {
//initialize the led pins as an outputs
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
pinMode(led4Pin, OUTPUT);
}
void loop() {
for (int ledPin=4;ledPin<8;ledPin++){//for pins 4-7
digitalWrite(ledPin, HIGH);//turn LED on
delay(1000);// wait for 1000 milliseconds (one second)
digitalWrite(ledPin, LOW);//turn LED off
delay(1000);//wait one second
}
}
Or we can use the loop again to make the code much shorter. In the following
boat I have two wires, the first LED lines rise from (0) to full light (255):
of (int bright = 0; brightness <256; bright ++) {
analogWrite (ledPin, light);
delays (5);
}
The second step across the street from full light to off:
of (int bright = 255; light> = 0; light -) {
analogWrite (ledPin, light);
delays (5);
}
(delay (5) is used to reduce dryness, so it takes 5 * 256 = 1280ms =
1.28seconds)
In the first line, we use "brightness--" to tell the loop to decrease the amount
of light by 1 each time the loop repeats. Also note how the loop will work
until light> = 0, using> = instead of> we enter the number 0 in width.
//LED fade
int ledPin = 9;//the Arduino pin that is connected to the LED
void setup() {
pinMode(ledPin, OUTPUT);// initialize the pin as an output
}
void loop() {
//ramp LED up to full brightness (0 to 255)
for (int brightness=0;brightness<256;brightness++){
analogWrite(ledPin,brightness);
delay(5);
}
delay(1000);// wait one second
//ramp LED down to no brightness (255 to 0)
for (int brightness=255;brightness>=0;brightness--){
analogWrite(ledPin,brightness);
delay(5);
}
delay(1000);//wait one second
}
Here's what it looks like (This impersonation isn't really good, but you get the
idea). Try setting the delay to see how it affects the speed of the ramps.
www.bookbenefits.com
Step 8: RGB LED and Arduino
RGB LEDs are really fun, as you can see in the first picture above, each RGB
LED is actually made up of three LEDs: one red, one green, and one blue.
When you turn on a few LEDs at the same time they will combine to form
new colors.
The RGB LEDs we use in this class are standard cathodes, which means all
three LEDs share the same ground axle (some RGB LEDs, called standard
anodes, share a normal supply pin and for different reasons). We will plug
our circuit into the circuit as the first picture above, each LED on the RGB
LED has a single 220Ohm resistor in the series which also has a cord that
reaches the Arduino pin powered by PWM (I used pins 9-11). In this way, we
can optionally turn each LED on the RGB LED and turn it off individually.
See the second image above to find out which RGB LED direction
corresponds to red, green, blue, and ground (counting numbers 1-4).
The first drawing will rotate with each color on the LED:
//RGB LED - test
//pin connections
int red = 9;
int green = 10;
int blue = 11;
void setup(){
pinMode(red, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(green, OUTPUT);
}
void loop(){
//turn red led on
digitalWrite(red, HIGH);
delay(500);
digitalWrite(red, LOW);
delay(500);
//turn green led on
digitalWrite(green, HIGH);
delay(500);
digitalWrite(green, LOW);
delay(500);
//turn blue led on
digitalWrite(blue, HIGH);
delay(500);
digitalWrite(blue, LOW);
delay(500);
}
Then use analogWrite () and random () to set random light levels for each
color in the LED. The three colors will combine in different sizes (depending
on their brightness) to make a variety of colors (255 ^ 3 = 16,581,375
possible colors).
//RGB LED - random colors
//pin connections
int red = 9;
int green = 10;
int blue = 11;
void setup(){
pinMode(red, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(green, OUTPUT);
}
void loop(){
//pick a random color
analogWrite(red, random(256));
analogWrite(blue, random(256));
analogWrite(green, random(256));
delay(1000);//wait one second
}
The diagram above works, but there is a lot of code repeated. We can make it
easier by writing our own assistant work that fades from one color to another.
Here's what the job will look like:
void fader (int color1, int color2) {
of (int bright = 0; brightness <256; bright ++) {
analogWrite (color1, 255-light);
analogWrite (color2, light);
delays (10);
}
}
Let's take a closer look at this job description. The function is called "fader"
and takes two arguments. Each argument is comma-separated and has the
type declared in the first line of the job description:
void fader (int color1, int color2) {
We recognize that both fader-accepted arguments are ints, and we use the
words "color1" and "color2" as dummy variations in our definition of work.
"Void" refers to the type of data that is returned to work, because our work
returns nothing (simply issuing commands), we set the return type to zero. If
we were to build a two-digit operation and return a product we would
describe it as follows:
int multiplier (int number1, int number2) {
int product = number1 * number2;
return product;
}
Note how we declared int as a return type here instead of void.
Intestinal function is something we have seen before. It is the same with the
loop we repeated in our last drawing, but the pin numbers have been replaced
by the color change1 and color2. When we call:
fader (red, green);
from Arduino loop (), Arduino checks the fader function for color 1 = red and
color2 = green.
To sum up all this we can rewrite this drawing and use this function as
follows, this will work exactly the same as the diagram above this step.
//RGB LED - fading between colors
//pin connections
int red = 9;
int green = 10;
int blue = 11;
void setup(){
pinMode(red, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(green, OUTPUT);
}
void loop(){
fader(red,green);
fader(green,blue);
fader(blue, red);
}
void fader(int color1, int color2){
for (int brightness=0;brightness<256;brightness++){
analogWrite(color1, 255-brightness);
analogWrite(color2, brightness);
delay(10);
}
}
Time for a new type of circuit, we will now look at how we use Arduino
pressure buttons. Buttons are a type of switch, the type of button we use is
called the "normal temporary open button". "Normally open" means that
when the button is not pressed, no current will flow to the button because
both sides are not connected - it forms like an open circuit (see first image
above). "Temporary" means the fact that this switch only opens when you
press it with your finger; this makes it different from the switch switch,
which switches between open and closed positions every time you press it.
The button circuit we will use is made up of two elements - the Push button
and the pixel. Unlike an LED circuit, we are not concerned with the current
value of the pass button (at very high current levels we may have to worry
about melting the button, but the Arduino is less powerful), so the resistor
does not work as a current resistor in the LED cycle. Instead this Resistor acts
as a pull resistor. The pull resistor binds the button down, so that the voltage
measurement at the junction between the button and the resistor will remain
0V (set) when the button is not pressed (and the circuit is open). In this
region, the amount of gravity resistor does not matter, I like to use something
around 10kOhms.
Here is a button drawing:
//Button Press Detection
int buttonPin = 7;
void setup(){
pinMode(buttonPin, INPUT);//this time we will set the pin as INPUT
Serial.begin(9600);//initialize Serial connection
}
void loop(){
if (digitalRead(buttonPin)==HIGH){//if button pressed
Serial.println("pressed");
} else {
Serial.println("unpressed");
}
}
Now open the pot and see how the printed value of potVal changes. You
should see the arduino 1023 print when you open the pot all the way to the
connected side in 5V, and 0 when you open the pot to the other side. You
should also see a list of printed prices among those extremes.
Also check the pressure (x, a, b) - prevents the number x between a and b. If
x is under the return pressure a, x is greater than b constrain Return b,
otherwise the return is x.
The diagram turns off the LED and continues depending on the position of
the button (pressed / pressed), and at the same time uses a potentiometer to
control the LED light when it is in “on” position.
Step 15: Button As Change Switch
Sometimes you will be interested in the exact moment when the button is
pressed or released, so you can start the event in your drawing. In this case
you will need to save the currentState button and compare it to the last
recorded country. If currentState is high and lastState is low, you will know
that the button has just been pressed. See code below:
//Button Press Detection - single message
int buttonPin = 7;
boolean currentState = LOW;//stroage for current button state
boolean lastState = LOW;//storage for last button state
void setup(){
pinMode(buttonPin, INPUT);//this time we will set the pin as INPUT
Serial.begin(9600);//initialize Serial connection
}
void loop(){
currentState = digitalRead(buttonPin);
if (currentState == HIGH && lastState == LOW){//if button has just been pressed
Serial.println("pressed");
delay(1);//crude form of button debouncing
} else if(currentState == LOW && lastState == HIGH){
Serial.println("released");
delay(1);//crude form of button debouncing
}
lastState = currentState;
}
I added an extra assistant function in the code above to help select the next
color that will end in:
int getNextColor (int color) {
if (color == red) returns green;
if (color == green) return to blue;
if (color == blue) returns red;
}
I announced this function by int to tell Arduino that he should expect the
function to return the number (in this case, the Arduino pin value connected
to one of the RGB LED pins.
int getNextColor(int color){
if (color == red) {
return green;
}
if (color == green) {
return blue;
}
if (color == blue) {
return red;
}
}
and it will work the same way. If you only need to make one line in the if
statement, you can use shorthand:
if (something) doSomething;
without curly braces or linebreaks.
So far we have been using the delay function () to pause the Arduino drawing
so that the time interval between the two Arduino commands passes. In the
LED blink panel, we used the delay to set the Arduino turn on and off:
loop () {
digitalWrite (ledPin, HIGH);
delay (1000);
digitalWrite (ledPin, LOW);
delay (1000);
booleanState = digitalRead button (7);
}
In the code above, we measure the button once every two seconds, so it can
take up to two seconds before a button is detected, and very short machines
may not be available at all.
millis gives us control over when events occur without pausing in the
diagram. Each time we call millis in an Arduino diagram, it returns the
number of milliseconds since the Arduino was opened.
4,294,967,295 ms
= 4,294,967 seconds
= 49.71 days
If you use millis () and plan to keep the project running for a long time
without shutting down or resetting, you should keep this in mind.
One comment about data types: We may have been using the remote or
unsigned all this time when announcing pin numbers or other variables in the
image to date, but it is usually a good idea to use very small data type to
change, thus having more space in Arduino memory for other items . In
Arduino, desires are rarely used, but millis () are a good example when they
come in handy.
Going back to the diagram, it is a common idea to save one last time when
you turn on or off the LED and compare it to the current restored by millis ().
When the difference between the two times is greater than the interval, you
know it is time to change the LED again. To do this I set a new storage
variable:
void setup() {
//all connections to 595 are outputs
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
void loop() {
//first set latch pin low so we can shift in data without disrupting the outputs
digitalWrite(latchPin, LOW);
// shift out bits of data
shiftOut(dataPin, clockPin, LSBFIRST, numberToDisplay);
//set latch pin high to send data to output pins
digitalWrite(latchPin, HIGH);
}
This code introduces a new type of data called byte, byte is the same as an
int, but since bytes require only 8 pieces of memory, they store numbers
between 0 and 255 (2 ^ 8 = 256).
Some code is straight, out of line:
Now try some numbers, the number 15 is binary 00001111, you can get
another decimal in the google conversion by typing # and then the phrase "to
binary" (the output number will start with 0b, ignore that section and hold the
last 8 digits) . Keep in mind that we can only send 8-digit binary numbers to
them (8-bit) in the exchange register because it has only 8 output pins, so the
number of ToTisDisplay values must be between 0 and 255.
Now try changing the parameter LSBFIRST to MSBFIRST, you should see
the LED setting back, this switch sets the indicator that we are sending the
binary number to 595: LSBFIRST means "less important first" and
MSBFIRST means " most importantly first ".
To make it a little more interesting, try the following:
int clockPin = 7;
int latchPin = 6;
int dataPin = 5;
void setup() {
//all connections to 595 are outputs
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
void loop() {
for (byte numberToDisplay=0;numberToDisplay<256; numberToDisplay++){
digitalWrite(latchPin, LOW);
// shift out bits of data
shiftOut(dataPin, clockPin, MSBFIRST, numberToDisplay);
//set latch pin high to send data to output pins
digitalWrite(latchPin, HIGH);
delay(500);
}
}
You have now turned your LEDs into a binary counter:
Next we will look at using Arduino to control an 8x8 LED matrix, a 64-LED
grid. The 8x8 matrix we are going to use has 16 connectors: eight pins
connect the positive LED track to each column of the matrix, and the other
eight pins connect the ground track of all the LEDs to each matrix line. This
gives us control over each individual LED. Look at the picture in the second
picture above. The image that all the columns are fixed except column 8,
connected (with the current fixed resistance) to 5V. There is no image that all
lines are connected to 5V except line 1, which is connected to the ground.
The only LED that will light up in this case is found in line 1 and column 1.
Place the LED matrix on the bread board as shown in the first picture. Use a
current-limit resistor to connect the columns (see pin numbers in the second
picture) to 5V and then use a standard jumper wire to connect the lines to the
ground. You should see all the LED light light up. Now try unplugging to
connect the line to the ground and connect it to 5V instead, all the LEDs in
that list will turn off. Try connecting column down, all LEDs in that column
will be turned off.
Now disconnect all but one of the connectors from the line pins to the
bottom, so only one line of LEDs glows. Instead of connecting the columns
to 5V, connect them to Arduino (you are still installing current limiters that
limit the region). See the third picture for a better idea of what this should
look like. Here's how the columns should be linked to Arduino:
column 1 - Arduino A0 (analog pin 0)
column 2 - Arduino A1
column 3 - Arduino A2
column 4 - Arduino A3
column 5 - Arduino A4
column 6 - Arduino A5
column 7 - Arduino D2 (2-digit digital)
column 8 - Arduino D3
Generate the following code:
void setup(){
//set pins A0-A6 as outputs
for (int pinNum=A0;pinNum<A6;pinNum++){
pinMode(pinNum, OUTPUT);
}
//set pins 2 and 3 as outputs
for (int pinNum=2;pinNum<4;pinNum++){
pinMode(pinNum, OUTPUT);
}
}
void loop(){
//some arbitrary set of states
digitalWrite(A0, HIGH);
digitalWrite(A1, LOW);
digitalWrite(A2, HIGH);
digitalWrite(A3, LOW);
digitalWrite(A4, HIGH);
digitalWrite(A5, LOW);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
}
The only unusual thing about this code is that we use analog pins as digital
output, this is approved by Arduino. Analog anchors can work as digital
inputs and outputs, but they have additional functionality for analog inputs.
We’re going to use a lot of Arduino pins in this example (a total of 16), so I
had to start by assembling some analog pins. Also, I deliberately left 0 pins
and 1 with no attachments. Arduino uses these pins to communicate via USB,
and sometimes having objects connected to pins 0 and 1 limits your ability to
edit the board.
You should see a pattern of LEDs shining on the bottom line. One LED on,
one off, one on, one off ... and so on. This pattern is shown in Figure # 3.
Now remove the ground connection from the LED matrix, and then tick a
separate line at the bottom. You should see the same pattern on a different
line (picture # 4). In the next step we will use Arduino to select each line
selectively.
First try one thing, change the pattern on and off the LEDs, here's what I did:
void setup(){
//set pins A0-A6 as outputs
for (int pinNum=A0;pinNum<A6;pinNum++){
pinMode(pinNum, OUTPUT);
}
//set pins 2 and 3 as outputs
for (int pinNum=2;pinNum<4;pinNum++){
pinMode(pinNum, OUTPUT);
}
}
void loop(){
//some arbitrary set of different states
digitalWrite(A0, LOW);
digitalWrite(A1, HIGH);
digitalWrite(A2, LOW);
digitalWrite(A3, HIGH);
digitalWrite(A4, LOW);
digitalWrite(A5, HIGH);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
}
The output is shown in the last image above.
Step 6: LED Matrix Multiplexing
Now hit all the ground LED matrix pins on Arduino 6-13 pins. Here is the
pin connection:
Line 1 - Arduino D6 (6-digit digital pin)
line 2 - Arduino D7
line 3 - Arduino D8
line 4 - Arduino D9
line 5 - Arduino D10
line 6 - Arduino D11
line 7 - Arduino D12
line 8 - Arduino D13
Also use the following code:
void setup(){
//set pins 6-13 as outputs and initialize HIGH (so all LEDs are off to start)
for (int pinNum=6;pinNum<14;pinNum++){
pinMode(pinNum, OUTPUT);
digitalWrite(pinNum, HIGH);
}
//set pins A0-A6 as outputs
for (int pinNum=A0;pinNum<A6;pinNum++){
pinMode(pinNum, OUTPUT);
}
//set pins 2 and 3 as outputs
for (int pinNum=2;pinNum<4;pinNum++){
pinMode(pinNum, OUTPUT);
}
}
void loop(){
for (int pinNum=6;pinNum<14;pinNum++){
//set columns
digitalWrite(A0, LOW);
digitalWrite(A1, HIGH);
digitalWrite(A2, LOW);
digitalWrite(A3, HIGH);
digitalWrite(A4, LOW);
digitalWrite(A5, HIGH);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
//pulse row low to light LEDs
digitalWrite(pinNum, LOW);
delay(500);
digitalWrite(pinNum, HIGH);
}
}
You should see the same pattern of LEDs illuminating one line at a time
(second image) as each line is placed one by one. Now try to reduce the
delay, the pattern will go across the lines faster and faster. Now remove the
delay completely, you should see all lines highlighted in (what appears to be)
at the same time (third image). Here is a video sign (in this case, I use
different patterns to illuminate each line, I will explain more about that soon):
This is called multiplexing. Although it may seem like we are lighting up
many lines of LEDs at the same time, we know that we are actually lighting
up one line at a time, but we are doing it so fast that we deceive our eyes into
thinking everything is happening at once. Multiplexing helps in any time you
want to control a lot of things with a few pins. Instead of connecting each
LED individually, we can duplicate it and reduce the cost and difficulty
significantly.
Now try posting different patterns in different lines:
void setup(){
//set pins 6-13 as outputs and initialize HIGH
for (int pinNum=6;pinNum<14;pinNum++){
pinMode(pinNum, OUTPUT);
digitalWrite(pinNum, HIGH);
}
//set pins A0-A6 as outputs
for (int pinNum=A0;pinNum<A6;pinNum++){
pinMode(pinNum, OUTPUT);
}
//set pins 2 and 3 as outputs
for (int pinNum=2;pinNum<4;pinNum++){
pinMode(pinNum, OUTPUT);
}
}
void loop(){
for (int pinNum=6;pinNum<14;pinNum++){
if (pinNum%2==1){//is pinNum is odd
//some arbitrary set of states
digitalWrite(A0, HIGH);
digitalWrite(A1, LOW);
digitalWrite(A2, HIGH);
digitalWrite(A3, LOW);
digitalWrite(A4, HIGH);
digitalWrite(A5, LOW);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
} else {
//some arbitrary set of different states
digitalWrite(A0, LOW);
digitalWrite(A1, HIGH);
digitalWrite(A2, LOW);
digitalWrite(A3, HIGH);
digitalWrite(A4, LOW);
digitalWrite(A5, HIGH);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
}
digitalWrite(pinNum, LOW);
delay(500);
digitalWrite(pinNum, HIGH);
}
}
To implement this code slightly at the beginning, we see that one line is sent
to one pattern, and then the next row is sent to a different pattern, this
alternates across all lines in the matrix. Here's how I do it:
This code will set only the A5 HIGH line connected to pin 8. This illuminates
only one LED in the matrix (picture five).
Remember how we used a number between 0 and 255 (a byte) to set 8 LED
states connected to 595? Now I have added a function called setStates ()
which allows us to set up 8 LED circuits in each LED matrix line using a
byte. The first thing SetStates does is set up all the pins connected to the
LOW LED matrix columns to turn off any possible LEDs. It then looks at
each 8-digit binary byte using the & operator; if another 1 digit, sets the
corresponding PIN HIGH.
The code above places a line connected to an 8-digit digital digit using the
number 56. For binary, 56 is represented as:
00111000
And the LED output shown is shown in the first image above, you can see
that each 1 in the binary number is the same as the LED illuminated in the
matrix.
Next try to arrange each line in the LED matrix to indicate the digital pin
number attached to it:
void setup(){
//set pins 6-13 as outputs and initialize HIGH
for (int pinNum=6;pinNum<14;pinNum++){
pinMode(pinNum, OUTPUT);
digitalWrite(pinNum, HIGH);
}
//set pins A0-A6 as outputs
for (int pinNum=A0;pinNum<A6;pinNum++){
pinMode(pinNum, OUTPUT);
}
//set pins 2 and 3 as outputs
for (int pinNum=2;pinNum<4;pinNum++){
pinMode(pinNum, OUTPUT);
}
}
void loop(){
for (int pinNum=6;pinNum<14;pinNum++){
setStates(pinNum);
digitalWrite(pinNum, LOW);
digitalWrite(pinNum, HIGH);
}
}
void setStates(byte states){
zeroStates();//first turn all pins off
//look at each bit of binary number and set HIGH if need
if (states & 1) digitalWrite(A0, HIGH);
if (states & 2) digitalWrite(A1, HIGH);
if (states & 4) digitalWrite(A2, HIGH);
if (states & 8) digitalWrite(A3, HIGH);
if (states & 16) digitalWrite(A4, HIGH);
if (states & 32) digitalWrite(A5, HIGH);
if (states & 64) digitalWrite(2, HIGH);
if (states & 128) digitalWrite(3, HIGH);
}
void zeroStates(){
digitalWrite(A0, LOW);
digitalWrite(A1, LOW);
digitalWrite(A2, LOW);
digitalWrite(A3, LOW);
digitalWrite(A4, LOW);
digitalWrite(A5, LOW);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
}
void setup(){
//set pins 6-13 as outputs and initialize HIGH
for (int pinNum=6;pinNum<14;pinNum++){
pinMode(pinNum, OUTPUT);
digitalWrite(pinNum, HIGH);
}
//set pins A0-A6 as outputs
for (int pinNum=A0;pinNum<A6;pinNum++){
pinMode(pinNum, OUTPUT);
}
//set pins 2 and 3 as outputs
for (int pinNum=2;pinNum<4;pinNum++){
pinMode(pinNum, OUTPUT);
}
}
void loop(){
for (int i=0;i<8;i++){
setStates(ledStates[i]);
int pinNum = getPinNumForLEDIndex(i);
digitalWrite(pinNum, LOW);
digitalWrite(pinNum, HIGH);
}
}
int getPinNumForLEDIndex(int index){
return index+6;
}
void setStates(byte states){
zeroStates();//first turn all pins off
//look at each bit of binary number and set HIGH if need
if (states & 1) digitalWrite(A0, HIGH);
if (states & 2) digitalWrite(A1, HIGH);
if (states & 4) digitalWrite(A2, HIGH);
if (states & 8) digitalWrite(A3, HIGH);
if (states & 16) digitalWrite(A4, HIGH);
if (states & 32) digitalWrite(A5, HIGH);
if (states & 64) digitalWrite(2, HIGH);
if (states & 128) digitalWrite(3, HIGH);
}
void zeroStates(){
digitalWrite(A0, LOW);
digitalWrite(A1, LOW);
digitalWrite(A2, LOW);
digitalWrite(A3, LOW);
digitalWrite(A4, LOW);
digitalWrite(A5, LOW);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
}
To find variations stored in the same members, use the following syntax:
arrayName [variableIndex]
where variableIndex is a place of diversity for the same members. For
example:
ledStates [0] equals 0
ledStates [1] equals 15
because the first item (index = 0) in the array is 0, and the second item (index
= 1) in the array is 15.
Since I no longer go beyond pinNums in my loop, I create a help function
that switches between the variation of my iterator "i" and the Arduino pin
connected to the interest line:
int getPinNumForLEDIndex (int index) {
return index +6;
}
Try switching bytes on ledStates of the same members to see how it affects
the output of the matrix. And try changing the variables stored in ledStates as
the drawing works, use the following syntax:
ledStates [indexNum] = newValue
You can also set LEDs using bytes marked as follows:
B11011011
B informs Arduino that he has to wait for the binary number, and then the
next 8 numbers are 8 digits on the banner you want to show. This makes it
slightly easier to design a 2D pattern on an LED matrix. Try using this
listing:
byte ledStates [8] = {B0111100, B01000010, B10110101, B10000101,
B10000101, B10110101, B01000010, B0111100};
The easiest way I found to control the LED matrix with the MAX7219 chip,
basically manages all the multiplexing inside, each chip can control up to 64
LEDs, and requires only three Arduino digital output pins, however at a
relatively low cost at $ 11 each. There is a ton of information about using that
chip on the Arduino website. MAX7219 will not allow you to adjust the
brightness of the LEDs individually, it will only control its turn off / off
mode. If you need to control the brightness of most LEDs, you can check out
the TLC5940 (Arduino library here), or I agree, it is tricky to duplicate this
chip for some time view - that could be an advanced project - but each chip
easily controls no LEDs -16 and you can put them together for more control.
Goods:
1 Arduino Nano (or Uno, with 328P CPU)
1 Bread Board (830 Holes)
14 Jumper Cables (male to male, 10 ... 20 cm)
12 Jumper threads (male to female, 10 ... 20 cm)
2 LEDs (5mm, 20mA, around 2V)
2 opponents (220 Ohm)
2 Microswitches
1 Potentionmeter (5 or 10 kOhm)
1 5V Power Supply
2 28BYJ-48 Stepper Motors (5V Version) + ULN 2003
Driver Boards
The main idea of moving stepper motors is to turn on and off the coils on the
motors using digital output pins. In Arduino, the PORTB and PORTD
registers are represented, where D2… 5 (= motor 1) is 2… 5 bits of PORTD,
D7… 8 (the first two wires to motor 2) are bits 6… 7 for PORTD and D9…
10 (two second engine cables 2) are 0… 1 pieces of PORTB. Therefore, the
lines
“Keep” the current state of all unrelated antenna motors (AND operation) and
then apply the output pattern for the next step (half) (OR operation).
28BYJ-48 motors are discussed on many other websites, so more information
- if needed - can be found here, here or here.
After compiling and uploading the code, select "Tools → Serial Monitor" in
Arduino IDE. You should see running motors, LEDs (on the bread board and
ULN2003 boards) flashing. Response to pressing (and holding) / removing
microswitches and switching ports will be built into the serial monitor
monitor.
However: we are not done yet, as the program is only going back and forth.
So let’s make the next impression next.
/*
* Unipolar stepper motor speed and direction control with Arduino.
* Full step control.
* This is a free software with NO WARRANTY.
* https://simple-circuit.com/
*/
void setup()
{
// configure button pin as input with internal pull up enabled
pinMode(button, INPUT_PULLUP);
}
void loop()
{
if ( digitalRead(button) == 0 ) // if button is pressed
if ( debounce() ) // debounce button signal
{
direction_ *= -1; // reverse direction variable
while ( debounce() ) ; // wait for button release
}
}
// a small function for button debounce
bool debounce()
{
byte count = 0;
for(byte i = 0; i < 5; i++) {
if (digitalRead(button) == 0)
count++;
delay(10);
}
if(count > 2) return 1;
else return 0;
}
Arduino Microcontroller
LED
Resistor (220 ohms or less)
Bread board
4pcs Jumper cables
The latest Arduino IDE
Step 1: Know What a LED Is
Light Emitting Diode or LED is basically a Diode that can emit light.
Being a diode means having a Cathode pin, a badly charged electrode where
electrons enter the electrical field, and an Anode pin, a well-charged
electrode where the electrons leave the device.
Take an LED and look !. One leg long and one short. The long leg is Positve
(Ano). The short leg is the Cathode.
Same length? No problem brother! Look inside the LED, the largest metal
does not have and the smallest goes to the Anode pin
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin ledPin, as an output.
pinMode(ledPin, OUTPUT);
}
If the onLed condition is true turn up the light and the other goes down
if(onLed)
val++; // increase brightness
else
val--; //decrease brightness
Update LED light
analogWrite(ledPin, val);
When the light reaches a high value, change the Led mode to false
otherwise if 0 converts Led status to true then wait 1000 miliseconds
then wait 10 milliseconds
if(val==255)
onLed = false; // turns off LED
else if(val==0){
onLed = true; // lights on LED
delay(1000); // waits for a second before on again
}
delay(10);
You can follow up almost using Tinkercad Circuits. You can watch this
tutorial within Tinkercad (free sign-in required)! Examine the regional
sample and create your own right next to it. Tinkercad Circuits is a free
browser-based program that allows you to create and emulate circuits. It is
ideal for learning, teaching and prototyping.
As you learned from the introduction, first connect your Arduino wires to the
bread board with power and ground near the circuit pattern, and then place
the three red LEDs on the bread board, as shown. These will be "bar graph"
lights to visually display the distance of the sensor.
Drag the Arduino Uno with the bread board from the object panel to the
workspace, next to the existing circuit.
Connect 5 volt and ground pins to Arduino power (+) and ground (-) to the
breadboard with wires. You can change the phone colors if you want! It can
be the use of a checker download or numeric keys on your keyboard.
Drag the three LEDs on the bread board in line E, separated by 2 socket
sockets. You can change the color of the LED using the tester that appears
when you click on each one.
Use a 220 Ohm resistor to connect each LED cathode (left leg) to the lower
(black) rail of the bread board. You can change the value of the resistor by
highlighting it and using the drop-down menu.
Connect LED anodes (right legs) to digital pins 4, 3, and 2 in Arduino. The
LED (+) anode is the only current flowing through it. This will connect to
digital output pins in Arduino. The cathode (-) is the only current flowing
through it. This will connect to the subway.
Before setting (), we create a variable to keep the target distance limit, and
the sensor value in inches (cm) and inches. They are called int because they
are whole numbers, or another whole number.
long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}
The next section is a special code for sensing the ultrasonic range sensor. It's
called work. So far you are accustomed to setting () and loop (), but in this
diagram, the readUltrasonicDistance () function is used to define sensor code
and keep it separate from the main body of the system. The job description
starts with what kind of data the job will return, or send back to the main
program. In this case the function returns the length, which is the number of
multi-digit decimal points. Next up is the job title, which is up to you. After
that in brackets there are conflicts the work you take. int triggerPin, int
echoPin flexible announcements for your sensor connection pins. The pin
numbers will be specified when calling the function in the main loop loop ().
Within a function, this local variation is used to refer to information you have
transferred from a large loop (or other function). The function itself sends the
signal via triggerPin and reports the time it takes to retrieve the signal over
echoPin.
void setup()
{
Serial.begin(9600);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
}
Within setup, anchors are configured using the pinMode () function. A serial
monitoring connection is established with Serial.begin. Anchors 2, 3, and 4
are configured as LED control effects.
void loop()
{
// set threshold distance to activate LEDs
distanceThreshold = 350;
// measure the ping time in cm
cm = 0.01723 * readUltrasonicDistance(7, 6);
Step 1: Parts
Arduino Board Uno * 1
USB cable * 1
Resistor (220Ω) * 8
LED * 8
Potentiometer * 1
Bread board * 1
Jumping ropes
Step 2: Schedule
8 consecutive LEDs light up and dimmer respectively. The goal of this
experiment is simply to turn on eight LEDs in a row. Eight LEDs are
connected to a 2-pin 9 pin respectively. Set them as high quality and the
corresponding LED on the pins will light up. Control each LED light time
and you will see flowing LED lights.
Step 4: Procedures
The goal of this experiment is simply to turn on eight LEDs in a row. Eight
LEDs are connected to a 2-pin 9 pin respectively. Set them as high quality
and the corresponding LED on the pins will light up. Control each LED light
time and you will see flowing LED lights.
Step 1:
Create a circuit.
Step 2:
Download code from https://github.com/primerobotics/Arduino
Step 3:
Insert the drawing on the Arduino Uno board
Click the upload icon to upload the code to the control board.
If "Uploaded" appears at the bottom of the window, it means that the drawing
has been successfully uploaded.
Now, you should see eight LEDs that light up one by one from the LED
connected to pin 2 to point 9, and then blur the rotation from LED to pin 9 to
pin 2. After that, the LEDs will light up from LED pin 9 to pin 2 and blur
from LED pin 2 to pin 9. The whole process will be repeated until the circuit
is turned off.
Step 5: Code
//Flowing
LED Lights
/*
Eight LEDs will light up one by one from left to right, and then go out one by
one from right to left.
After
that, the LEDs will light up one by one from right to left, and then go out one
by one from left to right.
This
process will repeat indefinitely.*
/Email:info@primerobotics.in
//Website:www.primerobotics.in
/**************************************/
const
int lowestPin = 2;//the lowest one attach to
const
int highestPin = 9;//the highest one attach to
/**************************************/
void
setup()
pinMode(thisPin,OUTPUT); //initialize
thisPin as an output
/****************************************/
void
loop()
for(int thisPin =
highestPin;thisPin>=lowestPin;thisPin--)
Before setting (), we create a variable to store the read current value from the
potentiometer. It is called an int because it is a whole number or another
whole number.
void setup()
{
pinMode(A0, INPUT);
pinMode(9, OUTPUT);
Serial.begin(9600);
}
Within setup, anchors are configured using the pinMode () function. Pin A0
is configured as an input, so we can "listen" to the electrical status of the
potentiometer. Pin 9 is configured as an LED control switch. In order to send
messages, Arduino opens a new serial communication channel. Start (),
which takes the baud rate (any connection speed), in this case, 9600 bits per
second.
void loop()
{
// read the value from the sensor
sensorValue = analogRead(A0);
// print the sensor reading so you know its range
Serial.println(sensorValue);
The line that follows the next comment does a lot at once. Remember
analogWrite () takes two arguments, the pin number (9 of us), and the write
value, which should be between 0 and 255. The performance line in line ()
captures five issues: the number to test (constant- to change the sensitivity),
the minimum expected and the maximum size, and the required dates and
sizes. So the map function () for us checks the incoming sensorValue, and
performs certain repetitions to output output from 0-1023 to 0-255. The effect
is returned to the second analogWrite () ;, set the brightness of the LED
connected to pin 9.
You will need the following items to make this LED Cube:
www.bookbenefits.com
wire to connect it.
Blue lines are where you have to scratch the copper so that it no longer
works.
/*
Don't forget to check out my YouTube channel:
https://www.youtube.com/channel/UCKp8cQWkiGGfAV5FM_Q0mxA
*/
void setup()
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
}
void allLayer() {
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
}
void noLayer() {
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
}
void rotate() {
allLayer();
www.bookbenefits.com
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
delay(100);
empty();
digitalWrite(2, HIGH);
digitalWrite(6, HIGH);
digitalWrite(10, HIGH);
delay(100);
empty();
digitalWrite(3, HIGH);
digitalWrite(6, HIGH);
digitalWrite(9, HIGH);
delay(100);
empty();
digitalWrite(4, HIGH);
digitalWrite(6, HIGH);
digitalWrite(8, HIGH);
delay(100);
empty();
noLayer();
}
void fill() {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
}
void swirl() {
digitalWrite(2, HIGH);
delay(50);
digitalWrite(3, HIGH);
delay(50);
digitalWrite(4, HIGH);
delay(50);
digitalWrite(7, HIGH);
delay(50);
digitalWrite(10, HIGH);
delay(50);
digitalWrite(9, HIGH);
delay(50);
digitalWrite(8, HIGH);
delay(50);
digitalWrite(5, HIGH);
delay(50);
digitalWrite(6, HIGH);
delay(50);
}
void myName() {
//Y
digitalWrite(11, HIGH);
digitalWrite(2, HIGH);
digitalWrite(4, HIGH);
digitalWrite(6, HIGH);
digitalWrite(9, HIGH);
delay(100);
noLayer();
digitalWrite(12, HIGH);
delay(100);
noLayer();
digitalWrite(13, HIGH);
delay(200);
noLayer();
digitalWrite(12, HIGH);
delay(100);
noLayer();
digitalWrite(11, HIGH);
delay(100);
noLayer();
empty();
//O
digitalWrite(11, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
delay(100);
noLayer();
digitalWrite(12, HIGH);
delay(100);
noLayer();
digitalWrite(13, HIGH);
delay(200);
noLayer();
digitalWrite(12, HIGH);
delay(100);
noLayer();
digitalWrite(11, HIGH);
delay(100);
noLayer();
empty();
//U
digitalWrite(11, HIGH);
digitalWrite(2, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
delay(100);
noLayer();
digitalWrite(12, HIGH);
delay(100);
noLayer();
digitalWrite(13, HIGH);
delay(200);
noLayer();
digitalWrite(12, HIGH);
delay(100);
noLayer();
digitalWrite(11, HIGH);
delay(100);
noLayer();
empty();
//R
digitalWrite(11, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(8, HIGH);
delay(100);
noLayer();
digitalWrite(12, HIGH);
delay(100);
noLayer();
digitalWrite(13, HIGH);
delay(200);
noLayer();
digitalWrite(12, HIGH);
delay(100);
noLayer();
digitalWrite(11, HIGH);
delay(100);
noLayer();
empty();
//I
digitalWrite(11, HIGH);
digitalWrite(3, HIGH);
digitalWrite(6, HIGH);
digitalWrite(9, HIGH);
delay(100);
noLayer();
digitalWrite(12, HIGH);
delay(100);
noLayer();
digitalWrite(13, HIGH);
delay(200);
noLayer();
digitalWrite(12, HIGH);
delay(100);
noLayer();
digitalWrite(11, HIGH);
delay(100);
noLayer();
empty();
}
void empty() {
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
}
void createX() {
digitalWrite(2, HIGH);
digitalWrite(4, HIGH);
digitalWrite(6, HIGH);
digitalWrite(8, HIGH);
digitalWrite(10, HIGH);
delay(1000);
digitalWrite(2, LOW);
digitalWrite(4, LOW);
digitalWrite(6, LOW);
digitalWrite(8, LOW);
digitalWrite(10, LOW);
delay(200);
digitalWrite(2, HIGH);
digitalWrite(4, HIGH);
digitalWrite(6, HIGH);
digitalWrite(8, HIGH);
digitalWrite(10, HIGH);
delay(200);
digitalWrite(2, LOW);
digitalWrite(4, LOW);
digitalWrite(6, LOW);
digitalWrite(8, LOW);
digitalWrite(10, LOW);
delay(200);
digitalWrite(2, HIGH);
digitalWrite(4, HIGH);
digitalWrite(6, HIGH);
digitalWrite(8, HIGH);
digitalWrite(10, HIGH);
delay(200);
digitalWrite(2, LOW);
digitalWrite(4, LOW);
digitalWrite(6, LOW);
digitalWrite(8, LOW);
digitalWrite(10, LOW);
delay(200);
digitalWrite(2, HIGH);
digitalWrite(4, HIGH);
digitalWrite(6, HIGH);
digitalWrite(8, HIGH);
digitalWrite(10, HIGH);
delay(200);
digitalWrite(2, LOW);
digitalWrite(4, LOW);
digitalWrite(6, LOW);
digitalWrite(8, LOW);
digitalWrite(10, LOW);
delay(200);
digitalWrite(2, HIGH);
digitalWrite(4, HIGH);
digitalWrite(6, HIGH);
digitalWrite(8, HIGH);
digitalWrite(10, HIGH);
delay(200);
digitalWrite(2, LOW);
digitalWrite(4, LOW);
digitalWrite(6, LOW);
digitalWrite(8, LOW);
digitalWrite(10, LOW);
delay(500);
}
void loop() {
digitalWrite(13, HIGH);
fill();
delay(200);
empty();
noLayer();
digitalWrite(12, HIGH);
fill();
delay(100);
empty();
noLayer();
digitalWrite(11, HIGH);
fill();
delay(200);
empty();
noLayer();
digitalWrite(12, HIGH);
fill();
delay(100);
empty();
noLayer();
digitalWrite(13, HIGH);
fill();
delay(200);
empty();
noLayer();
digitalWrite(12, HIGH);
fill();
delay(100);
empty();
noLayer();
digitalWrite(11, HIGH);
fill();
delay(200);
empty();
noLayer();
digitalWrite(12, HIGH);
fill();
delay(100);
empty();
noLayer();
digitalWrite(13, HIGH);
fill();
delay(200);
empty();
noLayer();
digitalWrite(12, HIGH);
fill();
delay(100);
empty();
noLayer();
digitalWrite(11, HIGH);
fill();
delay(200);
empty();
noLayer();
digitalWrite(12, HIGH);
fill();
delay(100);
empty();
noLayer();
digitalWrite(13, HIGH);
fill();
delay(200);
empty();
noLayer();
delay(1000);
digitalWrite(13, HIGH);
swirl();
delay(10);
empty();
noLayer();
digitalWrite(12, HIGH);
swirl();
delay(10);
empty();
noLayer();
digitalWrite(11, HIGH);
swirl();
delay(10);
delay(400);
empty();
noLayer();
allLayer();
swirl();
delay(400);
empty();
noLayer();
delay(1000);
//NAME COMES UP
myName();
delay(1000);
First we will clarify how we want to use our Arduino pins. We can do this by
pasting the following code into our useless "setup":
pinMode (2, OUTPUT);
pinMode (3, OUTPUT);
pinMode (4, OUTPUT);
pinMode (5, OUTPUT);
pinMode (6, OUTPUT);
pinMode (7, OUTPUT);
pinMode (8, OUTPUT);
pinMode (9, OUTPUT);
pinMode (10, OUTPUT);
pinMode (11, OUTPUT);
pinMode (12, OUTPUT);
pinMode (13, OUTPUT);
Anchors 2-10 should control the LEDs on each layer. Pin 11 control is the
lowest layer, pin 12 controls the middle and pin 13 controls the top layer.
To connect your Arduino to the subject you must use the following pinout
(depending on the image provided):
Left to right> D10 - D9 - D8 - D7 - D6 - D5 - D4 - D3 - D2 - GND - D11 -
D12 - D13
It helps to create a void to make all the layers work at the same time. You can
do this easily using the following void:
Void allLayer () {
digitalWrite (11, HIGH);
digitalWrite (12, HIGH);
digitalWrite (13, HIGH);
}
To disable all layers simultaneously you can use the same format. All you
have to do is change the void name and change the maximum value to LOW.
You can also use this structure to make all the LEDs at once.
void loop()
{ long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 10)
{ digitalWrite(led,HIGH);
}
else {
digitalWrite(led,LOW);
}
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
The RGB LED has 4 pins, one for each color (Red, Green, Blue) and a
standard cathode. It has color-coded diodes that separate the trees that can be
combined to create all kinds of color! Any color is possible depending on
how bright each diode is.
In this tutorial, you will learn how to use ArGBino RGB LED and create a
unique color combination.
The connection is very simple, see the picture above for the circuit board
scheme.
Step 3: Code
/*
SparkFun Inventor's Kit
Example sketch 03
RGB LED
Hardware connections:
*/
void setup()
{
// Here we'll configure the Arduino pins we're using to
// drive the LED to be outputs:
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
}
void loop()
{
// In this sketch, we'll start writing our own functions.
// This makes the sketch easier to follow by dividing up
// the sketch into sections, and not having everything in
// setup() or loop().
showSpectrum();
}
// This function displays the eight "main" colors that the RGB LED
// can produce. If you'd like to use one of these colors in your
// own sketch, you cancopy and paste that section into your code.
void mainColors()
{
// Off (all LEDs off):
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
delay(1000);
delay(1000);
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
delay(1000);
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
delay(1000);
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
delay(1000);
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);
delay(1000);
// Purple (turn red and blue on):
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
delay(1000);
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);
delay(1000);
}
// showSpectrum()
// This function steps through all the colors of the RGB LED.
// It does this by stepping a variable from 0 to 768 (the total
// number of colors), and repeatedly calling showRGB() to display
// the individual colors.
// For the for() loop below, these are the three statements:
{
showRGB(x); // Call RGBspectrum() with our new x
delay(10); // Delay for 10 ms (1/100th of a second)
}
}
// showRGB()
// Now that the brightness values have been set, command the LED
// to those values
You can follow up almost using Tinkercad Circuits. You can watch this
tutorial within Tinkercad (free sign-in required)! Examine the regional
sample and create your own right next to it. Tinkercad Circuits is a free
browser-based program that allows you to create and emulate circuits. It is
ideal for learning, teaching and prototyping.
Check the circuit sample here in the embedded circuit below by starting the
simulation and clicking the circular motion sensor. This will activate the
highlighted area in front of the sensor with the "object" circle inside. You
may need to expand the view when the circle is closed on the screen. Click
and drag the "object" circle before the sensor to represent the movement. The
LED will turn on for a moment when motion is detected.
The free cable type for this region is shown above. If necessary, take a
moment to refresh your breadboard experience. You can download the new
Tinkercad Circuits window and build your own version of this circuit next to
the sample.
Identify the PIR movement sensor, LED, resistor, and Arduino connected
cables.
Drag the Arduino Uno and bread board from the object panel to the
performance plane.
Connect the power of the breadboard (+) and the rail (-) rail to the Arduino
5V with the ground (GND), respectively, by clicking to create wires.
Increase the strength of the power rail and their proper buses on the opposite
side of the bread board by forming a red fence between both electric buses
and a black fence between both lower buses.
Connect the LED to different loops of bread so that the cathode (negative,
short leg) connects to one leg of the resistor (any from 100-1K ohms is fine).
The resistor can enter any position because the separate resistors, unlike the
LEDs, have to be connected in a certain way in order to work.
Connect the other leg to the ground resistance.
Install the LED anode (good leg, long leg) on Arduino pin 13.
Drag the PIR movement sensor from the component panel to your bread
board, so its legs connect in three different lines.
Click to create a call that connects the right leg to power.
Connect the center leg to the ground.
Create a wire that connects the left leg to the Arduino analog pin A0.
www.bookbenefits.com
Let's use the Blocks coding interface to listen to the PIR movement sensor,
and then make the decision to turn on the LED depending on the sensor state:
activated or not activated.
Click the "Code" button to open the code editor.
Click the Variables section in the code editor. Create a new variable called
sensorState.
Drag the "set" block.
We will maintain the status of our PIR motion sensor in our flexible
sensorState. Click the Input Input section, drag outside the "read digital PIN"
block, and place it in the "set" block behind the word "to".
Since our sensor is connected to Arduino on Pin 2, change the drop down of
the "read digital PIN" block to 2. Now your blocks should read "set
sensorState to read digital pin 2" which stores digital pin sensor reading in
our State variable sensor!
Click the Control section and remove the "if any" block
Set it up to test whether the sensorState equals HIGH using the Math
comparison block. Drag the Mat comparison block in your statement to see if
our flexible sensorState is equal to HIGH.
We want to turn on our LED when the sensor is working - otherwise, we
want our LED to be turned off. Under the Output block section, find the
block “set built-in LED up and down”. Try adding two of these blocks to our
if statement so that the LED will only light up when the sensor is working.
built-in built-in LED should be HIGH when sensitivity is high (otherwise, the
built-in LED should be LOW.
Before setting (), we create a variable to maintain the current sensor state. It
is called int because it is a whole number, or another whole number (although
we will be using values 0 and 1, LOW and HIGH).
void setup()
{
pinMode(2, INPUT);
pinMode(13, OUTPUT);
Serial.begin(9600);
}
Within setup, anchors are configured using the pinMode () function. Pin 2 is
set as input, so we can "listen" to the sensor's electrical condition. Pin 13 is
configured as an LED control switch. To be able to send messages, Arduino
opens a new serial communication channel with Serial.begin (), which
captures the baud rate (any connection speed), this time at 9600 bits per
second.
void loop()
{
// read the state of the sensor/digital input
sensorState = digitalRead(2);
Below the two lines of comment statement is a test to see if the sensorState is
HIGH (== is a comparison operator, not to be confused with =, which is a
share operator). When the condition is met, the built-in LED is METAL
(turned on). If not, the code contained within the rest {is used instead: the
built-in LED is set to LOW (off). If the statements can be alone, or with one
or more statements.
To customize your Arduino Uno, you will need to install free software (or a
web editor plugin) and open it.
Connect the Arduino Uno circuit by inserting parts and wires to match the
connections shown here in the Tinkercad Circuits. To get deeper into
working with your Arduino Uno board, check out the free section of Arduino
Instructables.
Copy the code from the Tinkercad Circuits code window and paste it into a
blank diagram in your Arduino software, or click the download button (down
arrow) and open the file using Arduino. You can also find this example in
Arduino software by navigating to File -> Examples -> 02. Digital -> Button
(with a different but different name).
Connect your USB cable and select your board and hole in the software tools
menu.
Download the code and watch your LED light as you go before the sensor!
You can also learn about many electronic skills with free command
categories in Arduino, Basic Electronics, LEDs and lighting, 3D printing, and
more.
Project 10 - DIY Arduino Obstacle
Avoiding Car at Home
In this article I will show you how to make an Arduino Barrier Avoidance
Car at home.
Here is the Component List
Arduino Uno
Driver's Shield
Wheels (4x)
TT Gear Motor (4x)
Servo Motor
Ultrasonic Sensor
18650 Li-on Battery (2x)
18650 Battery Holder
Jumpper and Female Phone
Acrylic Sheet
DC Power Switch
Step 1: Building materials
#include <AFMotor.h>
#include <NewPing.h>
#include <Servo.h>
#define TRIG_PIN A0
#define ECHO_PIN A1
#define MAX_DISTANCE 200
#define MAX_SPEED 190 // sets speed of DC motors
#define MAX_SPEED_OFFSET 20
boolean goesForward=false;
int distance = 100;
int speedSet = 0;
void setup() {
myservo.attach(10);
myservo.write(115);
delay(2000);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
}
void loop() {
int distanceR = 0;
int distanceL = 0;
delay(40);
if(distance<=15)
{
moveStop();
delay(100);
moveBackward();
delay(300);
moveStop();
delay(200);
distanceR = lookRight();
delay(200);
distanceL = lookLeft();
delay(200);
if(distanceR>=distanceL)
{
turnRight();
moveStop();
}else
{
turnLeft();
moveStop();
}
}else
{
moveForward();
}
distance = readPing();
}
int lookRight()
{
myservo.write(50);
delay(500);
int distance = readPing();
delay(100);
myservo.write(115);
return distance;
}
int lookLeft()
{
myservo.write(170);
delay(500);
int distance = readPing();
delay(100);
myservo.write(115);
return distance;
delay(100);
}
int readPing() {
delay(70);
int cm = sonar.ping_cm();
if(cm==0)
{
cm = 250;
}
return cm;
}
void moveStop() {
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void moveForward() {
if(!goesForward)
{
goesForward=true;
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid
loading down the batteries too quickly
{
motor1.setSpeed(speedSet);
motor2.setSpeed(speedSet);
motor3.setSpeed(speedSet);
motor4.setSpeed(speedSet);
delay(5);
}
}
}
void moveBackward() {
goesForward=false;
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid
loading down the batteries too quickly
{
motor1.setSpeed(speedSet);
motor2.setSpeed(speedSet);
motor3.setSpeed(speedSet);
motor4.setSpeed(speedSet);
delay(5);
}
}
void turnRight() {
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
delay(500);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
void turnLeft() {
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
delay(500);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
www.bookbenefits.com