Sunday, November 24, 2024

Rotate_Servo_using_4*4_Keypad

#include <Keypad.h>  // Include the keypad library

#include <Servo.h>   // Include the servo library


// Define the keypad configuration

const byte ROWS = 4;  // Four rows

const byte COLS = 4;  // Four columns


// Define the keys on the keypad

char keys[ROWS][COLS] = {

  {'1', '2', '3', 'A'},

  {'4', '5', '6', 'B'},

  {'7', '8', '9', 'C'},

  {'*', '0', '#', 'D'}

};


// Connect the keypad rows and columns to Arduino pins

byte rowPins[ROWS] = {2, 3, 4, 5}; // Rows connected to pins 2-5

byte colPins[COLS] = {6, 7, 8, 9}; // Columns connected to pins 6-9


// Create the Keypad object

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);


// Define the password

const char password[] = "1234"; // Set your desired password

char inputPassword[5];          // Store the entered password (4 digits + null terminator)

byte passwordIndex = 0;         // Track the current input position


// Create a Servo object

Servo myServo;


// Define servo parameters

const int servoPin = 10;        // Servo signal pin

const int openAngle = 90;       // Angle to rotate when the password is correct

const int closeAngle = 0;       // Default (closed) position


void setup() {

  // Attach the servo to the specified pin

  myServo.attach(servoPin);


  // Initialize the servo to the closed position

  myServo.write(closeAngle);


  // Start Serial Monitor for debugging

  Serial.begin(9600);

}


void loop() {

  // Get the key pressed on the keypad

  char key = keypad.getKey();


  if (key) { // If a key is pressed

    Serial.print("Key pressed: ");

    Serial.println(key);


    if (key == '#') { // '#' indicates the user is done entering the password

      inputPassword[passwordIndex] = '\0'; // Null-terminate the input

      if (strcmp(inputPassword, password) == 0) { // Compare input to stored password

        Serial.println("Correct Password!");

        rotateServo(); // Rotate servo to open position

      } else {

        Serial.println("Incorrect Password!");

        resetServo();  // Reset servo to closed position

      }

      passwordIndex = 0; // Reset input index for the next attempt

    } else if (passwordIndex < 4 && key != '*' && key != '#') {

      // Only allow up to 4 characters, ignore '*' and '#' as input

      inputPassword[passwordIndex] = key; // Store the key

      passwordIndex++;

    }

  }

}


// Function to rotate the servo to the open position

void rotateServo() {

  myServo.write(openAngle); // Rotate servo to the specified angle

  delay(2000);              // Hold the position for 2 seconds

  resetServo();             // Return the servo to the closed position

}


// Function to reset the servo to the closed position

void resetServo() {

  myServo.write(closeAngle); // Reset servo to the closed position

}


Keypad_Based_Password_Protected_System

 #include <Keypad.h> // Include Keypad library


// Define the keypad configuration

const byte ROWS = 4; // Four rows

const byte COLS = 4; // Four columns


// Define the keys on the keypad

char keys[ROWS][COLS] = {

  {'1', '2', '3', 'A'},

  {'4', '5', '6', 'B'},

  {'7', '8', '9', 'C'},

  {'*', '0', '#', 'D'}

};


// Connect the keypad rows and columns to Arduino pins

byte rowPins[ROWS] = {2, 3, 4, 5}; // Connect rows to pins 2-5

byte colPins[COLS] = {6, 7, 8, 9}; // Connect columns to pins 6-9


// Create the Keypad object

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);


// Define the password

const char password[] = "1234"; // Change this to your desired password

char inputPassword[5]; // Store the entered password (4 digits + null terminator)

byte passwordIndex = 0; // Track the current input position


// Define LED pins

const int greenLED = 10; // Green LED pin

const int redLED = 11;   // Red LED pin


