Add Character And Comma To Array

Hi,

I would like to ask, let's say I have an array, example:

char running[]={ 

1111100000000000
1111111000000000
1111111110000000
1111111111000000
1111111111100000
1111111111110000
1111111111111000
1111111111111000
1111111111111000
1111111111111100
1111111111111100
}

How can I add one character 'B' and comma every 8 bits to the array so that I can send the data to shift register (74HC595) as Byte?

I want the result to be like:

char running2[]={ 
B11111000,B00000000,
B11111110,B00000000,
B11111111,B10000000,
B11111111,B11000000,
B11111111,B11100000,
B11111111,B11110000,
B11111111,B11111000,
B11111111,B11111000,
B11111111,B11111000,
B11111111,B11111100,
B11111111,B11111100,
}

Is it possible?

I am actually trying to make small project using bitmap to ASCII. The ASCII code generated by the software doesn't have 'B' at the front and comma every 8 bits.

Hope somebody can help me out.
I've tried using

   for (int j = 0; j <= nooflines; j=j+noofmodules) {
    newval = 'B' + running[0];
   } ...........

But still stuck.

You want two different things:

  1. streaming the bytes to a shift register
  2. modifying your code?

for (1) you could use something like this pseudocode

int len = sizeof(running);
for (int i=0; i< len; i+=8)
{
  for (j=0; j<8; j++)
  {
    byte val = val << 1 + running[i+j] -'0';
  }
  // send byte to shift register
  // delay(100);
}

Thank you for the reply.

For the streaming and sending to the shift register if the data is in Bytes, I've got it.

e.g: B11110000.

I'm stuck on how to 'sort' the array from the long array,
(as in the example 1st - 1111000000001111) to bytes as per in example 2.
( To B11110000, B00001111, ...... )

It's like organizing the data back.

What function should I use?

Is it make sense?

Here's my full code.

