Go Down

Topic: Badly Stuck- Need help please.(Arduino and bytes through serial port) (Read 8558 times) previous topic - next topic

Khalid

I have made a software that converts black and white picture into binary data. The purpose is to send each row of this binary data to the  Arduino that will then ON/OFF the 16 solenoid valves. Currently i am using 16Leds with 74HC595 shift registers(No.02) to simulate the things. I am using Visualbasic to send the Data through the serial port. The problem is i do not know how to collect the data byte by byte or bit by bit by the Arduino... I know how the shiftout function works... i know a little about serial.read functionality.
My software can convert between binary to decimal and Hexadecimal.


The following is what i want to do:
Step-1:  in the following binary data, my VB software will split the first row into two bytes. Send the first byte (00000000) to Arduino and then the second byte (00000000) to Arduino.

0000000000000000
0000000111000000

Step-2: The Arduino recieve the data from the serial port in two Variables.
Step-3: The Arduino send the byte data to the two shift registers using Shiftout function.
Step-4: On the shiftout basis the LED will ON/OFF.

The process will then repeat to the second row of mentioned binary data i.e.(11000000) and (00000001)

Code: [Select]
int incomingByte = 0; // for incoming serial data

void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {

// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();

// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
}


The above code read bytes...but how can it read bits 0 or 1?
Thanks in advance.



MY SOFTWARE GENERATED OUTPUT FROM PICTURE:


BINARY DATA                        
   
0000000000000000
0000000111000000
0000001111000000
0000111101111000
0000111111111000
0000000000000000
0000111111110000
0011111111111100
0000001111100000
0000001111100000
0000001111100000
0000111111111000
0000011111110000
0000111111111000
0011111111111110
0011111111111110

DECIMAL REPRESENTATION
0   0   
1   192   
3   192   
15   120   
15   248   
0   0   
15   240   
63   252   
3   224   
3   224   
3   224   
15   248   
7   240   
15   248   
63   254   
63   254

HEXADECIMAL REPRESENTATION
0x00   0x00   
0x1   0xC0   
0x3   0xC0   
0xF   0x78   
0xF   0xF8   
0x00   0x00   
0xF   0xF0   
0x3F   0xFC   
0x3   0xE0   
0x3   0xE0   
0x3   0xE0   
0xF   0xF8   
0x7   0xF0   
0xF   0xF8   
0x3F   0xFE   
0x3F   0xFE   

Simply...You can't afford me..

Author Of:
http://my-woodcarving.blogspot.com/
http://www.free3dscans.blogspot.com/
http://my-diysolarwind.blogspot.com/

Oops..some one gave me Karma...:)

Khalid

Here is the software that i made to convert the Black and white picture into binary/Hexadecimal and decimal. This is to control the number of solenoid valves...

Simply...You can't afford me..

Author Of:
http://my-woodcarving.blogspot.com/
http://www.free3dscans.blogspot.com/
http://my-diysolarwind.blogspot.com/

Oops..some one gave me Karma...:)

AWOL

"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.

Khalid

Thank You AWOL.. I have comeacross bitread before.. I searched the internet, read the arduino forum and then posted the thread here to get help.. Just need advise:

My software currently generate the binary data contineously with given number of solenoid valves. The minimum number of solenoid valves =8 so 8bits and ibytes;).. should i add some character between the binarydata so that Arduino first recognize a character then after ward store the incoming bits in a array and then if received the de-limit character, it store the value into variable.
Supose i modify the software outputlike this:
<10000000><11100001>
Pseudocode:

