# Moving a dc motor shaft to 90 degrees

I’m doing a project that is a light detector, using the arduino but I’m restricted in having a H-bridge transistor circuit (not a chip with the H-bridge). The projects concept is to be able to give direction to a motor to the side where there is higher intensity of light (which are read via photoresistors). For example if the photo resistor on the left has more light intensity then the motors shaft should turn 90 degrees to the left and then back to its original place, and the same with thing if the right photo resistor on the right. If both of them have around the same level of intensity, then it should stay on its original position.

However doing the code for the motor (as a testing so we could know how to implement it later) while it repeats the movement of 90 degrees to the right I found out that it was moving a bit more than it should, resulting on having the same movement until it cover all 360 degrees. Can someone help me fix this problem? Here is the code:

void setup() {
// put your setup code here, to run once:
int runmotorR;
int runmotorL;
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
runRight();
//runLeft();

}

void runRight(){

digitalWrite(8,HIGH);
digitalWrite(7,LOW);
delay(480);
digitalWrite(8,LOW);
digitalWrite(7,LOW);

while(millis() < 900){
digitalWrite(8,HIGH);
digitalWrite(7,LOW);
}

digitalWrite(7,HIGH);
digitalWrite(8,LOW);
delay(480);
digitalWrite(8,LOW);
digitalWrite(7,LOW);
}

void runLeft(){
digitalWrite(8,LOW);
digitalWrite(7,HIGH);
delay(100);
digitalWrite(8,HIGH);
digitalWrite(7,HIGH);

//while() //para que el motor se quede detenido apuntando el lado que percibe mas luz

digitalWrite(7,LOW);
digitalWrite(8,HIGH);
delay(100);
digitalWrite(8,HIGH);
digitalWrite(7,HIGH);
}

You'll need some kind of sensors so the software knows when the motor is in the 3 stop positions. Slotted optical sensors are a common solution.

Please use code tags next time. The forum software eats some of your code if you don’t.

You said that the code makes the motor turn to the right. That is indeed what I think the code does. This is a good test that the code is able to control the motor. What do you want it to do differently?

``````while(millis() < 900){
``````

digitalWrite(8,HIGH);
digitalWrite(7,LOW);
}

The code above will only be active during the first 900 milliseconds of the program’s operation. Then it will be active for 900ms 49 days later after millis() rolls over to zero. What do you want it to do?

Also, the 7’s and 8’s all over the code are very messy. Define some names for these pins and use those names everywhere.

What we are looking for is after the 90 degrees we want to stop the dc motor position and then later on return to 90 degrees back the same amount of distance counter clockwise without moving in a 360 degrees.The same condition for the void function left but it will be the reverse of it since is counter clockwise to clockwise.

``````void loop() {
// put your main code here, to run repeatedly:
runRight();
delay(2000); //wait 2 seconds so we can look at it
runLeft();
delay(3000); //wait 3 seconds so we know this is the end of the loop
}
``````

Of course, without any feedback on the motor shaft, the rotations are just guesses. You could tune it so that 900ms goes right and 980ms goes left, if the motor is a little sticky going left. But once you get your photocell set up, you will have that feedback.

LordKrayt: What we are looking for is after the 90 degrees we want to stop the dc motor position and then later on return to 90 degrees back the same amount of distance counter clockwise without moving in a 360 degrees.The same condition for the void function left but it will be the reverse of it since is counter clockwise to clockwise.

I suggest you build a demo program using a servo and the servo library. Servos have position control so you can tell it to move to 90deg (or whatever) and know it will do that. Then you can concentrate on getting the rest of the program logic working properly.

Parallel with that you can write a short program with a function to make your bigger motor move to specified positions without needing to worry about anything else. As others have said you will need some sensors to provide position feedback. A rotary encoder might help, but the cheaper versions don't provide an absolute position. With them you would need some means to move to the Zero position when your Arduino starts.

When it all works properly as two separate pieces you can replace the calls to Servo.write() with a call to your own motor function.

...R

PS... You can probably figure from all of this that if you can get a suitable servo it would make life very much easier than using a DC motor.

