Serial Communication
I had recently bought a BS2e, and I am now wondering about how to deal with matching up the baud rate with peripherals. Is the BS2e 16-bit? If I am communicating to a device that operates at 8 bits, will that not work? I'm not sure of what a description means by "operates at 8-bits". Maybe I'm using the definition wrong? I need to communicate via serial communication with an Arduino (it's an AVR ATmega168 microcontroller on a fancy dev. board). Here is some info some info on the ATmeag168: I think it can operate at up to like 115,200bps (but it is capable of operating at speeds the BS2e can handle), it is an 8-bit microcontroller, has a clock sped of 20Mhz max (although I think it's using a 16Mhz crystal...), and of course uses standard 5V logic for serial communication. Someone mentioned to me that the 'parity' needs to be the same (in the case of the BS2e, apparently, it has none), as well as the 'stop bits' (once again, it's apparently 1 'stop bit' in the case of the BS2e). I have no idea what those mean; I have never heard of those terms before. I tried to get the two microcontrollers to talk to each other, but the values are very skewed. The reason why I need to 'team' up the BS2e and the ATmega168 is because I need a lot of computing power; I just got a refurbished Roomba Red, and I am going to expand on a previous software design that the BS2e could not take on alone, and implement it with the Roomba hardware and ROI (Roomba Open Interface). In my case, the AVR is doing all the A/D and sensory input, and the BS2e is acting upon it. Is there a way to combat this communication problem? I have attached the [noparse][[/noparse]basic stamp] code, as well as the Arduino code. Of course, it is based off of C, and it is actually pretty easy to interpret; it has some of the same syntax as the PBASIC code structure. Ignore any 'commented' out parts (in the case of the Arduino, it's a '//'). Just for the heck of it, here's a picture of the 'brain stack', as I call it. This setup makes it easy to connect things between the BS2e and Arduino, and it holds them together.
BS2e Code:
Arduino Code:
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Giggly Googley!
Post Edited (NoBo780) : 1/4/2008 8:52:35 PM GMT
BS2e Code:
' {$STAMP BS2e} ' {$PBASIC 2.5} value VAR Word(3) 'Tx PIN 0 Rx PIN 1 Baud CON $186 DO value = 0 SERIN Rx, Baud, [noparse][[/noparse]value] 'IF value > 700 THEN 'GOSUB threshold 'ENDIF DEBUG CR, DEC value 'ENDIF LOOP 'threshold: 'DEBUG "High" 'END
Arduino Code:
//int txPin = 7; int micPin = 0; int val = 0; void setup() { Serial.begin(2400); //pinMode(Tx_pin, OUTPUT); } void loop() { val = analogRead(micPin); Serial.println(DEC, val); //digitalWrite(txPin, val); delay(500); }
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Giggly Googley!
Post Edited (NoBo780) : 1/4/2008 8:52:35 PM GMT
Comments
Stamp
value···········VAR·····Word
SERIN·Rx,·Baud,·[noparse][[/noparse]DEC4 value]
Arduino
Serial.println(DEC,·val);
Serial.println(13);
The thinking is to send a carriage return from the Arduino immediately following "val" and for the Stamp to be ready for a decimal value that might contain as many as 4 digits.
Jeff T.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Giggly Googley!
Jeff T.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Giggly Googley!
I got the WAIT command working-- sort-of. When I run the program now, it displays ONLY value1, but not value2. Th reason why I have two values now is because I now have two sound sensor circuits set up instead of just one like before. I tried sticking in a random number for value2 to see what happens. It sends value2 on the FIRST send/receive cycle, and then no longer sends it anymore. It displays, I think, value1 from 2 send/receive cycles ago on the second line in the DEBUG window. I also checked the second circuit I set up on its own with the serial monitor in the Arduino software; it's working. What am I doing wrong?
Here's the new BS2e code:
...And the new Arduino code:
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Giggly Googley!
WAIT is looking for a character or string of characters that match the value inside the brackets it doesn't care if the character(s) are in the middle of a larger string so it is best to use different wait strings and try to make them unique. Something like WAIT("V#1") and WAIT("V#2") would be better. Better still would be to let the Stamp know which value you have sent
SERIN RX,BAUD,[noparse][[/noparse]WAIT("VAL"),DEC1 val_num,DEC4 value]
The above waits for the string "VAL" the second instruction (DEC1 val_num) lets us know if we have value 1 or value 2 and finally the actual value.
There may be a better way of doing this depending on exactly how you want to send the value's, if they are always sent consecutively you might be better waiting for both values with one SERIN instruction
SERIN RX,BAUD,[noparse][[/noparse]WAIT("VALS"),DEC4 value1,DEC4 value2]
Jeff T.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Giggly Googley!