Shop OBEX P1 Docs P2 Docs Learn Events
Serial Communication — Parallax Forums

Serial Communication

NoBo780NoBo780 Posts: 94
edited 2008-01-06 00:59 in BASIC Stamp
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:
' {$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

  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2008-01-04 21:34
    It looks pretty close to working, you could try these minor modifications

    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.
  • NoBo780NoBo780 Posts: 94
    edited 2008-01-05 04:12
    Awesome! It works! Thanks! I didn't think the solution would be that easy; how did you know to do that?? Before I close this thread, I have one final question: I am using the two micros to run a robot (or will be using, for that matter). I did this test for a reason: so one micro could input sensory data, and send it over a serial line to the other micro, which then makes decisions based on the input. Now, why did I do this, you ask? Well, I have an idea: if one micro is doing the input and decision making, and the other is receiving the instructions on how to act (the result of the decision-making), the 'act' part being, in this case, 'send this bundle of numbers to the Roomba' (signifying 'drive left' or 'drive right' or 'stop', etc), then that should speed up the whole system, as opposed to having one micro doing all the input, decision-making AND acting, correct? Or is that not true because the micro SENDING the sensory data is just as bogged down doing the serial communication (the sender and/or receiver) as it is doing anything else? Do you get what I'm trying to say? Correct me if I'm wrong. I originally was using JUST the BS2e as the main controller, but it got too overwhelmed doing 1) monitoring sensors, 2) driving servos, 3) making decisions, and 4) generating random numbers for the decision making. In this case, I am testing this scenario with a simple sound detection circuit running into the Arduino (ATmega168), which voltage level is then converted with the internal A/D, sent via (2400bps) serial to the BS2e, and then displayed in a DEBUG window run by the PBASIC software from Parallax. When I run it at 9600bps, and make it check every 10ms for a clap, it almost always registers it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Giggly Googley!
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2008-01-05 06:51
    The serial communication between the two processors will certainly be a bottleneck. There are things you can do to improve speed and a lot depends how much and how often you are sending data. Rather than waste time having a processor sitting waiting for serial input you could have a master slave configuration. The master transmits a "rdy" signal and the slave responds with data. If there is a lot of data you might be able to split the transmission over two or three processor cycles. Another consideration is ensuring you reliably capture the data you send, your original problem was partly solved by telling the receiving processor (Stamp) "data has ended" , the carriage return (13) informed the SERIN that data had ended lets move on. Not only can you mark the ending of a data string you can look for a "header" that signals the start. A simple header might be the exclamation mark "!" and in the case of a Stamp you would use the WAIT instruction to look for the header SERIN RX,BAUD,[noparse][[/noparse]WAIT("!"),DEC4 in_data]. Enclosing data between start and end marks improves speed and reliability.

    Jeff T.
  • NoBo780NoBo780 Posts: 94
    edited 2008-01-05 15:14
    Cool. Thanks for the help. I always wondered what that "WAIT" command was for, now I got my question answered! It seemed to me that a master-slave configuration would be hard, but now that you put it that way, I don't think it'll be that hard to pull off. So basically, I could have the BS2e be the master, and the Arduino the slave. The Arduino will do all the sensory input, and the BS2e can do all the decision making and acting upon those decisions. When the BS2e is in the [noparse][[/noparse]short] pauses between code, I can have it send a signal to the Arduino, signifying that it is ready to receive the data. The Arduino then sends a "start" signal, telling the BS2e that it's about to receive (using the WAIT command on the BS2e side), which then the BS2e acts upon. So the WAIT command will not slow down the BS2e with a time-sensitive process (the Arduino may send the information at any time; the BS2e will essentially have to be checking all the time to make sure the Arduino gets its attention)? Thanks!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Giggly Googley!
  • NoBo780NoBo780 Posts: 94
    edited 2008-01-05 21:50
    Another thing to add:

    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:
    ' {$STAMP BS2e}
    ' {$PBASIC 2.5}
    '**Communication set at 9600bps**
    
    value1           VAR     Word
    value2           VAR     Word
    
    'Tx              PIN
    Rx              PIN     0
    Baud            CON     $54
    
    DO
    
      value1 = 0
      value2 = 0
      SERIN RX,BAUD,[noparse][[/noparse]WAIT("!"),DEC4 value1]
      DEBUG CLS, CR,"Value 1 is: ", DEC4 value1
      SERIN Rx, Baud, [noparse][[/noparse]WAIT("!!"),DEC4 value2]
      DEBUG CR,"Value 2 is: ",DEC4 value2
      'IF value > 500 THEN
        'GOSUB threshold
      'ENDIF
      'ENDIF
    
    LOOP
    
    threshold:
    
      DEBUG CR, "***CLAP***"
      END
    
    



    ...And the new Arduino code:
    //int txPin = 7;
    int mic1Pin = 0;
    int mic2Pin = 1;
    int val1 = 0;
    int val2 = 0;
    
    void setup() {
        Serial.begin(9600);    
            //pinMode(Tx_pin, OUTPUT);
    }
    
    void loop()
    {
      val1 = analogRead(mic1Pin);
      delay(500);
      Serial.println("!");
      Serial.println(val1);
      //Serial.println(13);
      val2 = analogRead(mic2Pin);
      delay(500);
      Serial.println("!!");
      Serial.println(val2);
      //Serial.println(13);
      //digitalWrite(txPin, val);
      
    }
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Giggly Googley!
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2008-01-06 00:04
    The WAIT instruction for value·1 which is waiting for 1 exclamation "!" could also be satisfied by the first exclamation for value 2.

    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.
  • NoBo780NoBo780 Posts: 94
    edited 2008-01-06 00:59
    That works! I am not getting the best results in the world with this; I think I'll save super time sensitive stuff like this (monitoring the sound sensors) for the Arduino to do alone. The BS2e, as it seems, has to 'catch up' with the Arduino when doing super fast transmission speeds. Other than that, this should be great to use with other sensory input. Thanks for the help!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Giggly Googley!
Sign In or Register to comment.