Notifications
Clear all

I fell in love with the LED & KEY board

120 Posts
7 Users
19 Reactions
38.7 K Views
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

The first bit went quicker than I expected. Now the display is controlled with an array!

It is important that if any value in the tempCharArray[] is changed, it is followed immediately by calling updateRegisters().


   
ReplyQuote
hstaam
(@hstaam)
Mr.
Joined: 5 years ago
Posts: 61
 

Just downloaded the code. I  and will study it. still have to purchase the module. ? 

Thank you for sharing this.

hj


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@robo-pi @hstaam

Progress on displaying decimal numbers is slow. Converting a float to a string and then to a character array is throwing up some strange results but I'll figure it eventually.

Although there are libraries for this board available, I decided to use it to learn how to develop my own libraries. And after some initial learning curve, they are not as complicated as I originally thought. I have attached my first feeble attempts below. Comments welcome!

The latest idea from the Crazy Celt is to use the board as a "wackamole" game, keeping score with the display segments. I think I'll save this for when I get really bored. ? 


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

It's ugly but it works!!

Displays decimal and whole (integers) numbers.


void loop() {

if(Serial.available() > 0){
keyLed.clearAllSegs();
String tString = Serial.readStringUntil("\n"); // read in the number as a string
int decIdx = tString.indexOf("."); //find the position of the decimal point
tString.remove(decIdx, 1); //remove the decimal point
int tLen = tString.length();// get the string length
char tChar[] = {}; // declare character array
tString.toCharArray(tChar, tLen + 1); // convert string to character array
for(int i = 0; i < tLen; i++){
if (i + 1 == decIdx){
keyLed.showNum(i, (int(tChar[i]) - 48), true);
} else {
keyLed.showNum(i, int(tChar[i]) - 48);
}
}
}
}

The above code relies on the library files attached below.

All this code needs now is right justification.


   
ReplyQuote
Robo Pi
(@robo-pi)
Robotics Engineer
Joined: 5 years ago
Posts: 1669
Topic starter  

@pugwash

It's great that you are publishing all your work here Steve.  This will help others who are attempting to achieve similar results.

@dronebot-workshop

Thank you Bill for creating these forums.  We are learning from each other every day here.

DroneBot Workshop Robotics Engineer
James


   
ReplyQuote
JoeLyddon
(@joelyddon)
Member
Joined: 5 years ago
Posts: 157
 

@robo-pi

Hi, I'm still confused as to WHOM I'm replying to and WHO wrote the post that I'm replying about...  so, if I'm confusing you, that Makes Two of us 🙂  🙂

 

In studying the beautiful Code posted, I am wondering...   WHY HEX parameters are used in Functions?   They do not make sense to me...  Are they referring to Predefined Actions, etc. included in something else that is not shown here?

If possible, could you or someone please try to clear this up for me?

It looks like the code is doing something really COOL...  BUT, I cannot understand those Hex Function parameters.

Thank you for your help,

Joe

ps:  For example:

sendCommand(0x88)

functionON(0xc0, 0xff, 0x01);

functionOFF(0xc0, 0x00, 0x00);

functionON(0xc4, 0xff, 0x01);

sendCommand(0x40);

 

This post was modified 5 years ago 2 times by JoeLyddon

Have Fun,
Joe Lyddon

www.woodworkstuff.net


   
ReplyQuote
Robo Pi
(@robo-pi)
Robotics Engineer
Joined: 5 years ago
Posts: 1669
Topic starter  
Posted by: @joelyddon

It looks like the code is doing something really COOL...  BUT, I cannot understand those Hex Function parameters.

Hex numbers are being used to simplify binary when working with bytes.

A byte is 8 bits long.   And that can be broken down further into two nibbles with each nibble being only 4 bits long.   And programs could just use bytes.   But it requires more typing and it can also be more difficult to read looking at 1's and 0's all the time.   So programmers use Hex numbers.

The Hex digits represent a nibble (4 bits)

0 = 0000
1 = 0001
2 = 0010
3 = 0011
4 = 0100
5 = 0101
6 = 0110
7 = 0111
8 = 1000
9 = 1001
a = 1010
b = 1011
c = 1100
d = 1101
e = 1110
f = 1111

Using hex digits we can represent any byte with just two hex digits.

Examples:

00 = 0000 0000
ff = 1111 11111
47 = 0100 0111
ab = 1010 1011

So a pair of hex digits are just shorthand for bytes.

It's actually quite easy once you get the hang of it.

But if you wanted to you could work in binary.

You could also work using decimal numbers too, but converting decimal numbers to bytes is not so intuitive.  Once  you understand how Hex digits work they actually make working with bytes EASY.

