Hi all!
My first post on this great forum ![]()
First of all, I am a beginner so do not be too hard on me ...
I have a program that writes strings to a LED matrix. These come via a UDP server. I look at the first character in the string to estimate the string that is to be updated.
My problem is that strcpy seems to destroy something which is demonstrated in the if (readString.substring (0,1) ==" 1 ") can only be run once.
Debug from first time a string is sent:
This is the data!
1Hi!
This is the read string!
1Hi!
This is one substring
1
IF 1
This is the charBuf:
Hi!
Debug second time a string is sent:
This is the data!
1Hi!
This is the read string!
1Hi!
This is one substring
1
The program will not not get into the if statement though it's a 1 as the first character.
Would be grateful for any tips on how I should do to resolve this.
Regards, Rickard
Here is the code
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#include <EtherCard.h>
#include <IPAddress.h>
#define STATIC 1 // set to 1 to disable DHCP (adjust myip/gwip values below)
#if STATIC
// ethernet interface ip address
static byte myip[] = { 192,168,17,182 };
// gateway ip address
static byte gwip[] = { 192,168,17,31 };
#endif
// ethernet mac address - must be unique on your network
static byte mymac[] = { 0x70,0x69,0x69,0x2D,0x30,0x31 };
byte Ethernet::buffer[500]; // tcp/ip send and receive buffer
#define MAX_DEVICES 2
#define CLK_PIN 3 // or SCK
#define DATA_PIN 4 // or MOSI
#define CS_PIN 5 // or SS
MD_Parola P = MD_Parola(DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
#define PAUSE_TIME 1000 // in milliseconds
#define SPEED_DEADBAND 5 // in analog units
// Global variables
String readString = ""; //string for fetching data from address
textEffect_t effect[] =
{
PRINT,
SLICE,
WIPE,
WIPE_CURSOR,
OPENING,
OPENING_CURSOR,
CLOSING,
CLOSING_CURSOR,
BLINDS,
DISSOLVE,
SCROLL_UP,
SCROLL_DOWN,
SCROLL_LEFT,
SCROLL_RIGHT,
SCROLL_UP_LEFT,
SCROLL_UP_RIGHT,
SCROLL_DOWN_LEFT,
SCROLL_DOWN_RIGHT,
SCAN_HORIZ,
SCAN_VERT,
GROW_UP,
GROW_DOWN,
};
uint8_t curString = 0;
char *pc[] =
{
"1",
"2"
};
char *startup[] =
{
"Display Serv"
};
#define NEXT_STRING ((curString + 1) % ARRAY_SIZE(pc))
//callback that prints received packets to the serial port
void udpSerialPrint(word port, byte ip[4], const char *data, word len) {
IPAddress src(ip[0], ip[1], ip[2], ip[3]);
Serial.println(src);
Serial.println(port);
Serial.println(data);
Serial.println(len);
Serial.println("This is the data!");
Serial.println(data);
readString = data;
Serial.println("This is readString!");
Serial.println(readString);
Serial.println("This is substring 1");
Serial.println(readString.substring(0,1));
if (readString.substring(0,1) == "1")
{
Serial.println("If 1");
char charBuf[10];
readString.substring(1).toCharArray(charBuf, 8);
Serial.println("This is the charBuf: ");
Serial.println(charBuf);
strcpy(pc[0], charBuf);
}
if (readString.substring(0,1) == "2")
{
Serial.println("If 2");
char charBuf[20];
readString.substring(1).toCharArray(charBuf, 8);
Serial.println("This is the charBuf: ");
Serial.println(charBuf);
strcpy(pc[1], charBuf);
}
}
void setup(){
Serial.begin(57600);
Serial.println(F("\n[Displayserver UDP 328]"));
if (ether.begin(sizeof Ethernet::buffer, mymac, 10) == 0)
Serial.println(F("Failed to access Ethernet controller"));
#if STATIC
ether.staticSetup(myip, gwip);
#else
if (!ether.dhcpSetup())
Serial.println(F("DHCP failed"));
#endif
ether.printIp("IP: ", ether.myip);
ether.printIp("GW: ", ether.gwip);
ether.printIp("DNS: ", ether.dnsip);
//register udpSerialPrint() to port 8888
ether.udpServerListenOnPort(&udpSerialPrint, 8888);
Serial.println("Display-Server started");
// parola object
P.begin();
P.displayText(startup[0], CENTER, P.getSpeed(), PAUSE_TIME, PRINT, PRINT);
delay(2000);
P.displayText(pc[curString], CENTER, P.getSpeed(), PAUSE_TIME, PRINT, PRINT);
curString = NEXT_STRING;
P.setTextEffect(effect[10], effect[10]);
P.displayReset();
P.setSpeed(10);
P.setPause(2000);
P.setIntensity(0);
}
void loop(){
//this must be called for ethercard functions to work.
ether.packetLoop(ether.packetReceive());
if (P.displayAnimate())
{
P.setTextBuffer(pc[curString]);
P.displayReset();
curString = NEXT_STRING;
}
}