Zero crossing without delay

Hi all,
I have the following code,which outputs a 1ms pulse at every zero crossing of a sine wave.

#define readPin 3
#define outPin 2
bool state;


void setup() {
  pinMode(outPin, OUTPUT);
  pinMode(readPin, INPUT);
}

void loop() {
  if(digitalRead(readPin) == HIGH)state = 0;
  if(state == 0 && digitalRead(readPin) == LOW){
    digitalWrite(2,HIGH);
    state = 1;
    delay(1);
    digitalWrite(2,LOW);
  }
}

I want to modify the code by using timers to avoid problems when using push buttons to change the pulsewidth.I tried the millis() function,but can't figure out why is it not working like that;the output is always high.Here is the code for that:

#define readPin 3
#define outPin 2
bool state = 0,pulse = 0;
unsigned long prevmillis;


void setup() {
  pinMode(outPin, OUTPUT);
  pinMode(readPin, INPUT);
}

void loop() {
 if(!pulse){ 
  if(digitalRead(readPin) == HIGH)state = 0;
  if(state == 0 && digitalRead(readPin) == LOW){
    digitalWrite(2,HIGH);
    state = 1;
    pulse = 1;
    prevmillis = millis();
  }
 }
 else{
  if(millis() == prevmillis + 1){
    digitalWrite(2, LOW);
    pulse = 0; 
  }
 }
}

Any ideas would be appreciated!

Any ideas would be appreciated!

Useful names would, too.

state? Of what? The union? Your d**k?

if(millis() == prevmillis + 1)

That's not safe. See the Blink Without Delay example for an example of how to do this right. It should involve subtraction and the greater than operator.

Remember, every now and then millis skips a number. If you want to time off 1 millisecond then maybe you should use micros instead of millis.

You need to use micros not mills if you want a 1ms delay

Mark

PaulS:
state? Of what? The union? Your d**k?

Don't mind Paul. He's a bit, how shall we say, "Special". Your code is short enough that everyone else can see what your state variable does. Paul has an issue, if he can't see the problem then he feels inadequate so he lashes out and uses abusive language with the person who asked the question he isn't smart enough to answer. He can't just leave it for one of us who actually has some reading skills to come along and help.

Don't worry, he's just an old nobody. I guess his mom didn't teach him how to be considerate of anything. Not all of us have good mothers who teach us good things. Some folks have crack whores for moms. That doesn't make them bad people, but it sometimes makes them hard to be around. And maybe that's what we have with good ole PaulS. Or maybe it's something else. Who knows. It's usually best if you just ignore him and pretty soon he'll be back outside yelling at the clouds or something.

He is at his best when he is making up his own definitions for words, like "button", and then berates anyone who uses the commonly accepted (Websters / Oxford) definition instead of his special made up definition and calls them stupid for using words the way they are used in real English instead of his special autist speak language.

Hi again,
I managed to get the code working,by using the micros() function and also by changing the == to >= in these type of situations: if(millis() == prevmillis + 1)

I just think that those kind of people like the first answerer should not be allowed to comment offensive messages,no matter if they are just too lazy to answer,or know nothing.

Thanks for the help!

SmileXS4:
Hi again,
I managed to get the code working,by using the micros() function and also by changing the == to >= in these type of situations: if(millis() == prevmillis + 1)

That's still wrong. If you let the code run for a long time you will eventually find that one interval waits for 49 days. This must always be done with subtraction to take advantage of the variable rollover.

Do it the right way, the way that it is done in all the Blink Without Delay examples. There's plenty of tutorials around on how that works.

SmileXS4:
I just think that those kind of people like the first answerer should not be allowed to comment offensive messages,no matter if they are just too lazy to answer,or know nothing.

I think @PaulS should really be ashamed of himself for his continued use of vulgarity and profanity around children. The man has no shame. But what are you gonna do? This is the internet and there are all kinds of horrible people on the internet. You can't get rid of them. So you just have to ignore them.

What a pitiful little excuse for a man though. Don't you agree? I feel for him. I wish I could help him see that there is more to life than building yourself up in your own mind be being mean to others. You know how rotten his soul must be. It must really be horrible to live in that skin.

I think that I explained incorrectly.Here is the loop code,you can take a closer look at it,but I tested it out and works perfectly.

  if(!pulse){ 
  if(digitalRead(readPin) == HIGH)state = 0;
  if(state == 0 && digitalRead(readPin) == LOW){
    digitalWrite(2,HIGH);
    state = 1;
    pulse = 1;
    previousMicros = currentMicros;
  }
 }
 else{
  if(currentMicros - previousMicros >= pw){
    digitalWrite(2, LOW);
    previousTurn = currentMillis;
    pulse = 0; 
  }
 }

If you wonder what is the purpose of the code,well it is a mains sync interrupter for my Solid State Tesla Coil.

if(currentMicros - previousMicros >= pw){

OK, that's the right way.