Hey! I am new to programming, I want to try and make the servo motor move only when an object is near the sensor for less than 4 inches. However, even if the object is far or near, the servo motor keeps moving by itself. I am not sure what to do now.
Can you show us your wiring/schematic, and your Arduino code (in code tags).
Thx
Hi! Here, tysm
// C++ code
//
#include <LiquidCrystal.h>
#include <Servo.h>
int seconds = 0;
int i = 0;
int pos = 0;
int sensorvalue = 0;
LiquidCrystal lcd_1(12, 11, 5, 4, 3, 2);
long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}
Servo servo_9;
void setup()
{
lcd_1.begin(16, 2); // Set up the number of columns and rows on the LCD.
pinMode(A0, INPUT);
servo_9.attach(9, 500, 2500);
pinMode(8, OUTPUT);
lcd_1.print("Temperature");
}
void loop()
{
lcd_1.setCursor(0, 1);
lcd_1.print((-40 + 0.488155 * (analogRead(A0) - 20)));
lcd_1.setCursor(7, 1);
lcd_1.print("°C");
pos = 0;
sensorvalue = 0.006783 * readUltrasonicDistance(7, 6);
if (sensorvalue > 4) {
for (pos = 90; pos <= 1; pos += 1) {
servo_9.write(pos);
delay(15); // Wait for 15 millisecond(s)
}
for (pos = 1; pos >= 90; pos -= 1) {
servo_9.write(pos);
delay(15); // Wait for 15 millisecond(s)
}
}
if ((-40 + 0.488155 * (analogRead(A0) - 20)) > 38) {
tone(8, 523, 3000); // play tone 60 (C5 = 523 Hz)
delay(3000); // Wait for 3000 millisecond(s)
}
}
The schematic looks good but is hard to read (loss of resolution)…
First thing it looks like you’re powering the servo from the Arduino directly.
The Arduino can only power about 1/3 of a single servo reliably. The 5V rail will drop & sag - causing problems.
Do you think I would need another arduino? (I am not sure if I am understanding it correctly) Also, does that mean that the coding is okay overall?
Really this code runs?
It does, I am not sure if its correct but I am using code block in tinkercad, maybe that's why it may look weird when in text. The only problem that I have with that code is the servo motor, nothing else at the moment.
I think It should be
for (pos=90; pos >=1; pos -=1)
for(pos=0; pos <= 90; pos +=1)
I can’t B see enough detail, but usually a motor is supplied with a separate source of volts (via the driver)… typically 4xAA batteries in a holder, with 0V/Ground tied together across the project.
Hi, @aximo
No, just an external 5V supply for the servos.
The supply would share gnd with the UNO gnd.
Tom...
The generated code seems to have this test backward. You specified "distance <= 4" and it is checking for "distance > 4". This is a similar mistake to the two loops that @amitf mentioned above where comparisons were backwards. Something is very wrong with whatever is generating the code.
NOTE: If there is nothing in range of the ultrasonic sensor (about 5 meters maximum) the returned distance will be zero. You should probably check for zero before you compare the value to 4.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.