Go Down

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

Khalid

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

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


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..
http://www.youtube.com/watch?v=tnPnwET4x9k
http://www.youtube.com/watch?v=mFD__5FyXVY
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...:)

zoomkat

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.
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

wildbill

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

Khalid

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.
Code: [Select]
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.

Code: [Select]
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..

Code: [Select]

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

Quote
I do not know how can i collect two bytes in arduino

Code: [Select]
if (Serial.available() > 1)
   int c = Serial.read();
   int d = Serial.read();

Problem is if you get out of sync, and miss a byte.
Best to use some sort of delimiter
"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,
Thanks for the reply... Now some more questions as we are proceeding to end of this project.

1-  Now as per your suggestion the code will be look like:Please check carefully both of the shiftout functions.. are they right?

Code: [Select]

void loop() {

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



2- Any sketch you can share using the delimiter?
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...:)

zoomkat

Some simple code that looks for a comma in the sent string.

Code: [Select]

//zoomkat 9-9-10 simple delimited ',' string parce
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter

String readString;

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

void loop() {

        //expect a string like wer,qwe rty,123 456,hyre kjhg,
        //or like hello world,who are you?,bye!,
        while (Serial.available()) {
        delay(1);  //small delay to allow input buffer to fill
    if (Serial.available() >0) {
        char c = Serial.read();  //gets one byte from serial buffer
        if (c == ',') {break;}  //breaks out of capture loop to print readstring
        readString += c; } //makes the string readString
        }
     
      if (readString.length() >0) {
      Serial.println(readString); //prints string to serial port out
     
      readString=""; //clears variable for new input
      }
   }

Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Khalid

Okay guys .. I am very thankful for your help.. I somehow managed to run the program successfully.. My 16 LED simulator giving me best results without loosing any bits...Following is the visual basic code to send two bytes and then a small delay(for loop) and then again send two bytes..The arduino received exactly what i sent...
Code: [Select]
ComArduino.Output = Chr(&H7F)
ComArduino.Output = Chr(&H1C)
For i = 0 To 10000
Next i
ComArduino.Output = Chr(&H0)
ComArduino.Output = Chr(&H1)


I am not using any delimiter.. I have made a programmable delay in the Visualbasic to send two bytes and then a small delay and then send two bytes... At the Arduino end, following is the workable code..
Code: [Select]

//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;
byte c1;

void setup() {
 //Start Serial for debuging purposes
 Serial.begin(9600);
 //set pins to output because they are addressed in the main loop
 pinMode(latchPin, OUTPUT);

}

void loop() {
 
  if (Serial.available() > 0){
      c = Serial.read();
    c1= Serial.read();}
 //count up routine
    digitalWrite(latchPin, 0);
   //count up on 1st byte
   shiftOut(dataPin, clockPin, c);
   //count down on 2nd byte
   shiftOut(dataPin, clockPin, c1);
   //return the latch pin high to signal chip that it
   //no longer needs to listen for information
   digitalWrite(latchPin, 1);
   delay(1000);
 }

void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
 // 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;
   }

   //Sets the pin to HIGH or LOW depending on pinState
   digitalWrite(myDataPin, pinState);
   //register shifts bits on upstroke of clock pin  
   digitalWrite(myClockPin, 1);
   //zero the data pin after shift to prevent bleed through
   digitalWrite(myDataPin, 0);
 }

 //stop shifting
 digitalWrite(myClockPin, 0);
}


The above code is for 16 solenoid valves operated through Arduino and 02 74HC595... If we have to increase the solenoid valves upto multyple of 8i.e.  8, 16,24,32, 40,48,56 etc.. we just to add some more declaration of bytes and shiftout like following code:
Code: [Select]


// THIS CODE IS FOR 40 SOLENOID VALVES
byte c;
byte c1;
[b]byte c2;
byte c3;
byte c4;[/b]
void setup() {
 //Start Serial for debuging purposes
 Serial.begin(9600);
 //set pins to output because they are addressed in the main loop
 pinMode(latchPin, OUTPUT);

}

void loop() {
 
if (Serial.available() > 0){
      c = Serial.read();
    c1= Serial.read();
[b]       c2 = Serial.read();
    c3= Serial.read();
      c4 = Serial.read();[/b]
}
 //count up routine
    digitalWrite(latchPin, 0);
   //count up on 1st byte
   shiftOut(dataPin, clockPin, c);
   //count down on 2nd byte
   shiftOut(dataPin, clockPin, c1);
   //count down on 3rd byte
   shiftOut(dataPin, clockPin, c2);
   //count down on 4th byte
   shiftOut(dataPin, clockPin, c3);
   //count down on 5th byte
   shiftOut(dataPin, clockPin, c4);
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...:)

wildbill

AWOL nailed it: it'll work until it doesn't, especially your extension for more solenoids; there is no guarantee that the subsequent characters will have arrived when you look for them. If you wish to be safe, you need to tweak it a little to be sure that there is data to be read before you act on it. The more you extend your model, the greater the likelihood that the later values of c<n> will be incorrect.

Khalid

Thanks WildBill.. this venture was only possible with your help guys... Currently i have only 16 solenoid valves available... this project will not stopped here but i will post videos of the LED simulator model and the operation of actual solenoid valves...

As far as the proper protocol and formatting of the coding/decoding... I will need all of your help....so far i am really very happy what i have got..
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...:)

wildbill

The concept looks awesome - I'd love to see some video when it's working. It seems that there are plenty of folks here that can offer help with the code & electronics so you have no excuse for delays  ;) Is it done yet?

Khalid

Hi no problem with electronics.. I etched the simulator in the way that to add IRF540  8) ... As the simulator works perfect and i have a good experience with electronics in the past..so i envisaged no problem in near future..

The solenoid valves i purchased from JunkYard and are of 220mA 24V DC... Driving them with a 4-Amp 24VDC supply will be enough:)...
Now i can test and design the nozzles for the solenoid to get a good shape droplets..this is again no problem because i am a mechanical engineer;)
The youtube video of the simulator in working condition is uploading..soon will share the link with you guys :smiley-sweat:
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

Look at the video and the following data:

0000000000000000
0000001111000000
0000111111111000
0000111111110000
0000001111100000
0000001111100000
0000011111110000
0011111111111110


Just see how the leds on/off as per each row of data...
http://www.youtube.com/watch?v=bGZjb0nHGJQ


also you can see the PCB board...

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

Can you show us the schematic, a couple of things appear to be strange, the Molex connectors (they are for the solenoids right?) seem to be on the base resistors and I can't see how the LEDs are driven.

Also no explicit GND for the solenoids, you can use the Arduino connector but this will be a much larger wire (or indeed bunch of wires).

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

Go Up