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