Code: [Select]
if (Serial.available() > [color=red]2[/color]) {
// read the incoming byte:
incomingByte = Serial.read();
            if incomingbyte=='<' then
                             for i=0 to 7
                                x[color=yellow][i]=x[i]+serial.read()[/color]
                             next
            else if  incomingbyte=='>'
               
            endif

i dont know how to write the code for this..:(
Simply...You can't afford me..

Author Of:
http://my-woodcarving.blogspot.com/
http://www.free3dscans.blogspot.com/
http://my-diysolarwind.blogspot.com/

Oops..some one gave me Karma...:)

AWOL

I assume you're referring to the body of the loop here:
Code: [Select]
for i=0 to 7
                                 x=x+serial.read()
                              next


You could use bitRead's twin, bitWrite.
"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.

Khalid

Hi AWOL,
I am C++-challenged  :smiley-yell: Needs someone hands to helpme out...I am very very near to get this sort out...

Just think:
1- My software releases binary data in the form of <10101010><00101010><10101011>........to Arduino.
2- as i have only two shift register to control 16LEDS, so i want to get only two bytes of data.
3- I want to get  <10101010><00101010> into Arduino, parse it and get this 10101010 00101010, convert this into two bytes...
4- Send it to Shiftout() so that respective LED's lit/off.

The above process run continuously software send the binary data...Arduino collect two bytes at a time..parse it and send to the Shiftout..


Simply...You can't afford me..

Author Of:
http://my-woodcarving.blogspot.com/
http://www.free3dscans.blogspot.com/
http://my-diysolarwind.blogspot.com/

Oops..some one gave me Karma...:)

Graynomad

#6
Mar 11, 2011, 04:52 pm Last Edit: Mar 11, 2011, 04:57 pm by Graynomad Reason: 1
Here's a code snippet that reads a <xxxxxxxx> and turns it into an int.
Code: [Select]
void setup() {                
   Serial.begin(115200);    
}

void loop() {
byte val;
       char c;
if (Serial.peek() > 0) {  
c = Serial.read();
switch (c) {

case '<': // sync on the '<' ignore everything else
val = 0;
for (int i = 0; i < 8; i++) {
do {} while (Serial.available() == 0);
c = Serial.read();
val <<= 1;
val |= int(c - '0');
       }
                       Serial.println (val, BIN);
}
}
}


Not all you need but should give you a start.

NOTE no error checking etc.

EDIT: Change the 8 to 16 and make your VB prog send <1111111111111111> instead of two separate bytes.


______
Rob

Rob Gray aka the GRAYnomad www.robgray.com

AWOL

You said earlier that you had the hex and the decimal representations - is there a good reason to convert to binary?
Binary  - 8 characters per byte,
Hex - 2 characters per byte.
"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.

Khalid

Hi AWOL,
Yes i have the Hexadecimal representations too...Last night i was thinking to convert the binary into Hexadecimal to shorten up the whole things.. but i am stuck here with Arduino side... How can i achieve the above steps, lets say with Hexadecimal values?

Regards
Simply...You can't afford me..

Author Of:
http://my-woodcarving.blogspot.com/
http://www.free3dscans.blogspot.com/
http://my-diysolarwind.blogspot.com/

Oops..some one gave me Karma...:)

AWOL

A hex digit is 0..9 a..f (or A..F).
Assuming you're receiving the ASCII representation of the digit,
if (digit >= '0' &&digit <= '9')
  digitVal = digit - '0';
if (digit >= 'a' &&digit <= 'f')
  digitVal = digit - 'a' + 10;
Don't forget to shift the most significant digit right four places.
"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.

Khalid

Thanks AWOL for helping this project.
My foolish Questions(I hope you will bear with them): :~ :~

1- What is the data type of  "digitVal" and "Digit" ? i.e. int, char,byte??...
2- Can you please help me "Don't forget to shift the most significant digit right four places"

Best Regards


PS:
Sorry for my ignorance. thanks for helping me out.
Simply...You can't afford me..

Author Of:
http://my-woodcarving.blogspot.com/
http://www.free3dscans.blogspot.com/
http://my-diysolarwind.blogspot.com/

Oops..some one gave me Karma...:)

AWOL

"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.

wildbill

Why bother converting the byte data into a string of '<00000000>' that you have to parse on the arduino? Is there any reason why you can't simply send the bytes as is to the serial port, then, for the 8 solenoid case all you have to do is read it & shift it out to the shift register. For sixteen bits, just take every other byte & switch which shift register you send it to.

zoomkat

It is somewhat confusing as to what your VB program is actually sending on the serial port. Are you actually just sending two bytes, or are you sending a string of 1s and 0s to represent the two bytes?
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Graynomad

I understood a string of 1s and 0s, but as has been pointed out why bother, just send HEX or binary.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Go Up