Go Down

Topic: *NOOB* Arduino and binary data (Read 3298 times) previous topic - next topic

AWOL

I don't understand what you're doing here
Code: [Select]
for ( int i=1;i<=4;i++){
          NewMat[1]=input_line[i];

All that happens is that the second element of NewMat is assigned the value of the fifth element of "input_line" - you don't need a loop to do that.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Y2K986


I haven't followed which part you're stuck on, but I noticed that you're using ',' as a separator and seem to be printing the content of the buffer when you recognise it - but you only add the null terminator to the buffer when you recognise a '\n'. Aren't you printing an unterminated string at that point? I suggest that it would be safer to append a null terminator after every character is added to the buffer.


Null terminator is character we add in a string to tell where that string ends? If so, why do you suggest to add it after every character is added to the buffer?


The problem you have seems to be parsing the string tokens into an integer. You seem to be most of the way there and just need to call an integer parsing function which can work out what base your input string is in. This would be easier if your input string was in a known base instead of supporting hex and binary and who knows what else. Do you really need to support all those formats?


Well, I don't really. I thought it would be easier since I already made it work with hex and binary. I'm in a faze where if I make something work, I stick with it. :) Otherwise, I should work on other way of making a sketch. And that other way comes with new and other problems for which a really don't have nerves, and I suppose, neither do you.
Sketch works with decimal also, so if i put {255,255,255} it turns all LEDs on etc. I saw some topics where people used 0-255 numbers to control servo motors. But they still needed to convert that string into an integer. Would this be a good way to go? Something like:
Code: [Select]
info = Serial.read () - '0';
val += info*100;
info = Serial.read () - '0';
val += info*10;
nfo = Serial.read () - '0';
val += info*1;



I don't understand what you're doing here
Code: [Select]
for ( int i=1;i<=4;i++){
         NewMat[1]=input_line[i];

All that happens is that the second element of NewMat is assigned the value of the fifth element of "input_line" - you don't need a loop to do that.


That was a mistake. I thought that second element of NewMat ( should have been first ) will take all the values of input_line, char by char. Last solution mentioned above was your suggestion on one of the topics. Will it work? A mean will it remember a string as an integer?


AWOL

Quote
If so, why do you suggest to add it after every character is added to the buffer?

If you add a terminator after every character as you read it, then your string will always be correctly terminated, then your next read character overwrites the previous terminator, and so on.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Y2K986

This is the sketch for reading data from serial monitor. As you can see it can only process 10 lines, but this is only because i defined matrix like that.
What it does is, it reads serial data from serial monitor in a shape of  xxx,xxx,xxx , where xxx is a number between 0 and 255. Then that gets changed to integer and sent to shifters with shiftout function. You can try it out.

Code: [Select]
int latchPin = 8 ;    //Pin connected to ST_CP of 74HC595
int clockPin = 12;    //Pin connected to SH_CP of 74HC595
int dataPin  = 11;    //Pin connected to DS of 74HC595

char* prvi = "0";    // reads character
int a;               // stores char as int
int matrix[10][3];   // int array for storing serial data
int MaxNrChar = 4;   // max number of characters
int NrChar = 0;      // for counting characters

void setup ()
{
  Serial.begin(9600);
  pinMode(latchPin, OUTPUT);
} // end of setup
 
void loop(){
 
  static int V=0;      // changes vertical position in matrix
  static int H=0;      // changes horizontal position in matrix
  static int I=100;    // for making an integer
   
  if (Serial.available () > 0){
    digitalWrite(latchPin, 0);
    prvi[0] = Serial.read ();   Serial.print( "Character is: " ); Serial.println( prvi[0] );
    switch (prvi[0]){
     
      case '\n':   // end of text
        digitalWrite(latchPin, 1);  //return the latch pin high to signal chip that it no longer needs to listen for information
        NrChar=0;
        H=0;
        I=100;
        V=V+1;
      break;
       
      case ',' :
        shiftOut(dataPin, clockPin, matrix[V][H]);
        NrChar=0;
        H=H+1;
        I=100;
      break; 
 
      case '\r': 
        shiftOut(dataPin, clockPin, matrix[V][H]);
        NrChar=0;
      break;
 
      default:
        NrChar++;
        if (NrChar < MaxNrChar){
           a = atoi(prvi);   Serial.print( "Int character is: " ); Serial.println( a );
           matrix[V][H]=matrix[V][H] + a*I;
           I/=10;
        }
        Serial.print( "Integer array: " ); Serial.println ( matrix[V][H] );
      break;
      }      // end of switch
   }         // end of incoming data
}            // end of loop

void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {     // the heart of the program
  //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?
  //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=7; i>=0; 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;
    }
    digitalWrite(myDataPin, pinState);      //Sets the pin to HIGH or LOW depending on pinState
    digitalWrite(myClockPin, 1);            //register shifts bits on upstroke of clock pin 
    digitalWrite(myDataPin, 0);             //zero the data pin after shift to prevent bleed through
  }
  digitalWrite(myClockPin, 0);              //stop shifting
}


