Hi all,
I'm very new to Arduino and programming of this kind in general.
I've bashed together a sketch to make a BlinkM toggle colours via UDP for a tally light system. I thought I'd cracked it today but I've found that it works 35 times and then stops. It will work again after a reset but does the same again. I think it needs the packet clearing or something like that but can't seem to work out how to achieve this. Please help.
#include <Ethernet.h>
#include <WString.h>
#include <Wire.h>
#include <BlinkM_funcs.h>
#include <UdpString.h>
#include <WString.h>
#define maxLength 14
String inString = String(maxLength);
byte mac[] = { 0x51, 0x45, 0x44, 0x31, 0x30, 0x31 }; // mac address
byte ip[] = { 172, 16, 17, 101 }; // Arduino's IP address
int localPort = 3002; // local port to listen on
String packet(32); //packet can be max 32 bytes long
byte remoteIp[4]; // holds recvieved packet's originating IP
unsigned int remotePort[1]; // holds received packet's originating port
int i;
int r;
int g;
int b;
String binaryString(4);
/* SETUP: init Ethernet shield, start UDP listening, open serial port */
void setup() {
Ethernet.begin(mac,ip);
UdpString.begin(localPort);
Serial.begin(9600);
Wire.begin();
BlinkM_beginWithPower();
BlinkM_stopScript( 0x00 );
BlinkM_setRGB( 0x00, 0xFF, 0x00, 0x00 );
delay (1000);
BlinkM_setRGB( 0x00, 0xFF, 0xFF, 0x00 );
delay (1000);
BlinkM_setRGB( 0x00, 0x00, 0xFF, 0x00 );
delay (1000);
BlinkM_setRGB( 0x00, 0x00, 0x00, 0xFF );
delay (1000);
}
/* LOOP: wait for incoming packets and print each packet to the serial port */
void loop() {
boolean current_line_is_blank = true;
// if there's data available, read a packet
if(UdpString.available()) {
int packetSize = UdpString.readPacket(packet,remoteIp,remotePort);
char* c = packet;
if (inString.length() < maxLength) {
inString.append(c);
}
if (inString.contains("$")) {
int Pos_r = inString.indexOf("r");
int Pos_g = inString.indexOf("g");
int Pos_b = inString.indexOf("b");
int End = inString.indexOf("!");
r = atoi(inString.substring((Pos_r+1), (Pos_g)));
g = atoi(inString.substring((Pos_g+1), (Pos_b)));
b = atoi(inString.substring((Pos_b+1), (End)));
BlinkM_setRGB( 0x00, r, g, b );
}
}
//wait a bit
delay(10);
inString = "";
packet = "";
}