Arduino Programming
Arduino Documentation Blog Entry
There are 4 tasks that will be explained in this page:
Input devices:
Interface a potentiometer analog input to maker UNO board and measure/show its signal in serial monitor Arduino IDE.
Interface a LDR to maker UNO board and measure/show its signal in serial monitor Arduino IDE
Output devices:
Interface 3 LEDs (Red, Yellow, Green) to maker UNO board and program it to perform something (fade or flash etc)
Include the pushbutton on the MakerUno board to start/stop part 2.a. above
For each of the tasks, I will describe:
The program/code that I have used and explanation of the code. The code is in writable format (not an image).
The sources/references that I used to write the code/program.
The problems I encountered and how I fixed them.
The evidence that the code/program worked in the form of video of the executed program/code.
Finally, I will describe:
My Learning reflection on the overall Arduino programming activities.
Input devices: Interface a potentiometer analog input to maker UNO board and measure/show its signal in serial monitor Arduino IDE.
Below are the code/program I have used and the explanation of the code.
Code/program in writeable
format | Explanation of the code |
int sensorPin = A0; int ledPin = 13; int sensorValue = 0; void setup() {
Serial.begin(9600); pinMode(ledPin,OUTPUT); } void loop() {
sensorValue = analogRead(A0);
Serial.println(sensorValue); sensorValue = analogRead(sensorPin);
digitalWrite(ledPin, HIGH); delay(sensorValue);
digitalWrite(ledPin, LOW); delay(sensorValue); } | int sensorPin = A0; means the
potentiometer is connected to analog pin 0 int ledPin = 13; indicates that the LED is connected to digital pin 13 int sensorValue = 0; declares another integer variable to store the value of the potentiometer Serial.begin(9600);
means to start serial communication between Arduino and micro controller
through usb cable with a maximum communicate rate of 9600 bits per seconds pinMode(ledPin,OUTPUT); set pin 13
as the output sensorValue = analogRead(A0); is to read the voltage levels Serial.println(sensorVal);
prints data to the serial port sensorValue =
analogRead(sensorPin);Reads the value from analog pin, A0 Digitalwrite()
is used to set the output of pin 13 to HIGH, turning ON the LED Delay() is used
to create a delay of 0 milliseconds Digitalwrite()
is used to set the output of pin 13 to LOW, turning OFF the LED Delay() is used
to create a delay of 0 milliseconds |
Below are the hyperlink to the sources/references that I used to write the code/program.
Below are the problems I have encountered and how I fixed them.
Below is the short video as the evidence that the code/program work.
Input devices: Interface a LDR to maker UNO board and measure/show its signal in serial monitor Arduino IDE:
Below are the code/program I have used and the explanation of the code.
|
Code/program in writeable
format |
Explanation of the code |
|
int LDR = A0; int ledPin =
13; int LDRvalue
= 0; void setup() { Serial.begin(9600); pinMode(ledPin,OUTPUT); } void loop() { Serial.println(LDRvalue); LDRvalue =
analogRead(LDR); if(LDRvalue
> 600) digitalWrite(ledPin, HIGH); else digitalWrite(ledPin, LOW); } |
int LDR = A0;
is to define LDR pin as analog pin, A0 int ledPin =
13; is to define pin 13 Serial.begin(9600);
means to start serial communication between Arduino and micro controller
through usb cable with a maximum communicate rate of 9600 bits per seconds pinMode(ledPin,OUTPUT);
is to set pin 13 as the output Serial.println(sensorVal); prints data to the serial port LDRvalue =
analogRead(LDR); reads analog values from the LDR If the
LDRvalue > 600, digitalWrite() is then used to set pin13 to HIGH, turning
LED on If the LDR
< 600, digitalWrite() is then used to set pin13 to LOW, turning LED OFF |
Below are the hyperlink to the sources/references that I used to write the code/program.
Below are the problems I have encountered and how I fixed them.
Below is the short video as the evidence that the code/program work.
Output devices: Interface 3 LEDs (Red, Yellow, Green) to maker UNO board and program it to perform something (fade or flash etc)
Below are the code/program I have used and the explanation of the code.
|
Code/program in writeable
format |
Explanation of the code |
|
void setup() { pinMode(11,OUTPUT); pinMode(12,OUTPUT); pinMode(13,OUTPUT); } void loop() { digitalWrite(11,HIGH); delay(1000); digitalWrite(11,LOW); delay(1000); digitalWrite(12,HIGH); delay(1000); digitalWrite(12,LOW); delay(1000); digitalWrite(13,HIGH); delay(1000); digitalWrite(13,LOW); delay(1000); } |
pinMode(11,OUTPUT);
sets pin 11 as an output pinMode(12,OUTPUT);
sets pin 11 as an output pinMode(13,OUTPUT); sets pin 11 as an output digitalWrite()
is used to set pin11 to HIGH, turning LED on delay(1000)
creates a 1 second delay digitalWrite()
is used to set pin11 to LOW, turning LED off delay(1000)
creates a 1 second delay digitalWrite()
is used to set pin12 to HIGH, turning LED on delay(1000)
creates a 1 second delay digitalWrite()
is used to set pin12 to LOW, turning LED off delay(1000)
creates a 1 second delay digitalWrite()
is used to set pin13 to HIGH, turning LED on delay(1000)
creates a 1 second delay digitalWrite()
is used to set pin13 to LOW, turning LED off delay(1000)
creates a 1 second delay |
Below are the hyperlink to the sources/references that I used to write the code/program.
Below are the problems I have encountered and how I fixed them.
Below is the short video as the evidence that the code/program work.
Output devices: Include pushbutton to start/stop the previous task
Below are the code/program I have used and the explanation of the code.
|
Code/program in writeable
format |
Explanation of the code |
|
const int
greenLedVehicle = 5; const int
yellowLedVehicle = 6; const int
redLedVehicle = 7; const int
greenLedPedestrian = 3; const int
redLedPedestrian = 4; const int
pushButton = 2; void setup() { pinMode(greenLedVehicle,
OUTPUT); pinMode(yellowLedVehicle,
OUTPUT); pinMode(redLedVehicle,
OUTPUT); pinMode(greenLedPedestrian,
OUTPUT); pinMode(redLedPedestrian,
OUTPUT); pinMode(pushButton,
INPUT_PULLUP); digitalWrite(greenLedVehicle,
HIGH); digitalWrite(redLedPedestrian,
HIGH); } void loop() { if(digitalRead(pushButton)
== LOW) { digitalWrite(greenLedVehicle,
LOW); digitalWrite(yellowLedVehicle,
HIGH); delay(2000); digitalWrite(yellowLedVehicle,
LOW); digitalWrite(redLedVehicle,
HIGH); delay(1000); digitalWrite(redLedPedestrian,
LOW); digitalWrite(greenLedPedestrian,
HIGH); delay(5000); digitalWrite(greenLedPedestrian,
LOW); digitalWrite(redLedPedestrian,
HIGH); delay(1000); digitalWrite(redLedVehicle,
LOW); digitalWrite(greenLedVehicle,
HIGH); } } |
Pin 5 is
constant Pin 6 is
constant Pin7 is
constant Pin 3 is
constant Pin 4 is
constant Pin 2 is
constant pinMode(greenLedVehicle,
OUTPUT); sets pin 5 as an output pinMode(yellowLedVehicle,
OUTPUT); sets pin 6 as an output pinMode(redLedVehicle,
OUTPUT); sets pin 7 as an output pinMode(greenLedPedestrian,
OUTPUT); sets pin 3 as an output pinMode(redLedPedestrian,
OUTPUT); sets pin 4 as an output pinMode(2,
INPUT_PULLUP) assigned pin 2 as an input port, this enable pin 2 to read a
digital input base on the status of the switch connected to pin 2 with a
pullup resistor. digitalWrite()
is used to set pin 5 to HIGH to turn on green LED digitalWrite()
is used to set pin 4 to HIGH to turn on red LED if push button
is pressed, input value is low, hence sensorvalue is low. digitalWrite() is
used to turn green LED
from pin5 on, (5V) yellow LED
frompin6 off, (0V) Create a 2
seconds delay Yellow LED
from pin6 off, (0V) Red LED from
pin 7 on, (5V) Create a 1
second delay Red LED from
pin4 off, (0V) Green LED
from pin3 to on, (5V) Create a 5
seconds delay Green LED
from pin3 to off, (0V) Red LED from
pin 4 to on, (5V) Create a
delay of 1 second Red LED from
pin 7 to off, (0V) Green LED
from pin 5 to on, (5V) |
Below are the hyperlink to the sources/references that I used to write the code/program.
Below are the problems I have encountered and how I fixed them.
Below is the short video as the evidence that the code/program work.
Comments
Post a Comment