Hi,
I am planning to build a robot that has two LDR sensors and two Motors. The principle is simple: if it detects a sufficient amount of light in the left sensor then it turns left, if it detects enough on the right then it turns to the right.
In order to control the motors I am using a Motor Shield from LADYADA (http://www.ladyada/make/mshield).
I am having some problems with getting the syntax right. Here is what I came up with:
#include <AFMotor.h>
//inputs
int leftLDR=A0; //left LDR
int rightLDR=A1; //right LDR
//outputs
AF_DCMotor leftmotor(1, MOTOR12_64KHZ); //Shield code for running the left motor
AF_DCMotor rightmotor(2, MOTOR12_64KHZ); //shield code for running the right motor
//processing
int leftLDRchache=0; //variable storing value of left LDR
int rightLDRchache=0; //variable storing value of right LDR
int light=2; //the value controlling LDR sensitivity
void setup(){
Serial.begin(9600);
Serial.println("JIMMY");
leftmotor.setSpeed(200); //speed setup for the board
rightmotor.setSpeed(200); //speed setup for the board
}
void loop()
{
leftLDRchache=analogRead(A0); //read the value from left LDR
rightLDRchache=analogRead(A1); //read the value from right LDR
//Run the right motor if there is light on the LEFT
rightmotor.run(FORWARD), leftLDRchache/light;
//Run the left motor if there is light on the RIGHT
leftmotor.run(FORWARD), rightLDRchache/light;
}
I am not sure if I did this correctly, mainly this section:
//Run the right motor if there is light on the LEFT
rightmotor.run(FORWARD), leftLDRchache/light;
//Run the left motor if there is light on the RIGHT
leftmotor.run(FORWARD), rightLDRchache/light;
//JIMMY Program with Motor Board from LADYADA
//Sketch by Jakub Niemiec
//Property of Niemtec
//----------------------------------------------------------------
#include <AFMotor.h>
//inputs
int leftLDR=A1;
int rightLDR=A2;
//outputs
AF_DCMotor leftmotor(1, MOTOR12_64KHZ);
AF_DCMotor rightmotor(2, MOTOR12_64KHZ);
//processing
int leftLDRchache=0; //variable storing value of left LDR
int rightLDRchache=0; //variable storing value of right LDR
int light=330; //the value controlling LDR sensitivity
int threshold=330;
void setup(){
Serial.begin(9600);
Serial.println("JIMMY");
}
void loop()
{
leftLDRchache=analogRead(A1); //read the value from left LDR
rightLDRchache=analogRead(A2); //read the value from right LDR
if(leftLDRchache > threshold){
rightmotor.run(FORWARD);
}else{
rightmotor.run(FORWARD);
leftmotor.run(FORWARD);
}
if(rightLDRchache > threshold){
leftmotor.run(FORWARD);
}else{
rightmotor.run(FORWARD);
leftmotor.run(FORWARD);
}
}
But for some reason it does not work, I don't know why. When I use the digital log to test the LDRs using the serial monitor I can see that the value changes when light is applied.
The value for when there is no light: 318
The value for when there is light (above): 330
Any ideas why it does not work?
The robot does not move at all.