Pages: [1] 2 3 ... 6   Go Down
Author Topic: Badly Stuck- Need help please.(Arduino and bytes through serial port)  (Read 7095 times)
0 Members and 1 Guest are viewing this topic.
Pakistan
Offline Offline
Sr. Member
****
Karma: 6
Posts: 357
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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   

« Last Edit: March 11, 2011, 09:28:47 am by Khalid » Logged


Pakistan
Offline Offline
Sr. Member
****
Karma: 6
Posts: 357
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25898
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

http://arduino.cc/en/Reference/BitRead
Logged

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

Pakistan
Offline Offline
Sr. Member
****
Karma: 6
Posts: 357
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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..:(
« Last Edit: March 11, 2011, 10:41:28 am by Khalid » Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25898
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

You could use bitRead's twin, bitWrite.
Logged

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

Pakistan
Offline Offline
Sr. Member
****
Karma: 6
Posts: 357
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Logged


nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8477
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's a code snippet that reads a <xxxxxxxx> and turns it into an int.
Code:
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

 
« Last Edit: March 11, 2011, 10:57:23 am by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25898
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Pakistan
Offline Offline
Sr. Member
****
Karma: 6
Posts: 357
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25898
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Pakistan
Offline Offline
Sr. Member
****
Karma: 6
Posts: 357
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25898
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, I meant "left four place" Oops!.
Logged

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

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3643
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8477
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Pages: [1] 2 3 ... 6   Go Up
Jump to: