Pages: [1]   Go Down
Author Topic: Weird unexplainable behaviour  (Read 280 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I have wrote a simple program that tests external interrupts.

tx is connected to the gate of a mosfet and pin2 is the input of a square wave that goes up and down twice on each loop.

Here is the code:

int tx = 12;     
volatile int x = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(tx, OUTPUT);
  attachInterrupt(0, increment, RISING);
}

void loop()
{
  digitalWrite(tx, HIGH);   
  delay(10);
  digitalWrite(tx, LOW);

  Serial.println(x, DEC);
  delay(1000);
}

void increment()
{
    x++;
}



When i dont turn my circuits battery on, the output is:

1
2
3
4
5
6
7.....

When it is on it is

1
345
346
600
623
689
1100...

Why am i getting such weirdness?

Surely when my external battery off and the external circut recieves no power, the input pin (2) should always be low and the output should be:
0
0
0
0
0
0

when it is on it should be
2
4
6
8
10...

I am totally confused by this.

Any tips or suggestions would be appreciated.

Thanks
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 224
Posts: 6619
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It sounds to me that you don't have some grounds connected. Post a schematic.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Pages: [1]   Go Up
Jump to: