0% found this document useful (0 votes)
9 views

Code Arduino

Uploaded by

mohdd0607
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Code Arduino

Uploaded by

mohdd0607
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

LAB 1

//lab1: turn on/off LED at D13

void setup() {
// put your setup code here, to run once:
pinMode(13,OUTPUT); // assign D13 as output pin
}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite(13,HIGH); // give value of '1' to D13
}
LAB 2
//lab2: LED blinking at D13

void setup() {
// put your setup code here, to run once:
pinMode(13,OUTPUT); // assign D13 as output pin
}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite(13,LOW); // give value of '0' to D13
delay(500); // delay 100ms
digitalWrite(13,HIGH); // give value of '1' to D13
delay(500);

}
LAB 3
//lab3: read input signal at D10 and control LED at D13
void setup() {
// put your setup code here, to run once:
pinMode(13,OUTPUT); // assign D13 as output pin
pinMode(12,OUTPUT);
pinMode(10,INPUT); // assign D10 as input pin
}

void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(10)==1)digitalWrite(13,HIGH);
else digitalWrite(13,LOW);
digitalWrite(12,LOW);
delay(100);
digitalWrite(12,HIGH);
delay(100);

}
LAB 4
//lab4: constant and variable

//constant
#define LED 13
#define SW 10

//variable
int sw_status;

void setup() {
// put your setup code here, to run once:
pinMode(LED,OUTPUT); // assign D13 as output pin
pinMode(SW,INPUT); // assign D10 as input pin
}

void loop() {
// put your main code here, to run repeatedly:
sw_status = digitalRead(SW);
if(sw_status ==1)digitalWrite(LED,HIGH);
else digitalWrite(LED,LOW);
}
LAB 5
//lab5: display information at serial monitor

//constant
#define LED 13
#define SW 10

//variable
int sw_status;

void setup() {
// put your setup code here, to run once:
pinMode(LED,OUTPUT); // assign D13 as output pin
pinMode(SW,INPUT); // assign D10 as input pin
Serial.begin(9600); // setup serial monitor baud rate
}

void loop() {
// put your main code here, to run repeatedly:
sw_status = digitalRead(SW);
if(sw_status ==1){digitalWrite(LED,HIGH); Serial.println("lampu menyala");}
else {digitalWrite(LED,LOW);Serial.println("lampu padam");}
}
DHT22
//LIbraries
#include <DHT.h>;

//Consttants
#define DHTPIN 7
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

// variables
float hum; //Stores humidity value
float temp; //Stores temperature value

void setup() {
// put your setup code here, to run once:
pinMode (13,OUTPUT);
Serial.begin(9600);
dht.begin();
}

void loop() {
// put your main code here, to run repeatedly:

hum = dht.readHumidity ();


temp = dht.readTemperature ();
Serial.print ("Humidity: ");
Serial.print (hum);
Serial.print (" &, Temp: ");
Serial.print (temp);
Serial.println (" Celcius");
if(temp>34)digitalWrite (13,HIGH);
else digitalWrite (13,LOW);
delay(100000);

You might also like