Badly Stuck- Need help please.(Arduino and bytes through serial port)

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)

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

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

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

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:

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

I assume you're referring to the body of the loop here:

for i=0 to 7
                                 x=x+serial.read()
                              next

You could use bitRead's twin, bitWrite.

Hi AWOL,
I am C++-challenged :stuck_out_tongue_closed_eyes: 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..

Here's a code snippet that reads a and turns it into an int.

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

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.

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

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.

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.

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

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.

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?

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


Rob

Sorry.. My wife took the phone and talked contineously with her mother for 35minutes so my internet was down... 8)

Rob:)
Thank you for posting the sketch... Really a very helpful in all respect:)....

wildbill :slight_smile:
Hmmm.. Sending bytes from computer to Arduino... My program sending for example 10101011..... Then(From left to right) 1=1byte 0=1byte 1= 1byte 0=1byte i.e. 10101011=8bytes... whatever i send it is taken as byte.... i can't send binary throgh serial port..:frowning: or simply i don't know how to send ...

zoomkat:)
My VB program currently send 1010101010101011111111111110010101010............................ To define a little more...
I am working on a graphical fountain... The theme is :

1- Programming a software to generate binary data based on black and white picture.
2- Installing 16 solenoid valves at the reservoir.
3- Sending two bytes of data to make on/off respective solenoid valve.
4- In a ms delay again send another row of two bytes.
this way we will get a graphical fountain of water drops..

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


Rob

Rob,
a) If i send strings of 0 and 1..can the arduino consider these strings as bits?..
b) Again how can i send Hex or binary?..do you have any pointer or thread ?... I am using MSCOM control in visual basic to make a communication link between Arduino and my software... Can i send Hexadicimal or binary directly to Arduino?

Hi for anybody interested to see the video what i want to accomplish here..

If you are sending strings of 1s and 0s, how do you determine when one string is complete and a new string has arrived? If you use a delimiter, then you can capture the string and evaluate it with the string functions included in the arduino IDE.

Khalid, what does the VB code look like that sends your '1' and '0' strings to the serial port?

Okay.. Here is the snippet of Visual Basic..I am using MSCOMM control and using COM3 as port.

I made this function for initialization of MSCOMM.

Private Function SetupPort(ByRef objCom As MSComm)
   With objCom
       .Break = False
       .DTREnable = True
       .EOFEnable = False
       .Handshaking = comNone
       .InBufferCount = 0
       .InBufferSize = MAX_PACKETSIZE
       .InputLen = MAX_PACKETSIZE
       .InputMode = comInputModeBinary
       .NullDiscard = False
       .OutBufferCount = 0
       .OutBufferSize = MAX_PACKETSIZE
       .ParityReplace = Chr(0)
       .RThreshold = 1
       .SThreshold = 0
       .Settings = "9600,N,8,1" 'default settings
   End With
End Function

I made a button in which i have following code. This code just initialize /open the COM port. and send a Character value to COM.

On Error Resume Next
ComArduino.CommPort = 3     'I am using COM 3 on my PC
SetupPort ComArduino        ' Now I have setup the port where ComArduino is the name of MSCOMM1 control
ComArduino.PortOpen = True  ' Now I have opened the port and can output
If Err.Number = 0 Then                      ' some error check
       MsgBox "Hey it’s open!"
   Else
       MsgBox "Doh it’s NOT open!”"
End If

Dim Buffer As Byte 
Buffer = &H2F   ' i.e.e Decimal equivalent 47
MsgBox Buffer    ' Msgbox shows 47
ComArduino.Output = Buffer

I successfully able to send the Hexadecimal number to arduino... Here is the Arduino code..

//Pin connected to ST_CP of 74HC595
int latchPin = 6;
//Pin connected to SH_CP of 74HC595
int clockPin = 13;
////Pin connected to DS of 74HC595
int dataPin = 11;
byte  c;
void setup() {
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
  Serial.begin(9600);
  Serial.println("reset");
}

void loop() {

  if (Serial.available() > 0)
   c = Serial.read();
   digitalWrite(latchPin, LOW); 
    shiftOut(dataPin, clockPin, MSBFIRST, c);
    digitalWrite(latchPin, HIGH);
    delay(100);
  }

I am having two shift registers IC 74HC595. sending different values lit the 16 LED's in different pattern. I do not know how can i collect two bytes in arduino sketch and send to both shift register. please help...