Pages: [1]   Go Down
Author Topic: help with robot  (Read 504 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 1
Posts: 37
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26627
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

0
Offline Offline
Newbie
*
Karma: 1
Posts: 37
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26627
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: January 18, 2011, 05:29:08 am by AWOL » Logged

"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.

Pages: [1]   Go Up
Jump to: