Skip to content

Commit ad0c306

Browse files
author
StickyCoolDev
committed
Update project1
1 parent 577f184 commit ad0c306

2 files changed

Lines changed: 165 additions & 25 deletions

File tree

led and button (1)/LedAndButton.html

Lines changed: 86 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
</head>
99
<body>
1010
<h1>Button and LED Basics with Arduino</h1>
11-
<p>This project introduces the fundamental concepts of Arduino.</p>
11+
<p>This project introduces fundamental concepts of Arduino programming by demonstrating how to control an LED with a pushbutton.</p>
12+
1213
<p>You can simulate this project online using Wokwi: <a href="https://wokwi.com/projects/411336385024901121">Button and LED Simulation</a></p>
13-
<p>If you find this helpful, please consider giving it a like ❤️!</p>
1414

15-
<h2>What You Will Learn</h2>
15+
---
16+
17+
<h2>What You'll Learn</h2>
1618
<p>This project will guide you through the following essential Arduino concepts:</p>
1719
<ol>
1820
<li><strong>Declaring Variables:</strong> Learn how to create and use variables in your Arduino sketches.</li>
@@ -21,22 +23,94 @@ <h2>What You Will Learn</h2>
2123
<li><strong>Using the Serial Monitor:</strong> Discover how to use the Serial Monitor for debugging and displaying information.</li>
2224
</ol>
2325

26+
---
27+
2428
<h2>Project Diagram</h2>
25-
<p>Circuit setup:</p>
29+
<p>Here is the circuit setup for this project.</p>
2630
<img src="https://github.com/user-attachments/assets/6bb95536-b864-4568-97d8-5654944a87fb" alt="Button and LED Diagram">
2731

28-
<h2>Code</h2>
29-
<p>Let's dive deeper into the code elements:</p>
32+
---
33+
34+
<h2>Code Breakdown</h2>
35+
<p>Let's dive deeper into the code elements, explaining each part of the program.</p>
36+
37+
<h3>Declaring Variables</h3>
38+
<p>We start by declaring variables to represent our components. This makes our code easier to read and modify.</p>
39+
<pre><code>int button = 3;
40+
int LED1 = 10;</code></pre>
41+
<ul>
42+
<li><code>int button = 3;</code>: We create an integer variable named <code>button</code> and assign it the value <code>3</code>, which corresponds to digital pin 3 on the Arduino.</li>
43+
<li><code>int LED1 = 10;</code>: We create an integer variable named <code>LED1</code> and assign it the value <code>10</code>, which corresponds to digital pin 10.</li>
44+
</ul>
3045

