C CODE FOR RUNNING TWO SENSORS FOR BI-DIRECTIONAL COUNTING
-
I AM A LITTLE BIT EXPERIENCED WITH C PROGRAMMING AND PICS BUT AM WONDERING IF ANYONE OUT THERE CAN HELP WITH THE CODE TO RUN A PIC16F8777A OR AN ARDUINO WITH TWO IR SENSORS S1 AND S2 TO PERFORM BIDIRECTIONAL COUNTING IN AND OUT. IN THE FIRST SEQUENCE BREAKING FIRST S1 AND THEN S2 WILL TELL THE MICROCONTROLLER TO INCREMENT THE COUNTER WHILE BREAKING THE SECOND SEQUENCE, FIRST S2 AND THEN S1 WOULD TELL THE MICROCONTROLLER TO DECREMENT THE COUNTER.THE MICROONTROLLER SHOULD ALSO STORE AND TIMESTAMP THE COUNTS AND BE ABLE TO PASS THE TIMESTAMPED DATA TO A REMOTE COMPUTER VIA BLUETOOTH OR WIFI. ANY HELP WILL BE GREATLY APPRECIATED. THANK YOU.
-
I AM A LITTLE BIT EXPERIENCED WITH C PROGRAMMING AND PICS BUT AM WONDERING IF ANYONE OUT THERE CAN HELP WITH THE CODE TO RUN A PIC16F8777A OR AN ARDUINO WITH TWO IR SENSORS S1 AND S2 TO PERFORM BIDIRECTIONAL COUNTING IN AND OUT. IN THE FIRST SEQUENCE BREAKING FIRST S1 AND THEN S2 WILL TELL THE MICROCONTROLLER TO INCREMENT THE COUNTER WHILE BREAKING THE SECOND SEQUENCE, FIRST S2 AND THEN S1 WOULD TELL THE MICROCONTROLLER TO DECREMENT THE COUNTER.THE MICROONTROLLER SHOULD ALSO STORE AND TIMESTAMP THE COUNTS AND BE ABLE TO PASS THE TIMESTAMPED DATA TO A REMOTE COMPUTER VIA BLUETOOTH OR WIFI. ANY HELP WILL BE GREATLY APPRECIATED. THANK YOU.
Are the sensors tied to a GPIO line or do you have to talk to them using IIC or SPI? Need a little more info about the sensors before I could offer more assistance. I'm familiar with ARM (mostly Freescale and Atmel stuff) and I'd say that the sensors that I work with are generally tied to a GPIO pin. In that case you just need to read the GPIO pin status or tie it to an interrupt and read them in your app. The logic handling should be simple enough, if S1 is tripped before S2 then increment your counter and log the event to whatever permanent storage you are using, and decrement if you see the sensors in the opposite order. Not having much experience with PIC parts I don't know the exact registers you would need to check for the GPIO status though.
-
Are the sensors tied to a GPIO line or do you have to talk to them using IIC or SPI? Need a little more info about the sensors before I could offer more assistance. I'm familiar with ARM (mostly Freescale and Atmel stuff) and I'd say that the sensors that I work with are generally tied to a GPIO pin. In that case you just need to read the GPIO pin status or tie it to an interrupt and read them in your app. The logic handling should be simple enough, if S1 is tripped before S2 then increment your counter and log the event to whatever permanent storage you are using, and decrement if you see the sensors in the opposite order. Not having much experience with PIC parts I don't know the exact registers you would need to check for the GPIO status though.
I am tieing a pair of infrared sensors S1 and S2 onto GPIO lines(digital I/O) of any microcontroller that is easy to program in C as I am new to this.I am looking into ARDUINO UNO(ATmega328 MCU),ARDUINO MEGA(ATmega1280) or a PIC16F877A.I will use the Microcontroller to read the interrupts from the sensors as they change on being broken in the sequence mentioned before. My main problem is the real coding in C of the logic onto my app. Can you help with the code?? I wish to have the following functionality: 1) Read interrupts from sensors 2)Increment or decrement counter depending on sequence they are broken 3) timestamp the count and enable storing 4) Pass timestapmed data onto a remote host computer via bluetooth or Wifi. Do you suggest any parcticular Microcontroller for this application? Thank you very much.
-
I am tieing a pair of infrared sensors S1 and S2 onto GPIO lines(digital I/O) of any microcontroller that is easy to program in C as I am new to this.I am looking into ARDUINO UNO(ATmega328 MCU),ARDUINO MEGA(ATmega1280) or a PIC16F877A.I will use the Microcontroller to read the interrupts from the sensors as they change on being broken in the sequence mentioned before. My main problem is the real coding in C of the logic onto my app. Can you help with the code?? I wish to have the following functionality: 1) Read interrupts from sensors 2)Increment or decrement counter depending on sequence they are broken 3) timestamp the count and enable storing 4) Pass timestapmed data onto a remote host computer via bluetooth or Wifi. Do you suggest any parcticular Microcontroller for this application? Thank you very much.
Try something like the following for your interrupt handlers. Depending on the micro you select, how to install these will be different. I would stay away from PIC parts, I personally don't like them. The Atmega family of MPU should be better to work with in my opinion, although my personal favorite these days is the Freescale Kinetis family of MPU's. You can get demo boards in the form of expandable towers from Freescale. [EDIT] You may also want to handle rollover for the counter variable if you expect to have counts high enough for this to happen.
#define S1 (1 << 0)
#define S2 (1 << 1)unsigned char sensorFlags = 0;
int counter = 0;/* interrupt handler for S1 */
void s1_isr(void)
{
if(sensorFlags & S2)
{
/* Exiting, decrement counter */
--counter;
/* clear S2 flag */
sensorFlags &= ~S2;
/* Get timestamp, store it, transmit to remote host
this will depend a lot on your hardware */
}
else if(sensorFlags & S1)
{
/* ERROR same sensor tripped twice, handle as necessary */
}
else
{
/* Entering, set S1 flag */
sensorFlags |= S1;
}/* clear interrupt flag if necessary */
}/*interrupt handler for S2 */
void s2_isr(void)
{
if(sensorFlags & S1)
{
/* Entering, increment counter */
++counter;
/* clear S1 flag */
sensorFlags &= ~S1;
/* Get timestamp, store it, transmit to remote host
this will depend a lot on your hardware */
}
else if(sensorFlags & S2)
{
/* ERROR same sensor tripped twice, handle as necessary */
}
else
{
/* Exiting, set S2 flag */
sensorFlags |= S2;
}/* clear interrupt flag if necessary */
} -
Try something like the following for your interrupt handlers. Depending on the micro you select, how to install these will be different. I would stay away from PIC parts, I personally don't like them. The Atmega family of MPU should be better to work with in my opinion, although my personal favorite these days is the Freescale Kinetis family of MPU's. You can get demo boards in the form of expandable towers from Freescale. [EDIT] You may also want to handle rollover for the counter variable if you expect to have counts high enough for this to happen.
#define S1 (1 << 0)
#define S2 (1 << 1)unsigned char sensorFlags = 0;
int counter = 0;/* interrupt handler for S1 */
void s1_isr(void)
{
if(sensorFlags & S2)
{
/* Exiting, decrement counter */
--counter;
/* clear S2 flag */
sensorFlags &= ~S2;
/* Get timestamp, store it, transmit to remote host
this will depend a lot on your hardware */
}
else if(sensorFlags & S1)
{
/* ERROR same sensor tripped twice, handle as necessary */
}
else
{
/* Entering, set S1 flag */
sensorFlags |= S1;
}/* clear interrupt flag if necessary */
}/*interrupt handler for S2 */
void s2_isr(void)
{
if(sensorFlags & S1)
{
/* Entering, increment counter */
++counter;
/* clear S1 flag */
sensorFlags &= ~S1;
/* Get timestamp, store it, transmit to remote host
this will depend a lot on your hardware */
}
else if(sensorFlags & S2)
{
/* ERROR same sensor tripped twice, handle as necessary */
}
else
{
/* Exiting, set S2 flag */
sensorFlags |= S2;
}/* clear interrupt flag if necessary */
}Hi Tr@V, This is great mate and I appreciate your help so much. I am thinking of going for either ARDUINO MEGA/ARDUINO 2560 which have ATMEL 1280 and ATMEL 2560 chips respecetively.I will probably have to connect a bluetooth or wifi shield and a SD card for storing.Am not much aware of Freescale Kinetis family of MPU's you suggested but will check them out. Any chance I can get your email address or phone number incase i hit hard rocks?I may need your help.I'll also let you know how far have gone. Thanks.
-
Try something like the following for your interrupt handlers. Depending on the micro you select, how to install these will be different. I would stay away from PIC parts, I personally don't like them. The Atmega family of MPU should be better to work with in my opinion, although my personal favorite these days is the Freescale Kinetis family of MPU's. You can get demo boards in the form of expandable towers from Freescale. [EDIT] You may also want to handle rollover for the counter variable if you expect to have counts high enough for this to happen.
#define S1 (1 << 0)
#define S2 (1 << 1)unsigned char sensorFlags = 0;
int counter = 0;/* interrupt handler for S1 */
void s1_isr(void)
{
if(sensorFlags & S2)
{
/* Exiting, decrement counter */
--counter;
/* clear S2 flag */
sensorFlags &= ~S2;
/* Get timestamp, store it, transmit to remote host
this will depend a lot on your hardware */
}
else if(sensorFlags & S1)
{
/* ERROR same sensor tripped twice, handle as necessary */
}
else
{
/* Entering, set S1 flag */
sensorFlags |= S1;
}/* clear interrupt flag if necessary */
}/*interrupt handler for S2 */
void s2_isr(void)
{
if(sensorFlags & S1)
{
/* Entering, increment counter */
++counter;
/* clear S1 flag */
sensorFlags &= ~S1;
/* Get timestamp, store it, transmit to remote host
this will depend a lot on your hardware */
}
else if(sensorFlags & S2)
{
/* ERROR same sensor tripped twice, handle as necessary */
}
else
{
/* Exiting, set S2 flag */
sensorFlags |= S2;
}/* clear interrupt flag if necessary */
}I have made some good progress with my ISR and am serial printing the count on the serial monitor, now what I want to achieve is to call the count to be displayed on my 3 digit seven seg cc display or rather let the ISR trigger the counts whenever the sensors flag depending on their sequence. I have the code to count from 0-999 back to 0 so my problem is how to combine with the ISR. Any help is much appreciated. Much thanks.