void setup() {

  // Initialize LED pins

  pinMode(greenLED, OUTPUT);

  pinMode(redLED, OUTPUT);


  // Turn off both LEDs initially

  digitalWrite(greenLED, LOW);

  digitalWrite(redLED, LOW);


  // Start Serial Monitor for debugging

  Serial.begin(9600);

}


void loop() {

  // Get the key pressed on the keypad

  char key = keypad.getKey();


  if (key) { // If a key is pressed

    Serial.print("Key pressed: ");

    Serial.println(key);


    if (key == '#') { // '#' indicates the user is done entering the password

      inputPassword[passwordIndex] = '\0'; // Null-terminate the input

      if (strcmp(inputPassword, password) == 0) { // Compare input to stored password

        Serial.println("Correct Password!");

        blinkLED(greenLED); // Blink green LED

      } else {

        Serial.println("Incorrect Password!");

        blinkLED(redLED); // Blink red LED

      }

      passwordIndex = 0; // Reset input index for the next attempt

    } else if (passwordIndex < 4 && key != '*' && key != '#') {

      // Only allow up to 4 characters, ignore '*' and '#' as input

      inputPassword[passwordIndex] = key; // Store the key

      passwordIndex++;

    }

  }

}


// Function to blink an LED

void blinkLED(int ledPin) {

  for (int i = 0; i < 5; i++) { // Blink 5 times

    digitalWrite(ledPin, HIGH);

    delay(200);

    digitalWrite(ledPin, LOW);

    delay(200);

  }

}


Thursday, November 21, 2024

Bluetooth_RC_Car_(Sunil_Singh)

 



 #include<SoftwareSerial.h>

#define IN1 2

#define IN2 3

#define IN3 4

#define IN4 5

//#define EN1 6

//#define EN2 5

SoftwareSerial bt(10, 11); // TX, RX

char data;

void setup() {

  Serial.begin(9600);

  pinMode(IN1, OUTPUT);

  pinMode(IN2, OUTPUT);

  pinMode(IN3, OUTPUT);

  pinMode(IN4, OUTPUT);

  //pinMode(EN1, OUTPUT);

  //pinMode(EN2, OUTPUT);

  digitalWrite(IN1, LOW);

  digitalWrite(IN2, LOW);

  digitalWrite(IN3, LOW);

  digitalWrite(IN4, LOW);

  //analogWrite(EN1,63);

  //analogWrite(EN2,63);

  bt.begin(9600);

}

 

void loop()

{

   while (bt.available())

   {

      { data = bt.read();

        Serial.println(data);}

         

      switch (data)

       {

          case 'F':                              

            //Serial.println("Forward");

            forward();

            break;

   

          case 'B':                

           //Serial.println("Reverse");

            reverse();

            break;

   

          case 'L':        

           //Serial.println("Left");

            left();

            break;        

          case 'R':                    

            //Serial.println("Right");

            right();

            break;

           

          case 'S':                                          

            //Serial.println("Stop");

            stoprobot();

            break;    

          }

      }                                                            

       if (bt.available() < 0)                            

        {

         //Serial.println("No Bluetooth Data ");        

        }

  delay(100);

}

void forward() {

  digitalWrite(IN1, HIGH);

  digitalWrite(IN2, LOW);

  digitalWrite(IN3, LOW);

  digitalWrite(IN4, HIGH);

  delay(20);

}

void reverse() {

  digitalWrite(IN1, LOW);

  digitalWrite(IN2, HIGH);

  digitalWrite(IN3, HIGH);

  digitalWrite(IN4, LOW);

  delay(20);

}

void left() {

  digitalWrite(IN1, HIGH);

  digitalWrite(IN2, LOW);

  digitalWrite(IN3, HIGH);

  digitalWrite(IN4, LOW);

  delay(20);

}

void right() {

  digitalWrite(IN1, LOW);

  digitalWrite(IN2, HIGH);

  digitalWrite(IN3, LOW);

  digitalWrite(IN4, HIGH);

  delay(20);

}

