Outputting numbers from serial port to 7-segment display

Greetings!

I’m having trouble figuring out how to take a value coming in from the serial port and dumping out to my 6 digit 7-segment display.

I have a working 6 digit 7-segment display using LedControl (http://www.arduino.cc/playground/Main/LedControl)

I have successfully written a Python script to send my arduino a 6digit number formatted like XXXX.XX (ex: 5432.10) over the serial port.

I want to take this number, ex: 5432.10 and output it to the 7-segment display

I have 3 functions right now.

readSerialString();
printSerialString();
scrollDigits() (and I am using a hunk of code from an LedControl control example)

Inside of scrollDigits()

If I set each variable to “0”

//For a 6 digit 7-segement display ex: 5432.10
int thousands = 0;
int hundreds = 0;
int tens = 0;
int ones = 0;
int tenths = 0;
int hundredths = 0;

I can outputting zeros to each digit of the display like so.

    lc.setDigit(0,0,(byte)hundredths,false);
    lc.setDigit(0,1,(byte)tenths,false);
    lc.setDigit(0,2,(byte)ones,false);
    lc.setDigit(0,3,(byte)tens,false);
    lc.setDigit(0,4,(byte)hundreds,false);
    lc.setDigit(0,5,(byte)thousands,false);

I thought I could do something like

int thousands = serInString[0]; 
int hundreds = serInString[1]; 
int tens = serInString[2]; 
int ones = serInString[3]; 
// serInString 4 is the decimal place
int tenths = serInString[5]; 
int hundredths = serInString[6];

to bust up the 5432.10 number and output the
0 to the first digit
1, to the second digit, and so on…

but no luck.

Also, I know seems like a fairly hardcoded way to do it. Seems like I could loop through to do this. But for now the simplest and least elegant way is OK and then I’ll keep cranking to make the more elegant.

Any assistance, code snippets, etc. would be super appreciated.

//We always have to include the library
#include "LedControl.h"
int incomingByte;      // a variable to read incoming serial data into
int serIn; // var that will hold the bytes-in read from the serialBuffer
int serInString[7]; // array that will hold the different bytes 100=100characters;
// -> you must state how long the array will be else it won't work.
int serInIndx = 0; // index of serInString[] in which to insert the next incoming byte
int serOutIndx = 0; // index of the outgoing serInString[] array;

/*
 Now we need a LedControl to work with.
 ***** These pin numbers will probably not work with your hardware *****
 pin 12 is connected to the DataIn 
 pin 11 is connected to the CLK 
 pin 10 is connected to LOAD 
 We have only a single MAX72XX.
 */
LedControl lc=LedControl(12,11,10,1);

#define gold 6
long val = 0;


/* we always wait a bit between updates of the display */
unsigned long delaytime=1250;

void setup() {

  Serial.begin(9600); // initialize serial communication
  /*
   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,9);
  /* and clear the display */
  lc.clearDisplay(0);
}


void scrollDigits() {
  
////For a 6 digit 7-segement display ex: 5432.10
//int thousands = 0;
//int hundreds = 0;
//int tens = 0;
//int ones = 0;
//int tenths = 0;
//int hundredths = 0;

int thousands = serInString[0]; 
int hundreds = serInString[1]; 
int tens = serInString[2]; 
int ones = serInString[3]; 
// serInString 4 is the decimal place
int tenths = serInString[5]; 
int hundredths = serInString[6]; 


  for(int i=0;i<1;i++) {
    lc.setDigit(0,0,(byte)hundredths,false);
    lc.setDigit(0,1,(byte)tenths,false);
    lc.setDigit(0,2,(byte)ones,false);
    lc.setDigit(0,3,(byte)tens,false);
    lc.setDigit(0,4,(byte)hundreds,false);
    lc.setDigit(0,5,(byte)thousands,false);
    delay(delaytime);
  }
  lc.clearDisplay(0);
  delay(delaytime);
  
} 


//auto go_to_the_line function
//void printNewLine() {
// Serial.print(13, BYTE);
// Serial.print(10, BYTE);
//}

//read a string from the serial and store it in an array
//this func uses globally set variable so it's not so reusable
//I need to find the right syntax to be able to pass to the function 2 parameters:
// the stringArray and (eventually) the index count
void readSerialString () {
  int sb;
  if(Serial.available()) {
    //Serial.print("reading Serial String: "); //optional confirmation
    while (Serial.available()){
      sb = Serial.read();
      serInString[serInIndx] = sb;
      serInIndx++;
      //serialWrite(sb); //optional confirmation
    }
    //Serial.println();
  }
}

//print the string all in one time
//this func as well uses global variables
void printSerialString() {
  
  if( serInIndx > 0) {
    Serial.print("Arduino memorized that you said: ");
    //loop through all bytes in the array and print them out
    for(serOutIndx=0; serOutIndx < serInIndx; serOutIndx++) {
      Serial.print( serInString[serOutIndx] ); //print out the byte at the specified index
      //serInString[serOutIndx] = ""; //optional: flush out the content 
     } 
    scrollDigits(); //dump info to 7-segment display
    //reset all the functions to be able to fill the string back with content
    serOutIndx = 0;
    serInIndx = 0;
    Serial.println();
  }

}


void loop () {
  //simple feedback from Arduino

  //read the serial port and create a string out of what you read
  //readSerialString(serInString, serInIndx);
  readSerialString();

  //do somenthing else perhaps wait for other data or read another Serial string
  //Serial.println ("------------ arduino is doing somenthing else ");

  //try to print out collected information. it will do it only if there actually is some info.
  printSerialString();

  //slows down the visualization in the terminal
  delay(2000);
}

gold_digger.txt (3.82 KB)

got it!

a couple of issues.

one was a bug.

int serInString[7]; // array that will hold the different bytes 100=100characters;

should of been

char serInString[7]; // array that will hold the different bytes 100=100characters;

and the other was I should of been setting the variables I'm using for the digits as chars instead of ints.

char thousands = serInString[0]; 
char hundreds = serInString[1]; 
char tens = serInString[2]; 
char ones = serInString[3]; 
// I need a decimal place here.
char tenths = serInString[5]; 
char hundredths = serInString[6];

once I have everything like I like it, I'll post up the final code for anybody that wants to do something similar.

also needed to be using lecControl’s lc.setChar instead of lc.setDigit

  for(int i=0;i<1;i++) {
    lc.setChar(0,0,hundredths,false);
    lc.setChar(0,1,tenths,false);
    lc.setChar(0,2,(byte)ones,false);
    lc.setChar(0,3,(byte)tens,false);
    lc.setChar(0,4,hundreds,false);
    lc.setChar(0,5,thousands,false);
    delay(delaytime);
  }
  lc.clearDisplay(0);
  delay(delaytime);
  
}