What's new

Aokiji_Master

Forum Veteran
Established
swing servo motor back and forth when detected a sound by sound sensor or a rain sensor has detected and stop when thiers no detected
 
To swing a servo motor back and forth when a sound or rain sensor detects something, you can use an Arduino board and write a simple code that controls the servo motor based on the inputs from the sensors.

Here is an example code that you can use as a starting point:

Code:
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
int sensorPin = A0; // sound or rain sensor input pin
int sensorValue;  // variable to store the sensor value
int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  pinMode(sensorPin, INPUT);  // set the sensor pin as input
}

void loop() {
  sensorValue = analogRead(sensorPin);  // read the sensor value
  if (sensorValue > 0) {  // if the sensor value is greater than 0
    for (pos = 0; pos <= 180; pos += 1) { // sweep the servo from 0 to 180 degrees
      myservo.write(pos);              // tell servo to go to position in variable 'pos'
      delay(15);                       // waits 15ms for the servo to reach the position
    }
    for (pos = 180; pos >= 0; pos -= 1) { // sweep the servo from 180 to 0 degrees
      myservo.write(pos);              // tell servo to go to position in variable 'pos'
      delay(15);                       // waits 15ms for the servo to reach the position
    }
  }
  else {  // if the sensor value is 0
    myservo.write(90);  // stop the servo at 90 degrees
  }
}

In this code, we first include the Servo library and create a Servo object named myservo. We then define the input pin for the sound or rain sensor and a variable to store the sensor reading.

In the setup() function, we attach the servo motor to pin 9 and set the sensor pin as input.

In the loop() function, we read the sensor value using the analogRead() function and check if it's greater than 0. If it is, we use a for loop to sweep the servo from 0 to 180 degrees and then from 180 to 0 degrees, creating a back and forth motion. We use the write() function to set the servo position and the delay() function to wait for the servo to reach the position.

If the sensor value is 0, we stop the servo at the center position (90 degrees) using the write() function.

You can adjust the values in the delay() function to change the speed of the servo motion, and the for loop limits to change the range of motion. You can also modify the code to use multiple sensors or add other actions based on sensor inputs.
 
To swing a servo motor back and forth when a sound or rain sensor detects something, you can use an Arduino board and write a simple code that controls the servo motor based on the inputs from the sensors.

Here is an example code that you can use as a starting point:

Code:
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
int sensorPin = A0; // sound or rain sensor input pin
int sensorValue;  // variable to store the sensor value
int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  pinMode(sensorPin, INPUT);  // set the sensor pin as input
}

void loop() {
  sensorValue = analogRead(sensorPin);  // read the sensor value
  if (sensorValue > 0) {  // if the sensor value is greater than 0
    for (pos = 0; pos <= 180; pos += 1) { // sweep the servo from 0 to 180 degrees
      myservo.write(pos);              // tell servo to go to position in variable 'pos'
      delay(15);                       // waits 15ms for the servo to reach the position
    }
    for (pos = 180; pos >= 0; pos -= 1) { // sweep the servo from 180 to 0 degrees
      myservo.write(pos);              // tell servo to go to position in variable 'pos'
      delay(15);                       // waits 15ms for the servo to reach the position
    }
  }
  else {  // if the sensor value is 0
    myservo.write(90);  // stop the servo at 90 degrees
  }
}

In this code, we first include the Servo library and create a Servo object named myservo. We then define the input pin for the sound or rain sensor and a variable to store the sensor reading.

In the setup() function, we attach the servo motor to pin 9 and set the sensor pin as input.

In the loop() function, we read the sensor value using the analogRead() function and check if it's greater than 0. If it is, we use a for loop to sweep the servo from 0 to 180 degrees and then from 180 to 0 degrees, creating a back and forth motion. We use the write() function to set the servo position and the delay() function to wait for the servo to reach the position.

If the sensor value is 0, we stop the servo at the center position (90 degrees) using the write() function.

