I'm controlling 2 one meter led strips, they are to show bandwidth usage. I have an Arduino Uno with the Ethernet shield. The program goes to a server with a txt file and reads the bandwidth it is formatted like this <up,down,>. What happens is after awhile the Arduino just stops. I'm not sure whats happening. My guess is the Arduino is running out of RAM. Here is my code.
#include <SPI.h>
#include <Ethernet.h>
#include <Adafruit_NeoPixel.h>
#define PINUP 6
#define PINDOWN 5
#define NUMLED 60
#define WAIT 20
#define POTPIN 0
//#define DEBUG
//const float MAX_BAND = 1000; // In megabits i.e 1Gbit/s = 1000
Adafruit_NeoPixel stripUp = Adafruit_NeoPixel(NUMLED, PINUP, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel stripDown = Adafruit_NeoPixel(NUMLED, PINDOWN, NEO_GRB + NEO_KHZ800);
EthernetClient client;
String splitString(String s, char parser,int index);
int upLED = 0;
int downLED = 0;
void setup()
{
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
#ifdef DEBUG
Serial.begin(9600);
#endif
stripUp.begin();
stripDown.begin();
stripUp.show(); // Initialize all pixels to 'off'
stripDown.show(); // Initialize all pixels to 'off'
stripUp.setBrightness((255 - map( analogRead(POTPIN), 0, 1024, 0, 255 ) ));
stripDown.setBrightness((255 - map( analogRead(POTPIN), 0, 1024, 0, 255 ) ));
if (Ethernet.begin(mac) == 0)
{
error();
#ifdef DEBUG
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;) ;
#endif
}
setLED();
// Set the strip
setStrip(upLED, stripUp, true);
setStrip(downLED, stripDown, true );
}
void loop()
{
stripUp.setBrightness((255 - map( analogRead(POTPIN), 0, 1024, 0, 255 ) ));
stripDown.setBrightness((255 - map( analogRead(POTPIN), 0, 1024, 0, 255 ) ));
setStrip(upLED, stripUp, false);
setStrip(downLED, stripDown, false);
int upOldLED = upLED;
int downOldLED = downLED;
#ifdef DEBUG
if (Serial.available() > 0 )
{
upOldLED = upLED;
downOldLED = downLED;
upLED = percentOfStrip(Serial.parseInt());
Serial.println(upLED);
if (upOldLED != upLED)
{
if (upOldLED < upLED)
{
Serial.println("Should be higher");
setStrip(upLED, stripUp, true);
}
else{
Serial.println("Should be lower");
turnOffStrip(stripUp, upOldLED, upLED);
}
}
}
#endif
setLED();
if (upOldLED != upLED)
{
if (upOldLED < upLED)
{
#ifdef DEBBUG
Serial.println("Should be higher");
#endif
setStrip(upLED, stripUp, true);
}
else{
#ifdef DEBBUG
Serial.println("Should be lower");
#endif
turnOffStrip(stripUp, upOldLED, upLED);
}
}
if (downOldLED != downLED)
{
if (downOldLED < downLED)
{
#ifdef DEBBUG
Serial.println("Should be higher");
setStrip(downLED, stripDown, true);
#endif
}
else{
#ifdef DEBBUG
Serial.println("Should be lower");
turnOffStrip(stripDown, downOldLED, downLED);
#endif
}
}
#ifdef DEBBUG
Serial.println(upLED);
Serial.println(downLED);
#endif
}
void setLED()
{
String pageValue = connectAndRead(); //connect to the server and read the output
#ifdef DEBUG
Serial.println(pageValue); //print out the findings.
#endif
upLED = percentOfStrip(splitString(pageValue,',',0).toInt());
downLED = percentOfStrip(splitString(pageValue,',',1).toInt());
#ifdef DEBUG
Serial.println(upLED);
Serial.println(downLED);
#endif
}
void setStrip(int ledToLight, Adafruit_NeoPixel &strip, bool wait)
{
int ledLit = 0;
while (ledToLight > ledLit)
{
if (ledToLight > 0)
{
for(int i=0; i <= 9; i++)
{
strip.setPixelColor(i, strip.Color(0, 255, 0));
strip.show();
ledLit++;
if (wait)
delay(WAIT);
if (ledToLight == ledLit)
break;
}
}
if (ledToLight > 10)
{
int red = 12;
for(int i=10; i <= 29; i++)
{
strip.setPixelColor(i, strip.Color(red, 255, 0));
red += 12;
ledLit++;
strip.show();
if (wait)
delay(WAIT);
if (ledToLight == ledLit)
break;
}
}
if (ledToLight > 30)
{
for(int i=30; i <= 39; i++)
{
strip.setPixelColor(i, strip.Color(255, 255, 0));
strip.show();
ledLit++;
if (wait)
delay(WAIT);
if (ledToLight == ledLit)
break;
}
}
if (ledToLight > 40)
{
int green = 255;
for(int i=40; i <= 49; i++)
{
strip.setPixelColor(i, strip.Color(255, green, 0));
green -= 25;
strip.show();
ledLit++;
if (wait)
delay(WAIT);
if (ledToLight == ledLit)
break;
}
}
if (ledToLight > 50)
{
for(int i=50; i <= 59; i++)
{
strip.setPixelColor(i, strip.Color(255, 0, 0));
strip.show();
ledLit++;
if (wait)
delay(WAIT);
if (ledToLight == ledLit)
break;
}
}
}
}
void turnOffStrip(Adafruit_NeoPixel &strip, int oldLit, int newLit)
{
for(int i = oldLit; i > newLit; i--)
{
strip.setPixelColor(i, strip.Color(0, 0, 0));
strip.show();
delay(WAIT);
}
}
void error ()
{
stripUp.setBrightness(255);
stripDown.setBrightness(255);
stripUp.show();
stripDown.show();
for (int i = 0; i < 3; i++)
{
colorWipe(stripUp, stripUp.Color(255, 0, 0));
colorWipe(stripDown, stripDown.Color(0, 0, 0));
stripUp.show();
stripDown.show();
delay(5);
colorWipe(stripUp, stripUp.Color(0, 0, 0));
colorWipe(stripDown, stripDown.Color(255, 0, 0));
stripUp.show();
stripDown.show();
}
colorWipe(stripUp, stripUp.Color(255, 0, 0));
colorWipe(stripDown, stripDown.Color(255, 0, 0));
stripUp.show();
stripDown.show();
}
void colorWipe(Adafruit_NeoPixel &strip, uint32_t c)
{
for(int i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
}
}
int percentOfStrip(float band)
{
return (band / 1000) * NUMLED;
}
String connectAndRead(){
IPAddress server(158,91,5,101);
#ifdef DEBUG
Serial.println("connecting...");
#endif
if (client.connect(server, 80)) {
#ifdef DEBUG
Serial.println("connected");
#endif
client.print(F("GET "));
client.println(F("/bandwidth.txt HTTP/1.0"));
client.println();
//Connected - Read the page
return readPage(); //go and read the output
}
else{
error();
return "connection failed";
}
}
String readPage(){
char inString[15];
boolean startRead = false; // is reading?
int stringPos = 0;
memset( &inString, 0, 15 ); //clear inString memory
while(true){
if (client.available()) {
char c = client.read();
if (c == '<' ) { //'<' is our begining character
startRead = true; //Ready to start reading the part
}
else if(startRead){
if(c != '>'){ //'>' is our ending character
inString[stringPos] = c;
stringPos ++;
}
else{
//got what we need here! We can disconnect now
startRead = false;
client.stop();
client.flush();
#ifdef DEBUG
Serial.println("disconnecting.");
#endif
return inString;
}
}
}
}
}
String splitString(String s, char parser,int index){
// string for incoming serial data
// string index counter
int parserCnt=0;
int rFromIndex=0, rToIndex=-1;
while(index>=parserCnt){
rFromIndex = rToIndex+1;
rToIndex = s.indexOf(parser,rFromIndex);
if(index == parserCnt){
if(rToIndex == 0 || rToIndex == -1){
return '\0';
}
return s.substring(rFromIndex,rToIndex);
}
else{
parserCnt++;
}
}
return '\0';
}
I think it may have to do with how I pass the LED strip to other functions.
void colorWipe(Adafruit_NeoPixel &strip, uint32_t c)
Is that right? I'm trying to pass it as a reference that way I'm not creating new strips every time it runs that function. Any ideas?