Arduino Documentation Blog Entry
Input devices: Interface a potentiometer analog input to maker UNO board and measure/show its signal in serial monitor Arduino IDE.
The code:
int sensorValue = 0;
void setup()
{
pinMode(A0, INPUT);
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop()
{
// read the value from the sensor
sensorValue = analogRead(A0);
Serial.println(sensorValue);
// turn the LED on
digitalWrite(13, HIGH);
// pause the program for <sensorValue> milliseconds
delay(sensorValue); // Wait for sensorValue millisecond(s)
// turn the LED off
digitalWrite(LED_BUILTIN, LOW);
// pause the program for <sensorValue> milliseconds
delay(sensorValue); // Wait for sensorValue millisecond(s)
}
Explanation of code:
- int sensorValue = 0; : Writes input of 0 into a integer variable
- void setup(): Setup will run once the board is powered or reset button is pressed
- pinMode(A0, INPUT); :Sets pin A0 as input
- pinMode(13, OUTPUT); :Sets pin 13 as output
- Serial.begin(9600); :Starts serial connection
- void loop() :Loops code
- sensorValue = analogRead(A0); :Reads the voltage passing through the pin A0
- Serial.println(sensorValue); :Shows value of sensorval
- digitalWrite(13, HIGH); :Voltage in pin 13 is increased to high (LED on)
- delay(sensorValue); :Delay or wait for sensorval (seconds)
- digitalWrite(LED_BUILTIN, LOW); :Voltage in pin 13 is decreased to low (LED low)
Input devices: Interface a LDR to maker UNO board and measure/show its
signal in serial monitor Arduino IDE:
The code:
int light;
void setup() {
Serial.begin(9600);
}
void loop() {
light= analogRead(A0);
if(light<50){
digitalWrite(13, LOW);
}else{
digitalWrite(13, HIGH);
}
Serial.println(light);
delay(0);
}
Explanation of code:
- int light; :Setting an integer as “light”
- void setup() :Setup will run once board is powered or reset button is pressed
- Serial.begin(9600); :Starts serial connection
- void loop() :Loops code
- light= analogRead(A0); :Reads light that is shone on A0
if(light<50){
digitalWrite(13, LOW); :If light reading from A0<50, LED would not be lit
}else{
digitalWrite(13, HIGH); :Or else, LED would be lit (A0>50)
Serial.println(light); :Displays value of sensorval
delay(0); :Zero delay
Output devices: Interface 3 LEDs (Red, Yellow, Green) to maker UNO board
and program it to perform something (fade or flash etc)
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(2000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
digitalWrite(12, HIGH);
delay(2000);
digitalWrite(12, LOW);
delay(1000);
digitalWrite(11, HIGH);
delay(2000);
digitalWrite(11, LOW);
delay(1000);
}
- void setup() :Setup will run once board is powered or reset button is pressed
- pinMode(LED_BUILTIN, OUTPUT); :Sets pin 13 as output
- pinMode(12, OUTPUT); :Sets pin 12 as output
- pinMode(11, OUTPUT); :Sets pin 11 as output
- void loop() :Loops code
- digitalWrite(LED_BUILTIN, HIGH); :Pin 13's voltage high (LED on)
- digitalWrite(LED_BUILTIN, LOW); :Pin 13's voltage low (LED off)
- digitalWrite(12, HIGH); :Pin 12's voltage high (LED on)
- digitalWrite(12, LOW); :Pin 12's voltage low (LED off)
- digitalWrite(11, HIGH); :Pin 11's voltage high (LED on)
- digitalWrite(11, LOW); :Pin 11's voltage low (LED off)
- delay(2000); :Delay for 2000ms (2 seconds)
- delay(1000); :Delay for 1000ms (1 second)
Output devices: Include pushbutton to start/stop the previous task
The code:
void setup() {
Serial.begin(9600);
pinMode(2, INPUT_PULLUP);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
}
void loop() {
int sensorVal = digitalRead(2);
Serial.println(sensorVal);
if (sensorVal == HIGH) {
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
} else {
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
delay(1000);
digitalWrite(13, LOW);
digitalWrite(12, HIGH);
digitalWrite(11, LOW);
delay(1000);
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, HIGH);
delay(1000);
}
}
Explanation of code:
- void setup() :Setup will run once board is powered or reset button is pressed
- Serial.begin(9600); :Starts serial connection
- pinMode(2, INPUT_PULLUP); :Sets pin 2 as input
- digitalWrite(13, LOW); :Sets pin 13 as output
- digitalWrite(12, LOW); :Sets pin 12 as output
- digitalWrite(11, LOW); :Sets pin 11 as output
- void loop() :Loops code
- int sensorVal = digitalRead(2); :Read the sensor value at pin 2
- Serial.println(sensorVal); :Show value of Sensorval
if (sensorVal == HIGH) {
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW); :If sensor value high, pins 13,12 & 11 voltage decrease, LED off
} else { :Or else
digitalWrite(13, HIGH); :Pin 13's voltage high (LED on)
digitalWrite(12, LOW); :Pin 12's voltage low (LED off)
digitalWrite(11, LOW); :Pin 11's voltage low (LED off)
digitalWrite(13, LOW); :Pin 13's voltage low (LED off)
digitalWrite(12, HIGH); :Pin 12's voltage high (LED off)
digitalWrite(11, LOW); :Pin 11's voltage low (LED off)
digitalWrite(13, LOW); :Pin 13's voltage low (LED off)
digitalWrite(12, LOW); :Pin 12's voltage low (LED off)
digitalWrite(11, HIGH); :Pin 11's voltage high (LED on)
delay(1000); :Delay for 1000ms (1 second)
No comments:
Post a Comment