*NOOB* Arduino and binary data

0001110000011100 0011111101111110 1111111111111111 1111111111111111 0011111111111110 0001111111111100 0000011111110000 0000001111100000 0000000010000000

This "data" is stored in *.txt file and I want to send it to arduino and control 16 LEDs. Problem is I don't know how. What is the easiest way to do this? I plan on using 74hc595 shifters to control even more, but without this first step there is pretty much nothing I can do. I downloaded Gobetwino, but can't get it to work just yet. Is this good solution, or is there better/easier ?

Gobetwino would enable you to do it without needing to write code on the Windows side.

Shift register:

http://www.gammon.com.au/forum/?id=11518

Binary data?

unsigned int line1 = 0b0001110000011100;

PeterH: Gobetwino would enable you to do it without needing to write code on the Windows side.

Ok. Will try harder.

[quote author=Nick Gammon link=topic=129094.msg971386#msg971386 date=1351214146] Shift register: http://www.gammon.com.au/forum/?id=11518[/quote]

I'll use my breadboard and test it.

[quote author=Nick Gammon link=topic=129094.msg971386#msg971386 date=1351214146] Binary data?[/quote]

Did I say something wrong? :blush:

[quote author=Nick Gammon link=topic=129094.msg971386#msg971386 date=1351214146]

unsigned int line1 = 0b0001110000011100;

[/quote]

Don't understand this. This is the first line of data. Do you suggest to use variable for each line ( 9 of them ). Can you please explain?

Thank you all. :)

Did I say something wrong?

No. Just my sense of humour.

Are you planning to read from a file? If not, you can turn a line of 0s and 1s into a binary constant by putting 0b in front of it like I showed. Then shift that out to the shift register (high byte then low byte).

[quote author=Nick Gammon link=topic=129094.msg971413#msg971413 date=1351217321] Are you planning to read from a file? If not, you can turn a line of 0s and 1s into a binary constant by putting 0b in front of it like I showed. Then shift that out to the shift register (high byte then low byte). [/quote]

Yes I am. This is just a prototype version, just to see how it works. Eventually, I want to be able to send dozens of lines, using 10 or more shifters. This site you suggested, sketch does not read data from a file, or I'm missing that part?

Y2K986: Don't understand this. This is the first line of data. Do you suggest to use variable for each line ( 9 of them ). Can you please explain?

If you were willing to hard-code the binary data in your sketch, that would be easy to do.

Or you could enter them manually via the serial port and have your sketch save them to EEPROM. (The 'manually' part could consist of running a command on the PC to send the right sequence of strings to the Arduino's serial port.)

But your solution of having the Arduino read the file via Gobetwino is perfectly viable IMO.

void setup()
{
Serial.begin(9600);

Serial.println("#S|READFILE|[1]#");
delay(750);
Serial.println("#S|READFILE|[2]#");
delay(750);
Serial.println("#S|READFILE|[3]#");
delay(750);
Serial.println("#S|READFILE|[4]#");
delay(750);
Serial.println("#S|READFILE|[5]#");
delay(750);
Serial.println("#S|READFILE|[6]#");
delay(750);
Serial.println("#S|READFILE|[7]#");
delay(750);
Serial.println("#S|READFILE|[8]#");
delay(750);
Serial.println("#S|READFILE|[9]#");
}

void loop()
{
}

So here is the code for communicating between gobetwino and arduino. Couple of questions though:

  1. Is there a way to make it shorter? I tried using “for” but gobetwino does not understand "#S|READFILE| #".
    * *for (int i = 1; i < 10; i++) { Serial.println("#S|READFILE|[i]#"); }* *
    2. The first line is: “0001110000011100”. Is there a way of memorising it? I want to control LEDs so I need to send it to them somehow. If I understand well my previous sketch is only for communicating between *.txt file and arduino, but the data is yet to be send to the output pins.
for (int i = 1; i < 10; i++) {
Serial.println("#S|READFILE|[i]#");
}

Since i is contained within the string literal, you are sending it exactly like that, with the i and everything.

for (int i = 1; i < 10; i++)
{
  Serial.print("#S|READFILE|[");
  Serial.print(i);
  Serial.println("]#");
}

Also note the tabbing and formatting difference makes it easier to read.

2. The first line is: "0001110000011100". Is there a way of memorising it?

There are tons of memorization techniques, but a stream of 16 binary digits like that isn’t a tall task for your brain…

Unless you meant the microcontroller “memorizing” it. If so, you’ll have to example on what you mean by “memorizing”

You could easily generate those Gobetwino commands in a for loop instead of replicating the code.

Have you configured the READFILE command in Gobetwino? The Gobetwino log will show you whether it's being recognised and executed successfully, if you have tracing and logging turned on.

The sketch needs to actually read the response from Gobetwino to receive the contents of the file - you're sending lots of commands but not reading the responses.

I’ve changed the code like Arrch suggested:

void setup()
{
Serial.begin(9600);

for (int i = 1; i < 10; i++)
{
  Serial.print("#S|READFILE|[");
  Serial.print(i);
  delay ( 750 );
  Serial.println("]#");
}
}
void loop()
{
}

and this is what is happening with gobetwino:

Apparently READFILE command is configured and working fine.

PeterH:
The sketch needs to actually read the response from Gobetwino to receive the contents of the file - you’re sending lots of commands but not reading the responses.

I think we’re on the same page here. What i want is to use that lines ( one line at a time ) and send it to a LEDs to turn on ( if it is a “1” ) or to turn off ( if it is a “0” ), but all at the same time. So if a first line is 1111111111111110 arduino should “read” it and turn on all LEDs except the last one. When next line comes, depending on the structure, it should do the same thing and so on…So, my question is: How can I forward each line at a time to LEDs?
I’ll be using 74hc595 shifters for that, so i need 3 output pins for Data, Latch and Clock. This is where things get tricky for me, and I’ll be needing your help guys.
Thanks. :slight_smile:

So, my question is: How can I forward each line at a time to LEDs?
I’ll be using 74hc595 shifters for that, so i need 3 output pins for Data, Latch and Clock. This is where things get tricky for me, and I’ll be needing your help guys.

The sketch on the link I provided shows fiddling with bit patterns in memory, and then shifting the results out to the 595 chips. You can use SPI for that, the shifting is trivial.

For example, with 2 chips (16 LEDs):

  digitalWrite (LATCH, LOW);
  SPI.transfer (0xAB);
  SPI.transfer (0xCD);
  digitalWrite (LATCH, HIGH);

The tricky bit is to replace those constants with variables, which you compute at runtime, by reading a file. But it’s not that tricky. A bit of mucking around with checking if you get a 0 or 1, and then ORing in a 1 in the right position, is all that is required. Example snippet:

   // divide by 8 to work out which chip
   byte chip = led / 8;  // which chip

  // remainder is bit number
   byte bit = 1 << (led % 8);
   
   // turn bit on or off
   if (state)
     LEDdata [chip] |= bit;
   else
     LEDdata [chip] &= ~ bit;

Nick Gammon has explained how to convert the LED states to a bitmask and do the shifting side of things to output to the LEDs. You will also need to read the incoming data from Gobetwino and work through each line character by character deciding whether the corresponding bit is set or unset and using Nick's bitmasking code to apply that value to the appropriate output value. I assume you have figured out that in the real sketch you will need to send one command and process the response before you send the next command, not just send out all your commands in setup() as your example does.

Thank you all for your time, but this started to look very difficult for me. I feel like trying to understand, I don't know, maybe rocket science, and without knowing to multiply or divide... =(

Nick Gammon I've just started learning arduino IDE and all of it's commands, so working with libraries is really hard for me. Don't get me wrong, I really appreciate your time and effort, but simply I don't fell that currently I'm able to learn all that stuff and understand it. :blush: I tried your sketches on your forum, and I saw that they work with some simple pattern beforehand specified. Sketches for 4 shifters and commands in serial monitor don't work for me. I used 2 shifters ( currently don't have more ) and all that happens is LED No.9 ( first on shifter No.2 ) is always On, and none of those commands work. Maybe I fu*ed something up...

*PeterH I was kinda hoping that all of this could be done very easily. With For loop a get every line from *.txt file every 750mSec, with serial.available() check if there is any data on the serial port, and if so use some variable to "store" it ( maybe with serial.read() ) and send it to LEDs with shiftOut(). What happens is serial.available() gives "0" like there is no data, and I got stuck on very beginning of my idea...

Bottom line, I don't feel ready ( and I don't have the knowledge, hence NOOB in topic title ) to work with arduino on advanced level. I was hopping that all of this can be done more easily and beginners friendly. If not, all I can do now is learn as hard as I can, and try to figure something out. Every help is welcome... :roll_eyes:

and all that happens is LED No.9 ( first on shifter No.2 ) is always On, and none of those commands work.

Check your wiring I suggest. If you can’t get that demo to work, you will probably have troubles with something more complex.

For now, experiment with a simple example, eg.

#include <SPI.h>

const byte LATCH = 10;

void setup ()
{
  SPI.begin ();
}  // end of setup

byte c;
void loop ()
{
  c++;
  digitalWrite (LATCH, LOW);
  SPI.transfer (c);
  digitalWrite (LATCH, HIGH);
  delay (20);
}  // end of loop

If that doesn’t “count up” through 8 LEDs you have a wiring problem.

Y2K986:
What happens is serial.available() gives “0” like there is no data, and I got stuck on very beginning of my idea…

Maybe that was before you had got Gobetwino set up properly. We can see now that it’s sending data back to you and if your sketch tried reading the responses I expect you would find it was able to do that successfully.

This works just fine. Counting LEDs ( from right to left ? ), and then starting all over again.
Sketch to make the LEDs light up in sequence works perfectly also. And look at that, now all sketches are working. Except when I turn serial monitor on, all LEDs blink once and turn off except for LED No.9…Might be me, but it’s weird every time on same sketch.

PeterH:
Maybe that was before you had got Gobetwino set up properly. We can see now that it’s sending data back to you and if your sketch tried reading the responses I expect you would find it was able to do that successfully.

This is my code:

void setup()
{
Serial.begin(9600);
for (int i = 1; i < 10; i++)
  {
    Serial.print("#S|READFILE|[");
    Serial.print(i);
    delay ( 750 );
    Serial.println("]#");  
    int A = Serial.available();
    Serial.println(A);
    int data = Serial.read();
    Serial.println(data);
  } 
}
void loop()
{
}

So, if I use a variables to store any available data this is what I get:

Obviously, I’m doing something wrong. What I want is, that every time For loop changes “i” by 1, variable reads and stores a line and i can send it to shifters. Can it be done?
In short, every time gobetwino sends a line, somehow i " pick " him up and fast forward it to shifters. Like, " real-time " sketch. Other solution is to use variables to store every line and then send it forward. Any way suits me, just to make it work. Can’t believe this is becoming that complicated… =( =(

    int A = Serial.available();
    Serial.println(A);
    int data = Serial.read();

Don't read data unless it is available.

You got available = 0, so you read -1 as data. You have to restructure.

http://www.gammon.com.au/serial

Looks like you've got some sort of LED matrix display there, showing a heart :D

fellow noob here, but I'm thinking you could use a shift register (like the 74HC595) to shift out the data one by one? You'd need to daisy-chain quite a few of these though, but it's fairly simple. There's a great tutorial on Bildr:

http://bildr.org/2011/02/74hc595/

/*
Example of processing incoming serial data without blocking.

Author: Nick Gammon
Date:   13 November 2011

Released for public use.
*/

// how much serial data we expect before a newline
const unsigned int MAX_INPUT = 50;

void setup ()
{
  Serial.begin(9600);
} // end of setup

// here to process incoming serial data after a terminator received
void process_data (char * data)
  {
  // for now just display it
  Serial.println (data);
  }  // end of process_data
  
void loop()
{
static char input_line [MAX_INPUT];
static unsigned int input_pos = 0;

  if (Serial.available () > 0) 
    {
    char inByte = Serial.read ();
    switch (inByte)
      {
      case '\n':   // end of text
        input_line [input_pos] = 0;  // terminating null byte
    
        // terminator reached! process input_line here ...
        process_data (input_line);
       
        // reset buffer for next time
        input_pos = 0;  
        break;
  
      case '\r':   // discard carriage return
        break;
  
      default:
        // keep adding if not full ... allow for terminating null byte
        if (input_pos < (MAX_INPUT - 1))
          input_line [input_pos++] = inByte;
        break;
      }  // end of switch
  }  // end of incoming data
  // do other stuff here like testing digital input (button presses) ...
}  // end of loop

I studied these sketches, and if I figured it out right, this is the one for reading and displaying any serial data if available. Am i wrong? :blush:
Now, all what we need to do is send some data through serial port ( COM3 in my case ) . Correct me if I’m wrong but that is being done with:
Serial.println("#S|READFILE|[1]#");
which “calls” gobetwino to send data through COM3. I tried putting this line in couple of places in sketch ( where I thought it makes sense to be ) but with no result. Something here doesn’t make sense… So, can you please suggest where this line should be put. I’m afraid I’ll make a mistake and you’ll yell at me again. :wink: