Hello
I am making an object counter, as can be seen in the attached picture. The structure of the code is as follows.
void loop()
{
// read the inputs
readInputs();
// evaluate valA and valB
evalInputs();
// show output on LCD (and serial)
displayOutput();
// set value of prevCtr, ready for next loop
prevCtr=ctr;
}
It performs the basic duties of a object counter. The first function readInputs reads the analog inputs A1 and A2 using the analogRead function. The second function evalInputs compares valA and valB with the threshold - in this case 900. displayOutput hands the display of the counter on the attached 16*2 LCD as well as sending debugging info via serial.
The complete code is as below.
/*
IR Counter with Serial and LCD output
*/
// include the library code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(9,8,7,6,5,4); // pins for RS, E, DB4, DB5, DB6, DB7
// General Operation Varibles
const int inputA=1; //Pot on analog pin 1
const int inputB=2; //Pot on analog pin 2
int valA = 0; //variable to hold the analog reading from inputA
int valB = 0; //variable to hold the analog reading from inputB
int ctr=0; //variable to hold counter
int prevCtr=0;
int thresh=0;
void setup()
{
Serial.begin(9600); //Initiate serial connection
lcd.begin(16, 2); //define LCD
lcd.clear(); //Clear LCD display
}
void loop()
{
// read the inputs
readInputs();
// evaluate valA and valB
evalInputs();
// show output on LCD (and serial)
displayOutput();
// set value of prevCtr, ready for next loop
prevCtr=ctr;
}
void readInputs() {
valA = analogRead(inputA); //read analogue inputA
valB = analogRead(inputB); //read analogue inputB
}
void evalInputs() {
thresh = 900; // sensor threshold depends on ambient light
if(valA<thresh && valB>thresh)
{
ctr=ctr+1; // increment counter by one
delay(800);
}
else{
ctr=ctr;
}
if(valA>thresh && valB<thresh)
{
ctr=ctr-1; // decrease counter by one
delay(800);
}
else
{
ctr=ctr;
}
}
void displayOutput() {
//Print data to Serial
Serial.print("valA is : ");
Serial.print(valA );
Serial.print(" valB is : ");
Serial.print(valB );
Serial.print(" counter : ");
Serial.print(ctr);
Serial.print(" thresh val : ");
Serial.println(thresh);
// Only refresh the LCD if the counter is updated
// This reduces flickering
if (ctr == 0 || ctr != prevCtr)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Counter: "); //print varA on LCD
lcd.print(ctr);
}
}
While the above configuration works, there are two additional features that I am looking to implement.
- Stable behaviour when an object is held between the sensors
- Display of speed on LCD
To do this I understand that I will need to use some sort of interrupts to achieve the required pseudo-multitasking required. This is what I came up with.
/*
This is a rewrite of the Arduino Optocounter + Speedometer.
Interrups according to Jermey Blum's Book, pg 268 are used.
*/
// Variables
const int gateA =0; //Pin 2 on Arduino
const int gateB =1; //Pin 3 on Arduino
volatile boolean gateAActive =false; // Status of gateA
volatile boolean gateBActive =false; // Status of gateB
volatile int ctr=0;; // variable to store count
volatile unsigned long prevMillis; //CPU time at end of loop
volatile unsigned long deltaMillis; //time difference measured
volatile unsigned long currMillis;
unsigned long distance = 3; // distance between gates in mm
unsigned long itemSpeed; // speed of object in mm/ms (m/s)
void setup()
{
Serial.begin(9600); //Initiate serial connection
attachInterrupt(gateA ,evalGateA , RISING); //Upon Rising edge interrupt for gateA run function evalGateA
attachInterrupt(gateB ,evalGateB , RISING); //Upon Rising edge interrupt for gateB run function evalGateB
}
void evalGateA() // detect gate
{
if (gateBActive == false)
{
if (gateAActive == true) // In this case, the object came through gateA and left through gateA
{
gateAActive=false; // Pretend it never even entered
}
else
{
gateAActive=true;
prevMillis=millis();
}
}
else if (gateAActive == false && gateBActive == true)
{
ctr=ctr-1;
currMillis=millis();
deltaMillis=currMillis-prevMillis;
}
}
void evalGateB() // detect gate
{
if (gateAActive == false)
{
if (gateBActive == true) // In this case, the object came through gateA and left through gateA
{
gateBActive=false; // Pretend it never even entered
}
else
{
gateBActive=true;
prevMillis=millis();
}
}
else if (gateBActive == false && gateAActive == true)
{
ctr=ctr-1;
currMillis=millis();
deltaMillis=currMillis-prevMillis;
}
}
void calcSpeed() {
if (deltaMillis>50)
{
//Calculate speed
itemSpeed = distance / deltaMillis; // v=x/t
Serial.println(itemSpeed);
Serial.println(ctr);
}
}
void loop()
{
calcSpeed();
}
Interrupt 0 and 1, corresponding to pins 2 and 3 on the arduino are used. However, I am not able to detect the rising edge when an object passes through the sensor. When there is no object between the sensor, analogRead gives me a steady number about 800. When an object is between the sensor, it gives me 1023. I have used a 560 ohm pull-up resistor.
I'd appreciate any pointers in the right direction.
I should mention that the Arduino platform has been my first introduction to C. I have also worked with Matlab and Fortran before.