Is this possible?

Hello

Im building a robot using Arduino Uno and curious on if it can be done. It uses a servo, a motor shield controlling two motors (each need to input different values) and some ultrasonic sensors.

I've done a fair amount on it and i can seem to get all components working together. The ultrasonic and motors work together, same with motors and servo but put all three together they just seem to not work. I've looked a little into this and could the be due to Uno's limited timers and the servo using timer 1, delays for ultrasonic using timer 0 and the motors using timer 1 and 2?

Is there any way around this so all the systems can work together?

Many people have done it so it's almost certainly possible. It will probably need some changes to the code that you haven't shown us or maybe even to the components you've told us nothing about.

Steve

Boommaker:
Hello

Im building a robot using Arduino Uno and curious on if it can be done. It uses a servo, a motor shield controlling two motors (each need to input different values) and some ultrasonic sensors.

I've done a fair amount on it and i can seem to get all components working together. The ultrasonic and motors work together, same with motors and servo but put all three together they just seem to not work. I've looked a little into this and could the be due to Uno's limited timers and the servo using timer 1, delays for ultrasonic using timer 0 and the motors using timer 1 and 2?

Is there any way around this so all the systems can work together?

Give the folks here a diagram of your setup and let them know what components you're using. Otherwise, it's all a guess as to what the issue is. And I'll guess it's a power issue. Too much current draw on the Arduino.

An Uno timer can serve 2 DC motors (PWM outputs), a Mega timer can serve 3 motors.

The parts I am using are Ardiuno Uno, SKU_DRI0017 Motor Shield (2x2A Arduino Motor Shield Twin wiki-DFRobot), a Tamiya 70168 Double Gearbox Kit (https://core-electronics.com.au/tamiya-70168-double-gearbox-kit.html), Arduino Compatible 9G Micro Servo Motor, two ultrasonic sensors and a triple axis magnetometer.

And this is the code I'm running

#include <Servo.h>
Servo myservo;
int servoPos = 0; // variable to store the servo position and aimed position
int desirePos;
int servoDirection; //0 if 0 moving 0 to 180, if 1 moving from 180 to 0
int servoPin = 4;
int x, y, z; //triple axis data
int mine = 0;

#include <Wire.h> //I2C Arduino Library

#define addr 0x1E //I2C Address for The HMC5883

int trigPin1 = 3, echoPin1 = 4, trigPin2 = 5, echoPin2 = 6; //Ultrasonic
const int E1Pin = 6;
const int M1Pin = 12;
const int E2Pin = 11;
const int M2Pin = 13;

//motorshield setup
/inner definition for motorsield/
typedef struct {
byte enPin;
byte directionPin;
} MotorContrl;
const int M1 = 0;
const int M2 = 1;
const int MotorNum = 2;
const MotorContrl MotorPin[] = { {E1Pin, M1Pin}, {E2Pin, M2Pin} } ;
const int Forward = LOW;
const int Backward = HIGH;

//variables for ultrasonic sensor
float duration1, duration2, cm1, cm2, ratio1, ratio2, distance = 8;

void setup() {

Wire.begin();
Wire.beginTransmission(addr); //start talking
Wire.write(0x02); // Set the Register
Wire.write(0x00); // Tell the HMC5883 to Continuously Measure
Wire.endTransmission();

initMotor();
myservo.attach(2); //Set servo digital pin 2
//Ultrasonic Pinmode
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);

}

void loop() {
ultraSonic ();

//calculate ratios for motor speed
motorRun ();
servoMove();
}

void servoMove() {
if (mine == 0) {
if (servoPos == 7) { //Are you at the end to left
servoDirection = 0; //If so, start moving right
}
if (servoPos == 180) { //Are you at end on right
servoDirection = 1; //Start Moving left
}
if (servoDirection == 0) { //Move right if direction is right
servoPos += 1;
}
if (servoDirection == 1) { //Move left if direction is left
servoPos -= 1;
}
myservo.write(servoPos);
delayMicroseconds(3000);
Wire.requestFrom(addr, 6);
if (6 <= Wire.available()) {
z = Wire.read() << 8; //MSB z
z |= Wire.read(); //LSB z
}
if (abs(z) == 4096) {
mine = 1;
}
}
}

