Pololu Orangutan 1284 w/TCS3200 Color sensor

I’m trying to implement color sensing using my pololu orangutan 1284 with a taos tcs 3200 color sensor. http://www.dfrobot.com/wiki/index.php/TCS3200_Color_Sensor_(SKU:SEN0101)

This color sensor already comes with code that’s compatible with arduino (see url above for code), however, I’m trying to color sensor to work with the orangutan controller. I think I can at least set the orangutan’s pins as either an input/output and high/low with just one function(set_digital_output(IO_Pin, HIGH/LOW) instead of two like the code for the arduino. However, I’m lost about what the code below does, what it means, and if I can implement the same function using any of the pololu library.

void timer0_init(void)
 {
  TCCR2A=0x00;                 //W
  TCCR2B=0x07;   //the clock frequency source 1024 points
  TCNT2= 100;    //10 ms overflow again
  TIMSK2 = 0x01; //allow interrupt
 }
 int i=0;
 ISR(TIMER2_OVF_vect)//the timer 2, 10ms interrupt overflow again. Internal overflow interrupt executive function
{
    TCNT2=100;

COULD SOMEONE EXPLAIN THE ABOVE CODE TO ME AND IF I CAN IMPLEMENT THE SAME CODE USING THE POLOLU LIBRARY???

Hello.

The code snippet you pasted is just writing to some registers for Timer 2 and defining an interrupt service routine. You can learn more about what it is doing by reading the ATmega328P datasheet. The function name “timer0_init” is misleading.

You can just copy it directly to your Orangutan program, because it doesn’t rely on any special features of the Arduino IDE. However, the Orangutan SVP-1284 uses a different processor (ATmega1284P), so you will need to compare its datasheet to the ATmega328P datasheet to see if it has the same registers and whether they can be used in the same way. I haven’t looked into it, but it is possible that Timer 2 on the ATmega1284P is designed differently from Timer 2 on the ATmega328P, so you might need to change some code.

–David

Hello.

Here are a couple of other considerations. One, unfortunately, the Orangutan 1284 makes use of TIMER2 for its motor control, so you will probably have to change the code to use TIMER0, which is free. Two, the Orangutan SVP 1284’s AVR runs at 20MHz as opposed to the 16MHz of most Arduinos, so the timer configuration will have to be adjusted to take that into account.

- Ryan

I STILL HAVEN’T GOT THIS COLOR SENSOR(http://www.dfrobot.com/wiki/index.php/TCS3200_Color_Sensor_(SKU:SEN0101)) TO WORK.

DO I EVEN NEED TO USE INTERRUPTS TO GET HIS SENSOR TO WORK? THIS IS THE CODE I HAVE WRITTEN SO FAR:

#include <pololu/orangutan.h>
void blinkLED(int ms)	// ms is the blinking time for LED for milli-seconds
{
	set_digital_output(IO_B3,HIGH); TURNS LED ON
	delay(ms);
	set_digital_output(IO_B3,LOW); TURN LED OFF
	delay(ms);
}
void startLED() 
{
	set_digital_output(IO_B3,HIGH); TURNS LED ON
}
void stopLED()
{
	set_digital_output(IO_B3,LOW); TURNS LED OFF
}
void selectRed() // SELECT RED FILTER, ONLY LETS IN RED LIGHT
{
         COFIG. TO SELECT RED FILTER
	set_digital_output(IO_C1,LOW);
	set_digital_output(IO_D0,LOW);
}
void selectGreen() // SELECT GREEN FILTER, ONLY LETS IN GREEN LIGHT
{
        // COFIG. TO SELECT GREEN FILTER
	set_digital_output(IO_C1,HIGH); 
	set_digital_output(IO_D0,HIGH);
}
void selectBlue() // SELECT BLUE FILTER, ONLY LETS IN BLUE LIGHT
{
        COFIG. TO SELECT BLUE FILTER
	set_digital_output(IO_C1,LOW); 
	set_digital_output(IO_D0,HIGH);
}
int main()
{
	while(1)
	{
		startLED();
		set_digital_output(IO_B4,HIGH);      //sets output frequency to 100%
		set_digital_output(IO_C0,HIGH);      //sets output frequency to 100%
		selectRed();
		pulse_in_start(IO_D1,1);
		set_digital_input(IO_D1,PULL_UP_ENABLED);
		print(new_pulse(get_last_high_pulse(IO_D1)));
		delay(500);
		clear();
		stopLED();
		delay(500);
	}
}

I TRIED USING THE PULSE FUNCTIONS FROM THE ORANGUTAN LIBRARY BUT ALL THEY HAVE GIVEN IS EITHER 0’s or 1’s.
I TRIED SETTING THE OUTPUT PIN FROM THE COLOR SENSOR AS A DIGITAL INPUT TO GET BACK A VALUE FROM THE PIN BUT ALL IT GIVES IN 0.

ANY OTHER CONSIDERATIONS?

Please do not write in all capital letters because it reads like you are shouting.

Are you sure the frequency is being generated on PD1? Also, it looks like you are using the PulseIn library incorrectly. You do not initialize it correctly, you do not read it correctly, and you are strangely combining methods from it. Here is something that is probably closer, but I did not try compiling it:

int main()
{
   startLED();
   set_digital_output(IO_B4,HIGH);      //sets output frequency to 100%
   set_digital_output(IO_C0,HIGH);      //sets output frequency to 100%
   selectRed();
   pulse_in_start((unsigned char[]) {IO_D1}, 1);
   set_digital_input(IO_D1,PULL_UP_ENABLED);
   while(1)
   {
      print(get_last_high_pulse(0));
     delay(500);
   }
}

- Ryan

Sorry about that, I didn’t mean any harm I was just being lazy. This is the code I have written for my Arduino microcontroller and it works. However, what function in the pololu library can I use to imitate the pulsein() function from the arduino library. Here is the code I have:

void ledOn(){
	set_digital_output(IO_B3,HIGH);
}
void ledOff(){
	set_digital_output(IO_B3,LOW);
}
double readPulse(){
	set_digital_output(IO_C0,HIGH);      //sets output frequency to 100%
	set_digital_output(IO_C1,HIGH);      //sets output frequency to 100%
	pulse_in_start((unsigned char[]) {IO_D2}, 1);
	set_digital_input(IO_D2,PULL_UP_ENABLED);
	double duration = 0.0;

	duration = pulseIn(out, HIGH,80000); //<--pulsein() i need to implement using pololu lib.

	print("freq = ");
	return (((1/duration)/2)*1000);
}
void detectColor(){
	
	set_digital_output(IO_D0,LOW); // s2
	set_digital_output(IO_D1,LOW); // s3
	print("Red: ");
	double rReading = readPulse();
	print_long(rReading);
	delay(500);
	clear();
	
	//Blue Filter
	set_digital_output(IO_D0,LOW);
	set_digital_output(IO_D1,HIGH);
	print("Blue: ");
	double bReading = readPulse();
	print_long(bReading);
	delay(500);
	clear();
	
	//Clear Filter
	set_digital_output(IO_D0,HIGH);
	set_digital_output(IO_D1,LOW);
	print("Clear: ");
	double cReading = readPulse();
	print_long(cReading);
	delay(500);
	clear();
	
	//Green Filter
	set_digital_output(IO_D0,HIGH);
	set_digital_output(IO_D1,HIGH);
	print("Green: ");
	double gReading = readPulse();
	print_long(gReading);
	delay(500);
	clear();
	
	if((rReading > bReading) && (rReading>gReading) && (cReading > bReading)&&(cReading > gReading)&&(cReading==rReading))
	{
		print("Orange");
	}
	else if((rReading>bReading)&&(rReading>gReading)&& (cReading>rReading)&&(bReading>gReading))
	{
		print("Red");
	}
	else if((gReading>rReading)&&(gReading>bReading)&&(cReading>gReading))
	{
		print("Green");
	}
	else if((rReading>bReading)&&(rReading>gReading)&&(cReading == rReading))
	{
		//print("Anamoly");
		print("Yellow");
	}
	else if((bReading>rReading)&&(bReading>gReading)&&(cReading>bReading))
	{
		print("Blue");
	}
	else if((rReading>bReading)&&(rReading>gReading)&&(cReading<100)&&(cReading>rReading))
	{
		print("Brown");
	}
	else
	{
		print("Nothing");
	}
}

int main()
{
	while(1)
	{
			ledOn();
			//filterSelect();
			detectColor();
			delay(2000);
			clear();
			ledOff();
	}
}

I think it is strange that you are using a double to store the unsigned long that Arduino’s pulseIn returns.

Something like this might work as a substitute for pulseIn (once again, I haven’t tested it, sorry!):

new_pulse(0);// discard a pulse
unsigned long timeout = get_ms() + 80000;
unsigned long pulseLengthInMicroseconds = 80000;
while (timeout > get_ms())  {
  if (new_high_pulse(0))
  {
     pulseLengthInMicroseconds = pulse_to_microseconds(get_last_high_pulse(0));
     break;
  }
}
// if pulseLengthInMicroseconds is 80000 probably there was no pulse!

I recommend moving your pulse_in_start and pull-up code to the setup part of your code. You don’t need to call it each time you read a pulse.

- Ryan

Thanks man by me adding the if(new_high_pulse(0)) it is now giving me back numbers. I am trying to measure the duration of the pulse by receiving input from the color sensor output pin. This pulse is supposed to correspond with color intensity and I’m supposed to use that to get the frequency, reasong the name color light to frequency converter. How could the value of the pulse allow me to calculate frequency? To measure the frequency should i just use the getLastHighPulse() function or do I need to the getLastHighPulse() and getLastLowPulse() functions?

The output has a 50% duty cycle, so the frequency is 1 / (2 * high pulse time) -Ryan