I have been working on a system that reads from the 6 analog inputs calculates the low and high byte and places them in and array, it then adds an end character and then sends it over UDP. It is a modified (slightly) example from the 'Arduino Cookbook'.
Everything works fine as long as the value read by the sensors is higher than 255. As soon as the value drops below 255 and it has to send a zero the packet length of the data packet then shortens. I was wondering if anyone can help me make a system that always sends 12 bytes of data even if a sensor reads 0.
My code is as follows.
#include <SPI.h> // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUDP.h> // Arduino 1.0 UDP library
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC address to use
byte ip[] = {10, 0, 0, 201 }; // Arduino's IP address
byte send_ip[] = {10, 0, 0, 255 };
unsigned int localPort = 50000; // local port to listen on
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
// A UDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
void setup() {
Ethernet.begin(mac,ip);
Udp.begin(localPort);
;
}
void loop() {
sendAnalogValues(send_ip, 50000); // tell the sender the values of our analog ports
delay(1000); //wait a bit
}
void sendAnalogValues( IPAddress targetIp, unsigned int targetPort )
{
int index = 0;
for(int i=0; i < 6; i++)
{
int value = analogRead(i);
Serial.println(value);
packetBuffer[index++] = lowByte(value); // the low byte);
packetBuffer[index++] = highByte(value); // the high byte);
}
packetBuffer[12] = 0x78;// end of data
//send a packet back to the sender
Udp.beginPacket(targetIp, targetPort);
Udp.write(packetBuffer);
Udp.endPacket();
}
Thanks for any help.