The servo is banned for my project I can't use it. I am using a dc motor for my project.Also someone told me to use code tags but I have never use it before. Witch one you recommend to download so I can put the code the forum since I got the information that the software of the forum eats the code.

The servo is banned for my project I can't use it

Is this a homework assignment? If so, ask your instructor for some hints, and/or discuss it with your classmates.

3 [u]slotted switches[/u] and a "flag" on the motor shaft. If the DC motor isn't geared-down you'll probably need to slow it down (with PWM) so it doesn't overshoot. Then of course, an if-statement to stop the motor when the appropriate slotted switch senses the target location.

If you can use a stepper motor, that's another option but you need at least one home sensor to fine the .

.Also someone told me to use code tags but I have never use it before

Look at #7 [u]here[/u]

then back to its original place,

If the "original place" is not always the same (half way between +90 & -90?) then you can use a stepper motor with no positions sensors. (A DC motor would require a rotary encoder if the original position is arbitrary.)

I am still testing but it looks better now. The reason runleft is in a comment is because I am testing first the right side from 90 degrees to 180 degrees and return from 180 degrees to 90 degrees and stop there in 90 degrees.

Here is my code that I have right now:

``````void setup() {
// put your setup code here, to run once:
int runmotorR;
int runmotorL;
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
runRight();
delay(2000);//wait 2 seconds so we can look at it
//runLeft();
delay(3000);//wait 3 seconds so we know this is the end of the loop

}

void runRight(){

digitalWrite(8,HIGH);
digitalWrite(7,LOW);
delay(240);
digitalWrite(8,LOW);
digitalWrite(7,LOW);

while(millis() < 900){ //for the dc motor stay still at 180 degrees when it perceiving light from the photo-resistor
digitalWrite(8,HIGH);
digitalWrite(7,LOW);
}

digitalWrite(7,HIGH);
digitalWrite(8,LOW);
delay(245);
digitalWrite(8,LOW);
digitalWrite(7,LOW);
}

void runLeft(){
digitalWrite(8,LOW);
digitalWrite(7,HIGH);
delay(240);
digitalWrite(8,HIGH);
digitalWrite(7,HIGH);

//while() //for the dc motor stay still at 180 degrees when it perceiving light from the photo-resistor

digitalWrite(7,LOW);
digitalWrite(8,HIGH);
delay(245);
digitalWrite(8,HIGH);
digitalWrite(7,HIGH);
}
``````

LordKrayt:
The servo is banned for my project I can’t use it. I am using a dc motor for my project.

I understand that you don’t want to have a servo in the final project. But I can’t see why you would not use one during the development process like I suggested.

Also someone told me to use code tags but I have never use it before. Witch one you recommend to download so I can put the code the forum since I got the information that the software of the forum eats the code.

Read How to use the Forum and look at how code is presented in other Forum Threads. The code button </> is the first one on the left.

…R

I already have the servo running for my main program for project as a test. I am just adding the dc motor part over here since it not the same as a servo and I can't use it for my project since the professor doesn't allow it. This is the only part i have left to finish. But I really appreciate your suggestion Robin since I already did that part that you suggested.

What does your function runRight() do?

It does not seem to me like it is anything closely equivalent to the functions Servo.write(90) or Servo.write(180).

And

the professor he told me to use a hold function outside the void loop to stop the dc motor

I doubt very much that your Professor intended you to get a solution from someone else. I reckon he expects you to work out the solution yourself.

What is the course you are studying and how far have you progressed through the course?

...R

The servo was a test to see the program works that I already mentioned here that's why they are in comments and I really don't understand what you mean about progress since I have been testing out my program and I it has been successful only the part to stop the dc motor is needed that it but the project works perfectly. This is a forum to help people with the projects and doubts from the program that they have work that why is call project guidance so I really don't understand the issue here since I am almost done with the project by myself. The function runRight() is for the dc motor goes to the right.

It doesn't matter now I finish the project. I fix the problem I really thanks everyone that guided me and help me out in some parts that I have doubts for this project.May you have Merry Christmas soon. :)

LordKrayt: It doesn't matter now I finish the project. I fix the problem I

It would be helpful to other readers who may have the same problem if you would take a few minutes to explain your solution.

Merry Xmas

...R