Get data from web and display them on 7-segment display

Hi to everyone,

I am new user of Arduino. I want to make a project getting some data from web and display them on 7-segment display. I have already write my code to get the data from the web and the code for display them on the display. My problem is that in the loop the program get the data from the web and then they displayed ok, but when the loop beginning from the start my display switched off while the code get data from web. I want while the code get the data from the web, the 7-segment display not turn off but display the last data received.

Thanks in advance

Hello and welcome :slight_smile:

You have to post your code so we can look at it :slight_smile:

I think you need to store the last result in EEPROM but, only do it when absolutely needed because EEPROM have a short life time. Do not write to EEPROM every loop() ! And then, when Arduiono is reset, load the last value from EEPROM.

Start here, particularly #6:

http://arduino.cc/forum/index.php/topic,97455.0.html

thanasisloi7:
when the loop beginning from the start my display switched off while the code get data from web.

What sort of display is it? If it's a persistance-of-vision display that relies on each segment being flashed on briefly, then you will need to change your sketch architecture to refresh the display periodically. If it's some form of display which maintains the same state until you update it, which would imply that somewhere in loop() you have code which is switching off or blanking the display.

If you post your code, preferably in [ code ] tags and preferably with information about what sort of display you have and how it is connected, perhaps we can suggest how to cure the problem.

Are you using conflicting pins between the devices you have connected to the arduino. Are you sending stuff to the display far faster than it can actually be displayed?

//some includes
#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  0x90, 0xA2, 0xDA, 0x00, 0x35, 0x1C }; // if your shield comes with a mac address on a sticker add it here.
byte ip[] = { 192,168,0,80 }; //only required if first connection attempt fails

//byte gateway[] = { 192,168,0,1 }; // router
//byte subnet[] = { 255, 255, 255, 0 }; //subnet mask of the network 
//byte server[] = { 98,129,229,36 }; // Google 74.125.67.99 RPlab 98.129.229.36 -- removed & changed for server name below

// Define the LED Pins
const int ledPin =  13; // diagnostics pin
int digit1 = 2; //PWM Display pin 1
int digit2 = 10; //PWM Display pin 2
int digit3 = 9; //PWM Display pin 6
int digit4 = 6; //PWM Display pin 8
int x=1986;
int segA = A1; //Display pin 14
int segB = 3; //Display pin 16
int segC = 4; //Display pin 13
int segD = 5; //Display pin 3
int segE = A0; //Display pin 5
int segF = 7; //Display pin 11
int segG = 8; //Display pin 15

// Make variables? Yes please!
String serverData = String();


char serverName[] = "0.0.0.0"; //replace this with the host of your php script


//int ledState = LOW;  // init leds to low
int isTransferring = 0;
long likeDifference = 0;
long likeCount = 0;
long likeNumber = 0;
long lastLikeCount = 0;

EthernetClient client;



void setup() {
  
  Serial.println("start code run");
  
  // Set the digital pin as output:
  pinMode(ledPin, OUTPUT);

  
  // Start the serial library:
  Serial.begin(9600);
    
  // Start the Ethernet connection:
  if(Ethernet.begin(mac)==0){
    Serial.println("Failed to configure Ethernet using DHCP");
    Ethernet.begin(mac,ip);
  }else{
    Serial.println("connected!");
  } 
  pinMode(segA, OUTPUT);
  pinMode(segB, OUTPUT);
  pinMode(segC, OUTPUT);
  pinMode(segD, OUTPUT);
  pinMode(segE, OUTPUT);
  pinMode(segF, OUTPUT);
  pinMode(segG, OUTPUT);
  pinMode(digit1, OUTPUT);
  pinMode(digit2, OUTPUT);
  pinMode(digit3, OUTPUT);
  pinMode(digit4, OUTPUT);
  
}
void loop() 
  
