-1

I am trying to write an Arduino sketch and I am getting a value from an external source which I am trying to display on three daisy-chained 8-digit led displays.

Currently I can display the individual digits like this:

//mydisplay.setDigit(displaynumber, position, value, comma);

mydisplay.setDigit(0, 7, 0, false);
mydisplay.setDigit(0, 6, 9, false);
mydisplay.setDigit(0, 5, 2, false);
mydisplay.setDigit(0, 4, 7, true);
mydisplay.setDigit(0, 3, 5, false);
mydisplay.setDigit(0, 2, 5, false);
mydisplay.setDigit(0, 1, 2, false);
mydisplay.setDigit(0, 0, 5, false);

mydisplay.setDigit(1, 7, 5, false);
mydisplay.setDigit(1, 6, 6, false);


mydisplay.setDigit(1, 3, 5, false);
mydisplay.setDigit(1, 2, 5, false);
mydisplay.setDigit(1, 1, 5, false);
mydisplay.setDigit(1, 0, 6, false);

mydisplay.setDigit(2, 7, 7, false);
mydisplay.setDigit(2, 6, 7, false);
mydisplay.setDigit(2, 5, 8, false);
mydisplay.setDigit(2, 4, 8, false);
mydisplay.setDigit(2, 3, 9, false);
mydisplay.setDigit(2, 2, 9, false);
mydisplay.setDigit(2, 1, 0, false);
mydisplay.setDigit(2, 0, 0, false);

This displays : 09275525 56 5556 77889900 but I don't know where to start if I just want to have a simple function where I can just input any variable and call a single function like this:

mydisplay.myfunctiontodisplay("09275525 56  5556 77889900");

I know I need to do a loop of some kind but i am not sure how to do it.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
Bruno
  • 511
  • 2
  • 6
  • 19
  • What do you pass to `mydisplay.setDigit` to display a space (i.e. to not display anything)? – Sergey Kalinichenko Oct 04 '17 at 12:06
  • Since I control each segment individually, to display nothing I just don't add a command such as "mydisplay.setDigit(1, 7, 5, false); mydisplay.setDigit(1, 6, 6, false);" – Bruno Oct 04 '17 at 12:07
  • What if you have previously called `mydisplay.setDigit(1, 6, 6, false)` as part of displaying some other string, but now you want to not display anything in the sixths position? Is there something you could pass to turn off a digit? – Sergey Kalinichenko Oct 04 '17 at 12:09
  • Convert to string Then Call mydisplay.setDigit into a loop – sam Oct 04 '17 at 12:10
  • there is a clear function. The issue I am having right now is I have to do it individually for each led segment like in the code above. Since I am receiving a float or int value, I just want to plug that into the display function. Either that, or I have to separate the float value into individual digits and display each one. I dont know how to do either. – Bruno Oct 04 '17 at 12:11
  • Your title and question example are about different concerns. Question example shows a **string** (answer would be trivial) while the title is about int (easy) or float (hard). What is your real problem? – Serge Ballesta Oct 04 '17 at 12:11
  • You're right. If I can do it either way it would solve my issue but I honestly didn't know how to ask the question. I am that much of a beginner. My real problem is that I have to individually write each digit to each segment but since I won't know what the value of the variable is, I don't know how to display a variable (string, float or int) in a single command to display the digits on the led display. – Bruno Oct 04 '17 at 12:14
  • You could use a delimited string like `09275525,56__5556,77889900`. The comma separates the displaynumber and the underscore means no digits in that position. Not sure what `true` and `false` in your example means. – Superman Oct 04 '17 at 12:14
  • This is fundamental C++, involving a simple conversion using a `std::ostringstream`. stackoverflow.com is not a site that's suitable for a basic C++ tutorial. [Go and get a good C++ book, then read it](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Sam Varshavchik Oct 04 '17 at 12:18

1 Answers1

1

First, you map positions in a 24-character string, i.e. numbers from 0 to 23, inclusive, to (display, digit) pair. It appears from your example that the mapping should be as follows:

int pos = ...      // This is the position inside your 24-character string
int dsp = pos / 8; // Display index
int dig = 7-pos%8; // Digit index

Now your setDigits function can be written as follows:

void setDigits(Display& display, const char* str) {
    if (!str || strlen(str) != 24) {
        return; // Accept only 24-character strings
    }
    for (int pos = 0 ; pos != 24 ; pos++) {
        char c = str[pos];
        int dsp = pos / 8;
        int dig = 7-pos%8;
        if (isdigit(c)) {
            display.setDigit(dsp, dig, c-'0', false);
        } else {
            display.clear(dsp, dig);
        }
    }
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • This didn't work but it pointed me in the right direction. I now know the basic approach, so thank you very much sir. – Bruno Oct 04 '17 at 12:21
  • 1
    @Bruno You are welcome. When you make this work for your situation, please click [suggest an edit] above, and make corrections to the answer that would make it work for Stack Overflow users trying to build the same thing. Thank you! – Sergey Kalinichenko Oct 04 '17 at 12:30