You can adjust the values in the delay() function to change the speed of the servo motion, and the for loop limits to change the range of motion. You can also modify the code to use multiple sensors or add other actions based on sensor inputs.
connection of rain sensor with vcc DO GND A0 to arduino
 
wiring a rain sensor with vcc a0 DO GND
To wire a rain sensor with VCC, A0, DO, and GND pins to an Arduino board, you can follow these steps:

1. Connect the VCC pin of the rain sensor to the 5V pin of the Arduino board.
2. Connect the GND pin of the rain sensor to the GND pin of the Arduino board.
3. Connect the A0 pin of the rain sensor to any analog input pin (e.g., A0) of the Arduino board.
4. Connect the DO pin of the rain sensor to any digital input pin (e.g., D2) of the Arduino board.

Once the rain sensor is wired to the Arduino board, you can use the analogRead() and digitalRead() functions to read the sensor data.

For example, you can use the following code to read the analog value of the rain sensor and print it to the serial monitor:

Code:
int sensorPin = A0; // rain sensor input pin
int sensorValue;  // variable to store the sensor value

void setup() {
  Serial.begin(9600);  // initialize serial communication
}

void loop() {
  sensorValue = analogRead(sensorPin);  // read the sensor value
  Serial.print("Rain sensor value: ");
  Serial.println(sensorValue);  // print the sensor value to the serial monitor
  delay(1000);  // wait for a second before reading again
}

Alternatively, you can use the following code to read the digital value of the rain sensor and print a message to the serial monitor if it's raining:

Code:
int sensorPin = 2; // rain sensor input pin
int sensorValue;  // variable to store the sensor value

void setup() {
  Serial.begin(9600);  // initialize serial communication
  pinMode(sensorPin, INPUT_PULLUP);  // set the sensor pin as input with pull-up resistor
}

void loop() {
  sensorValue = digitalRead(sensorPin);  // read the sensor value
  if (sensorValue == LOW) {  // if it's raining
    Serial.println("It's raining!");  // print a message to the serial monitor
  }
  delay(1000);  // wait for a second before reading again
}

In this code, we first define the rain sensor pins and variables. In the setup() function, we initialize the serial communication and set the input pin as either an analog or digital input with a pull-up resistor.

In the loop() function, we read the sensor value using either the analogRead() or digitalRead() function depending on the type of sensor. We then print the sensor value or a message to the serial monitor and wait for a second before reading again using the delay() function.
 
connection of rain sensor with vcc DO GND A0 to arduino
Here is how you can connect a rain sensor with VCC, DO, GND, and A0 pins to an Arduino board:

1. Connect the VCC pin of the rain sensor to the 5V pin on the Arduino board.
2. Connect the GND pin of the rain sensor to the GND pin on the Arduino board.
3. Connect the DO (digital output) pin of the rain sensor to any digital pin on the Arduino board, such as pin 2 or 3.
4. Connect the A0 (analog output) pin of the rain sensor to the A0 pin on the Arduino board.

Once you have connected the rain sensor to the Arduino board, you can use the analogRead() function in your code to read the sensor value from the A0 pin. The digital output from the DO pin can be used to trigger other actions or events in your code based on the sensor readings.
 
Sure, I can help you with that!

Here is some sample code that will move a servo motor to two different positions when a sound is detected with a sound sensor:

Code:
#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int soundPin = A0;  // analog pin used to connect the sound sensor
int soundThreshold = 500;  // adjust this value to set the sound sensitivity

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  Serial.begin(9600);  // initialize serial communication
}

void loop() {
  int soundVal = analogRead(soundPin);  // read the analog value from the sound sensor

  if (soundVal > soundThreshold) {  // if the sound value is greater than the threshold
    myservo.write(90);  // move the servo to position 90
    delay(1000);  // wait for 1 second
    myservo.write(0);  // move the servo to position 0
  }

  delay(50);  // wait for a short time before reading the sound sensor again
}

Here's how it works:

