Hello forum. I am experimenting with the ethernet shield and have made a basic webpage that randomly generates a 2 digit number that I am displaying via a MAX7219 to two 7 segment LEDS. Exciting stuff I know, but this is a starting off point for a larger project!
Everything seems to be running along just dandy and the 7 segments are happily displaying their numbers but then randomly the 7 segments will go blank (off) and will not come back. This happens sometimes after 10+ loops and sometimes after only 2. I would suspect my wiring (not ruling it out though!) but it seems to work just fine for awhile. Resetting it will get things going again but then it'll blank out after a bit.
Below is my code.. Is there something that I've done wrong here to cause this to happen? I added the disp_cnt variable to see if it was always happening after X amount of loops but saw no consistency whatsoever. Any advice would be greatly appreciated!! Thanks!
/*
Silly Data Getter
*/
#include <SPI.h>
#include <Ethernet.h>
#include "LedControl.h"
//digits
int disp1 = 0; //digit 1
int disp2 = 1; //digit 2
char Str[11];
int num = 0;
int disp_cnt = 0;
long pollingInterval = 10000; // in milliseconds
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x98, 0x31 };
char serverName[] = "server.com";
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
/*
Now we need a LedControl to work with.
pin 4 -> DataIn | pin 3 -> CLK | pin 2 -> LOAD | One MAX72XX.
*/
LedControl lc=LedControl(4,3,2,1);
void setup() {
/*
The MAX72XX is in power-saving mode on startup,
we have to do a wakeup call
*/
lc.shutdown(0,false);
/* Set the brightness to a medium values */
lc.setIntensity(0,8);
/* and clear the display */
lc.clearDisplay(0);
// start the serial library:
Serial.begin(9600);
//pinMode(relayPin, OUTPUT);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
while(true);
}
// give the Ethernet shield time to initialize:
delay(2000);
}
void displayServing(int number) {
disp_cnt++;
Serial.println(disp_cnt);
int dig_ones;
int dig_tens;
//set ones and tens
dig_ones = number%10;
dig_tens = number/10;
//now serving
lc.setDigit(0,disp1,dig_tens,false);
lc.setDigit(0,disp2,dig_ones,false);
delay(250);
}
void loop()
{
Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect(serverName, 80)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET path/to/data");
client.println();
int timer = millis();
delay(1000);
}
else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
// if there's data ready to be read:
if (client.available()) {
int i = 0;
//put the data in the array:
do {
Str[i] = client.read();
i++;
delay(1);
} while (client.available());
// Pop on the null terminator:
Str[i] = '\0';
//convert server's repsonse to a int so we can evaluate it
num = atoi(Str);
Serial.print("Server's response: ");
Serial.println(num);
} else {
Serial.println("No response from server.");
}
Serial.println("Disconnecting.");
client.stop();
//show the number
displayServing(num);
delay(pollingInterval);
}