Let's Make Robots!

arduino code help

  So I need help with writing code for my light following robot I understood how to make motors move using a l293d and I got them working now but I dont know how to use the LDRs readings to follow light.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

the analog-in pins is were you do this kind of thing most of the time

analogRead()

Description

Reads the value from the specified analog pin. The Arduino board contains a 6 channel (8 channels on the Mini and Nano, 16 on the Mega), 10-bit analog to digital converter. This means that it will map input voltages between 0 and 5 volts into integer values between 0 and 1023. This yields a resolution between readings of: 5 volts / 1024 units or, .0049 volts (4.9 mV) per unit. The input range and resolution can be changed using analogReference().

It takes about 100 microseconds (0.0001 s) to read an analog input, so the maximum reading rate is about 10,000 times a second.

Syntax

analogRead(pin)

Parameters

pin: the number of the analog input pin to read from (0 to 5 on most boards, 0 to 7 on the Mini and Nano, 0 to 15 on the Mega)

Returns

int (0 to 1023)

Note

If the analog input pin is not connected to anything, the value returned by analogRead() will fluctuate based on a number of factors (e.g. the values of the other analog inputs, how close your hand is to the board, etc.).

your after

if left  > right then left 

if right> left then right

if right == left then forward

I got that part down the logic behind how to do it but I have a problem with the programming i dont understand how to tell the arduino to compare the left sensor and right sensor and take the best choice.

if left ldr > right ldr then turn left 

if right ldr > left ldr then turn right

if right Ldr == left Ldr then goforward

it is as simple as asking your self the same qustions  , think of your self in a box with 2 little holes just big enoughf to see if it's bright or dark on the left or right side and you wont to go towards the light

