Sunday, July 28, 2024

Servo test

 #include <Servo.h>


Servo myServo;  // Create a servo object to control a servo


int pos = 0;    // Variable to store the servo position


void setup() {

  myServo.attach(9);  // Attaches the servo on pin 9 to the servo object

}


void loop() {

  // Sweep the servo from 0 to 180 degrees

  for (pos = 0; pos <= 180; pos += 1) { 

    myServo.write(pos);              // Tell servo to go to position in variable 'pos'

    delay(15);                       // Wait 15 ms for the servo to reach the position

  }

  // Sweep the servo from 180 to 0 degrees

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

    myServo.write(pos);              // Tell servo to go to position in variable 'pos'

    delay(15);                       // Wait 15 ms for the servo to reach the position

  }

}


Ultrasonic Test.

 

const int trigPin = 9;

const int echoPin = 10;


long duration;

int distance;


void setup() {

  pinMode(trigPin, OUTPUT);

  pinMode(echoPin, INPUT);

  Serial.begin(9600);  // Start the serial communication

}


void loop() {

  // Clear the trigPin

  digitalWrite(trigPin, LOW);

  delayMicroseconds(2);


  // Set the trigPin on HIGH state for 10 micro seconds

  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10);

  digitalWrite(trigPin, LOW);


  // Read the echoPin, return the sound wave travel time in microseconds

  duration = pulseIn(echoPin, HIGH);


  // Calculate the distance

  distance = duration * 0.034 / 2;


  // Print the distance on the Serial Monitor

  Serial.print("Distance: ");

  Serial.print(distance);

  Serial.println(" cm");


  delay(500);

}


Friday, July 26, 2024

Servo rotate when ultrasonic detect

#include <Servo.h>

const int trigPin = 9;
const int echoPin = 10;
const int servoPin = 11;
const int distanceThreshold = 10; // Distance threshold in cm

long duration;
int distance;

Servo myServo;  // Create a servo object to control the servo

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  myServo.attach(servoPin);  // Attach the servo on pin 11 to the servo object
  Serial.begin(9600);  // Start the serial communication
  myServo.write(0);  // Set initial position of the servo to 0 degrees
}

void loop() {
  // Clear the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  // Set the trigPin on HIGH state for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the echoPin, return the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);

  // Calculate the distance
  distance = duration * 0.034 / 2;

  // Print the distance on the Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  // Check if the distance is less than the threshold
  if (distance <= distanceThreshold) {
    myServo.write(170);  // Rotate servo to 90 degrees
  } else {
    myServo.write(0);   // Rotate servo back to 0 degrees
  }

  delay(500);  // Wait for half a second before the next loop
}


Tuesday, July 23, 2024

LED Control using Mic

 const int micPin = A0;  // Analog pin connected to the microphone sensor

const int ledPin = 8;   // Digital pin connected to an LED
const int threshold = 500; // Sound threshold for detecting a clap

bool ledState = false;  // To track the state of the LED
unsigned long lastClapTime = 0; // To store the time of the last clap

void setup() {
  pinMode(micPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);   // Initialize serial communication for debugging
}

void loop() {
  int soundLevel = analogRead(micPin); // Read the sound level
  Serial.println(soundLevel);          // Print sound level to the Serial Monitor
 
  // Check if the sound level exceeds the threshold
  if (soundLevel > threshold) {
    // Ensure a delay between clap detections to avoid multiple detections for a single clap
    if (millis() - lastClapTime > 500) { // 500 ms debounce time
      ledState = !ledState;             // Toggle the LED state
      digitalWrite(ledPin, ledState);   // Update the LED
      lastClapTime = millis();          // Update the time of the last clap
    }
  }

  delay(10); // Short delay to stabilize readings
}

Soil moisture measurement

 #include <Adafruit_SSD1306.h>


// Initialize OLED display object
Adafruit_SSD1306 display(128, 64, &Wire);

void setup() {
  Wire.begin();  // Initialize I2C communication
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // Initialize OLED display with I2C address 0x3C
  display.clearDisplay();  // Clear the display buffer
 
  pinMode(A0, INPUT);  // Define A0 pin as input for soil moisture sensor
  pinMode(7, OUTPUT);  // Define pin 7 as output for controlling an LED (assuming you want to indicate soil moisture status)
}

void loop() {
  // Clear previous display content
  display.clearDisplay();
 
  // Display "Soil moisture value: " text on OLED display
  display.setTextSize(2);  // Set text size
  display.setTextColor(SSD1306_WHITE);  // Set text color
  display.setCursor(0, 0);  // Set cursor position
  display.println("Soil moisture value:");
 
  // Read analog value from A0 pin (assuming it's connected to the soil moisture sensor)
  int soilMoisture = analogRead(A0);
 
  // Display soil moisture value on OLED display
  display.setTextSize(2);  // Larger text size for the value
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 16);  // Set cursor position for the value
  display.println(soilMoisture);
 
  // Update OLED display
  display.display();
 
  // Check soil moisture level
  if (soilMoisture < 250) {
    digitalWrite(7, LOW);  // Turn on LED or perform any other action
  } else {
    digitalWrite(7, HIGH);   // Turn off LED or perform any other action
  }
 
  delay(1000);  // Delay for one second (adjust as necessary)
}

