Digital Pulse Counter - Programming Queries

Hello,

I've designed a working circuit that, when the circuit completes it should create a 5V high pulse, which lasts up to a maximum of 0.5s.

I've then wired up my mega2560 to monitor an input and to then update, adding 1 count for every pulse, if the pulse stays high it shouldn't add to the count.

I have several queries regarding this and I'll try to number them:

1st. What does, what I've actually typed...mean, I've managed to skif together this program by looking at other similar type things but I'm not 100% sure what everything does, short of the very very basics in programming an arduino.. (I am new)

2nd. When I open the Serial Monitor, it is updating constantly, even when there are no changes, it spams this very quickly to the point it does sometimes miss a pulse, I want to point out that my pulses are by no means fast, I'm looking for this circuit to maybe complete like at most, a few times in under 5 seconds. so, how can I have the Arduino monitor the pin for changes in digital input without updating the Serial when there are no changes.

3rd. I'm looking to then export this count onto an LCD display or some form of digital counter, can anyone please advise me on this as I have absolutely no idea where or how to start.. :<

I've also attached a drawing of the circuit which creates the pulse, it is working in its current form on a breadboard but, it is incomplete as I have been unable to get the arduino to count in a way I am comfortable with in the last like 4 days!

Background: I'm pretty much brand new to Arduino programming and base level electronics, so this is a big learning curve. you can contact me on Telegram @DieselRulez if you want to have a more 1 to 1, live conversation with me, I'd very much appreciate the help and can show video's of whats going on incase I've described it poorly :>

The Circuit: The point above "C" is the +5V supply and all of the Grounds are connected together, the lower half can be ignored as its not actually needed right now, I am focusing on the circuit that is made between the two AND gates to achieve a U/S Count (Upstream Count).

photo_2021-08-15_20-47-56|236x500

The Code (Credit to whomever made the actual count code, this was not me!:

int AND1I = 2;
int AND1O = 3;
const int AND2I = 4;
int AND2O = 5;
int var = 0;
int pulse = 0;

void setup() {
  // put your setup code here, to run once:
pinMode(2, INPUT);
pinMode(4, INPUT);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
Serial.begin(9600);
Serial.println(F("no pulses yet... >:I"));
Serial.print(F("Program Entering Loop"));
}

void loop() {
  // put your main code here, to run repeatedly:
if (digitalRead(AND1I) == HIGH)
{
digitalWrite(AND1O, HIGH);
delay(1000);
}
if (digitalRead(AND1I) == LOW)

{ digitalWrite(AND1O, LOW);}
//BLANK FOR NEW LAYER OF CODE FOR PULSE COUNT
  if(digitalRead(4) > var) { var = 1; pulse++;
}
Serial.print(pulse); Serial.print(F(" pulse"));

// Put an "s" if the amount of pulses is greater than 1. if(pulse > 1) {Serial.print(F("s"));}

Serial.println(F(" detected."));

if(digitalRead(4) == 0) {var = 0;}
delay(50); 
}
// Delay for stability. }

Does this mean you only add the count if the pulse is less than certain value? For example, if the pulse is less than half a second, you add that, but if its more than 500 ms, it is not counted?

Gammon Forum : Electronics : Microprocessors : State machines

Hi Hzrnbgy,

no the duration of the pulse doesn't necessarily matter, it could be an instantaneous pulse if the Arduino could catch it, I've just added a 0.5s delay as I'm still "testing" my circuit, the problem I have is, its directional. the top half of the circuit is for counting up, i.e, if the left switch is triggered first and then the right switch, the bottom half of the circuit will be for counting down, so the right switch is triggered first and then the left. I needed a delay so as to make sure the AND gate logic would still achieve a true output, without being disabled by the relay when the other switch gets operated.

its also worth noting that, the up and down count do not detract from each other. there will be a separate counter for counting up and a separate one for counting down :slight_smile: .

Diesel

Maybe draw a waveform describing the input and what you want to do at each section?

This part

if (digitalRead(AND1I) == HIGH)
{
digitalWrite(AND1O, HIGH);
delay(1000);
}
if (digitalRead(AND1I) == LOW)

{ digitalWrite(AND1O, LOW);}
//BLANK FOR NEW LAYER OF CODE FOR PULSE COUNT
  if(digitalRead(4) > var) { var = 1; pulse++;
}

looks like when you detect HIGH on input AND1I, you set AND1O high, and if you see low on AND1I, you clear AND1O. And then there's the extra code after that.

I think it is easier if you can "draw" the behavior of the system

Uuhhm, I'll give it a shot I think? can you maybe give an example just so I know what you're looking to see?

I'll try and go through this tomorrow, but it looks good! thanks for the source :>

so, the reasion AND1I and AND1O exist, is because I don't have an delay Timer, the Arduino is filling in this purpose by monitoring AND gate A with an Input (looking for a HIGH) then producing a 0.5s long HIGH Pulse that goes to gate B. (I could eliminate that section of code if I had a 5V+ latch timer for 0.5s. so, AND1I is the output from Gate A going into the Arduino, AND1O is the output from the Arduino going into gate B!

I did attach a wave form but I'm not sure if its what you are meaning!

Diesel

The Arduino can perform an "and" operation very easily in software, and rapidly, only a few hundred nanoseconds. Also why not just feed both inputs into two input pins and do everything in software?

Cause I want to learn electronics aswell so it seemed like a good chance to practice a level of coding with a level of basic electronics!

The StateChangeExample sketch that ships with the IDE.

UPDATE:

So, Statechange did the trick! took a bit of buggering about to figure out how to do it, unfortunately my Arduino didn't come with an example of that but I was able to find some from the links you guys posted, thanks very much for the feedback!

<3

Examples->Digital->StateChangeDetection in the IDE

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.