Box function and writing ascii with dec

The Dot Matrix Display (DMD) is a 32x16 array of high-brightness LEDs for visually striking effects. [Product Page]
Post Reply
kyotee89
Posts: 4
Joined: Wed Dec 13, 2017 5:35 am

Box function and writing ascii with dec

Post by kyotee89 » Wed Dec 13, 2017 7:20 am

Hi,

I have a project to make an 8 bit binary typewriter as an education interactive display for kids. I have two 16 x 32 Freetronics LED matrixes connected.
It will have 8 switches which will be set to represent a single ASCII character, then a button to enter the character, one letter at a time, once the switches have been set.
The current character appear on a screen, changing as the switches are changed and when a button is pushed the character is stored and a new one can be entered.
I have the 8 switches converted into a dec number, which are stored one character at a time into an array. I am currently using the function "box.write(character[1])" to write a character however this results in the characters scrolling across the screen. if i use function "dmd.drawString(character[1])" it displays the dec value not the ascii character. can i either A) stop the box.write function from scrolling or B) drawString but print ascii character instead of binary.
Any help appreciated.

Casey

User avatar
stryker
Posts: 255
Joined: Sat Jan 14, 2012 2:44 pm

Re: Box function and writing ascii with dec

Post by stryker » Wed Dec 13, 2017 7:37 am

Hi Casey

Sounds interesting - can you share your code so we can see exactly what you have so far?

I might get a chance to try it on the weekend but it's possible something will stand out when you show us the code.

Cheers!
Geoff

kyotee89
Posts: 4
Joined: Wed Dec 13, 2017 5:35 am

Re: Box function and writing ascii with dec

Post by kyotee89 » Wed Dec 13, 2017 10:25 am

Hi,

Here is my code so far. If you can look over it that is much appreciated. Ask me any questions you may have.

Casey

Code: Select all

#include <SPI.h>
#include <DMD2.h>
#include <fonts/SystemFont5x7.h>
#include <fonts/Arial14.h>

// Set Width to the number of displays wide you have
const int WIDTH = 1;

// You can change to a smaller font (two lines) by commenting this line,
// and uncommenting the line after it:
const uint8_t *FONT = Arial14;
//const uint8_t *FONT = SystemFont5x7;

char *MESSAGE;

SoftDMD dmd(WIDTH,1);  // DMD controls the entire display
DMD_TextBox box(dmd);  // "box" provides a text box to automatically write to/scroll the display


#include <Button.h>

int sw8 = A0;
int sw7 = A1;
int sw6 = A2;
int sw5 = A3;
int sw4 = A4;
int sw3 = A5;
int sw2 = 3;
int sw1 = 2;



int sw1State;
int sw2State;
int sw3State;
int sw4State;
int sw5State;
int sw6State;
int sw7State;
int sw8State;

int decimal;
int i;
int j;
int character[20];

Button button1(4);
Button button2(5);

int relayPin = 12;

void setup() {
  
  // put your setup code here, to run once:

  Serial.begin(9600);
  
pinMode(2,INPUT);
pinMode(3,INPUT);

  button1.begin();
  button2.begin();

  dmd.setBrightness(255);
  dmd.selectFont(FONT);
  dmd.begin();

}

