Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Shifa Salsabila

13519106

Arduino Exercise 2

Exercise 2.1
Circuit link: Link Tinkercad Exercise 2.1

const int tmpSensorPin = A0;


const int photoSensorPin = A1;
const int gasSensorPin = A2;

void setup() {
// Initiate serial monitor connection
Serial.begin(9600);
}

void loop() {
readTmp();
readPhoto();
readGas();
delay(1000);
}

void readTmp() {
int reading = analogRead(tmpSensorPin); // Read tmpSensorPin
float voltage = reading * (5000 / 1024.0); // Convert to V
float temperature = voltage / 10; // Convert to C

Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" \xB0");
Serial.println("C");

void readPhoto() {
int reading = analogRead(photoSensorPin); // Read tmpSensorPin
Serial.print("Photo: ");
Serial.println(reading);

void readGas() {
int reading = analogRead(gasSensorPin);
Serial.print("Gas: ");
Serial.println(reading);
}
Output:
Exercise 2.2
Circuit link: Link Tinkercad Exercise 2.2

#include <Servo.h>
#include <LiquidCrystal.h>

// Serco setup
int pos = 0;
Servo servo;

//Speaker setup
int frequency = 262;
int piezoSensorPin = 10;

//LCD setup
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup()
{
servo.attach(1, 500, 2500); //Atach servo
lcd.begin(16, 2); //Set LED rows and columns
}
void loop()
{
writeServo();
writeSpeaker();
writeLCD();
}

void writeServo() {
for (pos=0; pos<=180; pos++){
servo.write(pos);
delay(10);
}

for (pos=180; pos>=0; pos--){


servo.write(pos);
delay(10);
}
}

void writeSpeaker() {
tone(piezoSensorPin, frequency, 1000);
delay(1000);
}

void writeLCD() {
lcd.setCursor(0, 0);
lcd.print("Shifa Salsabiila");
lcd.setCursor(0, 1);
lcd.print("13519106");
for (int i=0 ; i<=16; i ++) {
lcd.scrollDisplayLeft();
delay(400);
}
}
Exercise 2.3
Circuit link: Link Tinkercad Exercise 2.3
#include <Servo.h>
#include <LiquidCrystal.h>
#include <Wire.h>

// Servo setup
int pos = 0;
Servo servo;

//Speaker setup
int frequency = 262;
int piezoSensorPin = 10;

//LCD setup
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

//Initiate sensor threshold values


int gasThres = 100;
int photoThres = 500;
int tmpThres = 50;

void setup()
{
Wire.begin(4); //Initiate i2c bus
Wire.onReceive(receiveEvent);
lcd.begin(16, 2); //Set LED rows and columns
Serial.begin(9600); // Initiate serial monitor connection
}

void loop()
{
delay(100);

void writeServo() {

servo.attach(8, 500, 500); //Attach servo


servo.write(0);
}
void stopServo() {
servo.detach();
}

void writeSpeaker() {
tone(piezoSensorPin, frequency, 500);
}

void writeLCD() {
String row_1_text = "Shifa Salsabiila";
String row_2_text = " 13519106 ";

for (int i=0; i<=16; i++) {


lcd.setCursor(i, 0);
lcd.print(row_1_text[i]);
lcd.setCursor(i, 1);
lcd.print(row_2_text[i]);
}
}

void clearLCD() {
for (int i=0; i<=16; i++) {
lcd.setCursor(i, 0);
lcd.print(" ");
lcd.setCursor(i, 1);
lcd.print(" ");
}
}

void receiveEvent(int howMany) {


//Get photo sensor readings
int photoReading;
byte a, b;
a = Wire.read();
b = Wire.read();
photoReading = a;
photoReading = (photoReading << 8) | b;
Serial.print("Photo readings value : ");
Serial.println(photoReading);

//Get gas sensor readings


int gasReading = Wire.read();
Serial.print("Gas readings value : ");
Serial.println(gasReading);

//Get temperature sensor readings


int tmpReading = Wire.read();
Serial.print("Temp readings value : ");
Serial.print(tmpReading);
Serial.print("\xB0");
Serial.println("C");

Serial.println();

turnOnSensors(gasReading, tmpReading, photoReading);


}

void turnOnSensors(int gasVal, int tmpVal, int photoVal) {


if (gasVal >= gasThres
&&tmpVal >= tmpThres
&& photoVal >= photoThres) {
writeSpeaker();
writeLCD();
writeServo();
} else {
clearLCD();
stopServo();
Serial.println("Threshold not passed");
}
}
Output:

Ketika batas ambang (threshold) untuk setiap sensor telah dicapai, maka ketiga aktuator
(speaker, LCD, dan continuous servo akan menyala). Ketika terdapat setidaknya salah satu
batas ambang yang tidak terpenuhi, maka ketiga aktuator akan mati.

You might also like