roineust:
In the meantime, i decided to go for just duplicating parts in the code, so duplicate=2 sensors, triplicate=3 sensors. And also, changing the first instance of the command c to attachInterrupt(1, TSC_Count, RISING); in the second instance of the duplicated line.
Yet, The code still does not work for both the sensors at the same time, but only for one. :doh:
You were close but since coding is neither horseshoes nor hand-grenade tossing, your attempt falls short. What would be needed ? It would seem that you've figured out that each TCS230 output needs it's own, separate Arduino input pin. And if your memory serves you well you will recall that it must be a special pin, an external interrupt pin. A review of this page is in order.
http://arduino.cc/en/Reference/AttachInterrupt
It shows what pins can be used on which Arduino and what interrupt #, which is all the code “knows”, goes with each pin.
So if you wired your 2 devices to pins 2 and 3, your code statements above would be almost correct. Let’s look at that page a bit more, especially that 1’st line.
Specifies a named Interrupt Service Routine (ISR) to call when an interrupt occurs.
How does that line do that ? Let’s look down a few lines to the syntax.
attachInterrupt(interrupt, ISR, mode)
Yup, the ISR named TSC_Count is what will run when interrupt 0 or 1 occurs, per your statements. Hmmmm, that’s not right is it ?
Analogy time - you’re assigned to watch 2 kids, Dennis and Menace, but you also want to watch the Big Game. Since the kids are mostly good, needing minimal attention, you plan to tie a string from each to you arms, Dennis to the left and Menace to the right. When you feel a tug on the string you’ll pause the cable box and tend to the kid. When done you’ll go back and un-pause the game, having missed nothing due to the interruption. And while you’re at it you’ll keep a count of each kids total # of pulls so when asked you can say “Yes dear, I checked on the kids, X and Y times as a matter of fact”.
Alas you’ve tied (by analogy) Menace’s string, by mistake, to your left arm too. Since all you know is which arm has been tugged on, will Menace ever get any attention ?
So you need a separate ISR for each device. Let’s look at the existing ISR.
//this is the external interrupt / counter ISR
void TSC_Count()
{
count ++ ;
}
http://arduino.cc/en/Reference/Increment
All it does “keep count of the tugs”. It increments by 1 a variable (0, 1, 2, …) every time it runs. If you don’t use a different variable in that 2’nd ISR you’ll be looking at Menace when he pulls but mistakenly adding 1 to Dennis’s count.
So at this point you might have 2 ISRs, TSC1_Count() and TSC2_Count() using 2 variables, count1 and count2. You got this part right.
Is there more … yes. Look at this.
//declare the constants
#define S0 6
#define S1 5
#define S2 4
#define S3 3
#define OUT 2
You need a new, differently named OUT for the added device and a new pin assigned to S3 since pin3 is needed for that other “OUT”. FWIW all S0 pins can be wired together as can the S1s, S2s and S3s. Only 4 Arduino pins are, at most, needed to control all the TSC230s. You got this part right too.
And then there’s setting the pins up.
//set the pins to be I or O
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(OUT, INPUT);
And declaring the variables.
//declare the variables
volatile int count = 0; //a count of pulses from TSC230
What’s missing ? You got these parts right above.
At this point your Arduino will collect the data but you’ve yet to print it out. Yup, more changes in all of the “cases”. Look at just one, what needs changing/adding ?
Serial.print("Counts R = ");
Serial.println(count);
//reset the counter
count = 0;
You may have notice I’m using the fixed version of my code. It’s so much easier to read and devoid of unneeded code, you should too. You over did this part in your attempt, due to the original code being obtuse and your skill level.
fixed code:
//version 0.11 of TCS230 code
//declare the constants
#define S0 6
#define S1 5
#define S2 4
#define S3 3
#define OUT 2
const unsigned long smpl_period = 100; //sample period in msec
//declare the variables
volatile int count = 0; //a count of pulses from TCS230
int state = 0; //state machine control variable
int set = 0; //a count of the RGBW sets processed
void setup() {
//set the baud rate
Serial.begin(9600);
//set the pins to be I or O
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(OUT, INPUT);
//set the max freq of the TCS230 output
digitalWrite(S0, LOW); // OUTPUT FREQUENCY SCALING 2%
digitalWrite(S1, HIGH); // OUTPUT FREQUENCY SCALING 2%
//set the 1st color to be analyzed
//this color is red
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
//attach and enable the external interrupt(s)
attachInterrupt(0, TSC_Count, RISING);
//wait 1 sample period to collect counts from TCS230
delay(smpl_period);
//go to state machine ready to process 1st color
}
void loop() {
//run state machine to collect and process data
switch (state)
{
case 0:
//process the collected red count data
//freeze the counter(s)
noInterrupts();
//send the data
Serial.print("Data set number = ");
Serial.println(set);
Serial.print("Counts R = ");
Serial.println(count);
//reset the counter
count = 0;
//set next state
state ++;
//set next color to green
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
//re-enable the counter(s)
interrupts();
break;
case 1:
//process the collected green count data
//freeze the counter(s)
noInterrupts();
//send the data
Serial.print("Counts G = ");
Serial.println(count);
//reset the counter
count = 0;
//set next state
state ++;
//set next color to blue
digitalWrite(S2, LOW);
digitalWrite(S3, HIGH);
//re-enable the counter(s)
interrupts();
break;
case 2:
//process the collected blue count data
//freeze the counter(s)
noInterrupts();
//send the data
Serial.print("Counts B = ");
Serial.println(count);
//reset the counter
count = 0;
//set next state
state ++;
//set next color to white
digitalWrite(S2, HIGH);
digitalWrite(S3, LOW);
//re-enable the counter(s)
interrupts();
break;
case 3:
//process the collected "white" count data
//freeze the counter(s)
noInterrupts();
//send the data
Serial.print("Counts W = ");
Serial.println(count);
Serial.println(" ");
//reset the counter
count = 0;
//set next state
state = 0;
//increment the data set counter
set ++;
//set next color to red
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
//re-enable the counter(s)
interrupts();
break;
default:
//code should never get here
Serial.println("Error");
//reset the state machine
count = 0;
state = 0;
//set next color to red
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
break;
}
//wait 1 sample period to collect counts from TCS230
delay(smpl_period);
//state machine end
}
//this is the external interrupt / counter ISR
void TSC_Count()
{
count ++ ;
}
I reiterate that getting the code working in it’s final form for a single sensor will save you a lot of typing.