simple LINE FOLLOWER

my line follower has 3 LDR for sensors and 3 LED... this is my code..

int L=0; int R=0; int C=0;

void setup(){ pinMode(6,INPUT); pinMode(7,INPUT); pinMode(8,INPUT); pinMode(9,OUTPUT); pinMode(10,OUTPUT); pinMode(11,OUTPUT); pinMode(44,OUTPUT); pinMode(45,OUTPUT);}

void loop(){ C=digitalRead(6); L=digitalRead(7); R=digitalRead(8); digitalWrite(9,HIGH); digitalWrite(10,HIGH); digitalWrite(11,HIGH);

if(L==HIGH){ if(C==LOW){ if(R==HIGH){ digitalWrite(45,HIGH); digitalWrite(44,HIGH);}} } if(L==LOW){ if(C==LOW){ if(R==LOW){ digitalWrite(45,HIGH); digitalWrite(44,HIGH);}} } if(L==HIGH){ if(R==HIGH){ if(C==HIGH){ digitalWrite(45,LOW); digitalWrite(44,HIGH);}} } if(L==LOW){ if(C==LOW){ if(R==HIGH){ digitalWrite(44,HIGH); digitalWrite(45,LOW);}} } if(L==HIGH){ if(C==LOW){ if(R==LOW){ digitalWrite(44,LOW); digitalWrite(45,HIGH);}} } if(L==LOW){ if(C==HIGH){ if(R==HIGH){ digitalWrite(44,HIGH); digitalWrite(45,LOW);}} } if(L==HIGH){ if(C==HIGH){ if(R==LOW){ digitalWrite(44,LOW); digitalWrite(45,HIGH);}} } }

can someone suggests to make it better/ SMOOTHER.. please please..

You could make your code a lot simpler with an eight entry lookup table, where each entry gives the state of pins 44 and 45, and indexed by the value given by packing the values of the sensor pins into a single 3 bit value - the whole of "loop() reduces to three lines of code. Won't make it any smoother though.

You could move the code for writing to pins 9 10 and 11 into "setup()"

Naming the pins would make it a lot easier to read and debug.

And code tags; we really like code tags.

What code tags?

When you post your code put it between ... tags. You can do that by hitting the # button above the posting area.

In the IDE you can load example sketches. People new to C/C++ programming are especially served by the examples in 5. Control Structures as they explain how to group variables and easily work with those groups. You already know about if() but not else to go with if() or arrays. Those examples are basic tutorials that will help you envision and code simpler processes. You will be better.

Another thing is to learn about digital bits that make the bytes and manipulating those. LOW and HIGH can be represented by bits. L, C and R can turn into 3 bits of a byte variable (or int but byte is enough) as bits 0, 1, 2. Bit 0 is 0 or 1, bit 1 is 0 or 2, bit 2 is 0 or 4.

So I make a global variable,

byte sensors;

and I set the value bits from pin reads

//  C=digitalRead(6); // ---- this is what you have
//  L=digitalRead(7);
//  R=digitalRead(8);

// I made the bit order in byte sensors match the order of your if() checks below
  bitWrite( sensors, 1, digitalRead( 6 )); 
  bitWrite( sensors, 0, digitalRead( 7 )); 
  bitWrite( sensors, 2, digitalRead( 8 ));
 if(L==HIGH){
   if(C==LOW){
     if(R==HIGH){

Is same as sensors == 5

 if(L==LOW){
   if(C==LOW){
     if(R==LOW){

Is same as sensors == 0

  if(L==HIGH){
   if(R==HIGH){
     if(C==HIGH){

Is same as sensors == 7

  if(L==LOW){
    if(C==LOW){
      if(R==HIGH){

Is same as sensors == 4

   if(L==HIGH){
    if(C==LOW){
      if(R==LOW){

Is same as sensors == 1

  if(L==LOW){
    if(C==HIGH){
      if(R==HIGH){

Is same as sensors == 6

   if(L==HIGH){
    if(C==HIGH){
      if(R==LOW){

Is same as sensors == 3

The sensors value can be checked and acted upon with a single switch-case statement.

switch ( sensors )
{
    case 0 :
    digitalWrite(45,HIGH);
    digitalWrite(44,HIGH);
    break;

    case 1 :
    digitalWrite(44,LOW);
    digitalWrite(45,HIGH);
    break;

//  you do the rest but do the example first so you are familiar
//  not every number has to have a case 
//  but if ever the center is HIGH and the others are LOW, sensors will be 2
}

So I make a global variable,

Why?

thank you so much. :) i guess i have to learn more :)

AWOL:

So I make a global variable,

Why?

I could say it’s because I don’t want to explain the static keyword along with the rest (I also avoided using | and << through bitWrite) but simply in small code I use globals to keep them defined up top where they’re easy to find.

jeckjeck25: thank you so much. :) i guess i have to learn more :)

And you will get better and find solutions easier with each step. XD This our hobby home and yours too. We want to see you succeed well, I think you will.

But it doesn't need to be static or global - it'll change next time around "loop()", so it may as well be a local (automatic) and I can see no reason for persistence.

DOH!

hello everybody I found a very good site for robotics and arduino http://gbotics.weebly.com/arduino-projects.html