hey everyone, i'm trying to use interrupts in my project
i'm trying to create UDP communication between 2 Arduinos
i want the Arduino to just to listen if there's i packet is coming and doesn't care about sending
until i make an interrupts , i wrote the following code , but the problem when i push the button it send more than one message it's like going to the interrupts multiple time
the code
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
int LED = 7; // for Recving
const int push_ON = 2;
const int push_OFF = 3;
int count = 1;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
//UDP_TX_PACKET_MAX_SIZE = 24 from EthernetUdp.h;
// network parameters
byte mac[ ] = { 0x90, 0xA2, 0xDA, 0x0E, 0x05, 0x02}; // ethernet interface MAC address
IPAddress localIp(10, 131, 41, 72); // local ip address
IPAddress destIp(10, 131, 41, 74); // destination ip address
unsigned int port = 5677; // destination port
unsigned int localPort = 5678; // local port to listen on for Recving
// EthernetUDP to send and receive messages.
EthernetUDP Udp;
char message_ON [ ] = "ON" ;
char message_OFF [ ] = "OFF" ;
// setup the arduino and shields
void setup() {
// pinMode(push_ON, INPUT);
// pinMode(push_OFF, INPUT);
attachInterrupt(0, blink, CHANGE);
attachInterrupt(1, blink, CHANGE);
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start ethernet and udp
Ethernet.begin(mac, localIp); // static ip version
// open UDP port
Udp.begin(port);
// show the local ip address (useful for dhcp)
Serial.print("Local IP: ");
Serial.println(Ethernet.localIP());
// now for Recving
Ethernet.begin(mac, localIp);
Udp.begin(localPort);
pinMode(LED, OUTPUT);
// Serial.begin(9600);
Serial.print("Local IP: ");
Serial.println(Ethernet.localIP());
}
// do tasks
void loop() {
int packetSize = Udp.parsePacket();
// Serial.println("wait 2 seocnd to cehack if there's packet");
// delay(2000);
if (packetSize)
{
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = Udp.remoteIP();
for (int i = 0; i < 4; i++)
{
Serial.print(remote[i], DEC);
if (i < 3)
{
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(Udp.remotePort());
// read the packet into packetBufffer
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
if (strcmp(packetBuffer, "ON" ) == 0) {
digitalWrite(LED, HIGH);
}
else if (strcmp(packetBuffer, "OFF" ) == 0) {
digitalWrite(LED, LOW);
}
//to print the packetBuffer
Serial.print("packetSize is ");
Serial.println(packetSize);
Serial.println("the message is: ");
for (int y = 0; y < packetSize; y++) {
Serial.print(packetBuffer[y]);
}
Serial.println();
//to clear the buffer
for (int x = 0 ; x < UDP_TX_PACKET_MAX_SIZE ; x++) {
packetBuffer[x] = '\0';
}
//to know the end of message
Serial.println("END THE MESSAGE");
}
}
void blink() {
if (digitalRead(push_ON) == LOW) {
// Serial.println("im ON");
Udp.beginPacket(destIp, port);
Udp.write(message_ON);
Udp.endPacket();
// Serial.println("Sending UDP message");
}
else if (digitalRead(push_OFF) == LOW) {
// Serial.println("im OFF");
Udp.beginPacket(destIp, port);
Udp.write(message_OFF);
Udp.endPacket();
// Serial.println("Sending UDP message");
}
}
what's the soulation plases