void stoprobot() {

  digitalWrite(IN1, LOW);

  digitalWrite(IN2, LOW);

  digitalWrite(IN3, LOW);

  digitalWrite(IN4, LOW);

  delay(20);

}

Wednesday, November 20, 2024

RFID_to_Control_Servo_Motor

 




CODE

#include <SPI.h>

#include <MFRC522.h>

#include <Servo.h>


MFRC522 rfid(10, 9); // D10: pin of tag reader SDA. D9: pin of tag reader RST

unsigned char status;

unsigned char str[18]; // Maximum length of UID supported by MFRC522 library is 18 bytes


String accessGranted[2] = {"cee4e13f", "a1c1e21b"}; // RFID serial numbers to grant access to

int accessGrantedSize = 2;                                // The number of serial numbers


Servo lockServo; // Servo for locking mechanism

int lockPos = 0; // Locked position limit

int unlockPos = 90; // Unlocked position limit

boolean locked = true;


int redLEDPin = 5;

int greenLEDPin = 6;


void setup()

{

  Serial.begin(9600); // Serial monitor is only required to get tag ID numbers and for troubleshooting

  SPI.begin();        // Start SPI communication with reader

  rfid.PCD_Init();    // initialization

  pinMode(redLEDPin, OUTPUT);  // LED startup sequence

  pinMode(greenLEDPin, OUTPUT);

  digitalWrite(redLEDPin, HIGH);

  delay(200);

  digitalWrite(greenLEDPin, HIGH);

  delay(200);

  digitalWrite(redLEDPin, LOW);

  delay(200);

  digitalWrite(greenLEDPin, LOW);

  lockServo.attach(3);

  lockServo.write(lockPos); // Move servo into locked position

  Serial.println("Place card/tag near reader...");

}


void loop()

{

  if (rfid.PICC_IsNewCardPresent()) // Wait for a tag to be placed near the reader

  {

    if (rfid.PICC_ReadCardSerial()) // Anti-collision detection, read tag serial number

    {

      String temp = ""; // Temporary variable to store the read RFID number

      Serial.print("The card's ID number is : ");

      for (int i = 0; i < 4; i++) // Record and display the tag serial number

      {

        temp += String(rfid.uid.uidByte[i], HEX);

      }

      Serial.println(temp);

      checkAccess(temp); // Check if the identified tag is an allowed to open tag

    }

  }

}


void checkAccess(String temp) // Function to check if an identified tag is registered to allow access

{

  boolean granted = false;

  for (int i = 0; i < accessGrantedSize; i++) // Runs through all tag ID numbers registered in the array

  {

    if (accessGranted[i] == temp) // If a tag is found then open/close the lock

    {

      Serial.println("Access Granted");

      granted = true;

      if (locked) // If the lock is closed then open it

      {

        lockServo.write(unlockPos);

        locked = false;

      }

      else // If the lock is open then close it

      {

        lockServo.write(lockPos);

        locked = true;

      }

      digitalWrite(greenLEDPin, HIGH); // Green LED sequence

      delay(200);

      digitalWrite(greenLEDPin, LOW);

      delay(200);

      digitalWrite(greenLEDPin, HIGH);

      delay(200);

      digitalWrite(greenLEDPin, LOW);

      delay(200);

    }

  }

  if (!granted) // If the tag is not found

  {

    Serial.println("Access Denied");

    digitalWrite(redLEDPin, HIGH); // Red LED sequence

    delay(200);

    digitalWrite(redLEDPin, LOW);

    delay(200);

    digitalWrite(redLEDPin, HIGH);

    delay(200);

    digitalWrite(redLEDPin, LOW);

    delay(200);

  }

}

MUD Three Mode operation Manual Automatic GPS

 Code for three mode operation: /*   3-Mode Headlight Controller   - Manual mode (driver uses a toggle to pick high/low)   - Auto mode (LDR...