And this is why programmers use Hex numbers when working with data in bytes and/or in nibbles.  It's actually done to make things easier, not more difficult. ? 

Hope this helps.

DroneBot Workshop Robotics Engineer
James


   
ReplyQuote
JoeLyddon
(@joelyddon)
Member
Joined: 5 years ago
Posts: 157
 

@robo-pi

Yes!  I know that...  I have been a Programmer probably for longer than you have been alive!  🙂

BUT... 

When I see parameters passed to a Function that are nothing but Hexadecimal, Binary, decimal, NUMBERS, I should be able to KNOW what they mean to the Function.   That is what I'm talking about...  I am very New to C++ and my first experience to it (and not knowing it) was from Paul McWhorton and his teachings.  I, by no means pretend to know C++...  but, I want to learn it.  There is a lot that is rubbing off, making me learn it...

 

BUT, these Number Parameters being passed to a Function has me stumped.

 

Is that clearer of what I am trying to find out?  🙂

 

Thank you very much...  🙂  😀

 

 

 

Have Fun,
Joe Lyddon

www.woodworkstuff.net


   
ReplyQuote
Robo Pi
(@robo-pi)
Robotics Engineer
Joined: 5 years ago
Posts: 1669
Topic starter  
Posted by: @joelyddon

Yes!  I know that...  I have been a Programmer probably for longer than you have been alive!

Sorry, me bad.  I thought you were asking about the hex numbers.  

Posted by: @joelyddon

When I see parameters passed to a Function that are nothing but Hexadecimal, Binary, decimal, NUMBERS, I should be able to KNOW what they mean to the Function.   That is what I'm talking about...

If you could post a code snippet as an example I could try to explain what's going on.  However, to be fair, I could end up being just as clueless as you if the code has no comments and has been taken out of context.

Code without comments is an extremely bad practice.   Especially if it's unclear what's going on without the comments.  Some programmers tend to write code without comments because they think it's clear what they are doing.   However, even programmers who do that often find their own code extremely difficult to understand months later when they have forgotten what they had in mind when they wrote it.

But yeah, code without comments can be a nightmare even when using simple variable types.    If you don't know what the variable is used for, then how could you possibly have a clue what the function might be doing with it?

DroneBot Workshop Robotics Engineer
James


   
ReplyQuote
Robo Pi
(@robo-pi)
Robotics Engineer
Joined: 5 years ago
Posts: 1669
Topic starter  
Posted by: @joelyddon

ps:  For example:

sendCommand(0x88)

functionON(0xc0, 0xff, 0x01);

functionOFF(0xc0, 0x00, 0x00);

functionON(0xc4, 0xff, 0x01);

sendCommand(0x40);

Ok, I see that you edited  your post and added example code.

sendCommand(0x88)

In the program I wrote this was also accompanied with the following comment:

// activate and set brightness to low (change to 0x8f for full brightness)

Actually I changed the command to 0x88 (which is least brightness) and forgot to change the comment.

In any case 0x88 is the command to set the brightness on the LED board. (see the datasheet for the board)

This called the following method:

The uint8_t variable type named "value" is replaced with 0x88 the value from sendCommand(0x88) that calls this method.

