TCS3200 ccolour sensor hange to library timer 2?
jeff277
Posts: 2
found out that some word cannot be used in timer2 for arduino..
I need someone help to help me change this from timer 1 to 2
I am trying to make a servos controlled by colour sensor via arduino and found out that it keep on detecting everything i scan using tcs3200 as white colour when i used servo library..without is it works fine...that means there are conflict between library timer 1 and servos library..someone please help me change to library 2 from library 1? help is so much appreciated.
I need someone help to help me change this from timer 1 to 2
I am trying to make a servos controlled by colour sensor via arduino and found out that it keep on detecting everything i scan using tcs3200 as white colour when i used servo library..without is it works fine...that means there are conflict between library timer 1 and servos library..someone please help me change to library 2 from library 1? help is so much appreciated.
[/COLOR][COLOR=#000000][FONT=arial]#include <TimerOne.h>[/FONT][/COLOR] [COLOR=#000000][FONT=arial]#include <Servo.h> // servo library[/FONT][/COLOR] [COLOR=#000000][FONT=arial]#define S0 6[/FONT][/COLOR] [COLOR=#000000][FONT=arial]#define S1 5[/FONT][/COLOR] [COLOR=#000000][FONT=arial]#define S2 4[/FONT][/COLOR] [COLOR=#000000][FONT=arial]#define S3 3[/FONT][/COLOR] [COLOR=#000000][FONT=arial]#define OUT 2[/FONT][/COLOR] [COLOR=#000000][FONT=arial]int g_count = 0; // count the frequecy[/FONT][/COLOR] [COLOR=#000000][FONT=arial]int g_array[3]; // store the RGB value[/FONT][/COLOR] [COLOR=#000000][FONT=arial]int g_flag = 0; // filter of RGB queue[/FONT][/COLOR] [COLOR=#000000][FONT=arial]float g_SF[3]; // save the RGB Scale factor[/FONT][/COLOR] [COLOR=#000000][FONT=arial]Servo servo1; // servo control object[/FONT][/COLOR] [COLOR=#000000][FONT=arial]// Init TSC230 and setting Frequency.[/FONT][/COLOR] [COLOR=#000000][FONT=arial]void TSC_Init()[/FONT][/COLOR] [COLOR=#000000][FONT=arial]{[/FONT][/COLOR] [COLOR=#000000][FONT=arial]pinMode(S0, OUTPUT);[/FONT][/COLOR] [COLOR=#000000][FONT=arial]pinMode(S1, OUTPUT);[/FONT][/COLOR] [COLOR=#000000][FONT=arial]pinMode(S2, OUTPUT);[/FONT][/COLOR] [COLOR=#000000][FONT=arial]pinMode(S3, OUTPUT);[/FONT][/COLOR] [COLOR=#000000][FONT=arial]pinMode(OUT, INPUT);[/FONT][/COLOR] [COLOR=#000000][FONT=arial]digitalWrite(S0, LOW); // OUTPUT FREQUENCY SCALING 2%[/FONT][/COLOR] [COLOR=#000000][FONT=arial]digitalWrite(S1, HIGH); [/FONT][/COLOR] [COLOR=#000000][FONT=arial]}[/FONT][/COLOR] [COLOR=#000000][FONT=arial]// Select the filter color [/FONT][/COLOR] [COLOR=#000000][FONT=arial]void TSC_FilterColor(int Level01, int Level02)[/FONT][/COLOR] [COLOR=#000000][FONT=arial]{[/FONT][/COLOR] [COLOR=#000000][FONT=arial]if(Level01 != 0)[/FONT][/COLOR] [COLOR=#000000][FONT=arial]Level01 = HIGH;[/FONT][/COLOR] [COLOR=#000000][FONT=arial]if(Level02 != 0)[/FONT][/COLOR] [COLOR=#000000][FONT=arial]Level02 = HIGH;[/FONT][/COLOR] [COLOR=#000000][FONT=arial]digitalWrite(S2, Level01); [/FONT][/COLOR] [COLOR=#000000][FONT=arial]digitalWrite(S3, Level02); [/FONT][/COLOR] [COLOR=#000000][FONT=arial]}[/FONT][/COLOR] [COLOR=#000000][FONT=arial]void TSC_Count()[/FONT][/COLOR] [COLOR=#000000][FONT=arial]{[/FONT][/COLOR] [COLOR=#000000][FONT=arial]g_count ++ ;[/FONT][/COLOR] [COLOR=#000000][FONT=arial]}[/FONT][/COLOR] [COLOR=#000000][FONT=arial]void TSC_Callback()[/FONT][/COLOR] [COLOR=#000000][FONT=arial]{[/FONT][/COLOR] [COLOR=#000000][FONT=arial]int position;[/FONT][/COLOR] [COLOR=#000000][FONT=arial]switch(g_flag)[/FONT][/COLOR] [COLOR=#000000][FONT=arial]{[/FONT][/COLOR] [COLOR=#000000][FONT=arial]case 0: [/FONT][/COLOR] [COLOR=#000000][FONT=arial]Serial.println("->WB Start");[/FONT][/COLOR] [COLOR=#000000][FONT=arial]TSC_WB(LOW, LOW); //Filter without Red[/FONT][/COLOR] [COLOR=#000000][FONT=arial]break;[/FONT][/COLOR] [COLOR=#000000][FONT=arial]case 1:[/FONT][/COLOR] [COLOR=#000000][FONT=arial]Serial.print("->Frequency R=");[/FONT][/COLOR] [COLOR=#000000][FONT=arial]Serial.println(g_count);[/FONT][/COLOR] [COLOR=#000000][FONT=arial]g_array[0] = g_count;[/FONT][/COLOR] [COLOR=#000000][FONT=arial]TSC_WB(HIGH, HIGH); //Filter without Green[/FONT][/COLOR] [COLOR=#000000][FONT=arial]servo1.write(60); //Tell servo to go to 90 degrees[/FONT][/COLOR] [COLOR=#000000][FONT=arial]break;[/FONT][/COLOR] [COLOR=#000000][FONT=arial]case 2:[/FONT][/COLOR] [COLOR=#000000][FONT=arial]Serial.print("->Frequency G=");[/FONT][/COLOR] [COLOR=#000000][FONT=arial]Serial.println(g_count);[/FONT][/COLOR] [COLOR=#000000][FONT=arial]g_array[1] = g_count;[/FONT][/COLOR] [COLOR=#000000][FONT=arial]TSC_WB(LOW, HIGH); //Filter without Blue[/FONT][/COLOR] [COLOR=#000000][FONT=arial]servo1.write(90); //Tell servo to go to 90 degrees[/FONT][/COLOR] [COLOR=#000000][FONT=arial]break;[/FONT][/COLOR] [COLOR=#000000][FONT=arial]case 3:[/FONT][/COLOR] [COLOR=#000000][FONT=arial]Serial.print("->Frequency B=");[/FONT][/COLOR] [COLOR=#000000][FONT=arial]Serial.println(g_count);[/FONT][/COLOR] [COLOR=#000000][FONT=arial]Serial.println("->WB End");[/FONT][/COLOR] [COLOR=#000000][FONT=arial]g_array[2] = g_count;[/FONT][/COLOR] [COLOR=#000000][FONT=arial]TSC_WB(HIGH, LOW); //Clear(no filter)[/FONT][/COLOR] [COLOR=#000000][FONT=arial]servo1.write(120); //Tell servo to go to 90 degrees[/FONT][/COLOR] [COLOR=#000000][FONT=arial]break;[/FONT][/COLOR] [COLOR=#000000][FONT=arial]default:[/FONT][/COLOR] [COLOR=#000000][FONT=arial]g_count = 0;[/FONT][/COLOR] [COLOR=#000000][FONT=arial]servo1.write(0); //Tell servo to remain at 0 degrees[/FONT][/COLOR] [COLOR=#000000][FONT=arial]break;[/FONT][/COLOR] [COLOR=#000000][FONT=arial]}[/FONT][/COLOR] [COLOR=#000000][FONT=arial]}[/FONT][/COLOR] [COLOR=#000000][FONT=arial]void TSC_WB(int Level0, int Level1) //White Balance[/FONT][/COLOR] [COLOR=#000000][FONT=arial]{[/FONT][/COLOR] [COLOR=#000000][FONT=arial]g_count = 0;[/FONT][/COLOR] [COLOR=#000000][FONT=arial]g_flag ++;[/FONT][/COLOR] [COLOR=#000000][FONT=arial]TSC_FilterColor(Level0, Level1);[/FONT][/COLOR] [COLOR=#000000][FONT=arial]Timer1.setPeriod(1000000); // set 1s period[/FONT][/COLOR] [COLOR=#000000][FONT=arial]}[/FONT][/COLOR] [COLOR=#000000][FONT=arial]void setup()[/FONT][/COLOR] [COLOR=#000000][FONT=arial]{[/FONT][/COLOR] [COLOR=#000000][FONT=arial]TSC_Init();[/FONT][/COLOR] [COLOR=#000000][FONT=arial]Serial.begin(9600);[/FONT][/COLOR] [COLOR=#000000][FONT=arial]Timer1.initialize(); // defaulte is 1s[/FONT][/COLOR] [COLOR=#000000][FONT=arial]Timer1.attachInterrupt(TSC_Callback); [/FONT][/COLOR] [COLOR=#000000][FONT=arial]attachInterrupt(0, TSC_Count, RISING); [/FONT][/COLOR] [COLOR=#000000][FONT=arial]delay(15);[/FONT][/COLOR] [COLOR=#000000][FONT=arial]for(int i=0; i<3; i++)[/FONT][/COLOR] [COLOR=#000000][FONT=arial]Serial.println(g_array[i]);[/FONT][/COLOR] [COLOR=#000000][FONT=arial]g_SF[0] = 255.0/ g_array[0]; //R Scale factor[/FONT][/COLOR] [COLOR=#000000][FONT=arial]g_SF[1] = 255.0/ g_array[1] ; //G Scale factor[/FONT][/COLOR] [COLOR=#000000][FONT=arial]g_SF[2] = 255.0/ g_array[2] ; //B Scale factor[/FONT][/COLOR] [COLOR=#000000][FONT=arial]Serial.println(g_SF[0]);[/FONT][/COLOR] [COLOR=#000000][FONT=arial]Serial.println(g_SF[1]);[/FONT][/COLOR] [COLOR=#000000][FONT=arial]Serial.println(g_SF[2]);[/FONT][/COLOR] [COLOR=#000000][FONT=arial]servo1.attach(12);[/FONT][/COLOR] [COLOR=#000000][FONT=arial]}[/FONT][/COLOR] [COLOR=#000000][FONT=arial]void loop()[/FONT][/COLOR] [COLOR=#000000][FONT=arial]{[/FONT][/COLOR] [COLOR=#000000][FONT=arial]g_flag = 0;[/FONT][/COLOR] [COLOR=#000000][FONT=arial]for(int i=0; i<3; i++)[/FONT][/COLOR] [COLOR=#000000][FONT=arial]Serial.println(int(g_array[i] * g_SF[i]));[/FONT][/COLOR] [COLOR=#000000][FONT=arial]delay(15);[/FONT][/COLOR] [COLOR=#000000][FONT=arial]} [/FONT][/COLOR][COLOR=#333333]
Comments
http://forums.parallax.com/showthread.php/134682-Forum-Guidelines