programming check plezzz... urgent.!

Below is my coding for arduino... i used dc motor with encoder and clor sensor together... the problem is, after i combine two programming... i did not get the correct result... one motor cannot move and encoder cannot read.. here... the combine of the codes...

#include <TimerOne.h>

#define InB1 12 // INB motor1 pin
#define PWM1 11 // PWM motor1 pin
#define InB2 13 // INB motor2 pin
#define PWM2 10 // PWM motor2 pin
#define encodPinA1 9 // encoder motor 1 channel A pin
#define encodPinB1 8 // encoder motor 1 channel B pin
#define S0 6
#define S1 5
#define S2 4
#define S3 3
#define OUT 7
#define LOOPTIME 10 // PID loop time
#define FORWARD 1 // direction of rotation
#define BACKWARD 2 // direction of rotation

unsigned long lastMilli = 0; // loop timing
unsigned long lastMilliPrint = 0; // loop timing
long count = 0; // rotation counter
long countInit;
long tickNumber = 0;
boolean run = false; // motor moves

int g_array[3] = {0,1,2};
int g_flag=0;
int g_count=0;
int g_SF[3]={2};
// motor moves

void setup()
{
// TSC_Init();
Serial.begin(9600);
Timer1.initialize(); // defaulte is 1s
Timer1.attachInterrupt(TSC_Callback);
attachInterrupt(0, TSC_Count, RISING);
delay(4000);
for(int i=0; i<3; i++)
Serial.println(g_array*);*

  • g_SF[0] = 255.0/ g_array[0]; //R Scale factor*
  • g_SF[1] = 255.0/ g_array[1] ; //G Scale factor*
  • g_SF[2] = 255.0/ g_array[2] ; //B Scale factor*
  • Serial.println(g_SF[0]);*
  • Serial.println(g_SF[1]);*
  • Serial.println(g_SF[2]);*
  • pinMode(InB1, OUTPUT);*
  • pinMode(PWM1, OUTPUT);*
  • pinMode(InB2, OUTPUT);*
  • pinMode(PWM2, OUTPUT);*
  • pinMode(encodPinA1, INPUT);*
  • pinMode(encodPinB1, INPUT);*
  • digitalWrite(encodPinA1, HIGH); // turn on pullup resistor*
  • digitalWrite(encodPinB1, HIGH);*
  • attachInterrupt(1, rencoder, FALLING);*
    }
    void loop()
    {
  • moveMotor(FORWARD, 250, 30); // direction, PWM, ticks number*
  • delay(20000);*
  • //moveMotor(FORWARD, 250, 30); // direction, PWM, ticks number*
  • //delay(20000);*
  • //moveMotor(FORWARD, 250, 30000); // direction, PWM, ticks number*
  • //delay(20000);*
  • //motorBrake();*
  • //delay(30000);*
  • //sensor();*
  • //delay(1000);*
    }
    void TSC_Init()
    {
  • pinMode(S0, OUTPUT);*
  • pinMode(S1, OUTPUT);*
  • pinMode(S2, OUTPUT);*
  • pinMode(S3, OUTPUT);*
  • pinMode(OUT, INPUT);*
  • digitalWrite(S0, LOW); // OUTPUT FREQUENCY SCALING 2%*
  • digitalWrite(S1, HIGH);*
    }
    *void moveMotor(int direction, int PWM_val, long tick) *
    {
  • countInit = count; // abs(count)*
  • tickNumber = tick;*
    *if(direction==FORWARD) *
  • motorForward(PWM_val);*
    *else if(direction==BACKWARD) *
  • motorBackward(PWM_val);*
    }
    *void rencoder() *
    { // pulse and direction, direct port reading to save cycles
  • if (encodPinB1 & 0b00000001) *
  • count++; // if(digitalRead(encodPinB1)==HIGH) count_r ++;*
  • else *
  • count--; // if (digitalRead(encodPinB1)==LOW) count_r --;*
  • if(run) *
  • if((abs(abs(count)-abs(countInit))) >= tickNumber) *
  • motorBrake();*
    }
    *void motorForward(int PWM_val) *
    {
  • analogWrite(PWM1, PWM_val);*
  • digitalWrite(InB1, HIGH);*
  • analogWrite(PWM2, PWM_val);*
  • digitalWrite(InB2, HIGH);*
  • run=true;*
    }
    *void motorBackward(int PWM_val) *
    {
  • analogWrite(PWM1, PWM_val);*
  • digitalWrite(InB1, LOW);*
  • analogWrite(PWM2, PWM_val);*
  • digitalWrite(InB2, LOW);*
  • run=true;*
    }
    *void motorBrake() *
    {
  • analogWrite(PWM1, 0);*
  • digitalWrite(InB1, LOW);*
  • analogWrite(PWM2, 0);*
  • digitalWrite(InB2, LOW);*
  • run=false;*
    }
    // Select the filter color
    void TSC_FilterColor(int Level01, int Level02)
    {
  • if(Level01 != 0)*
  • Level01 = HIGH;*
  • if(Level02 != 0)*
  • Level02 = HIGH;*
  • digitalWrite(S2, Level01);*
  • digitalWrite(S3, Level02);*
    }
    void TSC_Count()
    {
  • g_count ++ ;*
    }
    void TSC_Callback()
    {
  • switch(g_flag)*
  • {*
  • case 0:*
  • Serial.println("->WB Start");*
  • TSC_WB(LOW, LOW); //Filter without Red*
  • break;*
  • case 1:*
  • Serial.print("->Frequency R=");*
  • Serial.println(g_count);*
  • g_array[0] = g_count;*
  • TSC_WB(HIGH, HIGH); //Filter without Green*
  • break;*
  • case 2:*
  • Serial.print("->Frequency B=");*
  • Serial.println(g_count);*
  • g_array[1] = g_count;*
  • TSC_WB(LOW, HIGH); //Filter without Blue*
  • break;*
  • case 3:*
  • Serial.print("->Frequency G=");*
  • Serial.println(g_count);*
  • Serial.println("->WB End");*
  • g_array[2] = g_count;*
  • TSC_WB(HIGH, LOW); //Clear(no filter)*
  • break;*
  • default:*
  • g_count = 0;*
  • break;*
  • }*
    }
    void TSC_WB(int Level0, int Level1) //White Balance
    {
  • g_count = 0;*
  • g_flag ++;*
  • TSC_FilterColor(Level0, Level1);*
  • Timer1.setPeriod(1000000); // set 1s period*
    }
    void sensor()
    {
  • g_flag = 0;*
  • for(int i=0; i<3; i++)*
    Serial.println(int(g_array * g_SF*));
    _ delay(4000);
    }*_

