I am currently working on a project that sends two types of signal out to a single pin. I would like to get my Arduino to recieve these signals and differentiate between the two and count how many of each type. currently it just counts every signal that goes down this pin and I don't know how to get it to seperate them. In regards to the signals it recieves, they are as below.
Capture signal in from pin 14. If signal = 100ms +- 30% var S1S +1
Capture signal in from pin 14. If signal = 390ms +- 30% var S1L +1
I am currently using an emulator as it makes life easier to make code adjustments as I go along on an Arduino Mega.
My current code is as follows;
/* Simple Pulse Counter
Counts digital pulses fed into pin 14
*/
#include <LiquidCrystal.h> //library to load
LiquidCrystal lcd(12, 11, 10, 9, 8, 7); //Pin Configuration
const int S1 = 14; // This is where the input is fed.
int S1L = 0; // Variable for saving pulses count.
int S1I = 0;
void setup(){
pinMode(S1, INPUT);
lcd.begin(20, 4);
lcd.setCursor(2, 0); //positioning for text
lcd.print("MGB:S1 L 0 S 0"); //print text that will stay
}
void loop(){
if(digitalRead(S1) > S1I)
{
S1I = 1;
S1L++;
lcd.setCursor(12, 0); // this will overwrite the initial figure of 0
lcd.print(S1L);
}
if(digitalRead(S1) == 0) {S1I = 0;}
delay(1); // Delay for stability.
}
I have had a look at pulse in and tried the following;
/* Simple Pulse Counter
Counts digital pulses fed into pin 14
*/
#include <LiquidCrystal.h> //library to load
LiquidCrystal lcd(12, 11, 10, 9, 8, 7); //Pin Configuration
const int S1 = 14; // This is where the input is fed.
int S1L = 0; // Variable for saving long pulses count.
int S1I = 0;
int S1S = 0; // Variable for saving short pulses count.
int S1II = 0;
void setup(){
pinMode(S1, INPUT);
lcd.begin(20, 4);
lcd.setCursor(2, 0); //positioning for text
lcd.print("MGB:S1 L 0 S 0"); //print text that will stay
}
void loop(){
if(pulseIn(S1, HIGH) > 500)
{
S1I = 1;
S1L++;
lcd.setCursor(12, 0); // this will overwrite the initial figure of 0
lcd.print(S1L);
}
if(pulseIn(S1, HIGH) < 500)
{
S1II = 1;
S1S++;
lcd.setCursor(17, 0); // this will overwrite the initial figure of 0
lcd.print(S1S);
}
delay(1); // Delay for stability.
}
Now, I am getting some separation in regards to pulses but not correct. the short pulse pounter is going up every second as well as on every click of the button whether long or short. the long pulse counter goes up slowly if i rapid click the button but even then it only goes up one after every five clicks.
You are measuring two separate pulses and </> 500 doesn't look at all like 110 +/- 30% or 390 +/- 30%. Especially since pulseIn() measure in microseconds and your 110 and 390 are in milliseconds (thousands of microseconds).
You want to record one pulseIn() value and THEN compare to the two pulse lengths:
110000 +/- 33000
390000 +/- 117000