High going pulse

Hi All, I have been using this program for a long time now but suddenly it doesn't work any more;
bool state=LOW;
int pin = 7;
int ledPin = 8;
void loop() {
(digitalRead(pin)==HIGH && state==LOW);{
delayMicroseconds (4500);
delayMicroseconds (4500);

I don't know now where I found the program using the line that measures a high going pulse. Can anybody give me an arduino example where this line is used? In my program all that is happening now is that the high and low delays are working. I have the hardware that is making a low to high going pulse. The line 'digital read' should be working like an 'if' statement and if no high going pulse is detected the program should not be using the delays or digital write.
So if anyone can give me an example program using the line detecting a high going pulse I will see if I can get it to work. Thanks.

See StateChangeDetection in the IDE examples.

Code doesn't stop working mysteriously, it doesn't break like a mechanical device. So if it did work before but not now, something changed. Either you edited and uploaded new, different code, or the characteristics of the inputs have changed and no longer match what the code's looking for.

If that line is supposed to act like an if, why is it not an if? Maybe that's the accidental edit you made?

(But the code you posted there is incomplete anyway, it has no setup(). And you initialise state as LOW then (at least in the code presented) never change it again.)

Is this the actual code in your Arduino right now?

Hi Sionaggutraidh, The code from the example which I cannot find now does not contain an ‘if’. I tried it anyway with the same result. Yes my code does have a setup I just left it out in the post to keep the post shorter. Apart from that it is the actual code.

I designed the hardware for a high going pulse myself. I tested it on LTspice simulation software. The pulse is nice and high at nearly 5 volts. I have change the circuit slightly which before gave a high of only 4v. I thought this was the problem but as I said earlier it worked perfectly for a long time.

I don’t have an oscilloscope to test the circuit. If you have one I would gladly let you have the circuit for testing. The problem could be a hardware failure on the circuit. But that doesn’t change the programming problem in that there should be no writing to the led or delays if the program has not picked up the high going pulse.

If you have the sketch where I obtained that line of code please let me have it so I can check my programming against it?

Hi Dr, Where would the State Change Detection be in the examples? What is it listed under? I will need that example program. Thanks.

(digitalRead(pin)==HIGH && state==LOW);

If that's the line that's supposed to be the "if" then it would surely need the word "if" at the front and to lose the ";" at the end...

if(digitalRead(pin)==HIGH && state==LOW)

State change detect is in the ide file / examples / 2 digital

edit- And here

None of the examples you give are the simple one line example which I found somewhere. The example needed a bool state at the declaration level.

Talking about a program mysteriously changing, it has Sion. I tried an ‘if’ in front and did away with the’;’ and the program is again working perfectly. As I said previously the program without the ‘if’ and with the’;’ was working perfectly for me in several programs that I will now have to change.

The answer must be in revisions the administrators make from time to time. I will post the line `(digitalRead(pin)==HIGH && state==LOW); on the forum and ask where this particular programming appeared because now it does not work anymore. It worked for me for a long time.```

`I am picking up the high going spike all the time now from my hardware circuit with the above change. Of course I was looking for a fault in my hardware circuit. Obviously there is no fault there. The hardware sends a high going spike to Arduino at zero crossing of the mains voltage. I have come across many very complicated zero crossing detectors that didn’t work properly. In the end I designed my own very simple detector.```


I find that very hard to believe...

I have no idea what that means...

What? Working perfectly? You're kidding. That line doesn't do anything, so the code following it will execute every time. Is that what you call "working"?

Revisions? What revisions?

Just to be clear, can you describe exactly what (digitalRead(pin)==HIGH && state==LOW); actually does when you use it? Perhaps that will help us understand it.

Hi Steve, The code picks up a low to high going pulse. In my application it is picking up a pulse at zero crossing of the mains voltage. The code following it does not execute if the pulse is not received. The program keeps looping until it picks up the pulse.

The line “`(digitalRead(pin)==HIGH && state==LOW);” with the curly brackets following worked for me for a long time. Then suddenly it stopped working. I could see nothing wrong with my zero crossing circuit. I then tried “if” in front of the above but made the mistake of leaving in “;” because an if statement doesn’t have this. ```

`And now as I said it is again working perfectly. You are right now the previous line I used now does nothing and the part in curly brackets is executed all the time. But this is not how it was for a long time. It worked with that line. That is why I think during one of the updates to Arduino the earlier line was made ineffective. ```

It makes more logical sense to have an “if” before digital read.

I doubt you're going to convince anyone of that, no matter how often you say it.

Nevertheless it is true Sionaggutraidh. I now have to go back to several programs that I previously used with that line and change them. Somewhere on the internet you will find that line which I used and which worked for some time.

I have a better idea... YOU find it :wink:

Sion is an abbreviation of your name. The issue is fixed now anyway so the matter can be dropped. I have no intention of convincing anyone.

When I lift code from anywhere I try to remember to stick the source url and author's details at the top of the sketch as a comment.

When you say it "worked", do you mean it worked like an 'if' statement? That is, the code in the curly braces executed only if the pin was HIGH and state was LOW?

How remarkable.

Anyway, it looks like we'll never be able to test that now.

Yes that’s right Steve it worked like an ‘if’ statement in that the code in the curly brackets was executed only if the pin was High and state was Low. Yes I agree it was remarkable.

But now it is working again when I added the if statement which is probably how it should have been in the first place.

Yes that would be a good idea.

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