This is the codes only for motor with encoder of the codes....

#define InB1 12 // INB motor1 pin
#define PWM1 11 // PWM motor1 pin
#define InB2 13 // INB motor2 pin
#define PWM2 10 // PWM motor2 pin
#define encodPinA1 9 // encoder motor 1 channel A pin
#define encodPinB1 8 // encoder motor 1 channel B pin
#define LOOPTIME 100 // PID loop time
#define FORWARD 1 // direction of rotation
#define BACKWARD 2 // direction of rotation

unsigned long lastMilli = 0; // loop timing
unsigned long lastMilliPrint = 0; // loop timing
long count = 0; // rotation counter
long countInit;
long tickNumber = 0;
boolean run = false; // motor moves

void setup()
{
pinMode(InB1, OUTPUT);
pinMode(PWM1, OUTPUT);
pinMode(InB2, OUTPUT);
pinMode(PWM2, OUTPUT);
pinMode(encodPinA1, INPUT);
pinMode(encodPinB1, INPUT);

digitalWrite(encodPinA1, HIGH); // turn on pullup resistor
digitalWrite(encodPinB1, HIGH);

attachInterrupt(1, rencoder, FALLING);
}

void loop()
{
moveMotor(FORWARD, 250, 30000); // direction, PWM, ticks number
delay(20000);
moveMotor(FORWARD, 250, 30000); // direction, PWM, ticks number
delay(20000);
moveMotor(FORWARD, 250, 30000); // direction, PWM, ticks number
delay(20000);
motorBrake();
delay(30000);
}

void moveMotor(int direction, int PWM_val, long tick)
{
countInit = count; // abs(count)
tickNumber = tick;
if(direction==FORWARD)
motorForward(PWM_val);
else if(direction==BACKWARD)
motorBackward(PWM_val);
}

void rencoder()
{ // pulse and direction, direct port reading to save cycles
if (encodPinB1 & 0b00000001)
count++; // if(digitalRead(encodPinB1)==HIGH) count_r ++;
else
count--; // if (digitalRead(encodPinB1)==LOW) count_r --;
if(run)
if((abs(abs(count)-abs(countInit))) >= tickNumber)
motorBrake();

}

void motorForward(int PWM_val)
{
analogWrite(PWM1, PWM_val);
digitalWrite(InB1, HIGH);
analogWrite(PWM2, PWM_val);
digitalWrite(InB2, HIGH);
run=true;
}

void motorBackward(int PWM_val)
{
analogWrite(PWM1, PWM_val);
digitalWrite(InB1, LOW);
analogWrite(PWM2, PWM_val);
digitalWrite(InB2, LOW);
run=true;
}

