programming color sensor and motor together, help

Right now I'm making a color trail tracking robot with different colors. i made my own color sensor using this tutorial http://letsmakerobots.com/node/23768 . The problem now is how to program it so the output of the color sensor will make it move forward. like when it sees blue go forward or when it sees yellow.

Can you provide any code?

Here is the code for color sensor. Ill be using a mod kit shield and not sure how to program the motor to output with these values. An example is if it reads red to go strait, or yellow to turn right.

//Pins
int photoResistor = 0;

int b = 2;
int r = 3;
int g = 4;
int y = 5;

//Calibration
// AKA Terrible software solution to a hardware problem....
double bAdj = 1.0;
double rAdj = 1.02;
double gAdj = 1.0;
double yAdj = 1.17;
double ambientAdj = 1.0;

void setup()
{
  pinMode(g, OUTPUT);
  pinMode(y, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(r, OUTPUT);
  pinMode(photoResistor, OUTPUT);
  Serial.begin(9600);
}

void loop()
{  
    allOff();
    delay(100);
    asses(100,100);
}

void allOff()
{
    digitalWrite(g, LOW);
    digitalWrite(y, LOW);
    digitalWrite(r, LOW);
    digitalWrite(b, LOW);
}

void allOn()
{
    digitalWrite(g, HIGH);
    digitalWrite(y, HIGH);
    digitalWrite(r, HIGH);
    digitalWrite(b, HIGH);
}

void pulse(int colour)
{
   for(int i=0;i<3;i++)
   {
      digitalWrite(colour, HIGH);
      delay(100);
      digitalWrite(colour, LOW);
      delay(100);
   }
}
  

void asses(int colldownTime, int saturationTime)
{
  
    delay(500);
    double ambient = analogRead(photoResistor);
    Serial.print("Ambient: ");
    Serial.println(ambient*ambientAdj);
    
    digitalWrite(g, HIGH);
    delay(saturationTime);
    double green = analogRead(photoResistor);
    Serial.print("Green: ");
    Serial.println(green*gAdj);
    digitalWrite(g, LOW);
    delay(colldownTime);
    
    digitalWrite(r, HIGH);
    delay(saturationTime);
    double red = analogRead(photoResistor);
    Serial.print("Red: ");
    Serial.println(red*rAdj);
    digitalWrite(r, LOW);
    delay(colldownTime);
    
    digitalWrite(b, HIGH);
    delay(saturationTime);
    double blue = analogRead(photoResistor);
    Serial.print("Blue: ");
    Serial.println(blue*bAdj);
    digitalWrite(b, LOW);
    delay(colldownTime);
    
    digitalWrite(y, HIGH);
    delay(saturationTime); 
    double yellow = analogRead(photoResistor);
    Serial.print("Yellow: ");
    Serial.println(yellow*yAdj);
    digitalWrite(y, LOW);
    delay(colldownTime);
    
    double rawData[] = {(green*gAdj), (red*rAdj), (blue*bAdj), (yellow*yAdj)};
    double maximum = ambient*ambientAdj;
    int decision;
    
    for (int i=0; i<4; i++)  
    {
       if (maximum<rawData[i])
      { 
          maximum = rawData[i];
          decision = i+1;
      }
    }
         
    Serial.println("");
    
    if(decision == 0)
    {
       Serial.println("No object detected.");
       allOn();
       delay(1000);
       allOff();
    }
    else if(decision == 1)
    {
       Serial.println("I think its green.");
       pulse(g);
    }
    else if(decision == 2)
    {
       Serial.println("Looks red to me.");
       pulse(r);
    } 
    else if(decision == 3)
    {
       Serial.println("Blue for sure!");
       pulse(b);
    }
    else if(decision == 4)
    {
       Serial.println("Booring old yellow.");
       pulse(y);
    }
    Serial.println("");

}
else if(decision == 4)
    {
       Serial.println("Booring old yellow.");
       pulse(y);
      // add code here to control your motors in the appropriate way when yellow is seen
    }

Why is your photoresistor an output and not an input?

I'm not sure why it's set to output, but the sensor works. I'm a complete beginner in programming. Thanks Grumpy_Mike , now i'll search around on how to program for controlling motors

here is the program i have so far. It is intended to turn the motor on when the color sensor read yellow.

int photoResistor = 0;

int b = 2;
int r = 3;
int g = 4;
int y = 5;

[color=green]int motorPin1 = 6;
int motorPin2 = 7[/color];// H-bridge enable pin
//Calibration
// AKA Terrible software solution to a hardware problem....
double bAdj = 1.0;
double rAdj = 1.02;
double gAdj = 1.0;
double yAdj = 1.17;
double ambientAdj = 1.0;

void setup()
{
 

    // set all the other pins you're using as outputs:
[color=green]pinMode(motorPin2, OUTPUT); 
pinMode(motorPin1, OUTPUT); [/color]



    // set enablePin high so that motor can turn on:
    

  pinMode(g, OUTPUT);
  pinMode(y, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(r, OUTPUT);
  pinMode(photoResistor, OUTPUT);
  Serial.begin(9600);
}

void loop()
{  

    
    allOff();
    delay(100);
    asses(100,100);
}

void allOff()
{
   [color=green] digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, LOW);[/color]
    digitalWrite(g, LOW);
    digitalWrite(y, LOW);
    digitalWrite(r, LOW);
    digitalWrite(b, LOW);
}

void allOn()
{
    [color=green]digitalWrite(motorPin1, HIGH);
    digitalWrite(motorPin2, HIGH);[/color]
  
    digitalWrite(g, HIGH);
    digitalWrite(y, HIGH);
    digitalWrite(r, HIGH);
    digitalWrite(b, HIGH);
}

void pulse(int colour)
{
   for(int i=0;i<3;i++)
   {
      digitalWrite(colour, HIGH);
      delay(100);
      digitalWrite(colour, LOW);
      delay(100);
   }
}
  
[color=green]void motorControlLoop()
{
  digitalWrite (motorPin1,HIGH);
  analogWrite(3,255);
  Serial.println("it works");[/color]
}
void asses(int colldownTime, int saturationTime)
{
  
    delay(500);
    double ambient = analogRead(photoResistor);
    Serial.print("Ambient: ");
    Serial.println(ambient*ambientAdj);
    
    digitalWrite(g, HIGH);
    delay(saturationTime);
    double green = analogRead(photoResistor);
    Serial.print("Green: ");
    Serial.println(green*gAdj);
    digitalWrite(g, LOW);
    delay(colldownTime);
    
    digitalWrite(r, HIGH);
    delay(saturationTime);
    double red = analogRead(photoResistor);
    Serial.print("Red: ");
    Serial.println(red*rAdj);
    digitalWrite(r, LOW);
    delay(colldownTime);
    
    digitalWrite(b, HIGH);
    delay(saturationTime);
    double blue = analogRead(photoResistor);
    Serial.print("Blue: ");
    Serial.println(blue*bAdj);
    digitalWrite(b, LOW);
    delay(colldownTime);
    
    digitalWrite(y, HIGH);
    delay(saturationTime); 
    double yellow = analogRead(photoResistor);
    Serial.print("Yellow: ");
    Serial.println(yellow*yAdj);
    digitalWrite(y, LOW);
    delay(colldownTime);
    
    double rawData[] = {(green*gAdj), (red*rAdj), (blue*bAdj), (yellow*yAdj)};
    double maximum = ambient*ambientAdj;
    int decision;
    
    for (int i=0; i<4; i++)  
    {
       if (maximum<rawData[i])
      { 
          maximum = rawData[i];
          decision = i+1;
      }
    }
         
    Serial.println("");
    
    if(decision == 0)
    {
       Serial.println("No object detected.");
       allOn();
       delay(1000);
       allOff();
    }
    else if(decision == 1)
    {
       Serial.println("I think its green.");
       pulse(g);
    }
    else if(decision == 2)
    {
       Serial.println("Looks red to me.");
       pulse(r);
      

   
 
 
     } 
    else if(decision == 3)
    {
       Serial.println("Blue for sure!");
       pulse(b);
    }
    else if(decision == 4)
    {
    [color=green]   Serial.println("Booring old yellow.");
       pulse(y);
    
      motorControlLoop();  [/color]
}
    Serial.println("");

}

I can't see the definition for the motorControlLoop function. Also do not use the format for internet option in the IDE when posting code because you get all that [ color = green ] stuff that prevents anyone else from copying and pasting your code to try it.

You need to understand how to turn your motors on an off. You can only do this if you know what pins control your motors.

Srry my bad. I assigned them to pin 6 and 7. I defined motorControlLoop to turn motorPin1 HIGH. Then setting the speed. Well, that is what I was trying to do.

int photoResistor = 0;

int b = 2;
int r = 3;
int g = 4;
int y = 5;

int motorPin1 = 6;
int motorPin2 = 7;// H-bridge enable pin
//Calibration
// AKA Terrible software solution to a hardware problem....
double bAdj = 1.0;
double rAdj = 1.02;
double gAdj = 1.0;
double yAdj = 1.17;
double ambientAdj = 1.0;

void setup()
{
 

    // set all the other pins you're using as outputs:
pinMode(motorPin2, OUTPUT); 
pinMode(motorPin1, OUTPUT); 



    

  pinMode(g, OUTPUT);
  pinMode(y, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(r, OUTPUT);
  pinMode(photoResistor, OUTPUT);
  Serial.begin(9600);
}

void loop()
{  

    
    allOff();
    delay(100);
    asses(100,100);
}

void allOff()
{
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, LOW);
    
    digitalWrite(g, LOW);
    digitalWrite(y, LOW);
    digitalWrite(r, LOW);
    digitalWrite(b, LOW);
}

void allOn()
{
    digitalWrite(motorPin1, HIGH);
    digitalWrite(motorPin2, HIGH);
  
    digitalWrite(g, HIGH);
    digitalWrite(y, HIGH);
    digitalWrite(r, HIGH);
    digitalWrite(b, HIGH);
}

void pulse(int colour)
{
   for(int i=0;i<3;i++)
   {
      digitalWrite(colour, HIGH);
      delay(100);
      digitalWrite(colour, LOW);
      delay(100);
   }
}
  
void motorControlLoop()
{
  digitalWrite (motorPin1,HIGH);
  analogWrite(3,255);
  Serial.println("it works");
}
void asses(int colldownTime, int saturationTime)
{
  
    delay(500);
    double ambient = analogRead(photoResistor);
    Serial.print("Ambient: ");
    Serial.println(ambient*ambientAdj);
    
    digitalWrite(g, HIGH);
    delay(saturationTime);
    double green = analogRead(photoResistor);
    Serial.print("Green: ");
    Serial.println(green*gAdj);
    digitalWrite(g, LOW);
    delay(colldownTime);
    
    digitalWrite(r, HIGH);
    delay(saturationTime);
    double red = analogRead(photoResistor);
    Serial.print("Red: ");
    Serial.println(red*rAdj);
    digitalWrite(r, LOW);
    delay(colldownTime);
    
    digitalWrite(b, HIGH);
    delay(saturationTime);
    double blue = analogRead(photoResistor);
    Serial.print("Blue: ");
    Serial.println(blue*bAdj);
    digitalWrite(b, LOW);
    delay(colldownTime);
    
    digitalWrite(y, HIGH);
    delay(saturationTime); 
    double yellow = analogRead(photoResistor);
    Serial.print("Yellow: ");
    Serial.println(yellow*yAdj);
    digitalWrite(y, LOW);
    delay(colldownTime);
    
    double rawData[] = {(green*gAdj), (red*rAdj), (blue*bAdj), (yellow*yAdj)};
    double maximum = ambient*ambientAdj;
    int decision;
    
    for (int i=0; i<4; i++)  
    {
       if (maximum<rawData[i])
      { 
          maximum = rawData[i];
          decision = i+1;
      }
    }
         
    Serial.println("");
    
    if(decision == 0)
    {
       Serial.println("No object detected.");
       allOn();
       delay(1000);
       allOff();
    }
    else if(decision == 1)
    {
       Serial.println("I think its green.");
       pulse(g);
    }
    else if(decision == 2)
    {
       Serial.println("Looks red to me.");
       pulse(r);
      

   
 
 
     } 
    else if(decision == 3)
    {
       Serial.println("Blue for sure!");
       pulse(b);
    }
    else if(decision == 4)
    {
       Serial.println("Booring old yellow.");
       pulse(y);
    
      motorControlLoop();  
}
    Serial.println("");

}

So what happens when you run the code?

i haven't actually been able to test it because I have an arduino uno only and read I needed a motor shield, right now I only have a modkit shield and I am trying to connect it with arduino so I can run motors also. I'm having my peer set that up, but that's getting lost ans more complicated.

and read I needed a motor shield

Well either that or some sort of driver, have a look at these pages for ways of driving a motor:- http://www.thebox.myzen.co.uk/Workshop/Motors_1.html

Before I made my motor driver, I used LEDs to test my motor outputs, and it worked great. The reason this works, is because your seeing if the motor will respond to the command you give it. Now because the brightness can be controlled by the analogWrite function, it will substitute as a motor. Give it a try. If you need to see if it goes in reverse, just flip the LED.

(note: use both motor pins to see if the Arduino outputs the correct commands.)

Thx, ill try that tomorrow, i leave everything at work because it is part of m job.

i decided to go with an h-bridge and setup dc motor reversing, but having trouble with that at the moment.

Well, when you've resolved it, be sure to post the solution.

okay so I was able to implement the code for the color sensor and motor together. I’m using an H-bridge by the way. without the sensor actually connected to the arduino it will read yellow as an output, so I sued that factor to test it. the motor worked. But when i connected the color sensor it stopped working, I’m going to check some more to see if I messed up the wiring, but that is where i am so far.

int photoResistor = 0;

int b = 2;
int r = 3;
int g = 4;
int y = 5;

int enablePin = 11;
int in1Pin = 10;
int in2Pin = 9;
//Calibration
// AKA Terrible software solution to a hardware problem....
double bAdj = 1.0;
double rAdj = 1.02;
double gAdj = 1.0;
double yAdj = 1.17;
double ambientAdj = 1.0;

void setup()
{
   pinMode(in1Pin, OUTPUT);
  pinMode(in2Pin, OUTPUT);
  pinMode(enablePin, OUTPUT);
  
  pinMode(g, OUTPUT);
  pinMode(y, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(r, OUTPUT);
  pinMode(photoResistor, OUTPUT);
  Serial.begin(9600);
}

void loop()
{  
   boolean reverse = false;
    int speed = 250;
    setMotor(speed, reverse);
    allOff();
    delay(100);
    asses(100,100);
}

void allOff()
{
    digitalWrite(g, LOW);
    digitalWrite(y, LOW);
    digitalWrite(r, LOW);
    digitalWrite(b, LOW);
}
void setMotor(int speed, boolean reverse)
{
  analogWrite(enablePin, speed);
  digitalWrite(in1Pin, ! reverse);
  digitalWrite(in2Pin, reverse);
}
void allOn()
{
    digitalWrite(g, HIGH);
    digitalWrite(y, HIGH);
    digitalWrite(r, HIGH);
    digitalWrite(b, HIGH);
}

void pulse(int colour)
{
   for(int i=0;i<3;i++)
   {
      digitalWrite(colour, HIGH);
      delay(100);
      digitalWrite(colour, LOW);
      delay(100);
   }
}
  

void asses(int colldownTime, int saturationTime)
{
  
    delay(500);
    double ambient = analogRead(photoResistor);
    Serial.print("Ambient: ");
    Serial.println(ambient*ambientAdj);
    
    digitalWrite(g, HIGH);
    delay(saturationTime);
    double green = analogRead(photoResistor);
    Serial.print("Green: ");
    Serial.println(green*gAdj);
    digitalWrite(g, LOW);
    delay(colldownTime);
    
    digitalWrite(r, HIGH);
    delay(saturationTime);
    double red = analogRead(photoResistor);
    Serial.print("Red: ");
    Serial.println(red*rAdj);
    digitalWrite(r, LOW);
    delay(colldownTime);
    
    digitalWrite(b, HIGH);
    delay(saturationTime);
    double blue = analogRead(photoResistor);
    Serial.print("Blue: ");
    Serial.println(blue*bAdj);
    digitalWrite(b, LOW);
    delay(colldownTime);
    
    digitalWrite(y, HIGH);
    delay(saturationTime); 
    double yellow = analogRead(photoResistor);
    Serial.print("Yellow: ");
    Serial.println(yellow*yAdj);
    digitalWrite(y, LOW);
    delay(colldownTime);
    
    double rawData[] = {(green*gAdj), (red*rAdj), (blue*bAdj), (yellow*yAdj)};
    double maximum = ambient*ambientAdj;
    int decision;
    
    for (int i=0; i<4; i++)  
    {
       if (maximum<rawData[i])
      { 
          maximum = rawData[i];
          decision = i+1;
      }
    }
         
    Serial.println("");
    
    if(decision == 0)
    {
       Serial.println("No object detected.");
       allOn();
       delay(1000);
       allOff();
    }
    else if(decision == 1)
    {
       Serial.println("I think its green.");
       pulse(g);
    }
    else if(decision == 2)
    {
       Serial.println("Looks red to me.");
       pulse(r);
      boolean reverse = true; 
  } 
    
    else if(decision == 3)
    {
       Serial.println("Blue for sure!");
       pulse(b);
    }
    else if(decision == 4)
    {
       Serial.println("Booring old yellow.");
      
       pulse(y);
      boolean reverse = false;  
  }
    Serial.println("");

}
pinMode(photoResistor, OUTPUT);

Again - why?