In this example, I send the byte from the array running[]. (which the data has been modified to byte form . e.g : B00000011, B00000001 .....

My main question is, how can I send the data to shift register using only raw array data. So that I don't have to modify every 8 bits and put 'B' character manually, and put comma at the end.

//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;



//holders for information you're going to pass to shifting function
byte module1;
byte module2;

byte newval;

int delaytime= 20;


 
byte running[]={ 
/* raw array
11111000,00000000,
11111110,00000000,
11111111,10000000,
11111111,11000000,
11111111,11100000,
11111111,11110000,
11111111,11111000,
11111111,11111000,
11111111,11111000,
11111111,11111100,
11111111,11111100,
11111111,11111100,
11111111,11111100,
11111111,11111100,
11111111,11111100,
11111111,11111100,
11111111,11111100,
11111111,11111100,
11111111,11111000,
11111111,11111000,
11111111,11110000,
11111111,11100000,
11111111,11100000,
11111111,10000000,
11111111,00000000,
11111100,00000000,
11100000,00000011,
00000000,00001111,
00000000,00111111,
00000000,01111111,
00000000,11111111,
00000001,11111111,
00000001,11111111,
00000011,11111111,
00000011,11111111,
00000111,11111111,
00000111,11111111,
00000111,11111111,
00000111,11111111,
00000111,11111111,
00000111,11111111,
00000111,11111111,
00000111,11111111,
00000111,11111111,
00000011,11111111,
00000011,11111111,
00000001,11111111,
00000000,11111111,
00000000,01111111,
00000000,00111111,
00000000,00011111,
00000000,00000111,

 */
 

B11111000,B00000000,
B11111110,B00000000,
B11111111,B10000000,
B11111111,B11000000,
B11111111,B11100000,
B11111111,B11110000,
B11111111,B11111000,
B11111111,B11111000,
B11111111,B11111000,
B11111111,B11111100,
B11111111,B11111100,
B11111111,B11111100,
B11111111,B11111100,
B11111111,B11111100,
B11111111,B11111100,
B11111111,B11111100,
B11111111,B11111100,
B11111111,B11111100,
B11111111,B11111000,
B11111111,B11111000,
B11111111,B11110000,
B11111111,B11100000,
B11111111,B11100000,
B11111111,B10000000,
B11111111,B00000000,
B11111100,B00000000,
B11100000,B00000011,
B00000000,B00001111,
B00000000,B00111111,
B00000000,B01111111,
B00000000,B11111111,
B00000001,B11111111,
B00000001,B11111111,
B00000011,B11111111,
B00000011,B11111111,
B00000111,B11111111,
B00000111,B11111111,
B00000111,B11111111,
B00000111,B11111111,
B00000111,B11111111,
B00000111,B11111111,
B00000111,B11111111,
B00000111,B11111111,
B00000111,B11111111,
B00000011,B11111111,
B00000011,B11111111,
B00000001,B11111111,
B00000000,B11111111,
B00000000,B01111111,
B00000000,B00111111,
B00000000,B00011111,
B00000000,B00000111,

};


int codesize = sizeof(running)/ sizeof(int);
int lines= codesize; //54  numbers of coding lines
int nooflines = (lines*2)-1;  
int noofmodules = 2; // numbers of module connected

  
// stringThree =  stringOne + 'A';  


void setup() {
  //set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);
  Serial.begin(9600);
}


void loop() {


  
  for (int j = 0; j <= nooflines; j=j+noofmodules) {
    //load the light sequence you want from array
 
    module1 =  running[j];
    module2 =  running[j+1];
    

    
    //stringThree =  stringOne + 'A'; 

    //ground latchPin and hold low for as long as you are transmitting
    digitalWrite(latchPin, 0);
    //move 'em out
    shiftOut(dataPin, clockPin, module2);
    shiftOut(dataPin, clockPin, module1);
    //return the latch pin high to signal chip that it 
    //no longer needs to listen for information
    digitalWrite(latchPin, 1);
    delay(delaytime);
  }
  

}



// the heart of the program
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
  // This shifts 8 bits out MSB first, 
  //on the rising edge of the clock,
  //clock idles low

  //internal function setup
  int i=0;
  int pinState;
  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, OUTPUT);

  //clear everything out just in case to
  //prepare shift register for bit shifting
  digitalWrite(myDataPin, 0);
  digitalWrite(myClockPin, 0);

  //for each bit in the byte myDataOut[ch65533]
  //NOTICE THAT WE ARE COUNTING DOWN in our for loop
  //This means that %00000001 or "1" will go through such
  //that it will be pin Q0 that lights. 
  
  for (i=0; i<=7; i++)  {
    digitalWrite(myClockPin, 0);

    //if the value passed to myDataOut and a bitmask result 
    // true then... so if we are at i=6 and our value is
    // %11010100 it would the code compares it to %01000000 
    // and proceeds to set pinState to 1.
    
    if ( myDataOut & (1<<i) ) {
      pinState= 1;
    }
    else {      
      pinState= 0;
    }

    //Sets the pin to HIGH or LOW depending on pinState
    digitalWrite(myDataPin, pinState);
    //register shifts bits on upstroke of clock pin  
    digitalWrite(myClockPin, 1);
    //zero the data pin after shift to prevent bleed through
    digitalWrite(myDataPin, 0);
  }

  //stop shifting
  digitalWrite(myClockPin, 0);
}

thank you.

Your first code has an array that was a char array, the function I proposed converted that to byte named val, that can be sent to the shift register.

Now you have a byte array (which is much 8 x more memory efficient) and that implies you need to add the B and the , manually.

The Arduino IDE supports no macro's , so find one that does and you can automate it. Other option is to use a python script to modify your source code.

robtillarrt, thank you for the reply.

I've just found out, when I compile the array with

char running[]={

1111100000000000
1111111000000000
1111111110000000
1111111111000000
1111111111100000
1111111111110000
1111111111111000
1111111111111000
1111111111111000
1111111111111100
1111111111111100
}

