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);
}*_