Aokiji_Master
Forum Veteran
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
#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
}
}
myservo
. We then define the input pin for the sound or rain sensor and a variable to store the sensor reading.setup()
function, we attach the servo motor to pin 9 and set the sensor pin as input.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.write()
function.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 arduinoTo 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 namedmyservo
. We then define the input pin for the sound or rain sensor and a variable to store the sensor reading.
In thesetup()
function, we attach the servo motor to pin 9 and set the sensor pin as input.
In theloop()
function, we read the sensor value using theanalogRead()
function and check if it's greater than 0. If it is, we use afor
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 thewrite()
function to set the servo position and thedelay()
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 thewrite()
function.
You can adjust the values in thedelay()
function to change the speed of the servo motion, and thefor
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 wire a rain sensor with VCC, A0, DO, and GND pins to an Arduino board, you can follow these steps:wiring a rain sensor with vcc a0 DO GND
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
}
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
}
setup()
function, we initialize the serial communication and set the input pin as either an analog or digital input with a pull-up resistor.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.Here is how you can connect a rain sensor with VCC, DO, GND, and A0 pins to an Arduino board:connection of rain sensor with vcc DO GND A0 to arduino
#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
}
myservo
to control a servo motor.soundPin
) used to connect the sound sensor, as well as a threshold value (soundThreshold
) to adjust the sensitivity of the sound detection.setup()
function, we attach the servo to pin 9 and initialize serial communication.loop()
function, we read the analog value from the sound sensor using the analogRead()
function.write()
function, wait for 1 second using the delay()
function, and then move the servo back to position 0.move servo motor 180 degress when a sound is detectedSure, 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 calledmyservo
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 thesetup()
function, we attach the servo to pin 9 and initialize serial communication.
4. In theloop()
function, we read the analog value from the sound sensor using theanalogRead()
function.
5. If the sound value is greater than the threshold, we move the servo motor to position 90 using thewrite()
function, wait for 1 second using thedelay()
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'd be happy to help you with that.move servo motor 180 degress when a sound is detected
#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);
}
write()
function to move it to position 180. After waiting for a second, we move the servo back to position 0.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);
.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
}
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.setup()
function, serial communication is initialized and the servo is attached to the servoPin
using the servo.attach()
function.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.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.#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
}
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);
}
heheangas
#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
}
code for oled 0.9 display testingHere'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.