Go Down

Topic: SLR2016 5x7 LED OSRAM Opto Semiconductors and the Arduino code to run them (Read 18452 times) previous topic - next topic

brassbuilder


PaulRB

Well, my house guests got delayed...

Code: [Select]

const byte SLR2016_D[] = {5, 6, 7, 8, 9, 10, 11};

#define SLR2016_A0 3
#define SLR2016_A1 4
#define SLR2016_WR 2

char msg[] = "    Mary had a little lamb    ";

void setup() {

  for (byte i = 0; i < 7; i++) {
    pinMode(SLR2016_D[i], OUTPUT);
  }

  pinMode(SLR2016_A0, OUTPUT);
  pinMode(SLR2016_A1, OUTPUT);
  pinMode(SLR2016_WR, OUTPUT);

}

void loop() {

  for (byte p = 3; p <= 30; p++) {
  
    for (byte c = 0; c < 4; c++) {
    
      digitalWrite(SLR2016_A0, bitRead(c, 0));
      digitalWrite(SLR2016_A1, bitRead(c, 1));
  
      for (byte i = 0; i < 7; i++) {
        digitalWrite(SLR2016_D[i], bitRead(msg[p-c], i));
      }
      
      digitalWrite(SLR2016_WR, LOW);
      digitalWrite(SLR2016_WR, HIGH);
    }
    
    delay(500);
  }
}

brassbuilder

Well....I have scrolling text kind of....the letters are jumbled.

From the looks of it, I think it is doing that Digit 0, Digit 2, Digit 1, Digit 3 thing yet.

Mike

brassbuilder

I fixed it! I fixed it!

I looked at the code. A0 and A1 had the pins reversed.

It works!!!

Mike

brassbuilder

Interesting. If I change the text "Mary had a little lamb" to anything else, I get 5 or 6 random characters at the beginning of the display before the text scrolls from the code. It doesn't matter what I use....my name....city I live in....I get them. Even on "Mary" I get one random character.

Would that have anything to do with the spaces between the quotation marks and the words displayed?

Mike

brassbuilder

Can we go through this code pretty much line by line and explain what it does? I think I understand parts of it.


const byte SLR2016_D[] = {5, 6, 7, 8, 9, 10, 11};
This is the data pins D0-D6 to the Arduino pins. I don't understand the [] though.


#define SLR2016_A0 4
#define SLR2016_A1 3
#define SLR2016_WR 2
This defines the pins to the Arduino. But can this also be used to substitute names. What I mean...instead of typing SLR2016_A0, it could be replaced by a 4 anywhere in the code? If so, what differentiates that? How does that #define tells it it is pins 4, 3, and 2?


char msg[] = "    Mary had a little lamb    ";
Pretty simple. The text I want displayed. Per an earlier post, if I change this to anything else, I get a bunch of random ASCII characters between scrolls. Whether it is at the end or the beginning, I don't know.


void setup() {
Start of the setup routine?


for (byte i = 0; i < 7; i++) {
    pinMode(SLR2016_D, OUTPUT);
  }
I am guessing this is setting the bytes to 7. I don't understand the i++, c++ or any other letter and ++. I've googled the heck out of that.

pinMode, I am guessing is setting all the Data pins to output? What is the i in the [] for? From my VisualBasic days, I am sure there is a rhyme or reason to all the {} brackets. I know they enclose groupings of the code....


pinMode(SLR2016_A0, OUTPUT);
 pinMode(SLR2016_A1, OUTPUT);
 pinMode(SLR2016_WR, OUTPUT);

}

Hmmmm....another pin mode....setting A0, A1, and WR to OUTPUT.....but....I thought D0-D6 were defined as OUTPUTS earlier? And another bracket...


void loop() {
Start of the loop sequence?


for (byte p = 3; p <= 30; p++) {
 
   for (byte c = 0; c < 4; c++) {
More LETTERS++ and brackets....must have something to do with setting the byte count?


digitalWrite(SLR2016_A0, bitRead(c, 0));
     digitalWrite(SLR2016_A1, bitRead(c, 1));
I've googled the heck out of digitalWrite and I still can't get this. It definitely does something with the address bits in this....
   

for (byte i = 0; i < 7; i++) {
       digitalWrite(SLR2016_D, bitRead(msg[p-c], i));
     }
Something to do with bytes....and digitalWrite....and bitRead. No clue on this. None.
     

digitalWrite(SLR2016_WR, LOW);
     digitalWrite(SLR2016_WR, HIGH);
   }
Now it sets WRITE to LOW and HIGH....why? What does this do?


delay(500);
 }
}
When I change that to 200...it goes really fast :)

