Go Down

Topic: Attach Interrupt inconsistencies (Read 22980 times) previous topic - next topic

dan744

HI All,

I am running an Arduino Due board, where I would like to connect 30 push buttons, and when a button is pressed, I send a message through tcp/ip with the button pressed and state (HIGH/LOW).

I followed the tutorial, and was able to detect when the button is pressed from High to Low, as the default state is HIGH. However, when testing, sometimes, the state remains LOW, even thought the button is not pressed (eg. ground to pin is disconnected), I am not sure what is wrong with this part.

Here is the code:
------------------------ Start of Code -------------------
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 1, 178);
IPAddress serverRemote(192, 168, 1, 75);
EthernetClient client;
const int serverRemotePort = 8000;
const int pin1 = 1;
const int pin2 = 2;
const int pin3 = 3;
volatile int pinChanged = 100;
volatile int newState = 100;
boolean pin1Changed = false;
boolean pin2Changed = false;
boolean pin3Changed = false;

void setup() {
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
    while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
   if (client.connect(serverRemote, serverRemotePort)) {
    Serial.println("connected");
  }
  Serial.println("Starting..");
  digitalWrite(pin1,HIGH);
  digitalWrite(pin2,HIGH);
  digitalWrite(pin3,HIGH);
  attachInterrupt(1,pci1,CHANGE);
  attachInterrupt(2,pci2,CHANGE);
  attachInterrupt(3,pci3,CHANGE); 
}

void loop() {
  if (pin1Changed) {
      client.print("Pin  1 changed to ");
      client.println(digitalRead(pin1));
      pin1Changed = false;
  }

  if (pin2Changed) {
      client.print("Pin  2 changed to ");
      client.println(digitalRead(pin2));
      pin2Changed = false;
  }

  if (pin3Changed) {
      client.print("Pin  3 changed to ");
      client.println(digitalRead(pin3));
      pin3Changed = false;
  }

if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  if (client.connect(serverRemote, serverRemotePort)) {
    Serial.println("connected");
  }
}
}

void pci1() {
if ( digitalRead(pin1) == HIGH)  digitalWrite(pin1,HIGH);
pin1Changed = true;
}

void pci2() {
if ( digitalRead(pin2) == HIGH) digitalWrite(pin2,HIGH);
pin2Changed = true;
}

void pci3() {
if ( digitalRead(pin3) == HIGH) digitalWrite(pin3,HIGH);
pin3Changed = true;
}
  --------------------- End of Coce ------------------

I appreciate your help,

Thanks,

Dan,

MarkT

You must define the pins as inputs with pullups enabled:

Code: [Select]

  pinMode (pin1, INPUT_PULLUP) ;
  pinMode (pin2, INPUT_PULLUP) ;
  pinMode (pin3, INPUT_PULLUP) ;
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up