void loop() {


sw1State = digitalRead(sw1);
sw2State = digitalRead(sw2);
sw3State = analogRead(sw3);
sw4State = analogRead(sw4);
sw5State = analogRead(sw5);
sw6State = analogRead(sw6);
sw7State = analogRead(sw7);
sw8State = analogRead(sw8);

if (sw1State == HIGH) { decimal += 1; }
if (sw2State == HIGH) { decimal += 2; }
if (sw3State > 1000) { decimal += 4; sw3State = 1; }
else {sw3State = 0;}
if (sw4State > 1000) { decimal += 8; sw4State = 1; }
else {sw4State = 0;}
if (sw5State > 1000) { decimal += 16; sw5State = 1; }
else {sw5State = 0;}
if (sw6State > 1000) { decimal += 32; sw6State = 1; }
else {sw6State = 0;}
if (sw7State > 1000) { decimal += 64; sw7State = 1; }
else {sw7State = 0;}
if (sw8State > 1000) { decimal += 128; sw8State = 1; }
else {sw8State = 0;}

character[i] = decimal;
decimal = 0;

if (button1.pressed()){  
  Serial.print("enter character  ");
i++;
}

if (button2.pressed()) {
  i = 0;
  
  while ( j <= 19) {
    Serial.print("clear  ");
    Serial.println(j);
    character[j] = 0;
    j++;
  }
}

else {j = 0;}

String string1 = String(character[0]);
String string2 = String(character[1]);
String string3 = String(character[2]);
String string4 = String(character[3]);
String string5 = String(character[4]);
String string6 = String(character[5]);
String string7 = String(character[6]);
String string8 = String(character[7]);
String string9 = String(character[8]);
String string10 = String(character[9]);
String string11 = String(character[10]);
String string12 = String(character[11]);
String string13 = String(character[12]);
String string14 = String(character[13]);
String string15 = String(character[14]);
String string16 = String(character[15]);
String string17 = String(character[16]);
String string18 = String(character[17]);
String string19 = String(character[18]);
String string20 = String(character[19]);

String stringSum = String(string1 + string2 + 
string3 + string4 + string5 + string6 + string7 + 
string8 + string9 + string10 + string11 + string12 + 
string13 + string14 + string15 + string16 + string17 + string18 + string19 + string20);

Serial.write(character[0]);
Serial.write(character[1]);
Serial.write(character[2]);
Serial.write(character[3]);
Serial.write(character[4]);
Serial.write(character[5]);
Serial.write(character[6]);
Serial.write(character[7]);
Serial.write(character[8]);
Serial.write(character[9]);
Serial.write(character[10]);
Serial.write(character[11]);
Serial.write(character[12]);
Serial.write(character[13]);
Serial.write(character[14]);
Serial.write(character[15]);
Serial.write(character[16]);
Serial.write(character[17]);
Serial.write(character[18]);
Serial.write(character[19]);

Serial.println(" ");
Serial.print(sw1State);
Serial.print(" ");
Serial.print(sw2State);
Serial.print(" ");
Serial.print(sw3State);
Serial.print(" ");
Serial.print(sw4State);
Serial.print(" ");
Serial.print(sw5State);
Serial.print(" ");
Serial.print(sw6State);
Serial.print(" ");
Serial.print(sw7State);
Serial.print(" ");
Serial.print(sw8State);
Serial.println("  ");
Serial.print(decimal);
Serial.print("  ");
Serial.write(decimal);
Serial.println("  ");

/* box.write(character[1]);
box.write(character[2]);
box.write(character[3]);
box.write(character[4]);
box.write(character[5]);
box.write(character[6]);
box.write(character[7]);
box.write(character[8]);
box.write(character[9]);
box.write(character[10]);
box.write(character[11]);
box.write(character[12]);
box.write(character[13]);
box.write(character[14]);
box.write(character[15]);
box.write(character[16]);
box.write(character[17]);
box.write(character[18]);
box.write(character[19]);
*/

dmd.drawString(0, 0, (stringSum));
delay(2);
}

User avatar
stryker
Posts: 255
Joined: Sat Jan 14, 2012 2:44 pm

Re: Box function and writing ascii with dec

Post by stryker » Wed Dec 13, 2017 2:13 pm

Hi Casey

There are a few things that stand out, though I've not yet read anything to do with the output. You've defined some of your switches as analog pins, which is fine. These pins are general purpose and can be used for digital IO also. In your case however you're using analogRead() in loop and you can't test for a simple HIGH/LOW. On an Arduino Uno though these analog pins have digital aliases, so rather than defining those switches by their analog names, use the digital alias and you can digitalRead() them just as you do for sw1 and sw2.

The aliases are:
A0/D14
A1/D15
A2/D16
A3/D17
A4/D18
A5/D19

That will mean you can treat them all alike, and you could make your switch states all stored in an array of int or boolean or whatever, and you could change that first section of loop() to be a for loop. Something like this (untested!)

Code: Select all

const int switchCount = 8;
int swState[switchCount];                                 // all the state variables now in an array
int sw[switchCount] = {2, 3, 19, 18, 17, 16, 15, 14};     // the Arduino pins

// and then later, at the start of loop()

for (int i = 0; i < switchCount; i++) {
  swState[i] = digitalRead(sw[i]);                       // each pin can be digitalRead() now
  if (swState[i]) decimal += (1 << (i + 1));             // adding a power of 2
}
Since it appears you don't reference the switchStates elsewhere in the code, you might even simplify this to not store them, something like

Code: Select all

const int switchCount = 8;
int sw[switchCount] = {2,3,19,18,17,16,15,14};