And evidently the } bracket is the last brackets enclosing something and something...

Thanks for all the help on this! I'd really like to understand this code better but Google has not been a huge help on this. I hesitate to change much of the code because I am concerned of some magic smoke making an appearance.

I am sure these displays have a lot more tricks they could do...and I'm sure you could probably write a book answering my questions.

Mike



brassbuilder

Interesting. I got rid of all the funky characters. I changed the 30 in this line to one less digit than the spaces between the quotation marks with the words that I want to scroll. If I went the same number, I got one funky character:

for (byte p = 3; p <= 30; p++) {
 
   for (byte c = 0; c < 4; c++) {

Figured out that the 4 means I have four digits and changing the 3 and 0 makes certain displays not work depending on the number.

Seeing if I can get a second display hooked up today so it scrolls across two of them....but does that mean I need to change something in the code to get two displays going?

Mike

 

PaulRB

Would that have anything to do with the spaces between the quotation marks and the words displayed?
Yes, I deliberately put 4 spaces before "Mary" to blank the display before the "M" of "Mary" appears. The random characters you have seen are memory bytes before the location where the "Mary has a little lamb" are stored. C and C++ to not check if you try to access an array element that is beyond the upper limit or below the lower limit of the character array/string.

PaulRB

I don't understand the [] though.
"[]" indicates to the compiler that the variable is an array. Normally you would put a number between the brackets to indicate how many elements the array contains. But because the array is initialised with a list of values, the compiler makes the array big enough to hold the list.

This defines the pins to the Arduino. But can this also be used to substitute names. What I mean...instead of typing SLR2016_A0, it could be replaced by a 4 anywhere in the code? If so, what differentiates that? How does that #define tells it it is pins 4, 3, and 2?
Lines beginning "#" are compiler-directives. "#define" are simple substitutions replacing every occurrence of "SLR2016_A0" with "4" for example.


Pretty simple. The text I want displayed. Per an earlier post, if I change this to anything else, I get a bunch of random ASCII characters between scrolls. Whether it is at the end or the beginning, I don't know.
See my previous post.

Start of the setup routine?
Errr... yes.

I am guessing this is setting the bytes to 7.
Don't understand...

I don't understand the i++, c++ or any other letter and ++. I've googled the heck out of that.
Putting "++" after a variable means "increment it after its value has been used". Putting "++" before a variable means "increment it before its value is used"

pinMode, I am guessing is setting all the Data pins to output?  
Yes
What is the i in the [] for?  
This specifies which element/index of the array is to be used.

From my VisualBasic days, I am sure there is a rhyme or reason to all the {} brackets. I know they enclose groupings of the code....
Correct. In VB, there is always a keyword that denotes the end of a sequence of commands, such as "else", "end if", "loop", "next" and so on. In C/C++ the curly brackets do this.

Hmmmm....another pin mode....setting A0, A1, and WR to OUTPUT.....but....I thought D0-D6 were defined as OUTPUTS earlier?
Yes they were, but A0, A1 and WR are connected to different Arduino pins to D0..D6, so they also need to be set to OUTPUT.

And another bracket...
Ummm... you should buy a book on C/C++. The little O'Reilly "Pocket Reference" books are nice and short if you are already a competent programmer.

Start of the loop sequence?
Yes

More LETTERS++ and brackets....must have something to do with setting the byte count?
Err...

I've googled the heck out of digitalWrite and I still can't get this. It definitely does something with the address bits in this....
Something to do with bytes....and digitalWrite....and bitRead. No clue on this. None.
...
Now it sets WRITE to LOW and HIGH....why? What does this do?
...
When I change that to 200...it goes really fast :)
...
And evidently the } bracket is the last brackets enclosing something and something...
All (or at least a heck of a lot) is revealed here:
http://arduino.cc/en/Reference/HomePage
 
I am concerned of some magic smoke making an appearance.
Little or no danger of that with these components. Tweak the code as much as you like.

I am sure these displays have a lot more tricks they could do...
I think we have covered most/all of it.

I'm sure you could probably write a book answering my questions.
Yes, but so many better writers than me have already done so.


PaulRB