1. First, we create a Servo object called myservo to control a servo motor.
2. Then, we define the analog pin (soundPin) used to connect the sound sensor, as well as a threshold value (soundThreshold) to adjust the sensitivity of the sound detection.
3. In the setup() function, we attach the servo to pin 9 and initialize serial communication.
4. In the loop() function, we read the analog value from the sound sensor using the analogRead() function.
5. If the sound value is greater than the threshold, we move the servo motor to position 90 using the write() function, wait for 1 second using the delay() function, and then move the servo back to position 0.
6. We add a short delay at the end of the loop to prevent the code from constantly reading the sound sensor.

I hope this helps you get started! Let me know if you have any questions.
 
Sure, I can help you with that!

Here is some sample code that will move a servo motor to two different positions when a sound is detected with a sound sensor:

Code:
#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int soundPin = A0;  // analog pin used to connect the sound sensor
int soundThreshold = 500;  // adjust this value to set the sound sensitivity

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  Serial.begin(9600);  // initialize serial communication
}

void loop() {
  int soundVal = analogRead(soundPin);  // read the analog value from the sound sensor

  if (soundVal > soundThreshold) {  // if the sound value is greater than the threshold
    myservo.write(90);  // move the servo to position 90
    delay(1000);  // wait for 1 second
    myservo.write(0);  // move the servo to position 0
  }

  delay(50);  // wait for a short time before reading the sound sensor again
}

Here's how it works:

1. First, we create a Servo object called myservo to control a servo motor.
2. Then, we define the analog pin (soundPin) used to connect the sound sensor, as well as a threshold value (soundThreshold) to adjust the sensitivity of the sound detection.
3. In the setup() function, we attach the servo to pin 9 and initialize serial communication.
4. In the loop() function, we read the analog value from the sound sensor using the analogRead() function.
5. If the sound value is greater than the threshold, we move the servo motor to position 90 using the write() function, wait for 1 second using the delay() function, and then move the servo back to position 0.
6. We add a short delay at the end of the loop to prevent the code from constantly reading the sound sensor.

I hope this helps you get started! Let me know if you have any questions.
move servo motor 180 degress when a sound is detected
 
move servo motor 180 degress when a sound is detected
Sure, I'd be happy to help you with that.

Here's some sample code that you can use to move a servo motor 180 degrees when a sound is detected:

Code:
#include <Servo.h>

Servo myservo;
int soundPin = A0;
int soundThreshold = 500;

void setup() {
  myservo.attach(9);
  Serial.begin(9600);
}

void loop() {
  int soundVal = analogRead(soundPin);

  if (soundVal > soundThreshold) {
    myservo.write(180);
    delay(1000);
    myservo.write(0);
  }

  delay(50);
}

This code is similar to the previous example, but instead of moving the servo motor to position 90, we use the write() function to move it to position 180. After waiting for a second, we move the servo back to position 0.

If you want to change the position of the servo motor, you can adjust the value passed to the write() function. For example, if you want to move the servo to position 135, you can replace the line myservo.write(180); with myservo.write(135);.

I hope this helps! Let me know if you have any questions or if you need further assistance.
 
Here is an example code for controlling a servo motor to move when sound is detected and stop when there is no sound. This code uses a sound sensor module to detect sound and a servo motor to control the cradle motion.

Code:
c++
#include <Servo.h>

int soundPin = A0; // Analog pin connected to sound sensor
int soundThreshold = 500; // Threshold value for sound detection
int servoPin = 9; // Digital pin connected to servo motor
Servo servo; // create servo object to control a servo

void setup() {
  Serial.begin(9600); // initialize serial communication
  servo.attach(servoPin); // attaches the servo on pin 9 to the servo object
}

void loop() {
  int soundValue = analogRead(soundPin); // read the sound sensor value
  Serial.println(soundValue); // print the sound sensor value to serial monitor

  // If the sound sensor value is greater than the threshold value
  if (soundValue > soundThreshold) {
    // move the servo to 0 degrees
    servo.write(0);
  } else {
    // move the servo to 90 degrees
    servo.write(90);
  }
  delay(50); // small delay for stability
}

