I've been trying to get these two arduinos mega 2560 talk to each other but they just don't do what i want. I don't know if i've set up the ip, the mac or what i've done wrong. I've tried using UDP to send a char through, turn that char into an int, and use that int to turn a set of leds to make a binary counter (From 0 to 7).
This if from the talking arduino:
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 92);
int i = 1;
unsigned int localPort = 8888;// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;void setup() {
// start the Ethernet and UDP:
Ethernet.begin(mac, ip);
Udp.begin(localPort);
Serial.begin(9600);Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(char(i));
Udp.endPacket();
Serial.begin(9600);
Serial.print(i);
char c = char(i);
Serial.print(char(i));
}void loop()
{
int packetSize = Udp.parsePacket();
if (packetSize)
{
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(char(i));
Udp.endPacket();
Serial.print("Received. ");
if (i<8) i=1;
else i++;
}
Serial.print("Loop. ");
delay(500);
}
And this is from the listener
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x10
};
IPAddress ip(192, 168, 1, 93);
int i;
unsigned int localPort = 8888;// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
void setup() {
// start the Ethernet and UDP:
Ethernet.begin(mac, ip);
Udp.begin(localPort);
Serial.begin(9600);pinMode(30, OUTPUT);
pinMode(31, OUTPUT);
pinMode(32, OUTPUT);Serial.print("Started. ");
}void loop()
{
int packetSize = Udp.parsePacket();
if (packetSize > 0)
{
char c = Udp.read();
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write('k');
Udp.endPacket();
i = (int)c;
for (int j=0; j<3; j++)
{
if (bitRead(i-1, j) == 0)
{
digitalWrite(30+j, LOW);
}
else digitalWrite(30+j, HIGH);
}
Serial.print("Received. ");
}
delay(500);
}
I've been trying to get the 'loop' advance one cycle whenever one of them gets the message of the other one.