|
Author |
Topic: help on BX-24 code (Read 849 times) |
|
olivier
|
help on BX-24 code
« on: Sep 25th, 2003, 7:24pm » |
|
Hello, I know how to write a value on a serial connection from processing, and how to get it on the BX-24. I saw in the reference that I could send lots of data in a byte array from p5. Useful in my case, because I need to control some servo-motors. But how do I 1/ get them on the BX-24 ? (silly question, I guess) 2/ separate the different values. I guess this is quite the same for one value or for more, but the hidden question is: "Is there a way in the BX-24 language to know which is the first value of the array?" This is crucial Because I'm not sure the very first value the chip will catch is actually the first of the array. Can someone give me a piece of code, please?
|
|
|
|
mKoser
|
Re: help on BX-24 code
« Reply #1 on: Sep 25th, 2003, 9:27pm » |
|
A work-around (that i've been using) could be something like this: 1. limit all your data to be within 1-255 2. send a zero in the beginning of every time you send stuff, in that way the chip can look for zero, when it gets one of those, it knows the next one is item number 1, 2, 3 etc. of course this means sending all your data as single bytes (not an array as you are asking for) ... but hey, i've done it with up to 12 pieces of data, and everything worked fine. btw. I use a BasicStampII, but it doesn't really matter since the above is meerely a concept. i hope that helped! + mikkel here's some code off the top of my head (let me know if there's errors!) Code: byte[] data = {11, 22, 33, 44, 55, 66}; void setup() { beginSerial(); } void loop() { sendMyData(); } void sendMyData(){ // sends a flag, saying, NEXT number is the first piece of data serialWrite(0); for(int i=0; i<6; i++){ // write the piece of data, (limited to above 1). serialWrite(max(1, data[i])); } } |
|
|
mikkel crone koser | www.beyondthree.com | http://processing.beyondthree.com
|
|
|
olivier
|
Re: help on BX-24 code
« Reply #2 on: Sep 25th, 2003, 10:42pm » |
|
thank you mKoser, i've already discovered this system, in an empirical way, but i thought it was my own personal "bricolage" and there was a cleaner way to do it. Now, i'd like to go further in understanding how serial communication works between p5 and basicX. For example, if i need to send a string, and use the byte array method in p5, will the chip receive the whole string in one time, or will it receive it byte by byte? By the way, it brings another question: how can I debug my basicX code (i mean use and see the result of the Debug.print() method) while keeping p5 running and sending serial events? If i try to do so, one of the two softwares refuses to open the port. Thank you for your help (and sorry for the newbie questions)
|
|
|
|
carlosgiffoni
|
Re: help on BX-24 code
« Reply #3 on: Sep 26th, 2003, 7:04am » |
|
I actually need the code to do the opposite thing, how exaclty can Processing READ from the serial port, I want to use sensors read them with the basic stamp and send values to processing trough the serial port to use them as variables... couldnt find anything on the learning section Thanks Carlos
|
|
|
|
mKoser
|
Re: help on BX-24 code
« Reply #6 on: Sep 26th, 2003, 10:58pm » |
|
Hi Carlos, As I have written above, I DO NOT use the BX-24, so in a way, posting the following code could confuse and mislead, anyway, here is some sample code for a BASIC STAMP II that reads the value of 8 sensors and spits it out the serial cable using the "protocol" mentioned in my post above (sending zero as a flag to say, THIS IS THE START!). I hope the code is working, I found several versions of it, but as far as I can see (withour hooking everything up and testing it) it should work: Code: '{$STAMP BS2} 'testing SERIAL continuety 'mikkel . crone . koser '18 . 02 . 2003 '-------------------------- tempData VAR Byte i VAR Nib j VAR Nib h VAR Nib Loop: 'set pins ready for action FOR i = 1 TO 8 HIGH i+7 NEXT 'sends STARTER ITEM SEROUT 4, 16468, [0] PAUSE 5 FOR j = 0 TO 7 RCTIME 8+j, 1, tempData 'reads data tempData = tempData MIN 5 'limits outgoing data to ABOVE 5 SEROUT 4, 16468, [tempData] PAUSE 5 NEXT GOTO Loop |
| I hope this helps! + mikkel
|
« Last Edit: Sep 26th, 2003, 11:01pm by mKoser » |
|
mikkel crone koser | www.beyondthree.com | http://processing.beyondthree.com
|
|
|
benelek
|
Re: help on BX-24 code
« Reply #7 on: Sep 27th, 2003, 3:19am » |
|
for those of us who know nothing about microcontrollers, or what is meant by a "BASIC stamp", HowStuffWorks has a good article on the subject: http://computer.howstuffworks.com/microcontroller.htm
|
|
|
|
carlosgiffoni
|
Re: help on BX-24 code
« Reply #8 on: Sep 27th, 2003, 5:36pm » |
|
thanks everyone mikkel whats your code on the processing side looking like for this basic stamp II code? are you just reading a value into a variable on the serialevent function from serial? I am actually going to work with basic stamp II but no luck yet. C
|
|
|
|
mKoser
|
Re: help on BX-24 code
« Reply #9 on: Sep 27th, 2003, 7:01pm » |
|
hi carlos, sorry, i should have thought of posting some P5 code before aswell...! try something like this: Code: // pulling data from BasicStampII // mikkel . crone . koser // sept. 27 2003 int data; int sens1, sens2, sens3, sens4; int sens5, sens6, sens7, sens8; void setup(){ beginSerial(); } void loop(){ println("--------------"); println("sens1 = " + sens1); println("sens2 = " + sens2); println("sens3 = " + sens3); println("sens4 = " + sens4); println("sens5 = " + sens5); println("sens6 = " + sens6); println("sens7 = " + sens7); println("sens8 = " + sens8); } void serialEvent(){ data = serial; if(data < 5){ num = 0; // STARTER-flag has been recieved! } if(num == 1){ sens1 = data; }else if(num == 2){ sens2 = data; }else if(num == 3){ sens3 = data; }else if(num == 4){ sens4 = data; }else if(num == 5){ sens5 = data; }else if(num == 6){ sens6 = data; }else if(num == 7){ sens7 = data; }else if(num == 8){ sens8 = data; } num++; } |
| + mikkel PS - I did not test this, i cut it out (any modifeied it) from a more complex piece of code.
|
mikkel crone koser | www.beyondthree.com | http://processing.beyondthree.com
|
|
|
Elrick
|
Re: help on BX-24 code
« Reply #10 on: Oct 15th, 2003, 8:32pm » |
|
My ponderation is this.. I don't know enough basicx code to know if this is plausible.. But what if you defined a "start segment" thing that got sent at the beginning of every data transmission (such as 0x00) which was immediatley followed by a data ID (0x01 for your light sensor, 0x02 for your motor input, 0x03 for motor output, etc) which told either p5 or the MC what do do with the following data, and perhasp other properties, like the total message length. Also, you could have a closing tag 0xFF, so that each peice knows when the data it has is useable. Just an idea. You know, kinda like internet packets.
|
|
|
|
|