In this code, the soundPin variable is set to the analog pin connected to the sound sensor, and the soundThreshold variable is set to the threshold value for sound detection. The servoPin variable is set to the digital pin connected to the servo motor.

In the setup() function, serial communication is initialized and the servo is attached to the servoPin using the servo.attach() function.

In the loop() function, the sound sensor value is read using the analogRead() function and printed to the serial monitor using the Serial.println() function. If the sound sensor value is greater than the threshold value, the servo is moved to 0 degrees using the servo.write() function, which moves the servo to the specified angle. If the sound sensor value is less than or equal to the threshold value, the servo is moved to 90 degrees.

The delay() function is used to add a small delay for stability, so the servo motor has time to move to its new position before the loop starts again.
 
Here's some sample code for a medicine reminder using Arduino Nano, 16x2 LCD, 10k pot, 3 push buttons, and RTC DS3231:

Code:
#include <Wire.h>
#include <LiquidCrystal.h>
#include <RTClib.h>

RTC_DS3231 rtc;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the lcd

int currentHour, currentMinute, currentSecond, currentDay, currentMonth, currentYear;
int setHour = 7; // set the hour for the reminder
int setMinute = 0; // set the minute for the reminder
int setSecond = 0;
int messageDisplayed = 0;

int button1State = 0;
int button2State = 0;
int button3State = 0;

void setup() {
  pinMode(6, INPUT);
  pinMode(7, INPUT);
  pinMode(8, INPUT);

  Serial.begin(9600); // initialize serial communication
  Wire.begin(); // initialize the I2C library
  rtc.begin(); // initialize the RTC module

  lcd.begin(16, 2); // initialize the lcd
}

void loop() {
  currentHour = rtc.now().hour();
  currentMinute = rtc.now().minute();
  currentSecond = rtc.now().second();
  currentDay = rtc.now().day();
  currentMonth = rtc.now().month();
  currentYear = rtc.now().year();

  // read the potentiometer value and print it on the LCD
  int potValue = analogRead(A0);
  potValue = map(potValue, 0, 1023, 0, 100);
  lcd.setCursor(0, 0);
  lcd.print("Pot Value: ");
  lcd.print(potValue);
  lcd.print("%");

  // read the button states
  button1State = digitalRead(6);
  button2State = digitalRead(7);
  button3State = digitalRead(8);

  // if the current time matches the set reminder time, display the reminder message
  if (currentHour == setHour && currentMinute == setMinute && currentSecond == setSecond && messageDisplayed == 0) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Time to take");
    lcd.setCursor(0, 1);
    lcd.print("your medicine!");
    messageDisplayed = 1;
  }

  // if the reminder message has been displayed and button 1 is pressed, reset the messageDisplayed variable
  if (messageDisplayed == 1 && button1State == HIGH) {
    messageDisplayed = 0;
  }

  // if button 2 is pressed, increment the setHour variable
  if (button2State == HIGH) {
    setHour++;
    if (setHour > 23) {
      setHour = 0;
    }
  }

  // if button 3 is pressed, increment the setMinute variable
  if (button3State == HIGH) {
    setMinute++;
    if (setMinute > 59) {
      setMinute = 0;
    }
  }

  // print the current time on the LCD
  lcd.setCursor(0, 1);
  lcd.print("Time: ");
  lcd.print(currentHour);
  lcd.print(":");
  lcd.print(currentMinute);
  lcd.print(":");
  lcd.print(currentSecond);
  lcd.print(" ");

  // print the set reminder time on the LCD
  lcd.setCursor(9, 1);
  lcd.print("Set: ");
  lcd.print(setHour);
  lcd.print(":");
  lcd.print(setMinute);

  delay(1000); // delay for 1 second
}

This code reads the current time from the RTC module and displays it on the LCD, along with the value of a potentiometer connected to analog input A0. It also reads the state of three push buttons connected to digital pins 6, 7, and 8.

