All code for Mr.
Daniel Wilson question
Question 1 : Bluetooth module to control for home appliance
Code :
void setup() {
pinMode(LED, OUTPUT);
pinMode(fan, OUTPUT);
[Link](9600);
}
void loop() {
while ([Link]() > 0)
{
data = Serial. read();
[Link](data);
if (data == 'A'){
digitalWrite(fan, HIGH);
}
else if (data == 'B'){
digitalWrite(fan, LOW);
}
else if (data == 'C'){
digitalWrite(LED, HIGH);
}
else if (data == 'D'){
digitalWrite(LED, LOW);
}
Hapo izo letter A,B,C,D huwa zinakua value flan kwenye app ya Bluetooth ambayo utatumia
QUASTION 2 : Security system based on motion detection
Code:
// security system based on motion detection
#define sensorPin 10
#define LED 9
#define BUZZER 12
void setup() {
pinMode(sensorPin, INPUT);
pinMode(BUZZER, OUTPUT);
pinMode(LED, OUTPUT);
[Link](9600);
}
void loop() {
int sensorRead = digitalRead(sensorPin);
if (sensorRead == HIGH) {
[Link]("SOMETHIG MOVING");
digitalWrite(LED, HIGH);
digitalWrite(BUZZER, HIGH);
} else {
digitalWrite(LED, LOW);
digitalWrite(BUZZER, LOW);
}
delay(1000);
}
QUASTION 3: Advanced blind stick
Code :
// blind stick
int trig_Pin = 12;
int echo_Pin = 5;
int Buzzer = 9;
int LED = 8;
void setup() {
pinMode(trig_Pin, OUTPUT);
pinMode(echo_Pin, INPUT);
pinMode(Buzzer, OUTPUT);
pinMode(LED, OUTPUT);
[Link](9600);
}
void loop() {
// clear high pulse in trigger pin by send it low.
digitalWrite(trig_Pin, LOW);
delayMicroseconds(2);
// set trigger pin into high state
digitalWrite(trig_Pin, HIGH);
delayMicroseconds(10);
digitalWrite(trig_Pin, LOW);
// PULSEIN used to measure length of pulse pin
int duration = pulseIn(echo_Pin, HIGH); // to get time taken by pulse to return
/* even time taken by pulse to returned was determined by duration to calculate distance in
ultrasonic is determined by time taken by pulse received by receiver (R) = (duration).*/
int DistanceMeasured = duration * 0.034/2; // 0.034 is a sound travel in air per microsecond
if (DistanceMeasured <= 100) {
[Link]("Near to obstacle");
digitalWrite(Buzzer, HIGH);
digitalWrite(LED, HIGH);
delay(500);
digitalWrite(LED, LOW);
delay(200);
} else {
digitalWrite(LED, LOW);
digitalWrite(Buzzer, LOW);
}
[Link]("Distance: "); // print distance
[Link](DistanceMeasured);
[Link]("cm");
delay(1000);
}
Question 4 : RFID to control door lock
Code:
#include <SPI.h> // Includes the SPI library to communicate with the RFID module
#include <MFRC522.h> // Includes the library for the MFRC522 RFID reader
#include <Servo.h> // Includes the library to control the servo motor
#define SS_PIN 10 // Defines the Slave Select (SS) pin for the RFID reader
#define RST_PIN 9 // Defines the Reset (RST) pin for the RFID reader
#define RELAY_PIN 7 // Defines the pin for the relay module
MFRC522 rfid(SS_PIN, RST_PIN); // Creates an instance of the MFRC522 class
Servo myServo; // Creates an instance of the Servo class
// The UID for the single authorized RFID card
byte authorizedUID[4] = {0xDE, 0xAD, 0xBE, 0xEF}; // Replace with your card's UID
void setup() {
[Link](9600); // Starts serial communication at 9600 baud rate
[Link](); // Initializes SPI communication
rfid.PCD_Init(); // Initializes the RFID reader
[Link](8); // Attaches the servo motor to pin 8
[Link](0); // Sets the servo to the lock position
pinMode(RELAY_PIN, OUTPUT); // Sets the relay pin as an output
digitalWrite(RELAY_PIN, LOW); // Deactivates the relay
[Link]("RFID Access Control System");
}
void loop() {
if ( ! rfid.PICC_IsNewCardPresent() || ! rfid.PICC_ReadCardSerial() ) {
// Checks if a new RFID card is present and reads its serial number
return; // Exits the loop if no card is present
}
[Link]("UID tag: ");
for (byte i = 0; i < 4; i++) {
[Link]([Link][i], HEX); // Prints the UID of the card
[Link](" ");
}
[Link]();
if ( checkUID([Link]) ) { // Checks if the read UID matches the authorized UID
[Link]("Access Granted");
[Link](90); // Unlocks the door by setting the servo to the unlock position
digitalWrite(RELAY_PIN, HIGH); // Activates the relay
delay(3000); // Keeps the door unlocked for 3 seconds
[Link](0); // Locks the door by setting the servo back to the lock position
digitalWrite(RELAY_PIN, LOW); // Deactivates the relay
} else {
[Link]("Access Denied");
}
rfid.PICC_HaltA(); // Halts the current RFID card communication
}
bool checkUID(byte *uid) {
for (byte i = 0; i < 4; i++) {
if (uid[i] != authorizedUID[i]) {
return false; // Returns false if the UID doesn't match
}
}
return true; // Returns true if the UID matches
}
Question 5 : Car engine control based on alcohol detection
Code :
#define engine 7
#define buzzer 9
#define MQ3 A2
float Alcohol = 0;
void setup() {
pinMode(engine, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(MQ3, INPUT);
}
void loop() {
Alcohol = analogRead(MQ3) * 5 / 1023; // HAPA MAANA YAKE UNA BADILISHA
ZILE VALUE 0-1023 KUWA VALUE KAMILI YANI 0-5
// condition ITAWEKWA KWAKUTUMIA IZO VALUE KAMILI 0-5
if (Alcohol >= 2) {
digitalWrite(buzzer, HIGH);
digitalWrite(engine, HIGH);
delay(1000);
} else {
digitalWrite(buzzer, LOW);
digitalWrite(engine, LOW);
delay(1000);
}
}