Hello,
I write to ask what's wrong with my code. I am making a robot follows lines, is my first project. I use a DC motor to give rear 2 whells, and a servo motor for steering on the front wheels. Thanks in advance,
PS: I have opened a thread in the forum in Spanish with photographs and graphics of the project. link is Duda programando 1 servo+1 motor CC ...Linefolloweeer!! (SOLVED in FORO INT) - Software - Arduino Forum
CODE:
#include <Servo.h>
Servo miServo;
int inPinDER = 4;
int inPinIZ = 2;
int motor = 10;
int valueDER = 0;
int valueIZ = 0;
int valueMotor = 40;
void setup() {
pinMode(inPinDER, INPUT);
pinMode(inPinIZ, INPUT);
pinMode (motor, OUTPUT);
analogWrite (motor, valueMotor); //enable motor DC
miServo.attach(11);
}
void loop() {
valueDER = digitalRead(inPinDER);
valueIZ = digitalRead(inPinIZ);
if (valueDER == 0) {
miServo.write (5);//right
}
else if (valueDER == 1 & valueIZ == 1){
miServo.write (90);//centre
}
else if (valueIZ == 0) {
miServo.write (130);//left
}
delay (250);
}
Thanks for all!
I write to ask what's wrong with my code.
You are supposed to tell US that. The code you posted does something that you have not described.
You want it to do something that you have not described.
Without that, there is no way for us to know what code changes are needed.
True, you're absolutely right.
The code consists of an IF statement that, according to the response of the two IR sensors, the servo motor rotates giving direction to the front wheels. This placed it in the function loop ()
In the setup () function configure the INPUTS and OUTPUTS, and plucked the DC motor that moves the linefollower forward.
The idea is that the DC motor works constantly, and according to the information PICKUP IR sensors, the servo motor turns the front wheels to the right, left or straight.
Sorry I did not explain well, I hope you understand my idea.
Thanks for your answer and I send a greeting.
The idea is that the DC motor works constantly, and according to the information PICKUP IR sensors, the servo motor turns the front wheels to the right, left or straight.
OK, so that's the idea, but what actually happens?
Ok!
The problem is that the code that linefollower can not get go forward and turn left / right at the same time.
If you just write the line relating to DC motor:
analogWrite (motor, valueMotor);
CC ... the engine starts and goes forward linefollower ok.
But if I write the IF statement (for the operation of the steering wheels) for the servo motor, the analogWrite (motor, valueMotor) does not work (the linefollower not going forward).
I do not understand why this happens, maybe I'm focusing bad programming.
Thank you for your attention and interest, it is very kind of you.
Greetings again and thank you very much
PD: I include some pictures. I search CNY70 component n the program Fritzing, but i not found. I use another IR sensor for sample.
Using the servo library means that you cannot output a PWM signal on pins 9 or 10. Change the motor output to another PWM pin.
See Servo - Arduino Reference
Bingo!

... great, great! ... Yes sir, thank you very much, it was by the PWM pin. There's nothing like reading the documentation. Sorry!
Arduino community, not I would have done without you!
UKHeliBob, a big virtual beer for you .... and for all fanatic Arduino´s community!
Thaaanks from Barcelona!