31-
<h3>Serial Monitor</h3>
32-
<p>The Serial Monitor is an tool for debugging your Arduino code and displaying text information from your board to your computer.</p>
33-
<pre><code>Serial.begin(9600);</code></pre>
34-
<p>by using the code given, we can set up serial communication. it's recommended to run it inside of the <code>setup</code> function as it's only need to be run once:</p>
46+
<h3>The <code>setup()</code> Function</h3>
47+
<p>The <code>setup()</code> function runs just once when the Arduino board is powered on or reset. We use it to initialize our pins and serial communication.</p>
3548
<pre><code>void setup()
3649
{
3750
Serial.begin(9600);
51+
pinMode(button, INPUT_PULLUP);
52+
pinMode(LED1, OUTPUT);
53+
}</code></pre>
54+
<ul>
55+
<li><code>Serial.begin(9600);</code>: This line starts serial communication at a <strong>baud rate</strong> of 9600 bits per second, which allows the Arduino to send messages to the computer's Serial Monitor.</li>
56+
<li><code>pinMode(button, INPUT_PULLUP);</code>: The <code>pinMode()</code> function configures a specified pin to behave as an input or an output. Here, we set the <code>button</code> pin to <code>INPUT_PULLUP</code>. This is a convenient way to read a button's state without needing an external <strong>pull-up resistor</strong>.</li>
57+
<li><code>pinMode(LED1, OUTPUT);</code>: We set the <code>LED1</code> pin as an <code>OUTPUT</code> so that we can send a voltage to it to turn the LED on or off.</li>
58+
</ul>
59+
60+
<h3>The <code>loop()</code> Function</h3>
61+
<p>The <code>loop()</code> function runs continuously after the <code>setup()</code> function has finished. This is where the main logic of our program resides.</p>
62+
<pre><code>void loop()
63+
{
64+
int Button1Pressed = digitalRead(button);
65+
66+
if (Button1Pressed == LOW)
67+
{
68+
Serial.println("Button pressed!");
69+
digitalWrite(LED1, HIGH);
70+
}
71+
else
72+
{
73+
digitalWrite(LED1, LOW);
74+
}
75+
}</code></pre>
76+
<ul>
77+
<li><code>int Button1Pressed = digitalRead(button);</code>: The <code>digitalRead()</code> function reads the state of the specified pin. It returns <code>LOW</code> (0 volts) if the pin is connected to ground and <code>HIGH</code> (5 volts) if it is connected to a power source. With <code>INPUT_PULLUP</code>, the pin is normally <code>HIGH</code>, and when the button is pressed, it connects the pin to ground, making the state <code>LOW</code>.</li>
78+
<li><code>if (Button1Pressed == LOW)</code>: We use an <code>if</code> statement to check if the button has been pressed. If the condition is true (the button is pressed), the code inside the curly braces <code>{}</code> will run.</li>
79+
<li><code>Serial.println("Button pressed!");</code>: This line prints the message <strong>"Button pressed!"</strong> to the Serial Monitor. The <code>ln</code> at the end of <code>println</code> adds a new line after the message, which makes the output much cleaner.</li>
80+
<li><code>digitalWrite(LED1, HIGH);</code>: The <code>digitalWrite()</code> function writes a <code>HIGH</code> (5 volts) or <code>LOW</code> (0 volts) value to a digital pin. When the button is pressed, we write <code>HIGH</code> to the <code>LED1</code> pin to turn on the LED.</li>
81+
<li><code>else</code>: If the <code>if</code> condition is false (the button is not pressed), the code inside this <code>else</code> block will run instead.</li>
82+
<li><code>digitalWrite(LED1, LOW);</code>: If the button is not pressed, we write <code>LOW</code> to the <code>LED1</code> pin, which turns the LED off.</li>
83+
</ul>
84+
85+
---
86+
87+
<h3>Full Code</h3>
88+
<p>Here's the complete code, ready to upload to your Arduino board.</p>
89+
<pre><code>int button = 3;
90+
int LED1 = 10;
91+
92+
void setup()
93+
{
94+
Serial.begin(9600);
95+
pinMode(button, INPUT_PULLUP);
96+
pinMode(LED1, OUTPUT);
97+
}
98+
99+
void loop()
100+
{
101+
int Button1Pressed = digitalRead(button);
102+
103+
if (Button1Pressed == LOW)
104+
{
105+
Serial.println("Button pressed!");
106+
digitalWrite(LED1, HIGH);
107+
}
108+
else
109+
{
110+
digitalWrite(LED1, LOW);
111+
}
38112
}</code></pre>
39-
<p>now let's print something . to print something in the serial monitor we need to use the function:</p>
40-
<pre><code>Serial.print("Super Cool");</code></pre>
113+
41114
</body>
42115
</html>
116+

led and button (1)/project1.md

Lines changed: 79 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Button and LED Basics with Arduino
22

3-
This project introduces the fundamental concepts of Arduino,
3+
This project introduces fundamental concepts of Arduino programming by demonstrating how to control an LED with a pushbutton.
44

