Go Down

Topic: help with robot (Read 1 time) previous topic - next topic


Hi, I'm trying to make a robot from this to tutorials



so everything is working fine separetly, but then I try to make it work together nothing works because of code


//motor pins
const int motorPin1 = 3;
const int motorPin2 = 4;

//Arduino pins
int LDR1 = 5;
int LDR2 = 4;

//This vars will be used in the loop function
int LDRVal1 = 0;
int LDRVal2 = 0;

const int ledPin = 13;

//servo and motor
#include <ServoTimer1.h>
ServoTimer1 myservo;

void setup(){
//servo pins
//set aoutputs
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(ledPin, OUTPUT);
//blink the led for 3s.
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin, LOW);

void loop(){
//read input LDR values
LDRVal1 = analogRead(LDR1);
LDRVal2 = analogRead(LDR2);

//print LDR alues into the console
Serial.print("   ");

//compare the values and turn servo
//turn rigkt
if (LDRVal1 > LDRVal2){
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);}
//turn left
else if (LDRVal1 < LDRVal2){
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);}
//move forward
else if (LDRVal1 == LDRVal2){
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);}


Jan 17, 2011, 08:02 pm Last Edit: Jan 17, 2011, 08:04 pm by AWOL Reason: 1
And what does 'doesn't work' mean, in this context?

Two LDR values being exactly equal seems unlikely.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.


servo works fine, but motor does not work at all


I just use transisters for my motors, but I don't know how to reverse them. Here's my site: https://sites.google.com/site/arduinosoapy29/motor-speed-controller. I put in there how to hook up a transister to a motor or relay.


I just use transisters for my motors, but I don't know how to reverse them.

You need to build an h-bridge; you can do this by using two 2n2222 (NPN) and two 2n3906 (PNP) transistors (the 2n3906 is the "complement" of the 2n2222 - that is, not only is it a PNP transistor, but it also has similar specifications to the 2n2222; this is important for a variety of reasons - especially in an h-bridge).

You will also need four diodes, and four base resistors. When using an h-bridge, you must make sure to turn on only one pair of transistors (diagonal legs), and leave the other pair off to avoid "shoot thru" which can smoke the transistors. This can be more difficult than it sounds, especially with a microcontroller, because if you just wire the outputs to the transistors (even if you only use two outputs - one for each pair) - when the microcontroller is powered on, its outputs may be in an unknown state - if both outputs are HIGH (all transistors on), the smoke is released.

There are ways around this, but until you understand what an h-bridge is and how it works, you won't really understand how to implement them.

You can also build an h-bridge with a couple of relays. Just do some research on h-bridge motor control - you'll find plenty of examples on the internet. BTW - it is possible to build an h-bridge using only NPN transistors, but it is generally better to use complementary transistor pairs of NPN and PNP. Also note that you may run across something called a "half h-bridge", which, if designed properly (and using a dual-ended power supply), can allow you to control the direction of a motor using only two transistors, instead of four. This used to be advantageous from a cost standpoint; the old Milton Bradley Big Trak used such a configuration for its drive motors. Nowadays, these transistors are dirt cheap, but its still something to keep in mind if space is tight on a board and you have a dual-ended power supply available...

Alternatively, you can buy small h-bridge driver ICs like the L293 and the L298 that make h-bridge implementation very easy (note: the L298 will -not- plug into a breadboard or standard 0.1" hole spacing PCB - but there are adapters available).

Good luck!

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

Go Up