// and then later, at the start of loop()

  for(int i=0; i<switchCount; i++) {
    if (digitalRead(sw[i]) decimal += (1 << (i + 1));      // result calc'd based on digitalRead that is not stored
  }
It's been a long day and it's late so apologies for the mistakes I'm bound to have made with this.

I'll continue to look further at this tomorrow when I get a chance.

Cheers, Geoff

User avatar
stryker
Posts: 255
Joined: Sat Jan 14, 2012 2:44 pm

Re: Box function and writing ascii with dec

Post by stryker » Thu Dec 14, 2017 3:48 am

Hi Casey,

Not sure if you had a chance to read the above post yet, however it does include an error, anywhere you see this expression

Code: Select all

decimal += (1 << (i + 1));
it should actually be

Code: Select all

decimal += (1 << i );
My apologies. And of course I'm presuming that the switches are indexed from 0 to 7, with 0 being the low end, the opposite way to how a human would read the value. So 0x0000001 would have 1 in swState[0]. If your logic is the other way then you'll need to modify.

Why your code is printing the value and not the ASCII character comes down to the lines that look like this one:

Code: Select all

String string15 = String(character[14]);
The type conversion from an int to a String actually is doing what you're asking it to - it's creating a String representation of the number.

To make the conversion therefore, I'd change your destination array to be of type char

Code: Select all

char character[20] = "                   "; // << that's only 19 spaces long of course
And then when assigning the integer value to it, only store the lowest byte of the two (integer is 16 bit, char is 8 on Arduino)

Code: Select all

character[i] = lowByte(decimal);
Another thing you need to be mindful of is ASCII table is only really usable between 32 (space) and 126 (~) (refer asciitable.com). So you'll need to add some logic to ensure the kids stay in the printable ASCII range to ensure you get sensible output on the DMD.

Hopefully that makes sense and gets you on track.

Cheers, Geoff

kyotee89
Posts: 4
Joined: Wed Dec 13, 2017 5:35 am

Re: Box function and writing ascii with dec

Post by kyotee89 » Sun Dec 17, 2017 8:16 pm

Hey Geoff.

Thanks so much for that comprehensive reply. Im working on the code today and will try these changes and let you know how we get on.

Casey

kyotee89
Posts: 4
Joined: Wed Dec 13, 2017 5:35 am

Re: Box function and writing ascii with dec

Post by kyotee89 » Mon Dec 18, 2017 1:22 am

Geoff,

I have the code working well now. using char instead of string has the display showing the ascii characters instead of the binary values. Thanks so much for your help.

Final code for anyone interested

Code: Select all

#include <SPI.h>
#include <DMD2.h>
#include <fonts/SystemFont5x7.h>
#include <fonts/Arial14.h>

// Set Width to the number of displays wide you have
const int WIDTH = 2;

// You can change to a smaller font (two lines) by commenting this line,
// and uncommenting the line after it:
const uint8_t *FONT = Arial14;
//const uint8_t *FONT = SystemFont5x7;

char *MESSAGE;

SoftDMD dmd(WIDTH,1);  // DMD controls the entire display
DMD_TextBox box(dmd);  // "box" provides a text box to automatically write to/scroll the display


#include <Button.h>

int sw6 = A2;
int sw5 = A3;
int sw4 = A4;
int sw3 = A5;
int sw2 = 3;
int sw1 = 2;



int sw1State;
int sw2State;
int sw3State;
int sw4State;
int sw5State;
int sw6State;
int sw7State;
int sw8State;

int decimal;
int oldDecimal;
int i;
int j;
char character[20];

Button button1(4);
Button button2(5);

int relayPin = 12;

void setup() {
  
  // put your setup code here, to run once:

  Serial.begin(9600);
  
pinMode(2,INPUT);
pinMode(3,INPUT);

  button1.begin();
  button2.begin();

  dmd.setBrightness(50);
  dmd.selectFont(FONT);
  dmd.begin();


}

void loop() {

decimal = 64;

sw1State = digitalRead(sw1);
sw2State = digitalRead(sw2);
sw3State = digitalRead(sw3);
sw4State = digitalRead(sw4);
sw5State = digitalRead(sw5);
sw6State = digitalRead(sw6);



if (sw1State == HIGH) { decimal += 1; }
if (sw2State == HIGH) { decimal += 2; }
if (sw3State == HIGH) { decimal += 4; }
if (sw4State == HIGH) { decimal += 8; }
if (sw5State == HIGH) { decimal += 16; }
if (sw6State == HIGH) { decimal += 32; }



character[i] = lowByte(decimal);

if (oldDecimal != decimal) { dmd.clearScreen(); } //if character changes clear screen to clear any remaining lights left on

oldDecimal = decimal;





if (button1.pressed()){  //store current character
  Serial.print("enter character  ");
i++;
}


if (button2.pressed()) { // clear characters and clear screen
  i = 0;
  dmd.clearScreen();
  while ( j <= 19) {
    Serial.print("clear  ");
    Serial.println(j);
    character[j] = 0;
    j++;
  }
}

else {j = 0;}


dmd.drawString(0,0,String(character));
Serial.print(character);

Serial.println(" ");
Serial.print(sw1State);
Serial.print(" ");
Serial.print(sw2State);
Serial.print(" ");
Serial.print(sw3State);
Serial.print(" ");
Serial.print(sw4State);
Serial.print(" ");
Serial.print(sw5State);
Serial.print(" ");
Serial.print(sw6State);

Serial.println("  ");



delay(2);
}

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests