The Arduino Code:
/*
UDPSendReceiveString:
This sketch receives UDP message strings, prints them to the serial port
and sends an “acknowledge” string back to the sender
A Processing sketch is included at the end of file that can be used to send
and received messages for testing with a computer.
created 21 Aug 2010
by Michael Margolis
This code is in the public domain.
*/
#include <SPI.h> // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
#define ENCODER_A_PIN 2
#define ENCODER_B_PIN 3
#define SWITCH_PIN 4
long p1;
long p2;
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 232);
IPAddress TargetIP(192, 168, 1, 121);
unsigned int localPort = 8999; // local port to listen on
// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char udpstr_head[] = "setInput "angle" “; // a string to send back
char udpstr_tail[] = " 0\r\n”; // a string to send back
char pos1[64];
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
void setup() {
pinMode(ENCODER_A_PIN, INPUT);
pinMode(ENCODER_B_PIN, INPUT);
pinMode(SWITCH_PIN, INPUT);
pinMode(ENCODER_A_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(ENCODER_A_PIN), read_quadrature, FALLING);
// start the Ethernet and UDP:
Ethernet.begin(mac, ip);
Udp.begin(localPort);
Serial.begin(115200);
}
void loop() {
//if (p2 != p1){
Udp.beginPacket(TargetIP, 3040);
ltoa(p1,pos1,10);
//Udp.write(udpstr_head);
Udp.write(“\r”);
Udp.write(pos1);
Udp.write(“\n”);
//Udp.write(udpstr_tail);
Udp.endPacket();
p1=p1+10;
delay(8);
//p2 = p1;
//t2 = t1;
//}
}
void read_quadrature() {
if (digitalRead(ENCODER_B_PIN) == LOW) {
p1++;
}
else {
p1–;
}
}
char* itostr(char *str, int i)
{
sprintf(str, “%d”, i);
return str;
}