Hello everyeone.
I'm working on a little robot project and I recently added a claw to my project. To control this claw I'm using the 5V output from my Arduino Uno. I also use this output for some other devices (2 distance sensors). Normally when I do testing with these sensor they give accurate results for distances even farther than 100cm. Also when I use some simple code on the servo it's working without a problem. But if I want to combine the two in something like the code below the sensors can only measure to around 8cm.
#define ENABLE_A 0
#define ENABLE_B 1
#define IN1 2
#define IN2 3
#define IN3 4
#define IN4 5
#define trigpin 6
#define echopin 7
#define trigpin2 8
#define echopin2 9
#define MAX_DISTANCE 154
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
void setup() {
Serial.begin(115200);
pinMode(trigpin, OUTPUT);
pinMode(echopin, INPUT);
pinMode(trigpin2, OUTPUT);
pinMode(echopin2, INPUT);
pinMode(ENABLE_A, OUTPUT);
pinMode(ENABLE_B, OUTPUT);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
myservo.attach(10);
myservo.write(0);
}
void loop() {
long duration, distance;
digitalWrite(trigpin, LOW);
delayMicroseconds(2);
digitalWrite(trigpin, HIGH);
delayMicroseconds(10);
digitalWrite(trigpin,LOW);
duration = pulseIn(echopin, HIGH);
distance = (duration/2)/29.1;
if (distance < 10)
{
myservo.write(45);
}
else
{
myservo.write(0);
}
Serial.print(distance);
Serial.println(" cm");
}
Is this a problem related to power consumption? And more importantly, how do I fix this?
Thanks for your help in advance!
Charles