{
  
  displayNumber(lastLikeCount);
  
   if(isTransferring == 0){
    if (client.connect(serverName, 80)) {
      
  
      // Make a HTTP request:
      client.println("GET /~test/index.php HTTP/1.1");
      client.println("Host: 0.0.0.0");
      client.println();
      
      isTransferring = 1;
    } else {
      isTransferring = 0; 
    }
  }
  
  while (client.available()) {
    
   char c = client.read();
    
    // Append to our returned server data to parse later
    serverData += c;

  }
  
  // During transfer if the connection stops kill the connection
  // and process the response
  if(isTransferring == 1){
    
    // if the server's disconnected, stop the client:
    if (!client.connected()) {

      Serial.println();
      Serial.println("disconnecting.");
      client.stop();
      
      isTransferring = 0;
       
      long likeLine = long(serverData.indexOf("?="));
      Serial.println("like line");
      Serial.println(likeLine);
      
      //substr out the count number
      String likeCount = String(serverData.substring(likeLine+2, likeLine+17));
      //remove white space
      likeCount.trim();
      
      Serial.println("like count");
      Serial.println(likeCount);
      //convert
      likeNumber = stringToLong(likeCount);
      
      Serial.println("likeNumber:");
      Serial.println(likeNumber);
            
      Serial.println("lastLikeCount:");
      Serial.println(lastLikeCount);
            
      // How many likes since last time?
      likeDifference = likeNumber - lastLikeCount;
      
      // Save the new like value
      lastLikeCount = likeNumber;
        
      Serial.println("likeDifference:");
      Serial.println(likeDifference);
    
      // Wait X more seconds before checking again. 5000 is 5 sec
      delay(1000);  
      
    }
  }

}

 
  
// Helper function to turn a string into a long int
long stringToLong(String s) {
    char arr[12];
    s.toCharArray(arr, sizeof(arr));
    return atol(arr);
}
void displayNumber(int toDisplay) {
#define DISPLAY_BRIGHTNESS  500

#define DIGIT_ON  LOW
#define DIGIT_OFF  HIGH



  for(int digit = 4 ; digit > 0 ; digit--) {

    //Turn on a digit for a short amount of time
    switch(digit) {
    case 1:
      digitalWrite(digit1, DIGIT_ON);
      break;
    case 2:
      digitalWrite(digit2, DIGIT_ON);
      break;
    case 3:
      digitalWrite(digit3, DIGIT_ON);
      break;
    case 4:
      digitalWrite(digit4, DIGIT_ON);
      break;
    }
    
    lightNumber(toDisplay % 10);
    toDisplay /= 10;

    //delayMicroseconds(DISPLAY_BRIGHTNESS); 
    //Display digit for fraction of a second (1us to 5000us, 500 is pretty good)

    //Turn off all segments
    lightNumber(10); 

    //Turn off all digits
    digitalWrite(digit1, DIGIT_OFF);
    digitalWrite(digit2, DIGIT_OFF);
    digitalWrite(digit3, DIGIT_OFF);
    digitalWrite(digit4, DIGIT_OFF);
  }
}
void lightNumber(int numberToDisplay) {

#define SEGMENT_ON  HIGH
#define SEGMENT_OFF LOW

  switch (numberToDisplay){

  case 0:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_OFF);
    break;

  case 1:
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    break;

  case 2:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_OFF);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 3:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 4:
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 5:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 6:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 7:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    break;

  case 8:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 9:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 10:
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_OFF);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    break;
  }
}

This is the code i run in te Arduino.
The display is http://grobotronics.com/7-segment-display-1-tall-white.html#.UNQry-QfIrU

When the code is running the LED Display is blinking .(~1sec on /2 sec off).

It looks to me as if that is a persistance-of-vision display, which means you will need to refresh the display regularly.

It also looks to me as if the code in loop() only updates the display once each time it is called, so you should try to ensure that nothing you do in loop() takes longer than a few milliseconds. That delay(1000) at the end of the response processing, for example, would have to go. I guess the intent is to wait for a moment after getting one response, before making the next request. A better way to do that without blocking would be to use the techniques demonstrated in 'blink without delay' to initiate the request periodically - the goal would be to start a new request if there is no request already in progress, and the previous request was submitted over a second ago (or whatever interval you choose).

I don't know what the longest execution time of loop() will be once you've done this. If it is still too long, you need to find out where the delay is occurring and look for further ways to have displayNumber() called frequently enough in all cases. Ultimately, if you can't get displayNumber() called frequently enough in all cases then your last resort is to remove this from loop() altogether and arrange for it to be executed from a timer interrupt. That would involve quite a lot more work and complication, though, so I suggest you only attempt that after you have exhausted the other options.