Seeing if I can get a second display hooked up today so it scrolls across two of them....but does that mean I need to change something in the code to get two displays going?
To add more displays, they can share the same D0-D6, A0 & A1 connections to the Arduino. But each WR pin will need a connection to a different Arduino pin.

brassbuilder

To add more displays, they can share the same D0-D6, A0 & A1 connections to the Arduino. But each WR pin will need a connection to a different Arduino pin.
OK. I almost had it. I was sharing the WR pin too.

And then add that pin to the #define?

Mike

brassbuilder

Ummm... you should buy a book on C/C++. The little O'Reilly "Pocket Reference" books are nice and short if you are already a competent programmer.

Actually bought two today specifically for programming Arduinos.

Beginning Arduino Programming (Technology in Action)

Beginning C for Arduino: Learn C Programming for the Arduino (Technology in Action)

for (byte i = 0; i < 7; i++) {
       digitalWrite(SLR2016_D, bitRead(msg[p-c], i));
^^^^That line....what does it do? I don't have a clue on what it does. Can't even make an educated guess after the last couple of days.

I have a happily scrolling SLR2016 today :)

Thanks so much for the help! I would have never figured this out on my own. I learned a lot the last couple of days. This would have taken me weeks to get.

Mike

PaulRB

Quote
And then add that pin to the #define?
Yes. You could call it SLR2016_WR2. The for(byte c...) loop would need to go from 0 to 7 so the sketch writes to all 8 positions. When c <= 4 you would use SLR2016_WR and when c > 4 use SLR2016_WR2. There will be other adjustments to make, I'll leave you to figure those out.

Quote
^^^^That line....what does it do?
Did you read up on the functions used on the link I gave above yet? In short, it picks out the character from the message corresponding to the  character position on the display. It breaks that character's ascii code down into 7 bits and writes each one to one if the data lines.

brassbuilder

You have a lot of confidence in me :) But I'm stuck....and I have read up on the functions link....multiple times. :) And Googled.

I'm having problems adding:

When c <= 4 you would use SLR2016_WR and when c > 4 use SLR2016_WR2.

Do I need an "if" or an "if....else" statement?

Here is what I have:



const byte SLR2016_D[] = {5, 6, 7, 8, 9, 10, 11};

#define SLR2016_A0 4
#define SLR2016_A1 3
#define SLR2016_WR 2
#define SLR2016_WR2 12   <<<<<<<<<<<<<<<   Added that line

char msg[] = "   Mary had a little lamb   ";

void setup() {

  for (byte i = 0; i < 7; i++) {
    pinMode(SLR2016_D, OUTPUT);
  }

  pinMode(SLR2016_A0, OUTPUT);
  pinMode(SLR2016_A1, OUTPUT);
  pinMode(SLR2016_WR, OUTPUT);
  pinMode(SLR2016_WR2, OUTPUT);      <<<<<<<<<<<<<<    Added that line

}

void loop() {

  for (byte p = 3; p <= 27; p++) {
 
    for (byte c = 7; c < 4; c++) {             

the 7 used to be a 0, changing c < 4 to c < = 4 is easy enough but to use SLR2016_WR and then use SLR2016_WR2 if c > 4 is where I am lost. I'm not getting the syntax of it. I've tried a few things...my guess is an "if" or "if...else" statement. Its been 10 years or more since my VisualBasic days...
   
      digitalWrite(SLR2016_A0, bitRead(c, 0));
      digitalWrite(SLR2016_A1, bitRead(c, 1));
 
      for (byte i = 0; i < 7; i++) {
        digitalWrite(SLR2016_D, bitRead(msg[p-c], i));
      }
     
      digitalWrite(SLR2016_WR, LOW);
      digitalWrite(SLR2016_WR, HIGH);
    }
   
    delay(300);
  }
}



I'm still working it. I kind of know what needs to be done....I just don't know the syntax of it.

PaulRB

Mike,

In VB you would write

Code: [Select]

for c = 0 to 7
  ...
next


In C the equivalent is

Code: [Select]

for (byte c = 0; c <= 7; c++) {
  ...
}


Your attempt above would have started c at 7 instead of 0 and increased it until it was less than 4. In other words done nothing!

For the second WR connection you would need something like
Code: [Select]

if (c < 4) {    
  digitalWrite(SLR2016_WR, LOW);
  digitalWrite(SLR2016_WR, HIGH);
else {
  digitalWrite(SLR2016_WR2, LOW);
  digitalWrite(SLR2016_WR2, HIGH);
}


Go Up