I am trying to make a paranormal music box . I have my code working but I am curios if what I am thinking would be possible to code. I am using the hc-sr04 sensor to detect the distance of 10 feet atm . If anything is closer it will turn my servo which turns the crank on my music box. It also displays the distance in inches to the l2c lcd display because I couldn't get the code to write it in feet and inches . what I am curious to try is have the sensor calibrate the distance at start up and have that set as the starting distance then anything that gets closer will activate the servo. Example : I have a door way with a wall 12 feet away it reads it as 12 feet and automatically sets that as the starting point at startup . or if its 6 feet it sets that ... is this possible? I can post my code if needed
Post your code.
Servos generally turn 180 degrees. A continuous rotation servo will be needed to turn a crank unless the crank had inertia, then you could use the servo in the same manner as an internal combustion engine connecting rod.
1cm = 1/30.48 feet
It is already calibrated at the speed of sound at sea level. You will want to measure heat and humidity to increase precision.
it is a continuous rotation servo its the fs90r. what i mean by calibrate is if where i place it only has a 6 foot walkway . when I turn it on I want it to see its 6 foot and set that as the starting point and then anything closer than 6 foot will set it off.. or if the distance is a 12 foot distance anything closer will set it off without having to go in and manually program it . The box I am trying to base it off of is the para4ce music box. when it turns on it calibrates the distance to the nearest object and uses that as its starting point for detection. I already have it programmed and assembled and working. I am hoping that what I want it to do can be done.
#include <Wire.h> // include I2C library
#include <LiquidCrystal_I2C.h> // include LCD library
#include <Servo.h> // include servo library
#define TRIGGER_PIN 2 // define trigger pin for HC-SR04 sensor
#define ECHO_PIN 3 // define echo pin for HC-SR04 sensor
#define MAX_DISTANCE 120 // define maximum distance in inches (10 feet)
LiquidCrystal_I2C lcd(0x27, 16, 2); // initialize LCD with I2C address and dimensions
Servo servo; // initialize servo motor
void setup() {
pinMode(TRIGGER_PIN, OUTPUT); // set trigger pin as output
pinMode(ECHO_PIN, INPUT); // set echo pin as input
pinMode(8, OUTPUT); // set pin 8 as output for LED
lcd.init(); // initialize LCD
lcd.backlight(); // turn on LCD backlight
servo.attach(9); // attach servo to pin 9
}
void loop() {
long duration, inches, cm; // variables for duration, distance in inches, and distance in cm
digitalWrite(TRIGGER_PIN, LOW); // set trigger pin to low
delayMicroseconds(2); // wait for 2 microseconds
digitalWrite(TRIGGER_PIN, HIGH); // set trigger pin to high
delayMicroseconds(10); // wait for 10 microseconds
digitalWrite(TRIGGER_PIN, LOW); // set trigger pin to low
duration = pulseIn(ECHO_PIN, HIGH); // measure duration of pulse
inches = microsecondsToInches(duration); // convert duration to inches
cm = microsecondsToCentimeters(duration); // convert duration to cm
if (inches < MAX_DISTANCE) { // if object is less than 10 feet away
lcd.setCursor(0, 0); // set cursor to first row
lcd.print("Distance: "); // print label
lcd.print(inches); // print distance in inches
lcd.print(" in"); // print unit
servo.write(180); // turn servo to 180 degrees
digitalWrite(8, LOW); // turn on LED
} else { // if object is greater than 8 feet away
lcd.setCursor(0, 0); // set cursor to first row
lcd.print("Distance: "); // print label
lcd.print(inches); // print distance in inches
lcd.print(" in"); // print unit
servo.write(90); // turn servo to 0 degrees
digitalWrite(8, HIGH); // turn off LED
}
delay(100); // wait for 100 milliseconds
}
long microsecondsToInches(long microseconds) { // function to convert microseconds to inches
return microseconds / 74 / 2; // formula for conversion
}
long microsecondsToCentimeters(long microseconds) { // function to convert microseconds to cm
return microseconds / 29 / 2; // formula for conversion
}
2cm to 400cm
Is this the line you want to calibrate on powerup?
I'd replace MAX_DISTANCE with a variable, and in setup() I'd measure the distance with your sensor and set that variable for loop() to use.
yes DaveX thats the one . I'll give that a shot . I am still fairly new at this . And xfpd I think u are misunderstanding what I am trying to do.
Write a function that returns the distance. that will make your life easier.
Remember also not to ping too often as you might get some echo from the previous ping. At the moment your code has a delay(100); // wait for 100 milliseconds
at the end of the loop so you are fine, but if you add a ping in the setup to "calibrate" MAX_DISTANCE, then don't forget to add a delay as well there so that the ping in the loop that follows is not impacted.
hi DaveX im also new to this could you give me a example?
The sketch already has examples within itself:
Making a variable:
....but it needs to be a global variable global so put this outsied of setup(){} or loop(){} so they can both share it:
int karlpxMaxDistanceInches;
...setting it in setup:
karlpxMaxDistanceInches = inches;
...etc...
search term: modulo. if you count seconds and display them modulo 60 you get minutes. 80 inches modulo 12 = 6 with a remainder of 8
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.