Arduino Exodus Beginner Arduino Projects ESP8266 Arduino IDE Guide Basic Arduino Coding (Nithukanth Sooriyan)
Arduino Exodus Beginner Arduino Projects ESP8266 Arduino IDE Guide Basic Arduino Coding (Nithukanth Sooriyan)
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
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?
Arduino is about connecting things. We'll do that in a few minutes
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
Beginner Arduino
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);
}
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
}
}
Step 7: Fade LEDs With
AnalogWrite
Sometimes we will want to control the LED light, in which case we
can use a command called analogWrite (). AnalogWrite works by
turning on and off the LED very quickly, very quickly so that our eyes
do not see blinking. If the LED spends its break time and half of its
time, then it will appear as the light part. This method is called pulse
wide modulation (PWM), which is often used electronically because
it allows us to control the part in an "analog" way using a digital pin.
Not all digital pins in Arduino can make PWM, if you look at your
Arduino, you will see that some pins have "~" next to them (pins 3, 5,
6, 9, 10, 11), these are enabled pins for PWM.
Insert one of your LEDs into the PWM-enabled pin, using pin 9. Try
using a blink diagram from the front, but use analogWrite instead of
digitalWrite to turn on the LED (see diagram below). analogWrite ()
takes up two issues: pin number and light level (between 0 and 255).
//LED Blink (half brightness)
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() {
analogWrite(ledPin, 255);//turn LED on at full brightness (255/255 = 1)
delay(1000);// wait for 1000 milliseconds (one second)
digitalWrite(ledPin, LOW);//turn LED off
delay(1000);//wait one second
analogWrite(ledPin, 191);//turn LED on at 3/4 brightness (191/255 ~= 0.75)
delay(1000);// wait for 1000 milliseconds (one second)
digitalWrite(ledPin, LOW);//turn LED off
delay(1000);//wait one second
analogWrite(ledPin, 127);//turn LED on at half brightness (127/255 ~= 0.5)
delay(1000);// wait for 1000 milliseconds (one second)
digitalWrite(ledPin, LOW);//turn LED off
delay(1000);//wait one second
analogWrite(ledPin, 63);//turn LED on at one quarter brightness (63/255 ~= 0.25)
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.
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
}
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.
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.
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.
Parts List:
(1x) Arduino Uno
(1x) usb cable
(1x) breadboard (this one comes with jumper wires)
(1x) jumper wires
(8x) red LEDs
(8x) 220Ohm resistors
(1x) 10kOhm resistor
(1x) tact button
(1x) 595 shift register
(1x) red LED dot matrix Adafruit 454
Step 1: Blink Without Delay
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);
}
}
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.
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).
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);
}
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 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
}
Requirements
Arduino Microcontroller
LED
Resistor (220 ohms or less)
Bread board
4pcs Jumper cables
The latest Arduino IDE
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
Step 3: Light Up
Open your Arduino IDE.
Be sure to add the Arduino 101 board when using it. Just open
tools> boards> board manager> look for Arduino 101 and install!
Select your Board and Port from the tools again.
Download the attached program file and open it.
Click the arrow icon or the upload button.
And see what happens to the magic! Your LED is flashing! It's scary!
About the code
we set our pin (13) as a removal from setup operation ()
turns on LED using digitalWrite function (ledPin, HIGH)
wait 1000 seconds using the delay function (1000)
turns off LED using digitalWrite (ledPin, LOW)
wait 1000 seconds using the delay function (1000)
and the loop continues forever!
As long as the microcontroller is enabled, it will blink forever.
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
*/
// 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);
}
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.
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);
}
If you are creating a visual version of this region, you can try it with a
serial monitor for Arduino software (magnifying the glass button at
the top right of the sketch window), activating the sensor with your
hand, body, notebook, etc.
When using the body board, place something in front of the sensor
and check the distance reading using a serial monitor, and set the
distanceThreshold to that value.
Adjust the "buckets" for a different distance to the correct distance
for your first value, for example if your hand was 60cm away, your
distances could be 60-40, 40-20, and 20-0.
Upload your code again, and try to walk before the sensor. As the
distance decreases, you should see the LEDs open one by one.
Project 04 - Flowing LED
Lights With Arduino Uno R3
In this study, we will conduct a simple yet fun test - we use LEDs to
create flowing LED lights. As the name suggests, these eight
consecutive LEDs glow and dim in sequence, like flowing water.
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()
{
//set pins 2 through 9 as output
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);
}
You will need the following items to make this LED Cube:
/*
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();
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);
//SWIRL - 1 LAYER EACH TIME
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 setup()
{ Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
}
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);
}
Step 2: Circuit
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 LE D
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().
mainColors();
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);
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
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);
// Cyan (turn green and blue on):
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);
delay(1000);
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:
// 1. x = 0; Before starting, make x = 0.
void showSpectrum()
{
int x; // define an integer variable called "x"
{
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
Here's the code, embedded using bender code!
We will use code from SparkFun Inventor's Kit - SIK Guide, Example
sketch 03. This is an excellent diagram to learn how RGB LED
works. The code is self-explanatory, and ideas do a better job than I
explain how it works.
Try downloading the plugin bender plugin and click on the Run on
Arduino button to edit your Arduino board with this drawing. And of
course, plan your Arduino with RGB LED drawing!
Let's learn to feel the movement in the PIR sensory chamber and
Arduino's digital input. We will connect the circuit using a breadboard
and use a simple Arduino code to control a single LED. We'll use
Tinkercad Circuits to mimic a circuit so you can follow without
elements, and show you how to build a visual circuit too.
So far you've learned to read Arduino's digital input button, so we'll
build on those skills in this tutorial. Although the motion sensor may
seem complicated with a dedicated circuit board, it is designed to
send a HIGH or LOW signal in the same way as a pushbutton.
The PIR stands for Passive InfraRed, which describes intermediate
technology - it absorbs light infrared light levels (unlike an infrared
camera that can emit even infrared light to capture its light). The
white dome is a lens that expands the field of vision of the IR
detector. The sensor automatically detects the LOW signal, reads
the amount of infrared light coming in, and triggers the HIGH signal
at a certain time when the light levels change, indicating movement.
It can tell you if there is any movement in the scene, but it can't find
the distance — so you can look at the type of analog input sensor
called the ultrasonic rangefinder.
To optionally select a virtual circuit, connect your Arduino Uno board,
USB cable, solderless breadboard, LED, resistor (any value from
100-1K), PIR motion sensor, and cables of bread board.
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.
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.
#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);
}