/functions/
void initMotor( ) {
int i;
for ( i = 0; i < MotorNum; i++ ) {
digitalWrite(MotorPin*.enPin, LOW);*
_ pinMode(MotorPin*.enPin, OUTPUT);_
_ pinMode(MotorPin.directionPin, OUTPUT);
}
}
/** motorNumber: M1, M2_

direction: Forward, Backward **/
void setMotorDirection( int motorNumber, int direction ) {
digitalWrite( MotorPin[motorNumber].directionPin, direction);
_}
/** speed: 0-100 * /
inline void setMotorSpeed( int motorNumber, int speed ) {
analogWrite(MotorPin[motorNumber].enPin, 255.0 * (speed / 100.0) ); //PWM
}

void ultraSonic () {
//ultrasonic sensors ping both
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);
delay(20);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
duration2 = (pulseIn(echoPin2, HIGH));
}
void motorRun () {
cm1 = duration1 / 58;
cm2 = duration2 / 58;
if (cm1 > 85) {
* cm1 = 85;
}
if (cm2 > 85) {
cm2 = 85;
}
distance += 0.01;
ratio2 = (cm1 - (distance)) / cm2;
ratio1 = (1 / ratio2);
//set motor speeds*
setMotorDirection( M1, Forward );
setMotorSpeed( M1, ratio1 * 20);
setMotorDirection( M2, Forward );
setMotorSpeed( M2, ratio2 * 25);
//delay for ultrasonic sensor
delay(20);
}_

DangerToMyself:
Give the folks here a diagram of your setup and let them know what components you're using. Otherwise, it's all a guess as to what the issue is. And I'll guess it's a power issue. Too much current draw on the Arduino.

I doubt it will be as using a 7.2V 30C LiPo battery

Boommaker:
And this is the code I'm running

...

Why does your code turn italic in the middle? As written here it doesn't compile.

Boommaker:
I doubt it will be as using a 7.2V 30C LiPo battery

So you won't share your schematic? You should read the "How to use..." FAQ post if you really want help with your project.

ChrisTenone:
Why does your code turn italic in the middle? As written here it doesn't compile.

It's always the same problem, people don't read instructions and don't enclose code in Code Tags </>.

ChrisTenone:
Why does your code turn italic in the middle? As written here it doesn't compile.

So you won't share your schematic? You should read the "How to use..." FAQ post if you really want help with your project.

Firstly, the italics was not the problem as the does not seem to italics in the actual code.

Secondly, I'm sorry didn't provide the schematic the wiring is pretty straight forward with things either going to digital or analog where needed and battery connected to the ardiuno and directly to motor sheild providing power directly to motors.

Thirdly, figured out the problem, not 100% why, but pin 3 needed to be connected to pin 10 (to run motors off motor shield) and the actual connection from back of motor shield connected moved so not connected with some other timing issues stemming from delays in ultrasonics.

Thanks for your minimal to no help

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html . Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

In your ultrasonics section, am I reading it right in that your triggering one sensor, then 20.01 millis later your triggering the other?

The second sensor might hearing bounce back/ echo from the first sensors original pulse in that time period in a real world environment.

Have you tried firing both sensors at “near” or same (using direct port write) time?

Boommaker:
I doubt it will be as using a 7.2V 30C LiPo battery

At the time I made my "guess", there had not been any mention of a battery.

Hi,
Can I suggest the NewPing library to get your ultrasonic readings?

Have you tried to combined the motor drive and ultrasonic codes, without the Wire and 5883?

Thanks.. Tom.. :slight_smile:
PS. Please post your code in code tags.