This sketch works with COM Port Data Emulator ( software which reads data from a file and sends it through a serial port ), but I can't make it to work with gobetwino. I would appreciate your help with this matter.

Code: [Select]
for ( int i=1; i<=10; i++ ){
  Serial.print("#S|READLINE|[");
  Serial.print(i);
  Serial.println("]#");
  delay ( 500 );
}


This is put in loop and does nothing. Gobetwino reads a line from file, apparently sends it through a serial port, but seems like Arduino doesn't see anything. Nothing happens...
Any help?

PeterH


This is put in loop and does nothing. Gobetwino reads a line from file, apparently sends it through a serial port, but seems like Arduino doesn't see anything. Nothing happens...
Any help?


The sketch you posted doesn't actually send the READLINE command.

The code fragment you posted sends multiple READLINE commands but does not read any responses, so it's not surprising that 'nothing happens'.

Your sketch needs to send one command and then read the response. None of the code you've posted so far does this. If you think you have already done this, then post your code.
I only provide help via the forum - please do not contact me for private consultancy.

Y2K986

This sketch does work with a program that sends data from file, without command in arduino that "activates" that program. Software sends data, arduino is at "stand by" and reacts after a data is sent through serial port. That works.
Gobetwino doesn't work that way, because I need to call it from arduino. What I want to do is read one/first line with "G=1" variable, and after a line gets to "/n" , G is increased by 1, so it can jump to the next line in file.
Code: [Select]
Serial.print("#S|READLINE|[");
  Serial.print(G);
  Serial.println("]#");


Like you said, that doesn't work. I can't understand why. I told the gobetwino to send line and that is happening ( I can see that from gobetwino that line is being read from file ). But, the problem is arduino doesn't see that line ( doesn't see anything because nothing is happening ). And first line is always being sent from gobetwino, because it actually never gets to "/n". What should I change?
Code: [Select]
int latchPin = 8 ;    //Pin connected to ST_CP of 74HC595
int clockPin = 12;    //Pin connected to SH_CP of 74HC595
int dataPin  = 11;    //Pin connected to DS of 74HC595

char* prvi = "0";    // reads character
int a;               // stores char as int
int matrix[10][3];   // int array for storing serial data
int MaxNrChar = 4;   // max number of characters
int NrChar = 0;      // for counting characters

void setup ()
{
  Serial.begin(9600);
  pinMode(latchPin, OUTPUT);
} // end of setup
 
void loop(){

  static int V=0;      // changes vertical position in matrix
  static int H=0;      // changes horizontal position in matrix
  static int I=100;    // for making an integer
  static int G=1;      // Nr. of line in *.txt file
 
  Serial.print("#S|READLINE|[");
  Serial.print(G);
  Serial.println("]#");
     
  if (Serial.available () > 0){
    digitalWrite(latchPin, 0);
    prvi[0] = Serial.read ();   Serial.print( "Character is: " ); Serial.println( prvi[0] );
    switch (prvi[0]){
     
      case '\n':   // end of text
        digitalWrite(latchPin, 1);  //return the latch pin high to signal chip that it no longer needs to listen for information
        NrChar=0;
        H=0;
        I=100;
        V=V+1;
        G=G+1;
      break;
       
      case ',' :
        shiftOut(dataPin, clockPin, matrix[V][H]);
        NrChar=0;
        H=H+1;
        I=100;
      break; 
 
      case '\r': 
        shiftOut(dataPin, clockPin, matrix[V][H]);
        NrChar=0;
      break;
 
      default:
        NrChar++;
        if (NrChar < MaxNrChar){
           a = atoi(prvi);   Serial.print( "Int character is: " ); Serial.println( a );
           matrix[V][H]=matrix[V][H] + a*I;
           I/=10;
        }
        Serial.print( "Integer array: " ); Serial.println ( matrix[V][H] );
      break;
      }      // end of switch
   }         // end of incoming data

}            // end of loop

void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {     // the heart of the program
  //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?
  //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=7; i>=0; 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;
    }
    digitalWrite(myDataPin, pinState);      //Sets the pin to HIGH or LOW depending on pinState
    digitalWrite(myClockPin, 1);            //register shifts bits on upstroke of clock pin 
    digitalWrite(myDataPin, 0);             //zero the data pin after shift to prevent bleed through
  }
  digitalWrite(myClockPin, 0);              //stop shifting
}

Go Up