Then (again, assuming you know how the LED board works,... the strobe is set LOW, and then this byte of 0x88 hex, is sent to the board via the shiftOut command as "value".   This then sets the display brightness,  And the next line sets the strobe back to HIGH.   All done.  We return back to the next line after the call to this method.

I didn't put any comments in this method because I'm assuming that anyone who is using this program knows how the LED board works and had read the data sheet.   the variable "value" should be self-explanatory.

void sendCommand(uint8_t value)
{
digitalWrite(strb, LOW);
shiftOut(data, clk, LSBFIRST, value);
digitalWrite(strb, HIGH);
}

NEXT

functionON(0xc0, 0xff, 0x01);

Me bad.  I probably should have had a comment here at least at Case 1.  These three hex numbers are Address, Display Value, and LED Value.

This called the following routine.   Note that it is asking for 3 byte values which is what we have just sent to it.  The first byte being the starting address, the second value being the value for the 7-seg display, the third value being the value for the LED above the display.

I see that I didn't update these comments either.   addr is the address of the 7-seg display (not necessarily the 8th one).   value is the hex value for the numeral (not necessarily the numeral 8)

Then I increment the address by one.  I could have just used addr++.   But here I actually equated it to addr2 and then used that to access the LED above the 7-seg display.    And then I used LED_value to turn the LED on or off.

void functionON(byte addr, byte value, byte LED_value){
sendCommand(0x44);
digitalWrite(strb, LOW);
shiftOut(data, clk, LSBFIRST, addr); // Address of 8th digit
shiftOut(data, clk, LSBFIRST, value); // Numeral 8
digitalWrite(strb, HIGH);
byte addr2 = addr + 0x01;
digitalWrite(strb, LOW);
shiftOut(data, clk, LSBFIRST, addr2); // Address of LED
shiftOut(data, clk, LSBFIRST, LED_value); // LED
digitalWrite(strb, HIGH);

Sorry, this code could indeed be cleaned up. I was just posting it as an example of how the thing works.

The other functions you've listed basically do the same things that I just describe.

I should have put in far more comments as 'tutorial code'. 

So it's all my fault and I take full blame.

Sorry for the poorly written code.

Edited to add:

I probably should note that I chose the following variable names.

addr

value

LED_value

You can change those to be anything you want.

value isn't very informative.  I probably should have called it SEG_value.

Also, in the first method called void sendCommand(uint8_t value) you could replace the variable name "value" with "Display_Command_Code"  or anything you want.

I could be throwing you off by using such extremely abstract variable names.

My assumption was that anyone who is using the program already understands what addresses and values we need to send to the LED board.   Understanding the device you are attempting to control helps a lot.

But yeah, I'll take responsibility for having written code that sucks! ? 

I could  have included far more comments to be sure.  Especially as tutorial code.   In fact, I meant to do that and then just never got around to it.  Just like everything else in my life.

Anyway, I hope this explanation helps.

DroneBot Workshop Robotics Engineer
James


   
ReplyQuote
JoeLyddon
(@joelyddon)
Member
Joined: 5 years ago
Posts: 157
 

@robo-pi

Thank you for your GREAT explanation...

Don't you think Variable Names being used would have made it easier to understand?

Thanks again...  Sorry to be so SLOW trying to learning C++ (without a book on it. 🙂 )

It's still hard to follow...  Just me, I presume...

 

Have Fun,
Joe Lyddon

www.woodworkstuff.net


   
ReplyQuote
JoeLyddon
(@joelyddon)
Member
Joined: 5 years ago
Posts: 157
 

@robo-pi

Thank you...  🙂

 

Have Fun,
Joe Lyddon

www.woodworkstuff.net


   
ReplyQuote
Robo Pi
(@robo-pi)
Robotics Engineer
Joined: 5 years ago
Posts: 1669
Topic starter  
Posted by: @joelyddon

functionON(0xc0, 0xff, 0x01);

By the way, for the purpose of tutorial code I could  have written it as follows:

 

byte address = oxco; // Address of the LED component
byte numeral = oxff; // Numeral value for the 7-Seg display
byte LED = oxo1; // Turn on the LED above the 7-Seg display
functionON(address, numeral, LED);

 

 

void functionON(byte address, byte numeral, byte LED){
byte Led_board_command = 0x44; // Command to address individual components.
sendCommand(Led_board_Command);
digitalWrite(strb, LOW);
shiftOut(data, clk, LSBFIRST, address); // Address of 7-seg display
shiftOut(data, clk, LSBFIRST, numeral); // Numeral value
digitalWrite(strb, HIGH);
digitalWrite(strb, LOW);
shiftOut(data, clk, LSBFIRST, address++); // Address of LED (just increment)
shiftOut(data, clk, LSBFIRST, LED); // LED value
digitalWrite(strb, HIGH);
}

void sendCommand(byte Led_board_Command)
{
digitalWrite(strb, LOW);
shiftOut(data, clk, LSBFIRST, Led_board_Command); // Send the command out to the board.
digitalWrite(strb, HIGH);
}

NOTE!

The above code has not been tested.   I'm just offering an example of how it could be written using different variable naming conventions for the variables.

DroneBot Workshop Robotics Engineer
James


   
ReplyQuote
JoeLyddon
(@joelyddon)
Member
Joined: 5 years ago
Posts: 157
 

@robo-pi

...  and I'm still left in wondering Where the Hex #'s came from and their justification.

Why not make it EASY...  Justify the Hex#'s...  How did you know to use the #'s you used?

 

Have Fun,
Joe Lyddon

www.woodworkstuff.net


   
ReplyQuote
Robo Pi
(@robo-pi)
Robotics Engineer
Joined: 5 years ago
Posts: 1669
Topic starter  
Posted by: @joelyddon

@robo-pi

...  and I'm still left in wondering Where the Hex #'s came from and their justification.

Why not make it EASY...  Justify the Hex#'s...  How did you know to use the #'s you used?

 

Go back and read this post I wrote on page 2 of this thread in detail:

Explanation of how the LED board works

Let me know if you still have questions after that.

And be sure to blow up the graphic of the 7-seg display and read the text on that graphic as well.

Hopefully everything you need to know should be in that post.

If not, please do let me know.

DroneBot Workshop Robotics Engineer
James


   
ReplyQuote
Page 4 / 8