Display your name (Oled) Display)

 #include <Adafruit_SSD1306.h>  // Include the Adafruit SSD1306 library


// Initialize an SSD1306 display object
Adafruit_SSD1306 display(128, 64, &Wire);

void setup() {
  Wire.begin();  // Initialize I2C communication
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // Initialize with the I2C address 0x3C
  display.clearDisplay();  // Clear the display buffer
}

void loop() {
  display.clearDisplay();  // Clear previous content
 
  // Display text on the OLED display
  display.setTextSize(2);  // Set text size
  display.setTextColor(SSD1306_WHITE);  // Set text color
  display.setCursor(0, 0);  // Set cursor position
  display.println("Sunil");
 
  display.display();  // Update the display
 
  delay(1000);  // Delay for one second (adjust as necessary)
}

Sunday, July 7, 2024

Smart School Bus ( Window accident prevention bus )

#define trigPin1 9
#define echoPin1 10
#define trigPin2 11
#define echoPin2 12
#define buzzer1 6
#define buzzer2 7

long duration1, distance1;
long duration2, distance2;
const int distanceThreshold = 20; // distance threshold in centimeters

void setup() {
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  pinMode(buzzer1, OUTPUT);
  pinMode(buzzer2, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // Read first ultrasonic sensor
  digitalWrite(trigPin1, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin1, LOW);
  duration1 = pulseIn(echoPin1, HIGH);
  distance1 = (duration1 / 2) / 29.1; // Convert duration to distance

  // Read second ultrasonic sensor
  digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin2, LOW);
  duration2 = pulseIn(echoPin2, HIGH);
  distance2 = (duration2 / 2) / 29.1; // Convert duration to distance

  // Check if any sensor detected an object within the threshold distance
  if (distance1 < distanceThreshold) {
    digitalWrite(buzzer1, HIGH); // Turn on buzzer 1
    Serial.println("Window 1 detected an object.");
  } else {
    digitalWrite(buzzer1, LOW); // Turn off buzzer 1
  }

  if (distance2 < distanceThreshold) {
    digitalWrite(buzzer2, HIGH); // Turn on buzzer 2
    Serial.println("Window 2 detected an object.");
  } else {
    digitalWrite(buzzer2, LOW); // Turn off buzzer 2
  }

  delay(100); // Short delay to avoid spamming the serial monitor
}

Wednesday, July 3, 2024

Temprature display Tinker Orbits

 #include <Wire.h>

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "DHT.h"

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for SSD1306 display connected using I2C
#define OLED_RESET    -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C // I2C address for the SSD1306 display
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define DHTPIN A0     // Pin where the DHT11 is connected
#define DHTTYPE DHT11 // DHT 11

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  // Initialize the I2C communication
  Wire.begin();

  // Initialize the OLED display
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
  display.clearDisplay();
  display.display();

  // Initialize the DHT sensor
  dht.begin();

  // Set up the serial monitor
  Serial.begin(9600);
}

void loop() {
  // Read temperature as Celsius
  float temperature = dht.readTemperature();

  // Check if any reads failed and exit early (to try again)
  if (isnan(temperature)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  // Print the temperature to the Serial Monitor
  Serial.print(F("Temperature: "));
  Serial.print(temperature);
  Serial.println(F("°C"));

  // Clear the display
  display.clearDisplay();

  // Display temperature on the OLED
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.print(F("Temp: "));
  display.print(temperature);
  display.print(F(" C"));

  // Update the display with the new data
  display.display();

  // Wait a few seconds between measurements
  delay(2000);
}

Tuesday, July 2, 2024

Blind stick tinker orbits

const int trigPin = 3;
const int echoPin = 2;
long duration;
int distinCM;

// Function to measure distance using the ultrasonic sensor
int distance() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distinCM = duration * 0.034 / 2;
  return distinCM;
}

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(7, OUTPUT); // Pin for first LED
  pinMode(8, OUTPUT); // Pin for second LED
  pinMode(9, OUTPUT); // Pin for third LED
  Serial.begin(9600); // Start the serial communication for debugging
}

void loop() {
  int dist = distance();
  Serial.print("Distance: ");
  Serial.print(dist);
  Serial.println(" cm");

  if (dist <= 10) { // Check if the distance is 10 cm or less
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
  } else {
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
  }

  delay(500); // Add a delay to avoid overwhelming the sensor and serial monitor
}

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...