Shop OBEX P1 Docs P2 Docs Learn Events
Hey, can someone help me change this code from library timer 1 to 2? — Parallax Forums

Hey, can someone help me change this code from library timer 1 to 2?

jeff277jeff277 Posts: 2
edited 2014-03-04 09:05 in Robotics
i 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.

#include <TimerOne.h>#include <Servo.h> // servo library


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


int g_count = 0; // count the frequecy
int g_array[3]; // store the RGB value
int g_flag = 0; // filter of RGB queue
float g_SF[3]; // save the RGB Scale factor


Servo servo1; // servo control object


// 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()
{
int position;
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
servo1.write(60); //Tell servo to go to 90 degrees
break;


case 2:
Serial.print("->Frequency G=");
Serial.println(g_count);
g_array[1] = g_count;
TSC_WB(LOW, HIGH); //Filter without Blue
servo1.write(90); //Tell servo to go to 90 degrees
break;


case 3:
Serial.print("->Frequency B=");
Serial.println(g_count);
Serial.println("->WB End");
g_array[2] = g_count;
TSC_WB(HIGH, LOW); //Clear(no filter)
servo1.write(120); //Tell servo to go to 90 degrees
break;


default:
g_count = 0;
servo1.write(0); //Tell servo to remain at 0 degrees
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(15);


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


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]);


servo1.attach(12);
}


void loop()
{
g_flag = 0;
for(int i=0; i<3; i++)
Serial.println(int(g_array[i] * g_SF[i]));
delay(15);


}

Comments

  • Hal AlbachHal Albach Posts: 747
    edited 2014-03-03 15:01
    Your first two sentences seem to contradict each other:

    i 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

    If there is more things involved than just changing <TimerOne.h> to <TimerTwo.h> in the #include statement, then you might want to try one of the Arduino Forums as this is a Parallax Forum.
  • GordonMcCombGordonMcComb Posts: 3,366
    edited 2014-03-04 09:05
    As Hal says, your code and your statements don't match. You are already using Timer1, which is internally used for the Servo library. This creates a conflict. You might be able to use a Timer2 library, like MsTimer2. You will need to revise your sketch, which will require a thorough reading of the documentation for whatever timer lib you end up using. Another alternative is to use a different Arduino that supports more hardware timers. Or, adapt this to a microcontroller like the Propeller which supports multitasking.
Sign In or Register to comment.