I will get error: integer constant is too large for 'long' type

So, I think it doesn't work that way.

And as your pseudocode,

int len = sizeof(running);
for (int i=0; i< len; i+=8)
{
  for (j=0; j<8; j++)
  {
    byte val = val << 1 + running[i+j] -'0';
  }
  // send byte to shift register
  // delay(100);
}

It's only work for the long array.

What if I would like to send bytes to the shift register if using this array. (8 bits per array)

char running2[]={
B11111000,B00000000,
B11111110,B00000000,
B11111111,B10000000,
B11111111,B11000000,
B11111111,B11100000,
B11111111,B11110000,
B11111111,B11111000,
B11111111,B11111000,
B11111111,B11111000,
B11111111,B11111100,
B11111111,B11111100,
}

Which part of your code should I change then.

At least, to eliminate the 'B' character, it should be ok for the first stage.
I don't have knowledge about the phython as for now.

Do I need to make a software that will sort all the raw arrays?

As you see in previous code, I use two shift out.

shiftOut(dataPin, clockPin, module1);
shiftOut(dataPin, clockPin, module2);

This is because I'm testing this code with two 74HC595 chips.
The final would be 10 pcs connected together.

aminbahar,

How big is your array? If it is small, you can just change it by hand. If it is large, you could fix it with 'vim' or 'perl'. Linux comes with these tools, and they are available for free download for the PC. Do you know how to use either of these tools?

To fix it, you would put the array into a separate file, use the tool to reformat it, and then paste the fixed array into your Arduino sketch.

Regards,

-Mike

Your questions seem to indicate a fundamental misunderstanding of how shift registers work, and how data is stored on the Arduino.

For a variable of type byte, you can store a value like 27 in that byte using base 2 representation, B00011111, base 8 representation, 033, base 10 representation, 27, or base 16 (hexadecimal) representation, 0x1F.

All ways result in the same bit pattern being stored. When sending data to the shift register, it is the bit pattern that is important.

Storing data in a character array, where the string of characters LOOKS like the binary representation of the number is not something you want to be doing.

@Mike,

The final array can be big. That's why I'm looking for a way to sort the array to the Binary type. (with 'B' and comma every 8 bits).

For this project, I'm just trying 16 outputs (2 x 74HC595). But the final, I will be using about 10 to maybe 20 74HC595. I'm replicating some sort of printer. All the nozzle will be in one row.

Mike, can you give me some info on how to that? (the one using 'vim' or 'perl'?)
Some links or tutorial would be good. Or is there any available software to do it?
As for now, I only got the bitmap to ASCII converter software. (which give me all the patterns in '1' and '0' as in the array) - All the array will be in raw type. (No 'B' Character and commas.)

@PaulS

Yes, I know that in order for the shift register to work, we need to send the bytes.
I've give the attached sample of the code so that you will get an idea on of what i'm doing.

