Hello all,
In my current project, I use an encoder and an optical sensor to generate an output on an O/P pin.
Encoder Used : 1000 PPR Opto encoder
Sensor Used : 1Mhz Optical Sensor
I wrote code for this and here it is. It is fully interrupt based !
int time_to_fire = 500 ;
int product_state[25] ;
int product_pulses[25] ;
int product_num = 1 ;
int product_num_fire = 0 ;
//int numbers = 1 ;
int general_counter = 1 ;
int count_flag = 0 ;
int sense_flag = 0 ;
void setup()
{
attachInterrupt(0,sense_flagfunc,RISING) ;
attachInterrupt(1,count_flagfunc,RISING) ;
pinMode(2,INPUT);
digitalWrite(2,HIGH);
pinMode(3,INPUT);
digitalWrite(3,HIGH);
pinMode(13,OUTPUT);
pinMode(9,OUTPUT);
flash();
pinMode(7,OUTPUT);
digitalWrite(7,HIGH);
Serial.begin(9600);
Serial.print("Loaded \n");
}
void loop()
{
fire();
count();
sense();
}
void sense()
{
if(sense_flag == 1)
{
//digitalWrite(13,HIGH);
Serial.print("Sensed :");
Serial.println(product_num);
Serial.print("\n");
product_state[product_num] = 1 ;
product_num = product_num + 1 ;
if(product_num > 24)
{
product_num = 1 ;
}
}
sense_flag = 0 ;
}
void count()
{
if(count_flag == 1)
{
for(general_counter = 1 ; general_counter < 26 ; general_counter = general_counter + 1)
{
if(product_state[general_counter] == 1)
{
product_pulses[general_counter] = product_pulses[general_counter] + 1 ;
//flash();
Serial.print("Pulse \n");
}
}
}
count_flag = 0 ;
}
void fire()
{
for(product_num_fire = 1 ; product_num_fire < 26 ; product_num_fire = product_num_fire + 1)
{
if(product_pulses[product_num_fire] == 5)
{
//digitalWrite(13,HIGH);
//delay(time_to_fire);
//digitalWrite(13, LOW);
flash();
Serial.print("Fire \n");
product_state[product_num_fire] = 0 ;
product_pulses[product_num_fire] = 0 ;
}
}
}
void flash()
{
digitalWrite(13,HIGH);
delay(500);
digitalWrite(13,LOW);
}
void flasher()
{
digitalWrite(9,HIGH);
delay(500);
digitalWrite(9,LOW);
}
void sense_flagfunc()
{
sense_flag = 1;
}
void count_flagfunc()
{
count_flag = 1;
}
Forget the Serial Commands, I used them earlier to debug. Also forget the LED delays, not required.
This is used in an industrial setup, where the sensor and pin output are on a conveyor. The encoder is also fitted to the conveyor to measure distance. The aim here to sense a product under the opto sensor and give an output after a set distance determined by the encoder. Ex - Product gets sensed, product travels a set distance and when the product is under the output pin, pin goes HIGH and LOW for a set delay.
This program works, but the problem comes when the speed reaches it's maximum. At maximum speed, I get roughly 25000 pulses a second to the encoder interrupt and 27 pulses a second on the sensor interrupt. What starts to happen is; errors multiply, the set distance starts to offset by a big margin and at times the display freezes since there is no room to refresh it at all.
I was thinking probably to use a timer tick to poll the encoder and sensor and call a function. The call delay for the encoder comes to 40 microseconds and sensor call delay is roughly 27 to 30 milliseconds. Now, this is also a type of interrupt I believe.
I want your advice people, should I use a faster chip, something like the DUE or is it just pure 'in-efficient code' .
Somebody suggested I have a separate controller for the display and send values to the main board via serial, I don't want to do that, since it just increases components.
Also, as you can see in the code, as the products get sensed, they get cued in an array, i.e if the distance between the sensor and output is large, there are upto 25 products waiting in cue to receive an output.
SO, Please people, all ears for suggestions !! =( =(