The code checks if the current time matches the set reminder time (7:00 AM by default), and if it does, it displays a message on the LCD reminding the user to take their medicine. If the message has been displayed and button 1 is pressed, the messageDisplayed variable is reset so the message will be displayed again the next time the reminder time is reached.

If button 2 is pressed, the setHour variable is incremented (with wraparound at 23), and if button 3 is pressed, the setMinute variable is incremented (with wraparound at 59). The set reminder time is displayed on the LCD alongside the current time.
 
Here is an example code for Arduino using a heart pulse sensor and OLED 0.9:

Code:
C++
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <PulseSensorPlayground.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#define PULSE_SENSOR A0
PulseSensorPlayground pulseSensor;

void setup() {
  Serial.begin(9600);

  // initialize OLED display
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0,0);
  display.println("Heart Pulse");
  display.display();

  // initialize pulse sensor
  pulseSensor.analogInput(PULSE_SENSOR);
  pulseSensor.blinkOnPulse(false);
  pulseSensor.setThreshold(550);

  // wait for pulse sensor to stabilize
  delay(1000);
}

void loop() {
  int pulse = pulseSensor.getBeatsPerMinute();
  Serial.println(pulse);

  // display pulse rate on OLED
  display.clearDisplay();
  display.setCursor(0,0);
  display.println("Pulse");
  display.setTextSize(3);
  display.setCursor(20,20);
  display.println(pulse);
  display.setTextSize(1);
  display.setCursor(100,30);
  display.println("BPM");
  display.display();

  delay(1000);
}

This code uses the Adafruit_GFX and Adafruit_SSD1306 libraries to display the pulse rate on a 0.9 inch OLED display. The PulseSensorPlayground library is used to read the pulse sensor data from analog input A0. The pulse rate is displayed in beats per minute (BPM).
 
Here's an example code for reading pulse sensor data and displaying it on a 0.96 inch OLED display with Arduino:

Code:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET     4
Adafruit_SSD1306 display(OLED_RESET);

const int pulsePin = A0;
int pulseValue = 0;
int bpm = 0; //beats per minute

void setup() {
  Serial.begin(9600);
  pinMode(pulsePin, INPUT);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // Initialize with the I2C address of the display
  display.clearDisplay();
}

void loop() {
  pulseValue = analogRead(pulsePin);
  bpm = map(pulseValue, 0, 1023, 20, 300); //map the sensor value to beats per minute range
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(10,0);
  display.print("Heart Rate");
  display.setTextSize(3);
  display.setCursor(20,25);
  display.print(bpm);
  display.setTextSize(1);
  display.setCursor(85, 40);
  display.print("BPM");
  display.display();
  delay(100); //wait for next reading
}

This code reads the pulse sensor data from pin A0 and maps it to beats per minute range. It then displays the heart rate on the OLED display in a large font with "BPM" in a smaller font below it. The display is updated every 100ms.
 
Here's an example code for reading pulse sensor data and displaying it on a 0.96 inch OLED display with Arduino:

Code:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET     4
Adafruit_SSD1306 display(OLED_RESET);

const int pulsePin = A0;
int pulseValue = 0;
int bpm = 0; //beats per minute

void setup() {
  Serial.begin(9600);
  pinMode(pulsePin, INPUT);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // Initialize with the I2C address of the display
  display.clearDisplay();
}

void loop() {
  pulseValue = analogRead(pulsePin);
  bpm = map(pulseValue, 0, 1023, 20, 300); //map the sensor value to beats per minute range
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(10,0);
  display.print("Heart Rate");
  display.setTextSize(3);
  display.setCursor(20,25);
  display.print(bpm);
  display.setTextSize(1);
  display.setCursor(85, 40);
  display.print("BPM");
  display.display();
  delay(100); //wait for next reading
}

This code reads the pulse sensor data from pin A0 and maps it to beats per minute range. It then displays the heart rate on the OLED display in a large font with "BPM" in a smaller font below it. The display is updated every 100ms.
code for oled 0.9 display testing
 

Similar threads

Back
Top