void motorBrake()
{
analogWrite(PWM1, 0);
digitalWrite(InB1, LOW);
analogWrite(PWM2, 0);
digitalWrite(InB2, LOW);
run=false;
}

Here is the codes for color sensor:- TSC 230 color sensor...

#include <TimerOne.h>

#define S0 6
#define S1 5
#define S2 4
#define S3 3
#define OUT 7

int g_array[3] = {0,1,2};
int g_flag=0;
int g_count=0;
int g_SF[3]={2};

// Init TSC230 and setting Frequency.
void TSC_Init()
{
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(OUT, INPUT);

digitalWrite(S0, LOW); // OUTPUT FREQUENCY SCALING 2%
digitalWrite(S1, HIGH);
}

// Select the filter color
void TSC_FilterColor(int Level01, int Level02)
{
if(Level01 != 0)
Level01 = HIGH;

if(Level02 != 0)
Level02 = HIGH;

digitalWrite(S2, Level01);
digitalWrite(S3, Level02);
}

void TSC_Count()
{
g_count ++ ;
}

void TSC_Callback()
{
switch(g_flag)
{
case 0:
Serial.println("->WB Start");
TSC_WB(LOW, LOW); //Filter without Red
break;
case 1:
Serial.print("->Frequency R=");
Serial.println(g_count);
g_array[0] = g_count;
TSC_WB(HIGH, HIGH); //Filter without Green
break;
case 2:
Serial.print("->Frequency B=");
Serial.println(g_count);
g_array[1] = g_count;
TSC_WB(LOW, HIGH); //Filter without Blue
break;

case 3:
Serial.print("->Frequency G=");
Serial.println(g_count);
Serial.println("->WB End");
g_array[2] = g_count;
TSC_WB(HIGH, LOW); //Clear(no filter)
break;
default:
g_count = 0;
break;
}
}

void TSC_WB(int Level0, int Level1) //White Balance
{
g_count = 0;
g_flag ++;
TSC_FilterColor(Level0, Level1);
Timer1.setPeriod(1000000); // set 1s period
}

void setup()
{
TSC_Init();
Serial.begin(9600);
Timer1.initialize(); // defaulte is 1s
Timer1.attachInterrupt(TSC_Callback);
attachInterrupt(0, TSC_Count, RISING);

delay(4000);

for(int i=0; i<3; i++)
Serial.println(g_array*);*

  • g_SF[0] = 255.0/ g_array[0]; //R Scale factor*
  • g_SF[1] = 255.0/ g_array[1] ; //G Scale factor*
  • g_SF[2] = 255.0/ g_array[2] ; //B Scale factor*
  • Serial.println(g_SF[0]);*
  • Serial.println(g_SF[1]);*
  • Serial.println(g_SF[2]);*
    }
    void loop()
    {
  • g_flag = 0;*
  • for(int i=0; i<3; i++)*
    Serial.println(int(g_array * g_SF*));
    _ delay(4000);
    }*
    ANYONE CAN HELP.!! its urgent.! i need to complete... already try so many times_

http://arduino.cc/forum/index.php/topic,97455.0.html

6. Getting help on the forum

There are quite a few experienced people on the forum anxious to help you, and help you get as much as you can out of your Arduino. You can help them do that by making helpful posts:

  • Make an informative subject description, not "help me, I'm a noob", nor something in all capitals. Try to avoid saying "urgent". That's your problem, not ours.
  • Describe your problem in detail.
  • If it relates to an electronics part (chip or board), give the exact part number and preferably a link to the data sheet.
  • Describe how you have connected things like switches. Are they wired to ground? Or +5V? Are there pull-up or pull-down resistors? Post a circuit if there is doubt.
  • Post your code! If you don't you waste time while people ask you to do that.
  • When you post your code put it between [code] ... [/code] tags. You can do that by hitting the # button above the posting area.
  • If you get an error, post the error (copy and paste). Not just "I got an error".
  • With coding problems, if possible post a "minimal" sketch that demonstrates the problem - not hundreds of lines of code.
  • If you have debugging information in your sketch, post your debugging displays.
  • Describe what you expected to happen, and what actually happened. Not just "it doesn't work".
  • If possible, describe what you are really trying to do, not what you think might work. For example "I am trying to turn on an aquarium heater and pump at the same time", not "how do I break out of an interrupt?".
  • Don't double-post (cross-post). Your question will be noticed. If you post it in multiple places you will just annoy people who might otherwise have answered.

Note the bolded.

If you're thinking we're just being picky asking you to post with code tags, look for the italics in your code in the IDE window.

int g_SF[3]={2};

Why does a 3 element array have one initializer?