Go Down

Topic: help with robot (Read 535 times) previous topic - next topic

mawe

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

http://letsmakerobots.com/node/2164

http://itp.nyu.edu/physcomp/Labs/DCMotorControl

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


Quote:
//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;

//LED
const int ledPin = 13;

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

void setup(){
Serial.begin(9600);
//servo pins
myservo.attach(9);
myservo.write(90);
delay(3000);
//set aoutputs
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(ledPin, OUTPUT);
//blink the led for 3s.
digitalWrite(ledPin, HIGH);
delay(3000);
digitalWrite(ledPin, LOW);
}

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

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

//compare the values and turn servo
//turn rigkt
if (LDRVal1 > LDRVal2){
myservo.write(125);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
delay(3000);
myservo.write(55);
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);}
//turn left
else if (LDRVal1 < LDRVal2){
myservo.write(55);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
delay(3000);
myservo.write(125);
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);}
//move forward
else if (LDRVal1 == LDRVal2){
myservo.write(90);
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);}
}

servo works a little bit but reacts very slow to changes of values


AWOL

It isn't surprising that it reacts slowly, with all those delays in there.
Get rid of them.
For clues, look at the blink without delay example.
"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.

mawe

and why motor doesn't work? but the thing is that when I check only the the part with resistors and servo like in robot here
http://letsmakerobots.com/node/2164
the data from arduino is changing very fast, but with this code it sends data to pc from arduino only every few seconds

AWOL

#3
Jan 18, 2011, 11:24 am Last Edit: Jan 18, 2011, 11:29 am by AWOL Reason: 1
The data will not change any faster until you get rid of the delays.

Indentation would make your code easier to read - this isn't 1970s BASIC.

Why doesn't the motor work?
How should I know?
You are the one with the hardware in front of you.

Please DO NOT cross-post.
"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.

Go Up