As per sample 1, all the data is send using Bytes.
(Sorry for the Char type - I'm newbie. It should be Byte).

Do you have any idea on how to do this?

If let's say I will be having about 10x 74HC595, it will be about 80 columns of array.
The row can be bigger than that, depends on the graphics that I converted using the bitmap> ASCII code software.

111111111111111100001111
111111111111111100001111
111111111111111100001111
111111111111111100001111
111111111111111100001111
111111111111111100001111
111111111111111100001111
111111111111111100001111
111111111111111100001111
111111111111111100001111
111111111111111100001111
111111111111111100001111
111111111111111100001111
111111111111111100001111
111111111111111100001111
111111111111111100001111
111111111111111100001111
111111111111111100001111
111111111111111100001111
111111111111111100001111
111111111111111100001111
111111111111111100001111
111111111111111100001111
111111111111111100001111
000011111111111100001111
000011111111111100001111
000011111111111100001111
000011111111111100001111
111100000000000011111111
111100000000000011111111
111100000000000011111111
111100000000000011111111
111111111111111111111111
111111111111111111111111
111111111111111111111111
111111111111111111111111
111111111111111111111111
111111111111111111111111
111111111111111111111111
111111111111111111111111

Here's an example of word 'J'. But in '0', and '1' form.
This is the raw data I got from the bitmap to ASCII software.

From this, I would like it to be sorted by the arduino, and output the difference '0' and '1' using the shift register. As for now, I am testing it using led. But at the end, I will be connecting it to the ULN2803A and drive the solenoid/printer.

Hope this helps you to understand more.

Before you go much further, I suggest you look at the use of PROGMEM

@ AWOL,

If considering about storage, it doesn't bother me much.
I might try putting all those arrays in the SD card and read it back and so on. That'll be the 2nd thing.

But now, I just want to know, how if let say I have the basic sample of those arrays given. (in previous post), how can I sort it out?

From raw array, >> put 'B' character in the front and commas at the end of every 8 bits. So that it will be ready to be send to the shift register.

Once the raw arrays has been organize, it will be more easy then.

I will just use this code to shift out it to 2nd, 3rd, 4th and ... shift register.

void loop() {
  for (int j = 0; j <lines; j=j+2) {   // for 2 74HC595


    module1 =  running[j];
    module2 =  running[j+1];


    digitalWrite(latchPin, 0);

    shiftOut(dataPin, clockPin, module2);   // module 2 first, then module 1
    shiftOut(dataPin, clockPin, module1);

    digitalWrite(latchPin, 1);

Is it possible?

I might try putting all those arrays in the SD card and read it back and so on

SD cards are slooooooowwwww.

how if let say I have the basic sample of those array given. (in previous post), how can I sort it out?

From raw array, >> put 'B' character in the front and commas at the end of every 8 bits.

I don't know which editor you're using, but I'd write a macro.
If your editor doesn't support macros, change editor.

@AWOL

Macro? Can you explain further?
If it's like a setting, I don't think so my editor have those settings. (to add n character every y line and etc etc).
Can you suggest me? Or any links that I can download from?

Ya, SDCARD might be slow. But, I've seen end product using serial to transfer the data. But do not know if they use some sort of encryption or maybe convert it to DEC.

Macro? Can you explain further?

In an editor that supports macro's you can write or record a number of actions and store them as a small editing program called a macro. A macro can be executed much faster than doing things manually.

From raw array, >> put 'B' character in the front and commas at the end of every 8 bits. So that it will be ready to be send to the shift register.

You still don't seem to understand that putting a 'B' in front of a string of characters does not make the string of characters a byte. Putting a comma after every 8 characters does not make the data byte sized.

Where is this "data" coming from? You really need to understand what a shift register needs, and to understand that you are not even close to providing that.

@robtillaart

Can you suggest me the name of the software? Do you have the one with free ware so that I can try first?

Sorry as I am newbie in this field.

@PaulS

Sorry if I didn't get it right.
So it is not possible for me to achieve the result this way?

One question, I've gone through one thread about the string.

stringThree = stringOne + 'A';

For this example, can't we change it to

stringThree = 'B' + stringOne;

And from the string, can we make it as a byte?

*sorry if it sounds silly. Hope that you can help.

I use notepad++ often, - Downloads | Notepad++

@robtillaart

thanks for sharing.

It helps a lot! (manually).

But hopefully I can do it automatically.

One question, I've gone through one thread about the string.

stringThree = stringOne + 'A';

For this example, can't we change it to

stringThree = 'B' + stringOne;

Sure. The order of the operations doesn't matter.

And from the string, can we make it as a byte?

Converting "B00010001" to a byte is not as easy as

byte b = stringThree;

though it can be done.

Start over again with where the data is coming from. The stream of '0' and '1' characters may be easier to deal with than stuff stored in an array of indeterminate type (you keep changing the type).