Arduino programming
Hello!! welcome back to my blog, today i will be covering my learning experience on arduino programming. specifically, input and output devices, with 2 parts to each
input device:
a) interface a potentiometer analog input to maker UNO board and measure/show its signal in serial monitor arduino IDE.
b) interface a LDR to maker UNO board and measure/show its signal in serial monitor arduino IDE
output device:
a) interface 3 LEDs (Red, Yellow, Green) to maker UNO board and program it to perform something
b) include the pushbutton on the MakerUno board to start/stop part a
input devices: interface a potentiometer analog input to maker UNO board and measure/show its signal in serial monitor arduino IDE.
1. The code that i have used and explanation of the code
|
Code/program in writeable format |
code |
Explanation of the code |
|
const int analogInPin = A0; const int analogOutPin = 9; int sensorValue = 0; int outputValue = 0; void setup() {
Serial.begin(9600); } void loop() { sensorValue = analogRead(analogInPin); outputValue = map(sensorValue, 0, 1023, 0, 255); analogWrite(analogOutPin, outputValue);
Serial.print("sensor = ");
Serial.print(sensorValue);
Serial.print("\t output = "); Serial.println(outputValue);
delay(2); } |
const int |
Const int are
constants used to give names to pins used.
|
|
const int analogInPin = A0
|
In this case, the
analog input pin that the potentiometer is attached to is A0. |
|
|
const int analogOutPin = 9
|
The analog output
pin that the LED is attached to is PIN9 |
|
|
int sensorValue =
0 |
reads the sensorValue,
which refers to the reading on the potentiometer |
|
|
int outputValue =
0 |
Reads the output values
of the potentiometer |
|
|
void setup() {
|
Sets up a
connection between the laptop and the program |
|
|
Serial.begin(9600); |
Initialize serial
communications at 9600 bps |
|
|
void loop() {
|
Tells the program
to run this line of code infinitely |
|
|
sensorValue =
analogRead(analogInPin); |
Read the analog in
value |
|
|
outputValue =
map(sensorValue, 0, 1023, 0, 255) |
Maps the value from
analog to a range of 0 to 1023 and 0 to 255 |
|
|
analogWrite(analogOutPin,
outputValue) |
Change the analog
out value |
|
|
Serial.print("sensor = "); Serial.print(sensorValue); Serial.print("\t output = "); Serial.println(outputValue); |
Prints the results
to the serial monitor |
|
|
delay(2) |
Wait for 2
milliseconds before repeating the whole loop code |
2.hyperlink/sources that i used to write the program:
3. Problems i encountered and how i fixed them
4. short video as the evidence that the code/program works
input devices: interface a LDR to maker UNO board and measure/show its signal in serial monitor arduino IDE
1. The code that i have used and explanation of the code
Code/program in writeable format | code | Explanation of the code |
const int analogInPin = A0; const int analogOutPin = 9; int sensorValue = 0; int outputValue = 0; void setup() { } void loop() { sensorValue = analogRead(analogInPin); outputValue = map(sensorValue, 0, 1023, 0, 255); analogWrite(analogOutPin, outputValue); Serial.print("sensor = "); Serial.print(sensorValue); Serial.print("\t output = "); Serial.println(outputValue); delay(2); } | const int | Const int are constants used to give names to pins used.
|
const int analogInPin = A0
| In this case, the analog input pin that the LDR is attached to is A0. | |
const int analogOutPin = 9
| The analog output pin that the LED is attached to is PIN9 | |
int sensorValue = 0 | reads the sensorValue, which refers to the reading on the potentiometer | |
int outputValue = 0 | Reads the output values of the LDR | |
void setup() {
| Sets up a connection between the laptop and the program | |
void loop() {
| Tells the program to run this line of code infinitely | |
sensorValue = analogRead(analogInPin); | Read the analog in value | |
outputValue = map(sensorValue, 0, 1023, 0, 255) | Maps the value from analog to a range of 0 to 1023 and 0 to 255 | |
analogWrite(analogOutPin, outputValue) | Change the analog out value | |
Serial.print("sensor = "); Serial.print(sensorValue); Serial.print("\t output = "); Serial.println(outputValue); | Prints the results to the serial monitor | |
delay(2) | Wait for 2 milliseconds before repeating the whole loop code |
2.hyperlink/sources that i used to write the program:
3. Problems i encountered and how i fixed them
4. short video as the evidence that the code/program works
Output devices: interface 3 LEDs (Red, Yellow, Green) to maker UNO board and program it to perform something (fade)
1. The code that i have used and explanation of the code
|
Code/program in writeable format |
code |
Explanation of the code |
|
int brightness = 0;
void setup() {
pinMode(9, OUTPUT); }
void loop() { for
(brightness = 0; brightness <= 255; brightness += 1) {
analogWrite(9, brightness);
delay(5); } for
(brightness = 255; brightness >= 0; brightness -= 1) {
analogWrite(9, brightness);
delay(5); } } |
int brightness = 0;
|
Creates a variable
called brightness and sets it to 0 |
|
pinMode(9, OUTPUT); |
Sets PIN9 as output
because we want to send signals to the LED |
|
|
for (brightness = 0; brightness <= 255; brightness
+= 1) |
Program will read the
brightness of the LED and if it is equal to 0 and lesser than or equals to 255.
It will slowly increase the brightness by 1. Causing the LED to slowly light
up |
|
|
{ analogWrite(9, brightness); |
Writes the brightness
into the selected pin, which is 9 in this case |
|
|
for (brightness = 255; brightness >= 0;
brightness -= 1) |
Program will read the
brightness of the LED and if it is equal to 255 and more than or equals to 0.
It will slowly decrease the brightness by 1. |
|
|
delay(5); |
Wait for 2 milliseconds
before repeating the whole loop code |
2.hyperlink/sources that i used to write the program:
3. Problems i encountered and how i fixed them
4. short video as the evidence that the code/program works
Output devices: include the pushbutton on the MakerUno board to start/stop part a
1. The code that i have used and explanation of the code
|
Code/program in writeable format |
code |
Explanation of the code |
|
int brightness = 0;
void setup() { Serial.begin(9600); pinMode(2, INPUT_PULLUP); pinMode(13, OUTPUT);
}
void loop() { int
sensorVal = digitalRead(2); Serial.println(sensorVal);
if
(sensorVal == HIGH) {}
else
{ for
(int i=0; i < 5; i++)
{for (brightness = 0; brightness <= 255; brightness += 1) {
analogWrite(9, brightness);
delay(10); } for
(brightness = 255; brightness >= 0; brightness -= 1) {
analogWrite(9, brightness); delay(10); } } } } } |
int brightness = 0; |
Creates a variable
called brightness and sets it to 0 |
|
Serial.begin(9600);
|
Initialize serial
communications at 9600 bps |
|
|
pinMode(2,
INPUT_PULLUP); |
Selects PIN2 as the
input. Program will read the signal sent from PIN2 |
|
|
pinMode(13, OUTPUT);
|
Selects PIN13 as the
output. Program will take signal from PIN13 |
|
|
int sensorVal =
digitalRead(2); |
Creates an integer for sensorVal and sets it to digitalRead(2) |
|
|
Serial.println(sensorVal); |
Displays the value of
sensorVal |
|
|
if (sensorVal == HIGH) {}
else
{ for
(int i=0; i < 5; i++) |
If the sensorVal is read
to be HIGH (button is not pressed), do nothing
If the sensorVal reads
other readings(sensorVal =LOW) or button is pressed, it will run the two brightness code 5
times |
|
|
{for (brightness = 0; brightness <= 255;
brightness += 1) {
analogWrite(9, brightness);
delay(10); } for
(brightness = 255; brightness >= 0; brightness -= 1) {
analogWrite(9, brightness); delay(10); |
Program will read the
brightness of the LED and if it is equal to 0 and lesser than or equals to 255.
It will slowly increase the brightness by 1.
Program will read the
brightness of the LED and if it is equal to 255 and more than or equals to 0.
It will slowly decrease the brightness by 1.
There will then be a
delay of 10 milliseconds after running the code above it
|



Comments
Post a Comment