if (LDRL > LDRR){left();           

if(LDRR > LDRL){right();

if (LDRL== LDRR){Forward();

no dought someone will fill in the blanks .at least push you in the right direction

#define motorA 3 //Direction Pin for Left Motor

#define motorB 4 //Direction Pin for Left Motor

#define motorSpeedA 6 //Speed Pin for Left Motor

#define motorSpeedB 5 //Speed Pin for Right Motor

#define motorC 7 //Direction Pin for Right Motor

#define motorD 8 //Direction Pin for Right Motor

#define LDRPin A0

#define LDRPin1 A1

 

 

 

void setup() //here we are telling the Arduino that the motor pins should be outputs (not inputs)

{

  pinMode(motorA, OUTPUT); 

  pinMode(motorB, OUTPUT);

  pinMode(motorC, OUTPUT);

  pinMode(motorD, OUTPUT);

  pinMode(motorSpeedA, OUTPUT);

  pinMode(motorSpeedB, OUTPUT);

  pinMode(LDRPin, INPUT);

  pinMode(LDRPin1,INPUT);

 

    Serial.begin(9600); 

)

 

 

 

}

 

void loop()

 

{

  analogWrite(motorSpeedA,255);

  analogWrite(motorSpeedB,255);

 

 

 

 

 

 

}

 

void forward()

{

  digitalWrite(motorC,LOW);  //forward 

  digitalWrite(motorD,HIGH);

  digitalWrite(motorA,LOW);

  digitalWrite(motorB,HIGH); 

}

 

void backward()

{

  digitalWrite(motorC,HIGH);  //Left spin

  digitalWrite(motorD,LOW);

  digitalWrite(motorA,HIGH);

  digitalWrite(motorB,LOW);  

}

 

void leftSpin()

{

  digitalWrite(motorC,HIGH);  //Left spin

  digitalWrite(motorD,LOW);

  digitalWrite(motorA,LOW);

  digitalWrite(motorB,HIGH); 

}

 

void rightSpin()

{

  digitalWrite(motorC,LOW);  

  digitalWrite(motorD,HIGH);

  digitalWrite(motorA,HIGH);

  digitalWrite(motorB,LOW); 

}

 

 

 

void allStop()

{

  digitalWrite(motorC,LOW);  

  digitalWrite(motorD,LOW);

  digitalWrite(motorA,LOW);

  digitalWrite(motorB,LOW);       

}

LDRL = analogread(LDRPin)
LDRR = analogread(LDRPin1)

if (LDRL > LDRR) {
    leftspin();
}

else if (LDRL < LDRR) {
    rightspin();
}

else if (LDRL = LDRR) AND (LDRL > dark) { //dark being a constant set earlier to make robot phototropic
    forward();
}

else {
     backward();

Thank you birdmun and chuckcrunch is dark supposed to be a const int and the code you gave will the light follower be smooth as in will it follow a flashlight.

your dark level is not needed to get your robot to follow light but it is a good idea , if your going to make it a constant you will need to Debug your values and find the value for dark it's not necessarily going to be 0, depending how you have your LDR set up it could be a high velue for dark and a low value for light and or the other way around , just so your aware 

you could have a small function that sets the dark level to the lowest reading and a set a bright level as well it kinda like learning but not realy, this is some thing i have been working on, is using past values as a kind of learning or referring back to the past to get some more meaning out of now data   

int dark = x; // x being a mid point in your values

int bright = y; // y being a mid point in your values  you do this so the your dark value is not 0 to start with

// put this after your AnalogReads this is just for the left side
    if (LDRL < darkL) {
      darkL = LDRL;
    }
    if (LDRL > brightL) {
      brightL = LDRL;
    }

well that's just one thing you could do or you could set dark as constant and keep it simple to start with 

just putting this out there

#define motorA 3 //Direction Pin for Left Motor

#define motorB 4 //Direction Pin for Left Motor

#define motorSpeedA 6 //Speed Pin for Left Motor

#define motorSpeedB 5 //Speed Pin for Right Motor

#define motorC 7 //Direction Pin for Right Motor

#define motorD 8 //Direction Pin for Right Motor

#define LDRPin A0

#define LDRPin1 A1

 

 

 

 

 

 

int dark;

 

void setup() //here we are telling the Arduino that the motor pins should be outputs (not inputs)

{

  pinMode(motorA, OUTPUT); 

  pinMode(motorB, OUTPUT);

  pinMode(motorC, OUTPUT);

  pinMode(motorD, OUTPUT);

  pinMode(motorSpeedA, OUTPUT);

  pinMode(motorSpeedB, OUTPUT);

  pinMode(LDRPin, INPUT);

  pinMode(LDRPin1,INPUT);

 

 

    Serial.begin(9600);

 

)

 

 

 

}

 

void loop()

 

{

  analogWrite(motorSpeedA,255);

  analogWrite(motorSpeedB,255);

 

 

 LDRL = analogread(LDRPin)

 LDRR = analogread(LDRPin1)

 

if (LDRL > LDRR) {

    leftspin();

}

 

else if (LDRL < LDRR) {

    rightspin();

}

 

else if (LDRL = LDRR) AND (LDRL > dark) { //dark being a constant set earlier to make robot phototropic

    forward();

}

 

else {

     backward();

 

 

 

 

 

 

}

 

void forward()

{

  digitalWrite(motorC,LOW);  //forward 

  digitalWrite(motorD,HIGH);

  digitalWrite(motorA,LOW);

  digitalWrite(motorB,HIGH); 

}

 

void backward()

{

  digitalWrite(motorC,HIGH);  //Left spin

  digitalWrite(motorD,LOW);

  digitalWrite(motorA,HIGH);

  digitalWrite(motorB,LOW);  

}

 

void leftSpin()

{

  digitalWrite(motorC,HIGH);  //Left spin

  digitalWrite(motorD,LOW);

  digitalWrite(motorA,LOW);

  digitalWrite(motorB,HIGH); 

}

 

void rightSpin()

{

  digitalWrite(motorC,LOW);  

  digitalWrite(motorD,HIGH);

  digitalWrite(motorA,HIGH);

  digitalWrite(motorB,LOW); 

}

 

 

 

void allStop()

{

  digitalWrite(motorC,LOW);  

  digitalWrite(motorD,LOW);

  digitalWrite(motorA,LOW);

  digitalWrite(motorB,LOW);       

}

is this right I did not understand what the constant dark is.