I'm working on a project that requires counting two separate pulse inputs, concurrently, that are between 60ms and 150ms apart, on an arduino uno. I'm currently counting on A(0) and A(1), because the crosstalk received on the incoming signals, is high enough to trigger low on the opposing input line if I use the regular digital inputs. My code currently polls the inputs looking for a pull-down below 500, which works great, as long as the two pins aren't being pulsed at the same time. If both are being pulsed simultaneously, one or both total count is usually off, depending on total pulses to be counted. The higher the pulse counts to be received, the more it misses on it least. I realize that this is because I'm using a loop to do the counting, instead of a hardware counter timer. After much research, I found where there are three hardware counter timers; two of which are 8bit counters, and one that is a 16bit counter. I see where the input for "timer0" is on pin 4 and "timer1" is located on pin 5. I'm having a hard time understanding how to implement the timers as a counter, and how to trigger an event to read the timer and clear it after a certain time has passed with no pulses. Also, will the cross talk issues I've been experiencing, affect the timers as it did when I was using digital pins?
Below is my current code that works as long as the inputs aren't being pulsed simultaneously.
#include <Ethernet.h>
#include <SPI.h>
#include <SD.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
//byte ip[] = { 192, 168, 100, 1 };
byte server[] = { 192, 168, 100, 251}; // tcpServer
IPAddress dnServer(192, 168, 100, 251);
// the r02er's gateway address:
IPAddress gateway(192, 168, 100, 251);
// the subnet:
IPAddress subnet(255, 255, 255, 0);
//the IP address is dependent on your network
IPAddress ip(192, 168, 100, 3);
int pin01 =0;
int pin02 =1;
int pulse01Count = 0;
int pulse02Count = 0;
int pulses01NotSent = 0;
int pulses02NotSent = 0;
int a = 0;
int b = 0;
boolean pulse01On = false;
boolean pulse02On = false;
unsigned long time01Int;
unsigned long time01Check;
unsigned long time02Int;
unsigned long time02Check;
unsigned long heartBeat = millis();
unsigned long heartBeatCheck;
boolean total01Check = false;
boolean total02Check = false;
int sampleSize1 = 0;
int sampleSize2 = 0;
EthernetClient client;
void setup() {
// put your setup code here, to run once:
Ethernet.begin(mac, ip, dnServer, gateway, subnet);
Serial.begin(9600);
client.connect(server, 8080);
delay(1000);
Serial.println("connect01g...");
Serial.println(Ethernet.localIP());
if (client.connected()) {
Serial.println("connected");
client.println("Connected|Pulse01|0");
client.println();
} else {
Serial.println("connection failed");
}
analogReference(DEFAULT);
}
void loop() {
// put your ma01 code here, to run repeatedly:
int pin0 = analogRead(pin01);
int pin1 = analogRead(pin02);
if (pin0 < 500){
// Serial.println(p010);
if (pulse01On == false){
pulse01Count= pulse01Count + 1;
// Serial.println("P0101: " + String(p010));
pulse01On = true;
time01Int = millis();
total01Check = true;
Serial.println("pulse 01: " + String(pulse01Count));
}
// Serial.println(p010);
}else{
pulse01On = false;
if (total01Check == true){
time01Check = millis();
if (time01Check - time01Int > 500){
Serial.println("Total Pulses 01: " + String(pulse01Count));
Serial.println("_______");
if (Ethernet.linkStatus() == LinkON){
if (client.available()){
client.println("Pulse01|Terminal01|" + String(pulse01Count));
delay(500);
pulse01Count = 0;
total01Check = false;
}else{
pulses01NotSent = pulses01NotSent + pulse01Count;
pulse01Count = 0;
total01Check = false;
Serial.println("Pulses Not Sent: " + String(pulses01NotSent));
}
}else{
client.stop();
pulses01NotSent = pulses01NotSent + pulse01Count;
pulse01Count = 0;
total01Check = false;
Serial.println("Pulses Not Sent: " + String(pulses01NotSent));
}
}
}
}
if (pin1 < 500){
if (pulse02On == false){
pulse02Count= pulse02Count + 1;
pulse02On = true;
time02Int = millis();
total02Check = true;
Serial.println("pulse 02: " + String(pulse02Count));
}
// Serial.println(p010);
}else{
pulse02On = false;
if (total02Check == true){
time02Check = millis();
if (time02Check - time02Int > 500){
Serial.println("Total Pulses 02: " + String(pulse02Count));
Serial.println("_______");
if (Ethernet.linkStatus() == LinkON){
if (client.available()){
client.println("Pulse02|Terminal01|" + String(pulse02Count));
delay(500);
pulse02Count = 0;
total02Check = false;
}else{
pulses02NotSent = pulses02NotSent + pulse02Count;
pulse02Count = 0;
total02Check = false;
Serial.println("Pulses 02 Not Sent: " + String(pulses02NotSent));
}
}else{
client.stop();
pulses02NotSent = pulses02NotSent + pulse02Count;
pulse02Count = 0;
total02Check = false;
Serial.println("Pulses 02 Not Sent: " + String(pulses02NotSent));
}
}
}
}
if (total01Check + total02Check == 0){
if (Ethernet.linkStatus() == LinkON){
heartBeatCheck = millis();
}else{
client.stop();
}
}
if (heartBeatCheck - heartBeat > 60000){
Serial.println("HeartBeat Check");
if (client.available()){
client.println("HeartBeat|Terminal01");
heartBeat = millis();
}
if(client.connected()){
Serial.println("client still thinks its connected");
}else{
client.connect(server, 8080);
//delay(1000);
Serial.println("connect01g...");
Serial.println(Ethernet.localIP());
if (client.connected()) {
Serial.println("connected");
client.println("Connected|Terminal01|0");
client.println();
delay(100);
if (pulses01NotSent > 0){
client.println("Pulse01|Terminal01|" + String(pulses01NotSent));
pulses01NotSent = 0;
delay(100);
}
if (pulses02NotSent > 0){
client.println("Pulse02|Terminal01|" + String(pulses02NotSent));
pulses02NotSent = 0;
delay(100);
}
} else {
Serial.println("connection failed");
heartBeat = millis();
}
}
}
//delay(1);
}