You need to read my other post for this to make sense!
http://arduino.cc/forum/index.php/topic,134058.0.html
#include <TimerOne.h>
#define SAMPLE 5000 // uSec for sample period - you need to experiment with this!
#define OE 7
#define S0 6
#define S1 5
#define S2 4
#define S3 3
#define OUT 2 // MUST be on this pin to allow the external HW interrupt
volatile long g_count; // count the frequecy
volatile long g_array[3]; // store the RGB value
volatile int color=0;
// Init TSC230 and setting Frequency. S0 & S1 parameterised for eas of play...
// default to LH i.e. 2% max freq
void Init(int bS0=LOW,int bS1=HIGH)
{
pinMode(WB_LED, OUTPUT);
pinMode(OE, OUTPUT);
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(OUT, INPUT);
digitalWrite(S0, bS0); // OUTPUT FREQUENCY SCALING
digitalWrite(S1, bS1);
digitalWrite(OE, HIGH); // silent
}
// Select the filter color
void setColor(int Level01, int Level02)
{
digitalWrite(S2, Level01);
digitalWrite(S3, Level02);
}
//
// ISRs
//
void freqCount() // ticks once per external INT (on pin 2)
{
g_count++ ;
}
void getFreq(){
noInterrupts(); // see notes
digitalWrite(OE,HIGH); // stops Sq Wav...guarantees g_count won't change
g_array[color]=g_count;
g_count=0;
switch(color){
case 0:
setColor(HIGH,HIGH); // GREEN
break;
case 1:
setColor(LOW,HIGH); // BLUE
break;
case 2:
setColor(LOW,LOW); // RED
color=-1;
break;
default:
Serial.println("WTF?");
break;
}
color++;
digitalWrite(OE,LOW); // carry on sampling
interrupts(); // see notes
void setup()
{
Init(HIGH,HIGH);
Serial.begin(115200);
/*
Contrary to the following comment from the TimerOne library initialise():
"// set mode as phase and frequency correct pwm, stop the timer"
The timer actually starts ticking as soon as the call to initialise is complete.
We don't want this (see above) so we disable INTS
*/
noInterrupts(); // prevent premature (any!) ticking
Timer1.initialize(SAMPLE);
Serial.println("Timer Initialised");
Timer1.stop(); // keep Timer1 internal state consistent
Timer1.attachInterrupt(getFreq);
Serial.println("Timer Ready");
attachInterrupt(0, freqCount, RISING);
Serial.println("Counter attached");
//
Timer1.restart(); // * like now for example
digitalWrite(OE,LOW);
interrupts(); // now things will only start ticking when *I* tell them to (which is more like it!)
}
void loop()
{
delay(10 * (SAMPLE / 1000));
for(int i=0; i<3; i++){
// noInterrupts(); // see notes
Serial.print(g_array[i]);
// interrupts(); // see notes
Serial.print(",");
}
Serial.println("");
}