55
You can simulate this project online using Wokwi: [Button and LED Simulation](https://wokwi.com/projects/411336385024901121)
66

7-
If you find this helpful, please consider giving it a like ❤️!
7+
---
88

9-
## What You Will Learn
9+
## What You'll Learn
1010

1111
This project will guide you through the following essential Arduino concepts:
1212

@@ -15,32 +15,98 @@ This project will guide you through the following essential Arduino concepts:
1515
3. **Writing Digital Output:** Learn how to control a digital output pin connected to an LED.
1616
4. **Using the Serial Monitor:** Discover how to use the Serial Monitor for debugging and displaying information.
1717

18+
---
19+
1820
## Project Diagram
19-
Circuit setup:
21+
Here is the circuit setup for this project.
2022

2123
![Button and LED Diagram](https://github.com/user-attachments/assets/6bb95536-b864-4568-97d8-5654944a87fb)
2224

23-
## Code
25+
---
26+
27+
## Code Breakdown
2428

25-
Let's dive deeper into the code elements:
29+
Let's dive deeper into the code elements, explaining each part of the program.
2630

27-
### Serial Monitor
31+
### Declaring Variables
2832

29-
The Serial Monitor is an tool for debugging your Arduino code and displaying text information from your board to your computer.
33+
We start by declaring variables to represent our components. This makes our code easier to read and modify.
3034

3135
```cpp
32-
Serial.begin(9600);
36+
int button = 3;
37+
int LED1 = 10;
3338
```
34-
by using the code given, we can set up serial communication. it's recommended to run it inside of the ***setup*** function as it's only need to be run once:
39+
40+
* int button = 3;: We create an integer variable named button and assign it the value 3, which corresponds to digital pin 3 on the Arduino.
41+
* int LED1 = 10;: We create an integer variable named LED1 and assign it the value 10, which corresponds to digital pin 10.
42+
43+
### The setup() Function
44+
The setup() function runs just once when the Arduino board is powered on or reset. We use it to initialize our pins and serial communication.
3545

3646
```cpp
3747
void setup()
3848
{
3949
Serial.begin(9600);
50+
pinMode(button, INPUT_PULLUP);
51+
pinMode(LED1, OUTPUT);
52+
}
53+
```
54+
55+
Serial.begin(9600);: This line starts serial communication at a baud rate of 9600 bits per second, which allows the Arduino to send messages to the computer's Serial Monitor.
56+
* pinMode(button, INPUT_PULLUP);: The pinMode() function configures a specified pin to behave as an input or an output. Here, we set the button pin to INPUT_PULLUP. This is a convenient way to read a button's state without needing an external pull-up resistor.
57+
* pinMode(LED1, OUTPUT);: We set the LED1 pin as an OUTPUT so that we can send a voltage to it to turn the LED on or off.
58+
59+
### The loop() Function
60+
The loop() function runs continuously after the setup() function has finished. This is where the main logic of our program resides.
61+
```cpp
62+
void loop()
63+
{
64+
int Button1Pressed = digitalRead(button);
65+
66+
if (Button1Pressed == LOW)
67+
{
68+
Serial.println("Button pressed!");
69+
digitalWrite(LED1, HIGH);
70+
}
71+
else
72+
{
73+
digitalWrite(LED1, LOW);
74+
}
4075
}
4176
```
4277

43-
now let's print something . to print something in the serial monitor we need to use the function:
78+
* int Button1Pressed = digitalRead(button);: The digitalRead() function reads the state of the specified pin. It returns LOW (0 volts) if the pin is connected to ground and HIGH (5 volts) if it is connected to a power source. With INPUT_PULLUP, the pin is normally HIGH, and when the button is pressed, it connects the pin to ground, making the state LOW.
79+
* if (Button1Pressed == LOW): We use an if statement to check if the button has been pressed. If the condition is true (the button is pressed), the code inside the curly braces {} will run.
80+
* Serial.println("Button pressed!");: This line prints the message "Button pressed!" to the Serial Monitor. The ln at the end of println adds a new line after the message, which makes the output much cleaner.
81+
* digitalWrite(LED1, HIGH);: The digitalWrite() function writes a HIGH (5 volts) or LOW (0 volts) value to a digital pin. When the button is pressed, we write HIGH to the LED1 pin to turn on the LED.
82+
* else: If the if condition is false (the button is not pressed), the code inside this else block will run instead.
83+
* digitalWrite(LED1, LOW);: If the button is not pressed, we write LOW to the LED1 pin, which turns the LED off.
84+
85+
### Full Code
86+
Here's the complete code, ready to upload to your Arduino board.
4487
```cpp
45-
Serial.print("Super Cool");
46-
```
88+
int button = 3;
89+
int LED1 = 10;
90+
91+
void setup()
92+
{
93+
Serial.begin(9600);
94+
pinMode(button, INPUT_PULLUP);
95+
pinMode(LED1, OUTPUT);
96+
}
97+
98+
void loop()
99+
{
100+
int Button1Pressed = digitalRead(button);
101+
102+
if (Button1Pressed == LOW)
103+
{
104+
Serial.println("Button pressed!");
105+
digitalWrite(LED1, HIGH);
106+
}
107+
else
108+
{
109+
digitalWrite(LED1, LOW);
110+
}
111+
}
112+
```

0 commit comments

Comments
 (0)