Reaing in a sine wave - 0V crossing detector

I am trying to read in a pulse train with the Arduino Mega 2560

I have to read in two pulse trains simultaneously, and to count the number of times the input changes to high therefore counting the number of times it crosses the 0V point. I am trying to use the interupt funtion to to since i have two inputs.

I havnt got the function generator yet but im trying to simulate it. I want to use this code:

int pin = 13; volatile int state = LOW;

void setup() { attachInterrupt(1, blink, CHANGE); pinMode(pin, OUTPUT); }

void loop() { digitalWrite(pin, state);

}

void blink() { state = !state; }

I attached a button to port 2 (interrupt 1) to simulate a high. There is large delay between when i release the button and the led goin off. I have tried changing the button.

I am worried when i input the sine wave it wont be able to record the crossings sufficently because of the large delay.

Help! ----> im new Arduino user

Not read in a ulse train, read in a sine wave

It could be that you have contact bounce and that is messing things up.

I have removed the button and just touched 2 wires together instead. when i look at the serial read output it still shows a high long after i released the the wires?

when i look at the serial read output it still shows a high long after i released the the wires?

There is not serial output in the code you posted.

Whether you are using a switch, or pretending to with two wires, there is still contact bounce to be dealt with.

Also, if you don't have a pull-up or pull-down resistor, you have a floating pin condition when the switch is not pressed/wires not connected. That will completely mess up your interrupt.

This could be a stupid question, but is your input being pulled against the switch? I mean if the button takes it low it should have something to pull it high when the button is open - or vice versa if the button takes it high.

Edit to add: too slow!

Yes I think PaulS has it right your pins are floating, that is why there is a delay. See:- http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

It works thanks so much! i added a pull down resistor.

I was wrong it still does not work: even when i upload the code with there output commented out it still just blinks??

#include <SoftwareSerial.h>

int pin = 10;
volatile int state = LOW;

void setup()
{
Serial.begin(9600);

pinMode(pin, OUTPUT);
attachInterrupt(1, blink, RISING);

}

void loop()
{
// digitalWrite(pin, state);
// goTo(0);
// Serial.print("State ") ;
// Serial.print(state);
//delay(10);
}

void blink()
{
state = !state;
}

This doesn’t make sense? Why is the led blinking??

I want it to stay alight after iv pushed the button, and only when i push it again for it to switch off.

The only code there that can run after initialisation is the interrupt handler, which doesn't do any output.

If your Arduino is still blinking, I'd suggest it's not running the code you're looking at.

because your digitalWrite() is commented out?

PeterH: The only code there that can run after initialisation is the interrupt handler, which doesn't do any output.

The interrupt handler is blink() but as soon as it's done, execution goes back to loop(). But since he commented everything out in loop(), the change in state that blink() makes does nothing.

The led seems to be faulty. when i replaced it it works now with the code commented back in. But its not working so great. i guess its coz of the contact bounce...

You could be accidentally readying the processor. Put something in the setup like a serial print to seeif you are.

BTW, the thread title says 0V crossing detector. That implies negative V, as in AC, on the pin. You're not doing that now and don't want to in future.

Digital input triggers from HIGH to LOW at one voltage and from LOW to HIGH at a higher voltage. They aren't the same. IIRC the word is hysteresis.

You might add a bit of debug info, a line in blink() saying

Serial.print(".");

then watch to see if you have bounce by the dots.....