Pages: [1] 2   Go Down
Author Topic: Serial Communication without computer?  (Read 2413 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 1
Posts: 28
Sort of...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all, Im trying to use serial to wirelessly communicate between two arduinos, a mega and a duemilanove.. I dont know if this is the right place to post, but ive tried to unplug them from my computer and then they dont use serial anymore... How do i fix this?


Thanks for the help

BTW, FIRST POST (in this section)!! UBER PWND  smiley-cool
Logged

New York
Offline Offline
Edison Member
*
Karma: 1
Posts: 1023
E != m*c^2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What hardware are you using to communicate wirelessly? To clarify - the arduino's you listed aren't capable of wireless serial communication without external hardware like xbees, bluetooth, or rf modules.
Logged

0
Offline Offline
Newbie
*
Karma: 1
Posts: 28
Sort of...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

TELEPATHY  smiley (i wish)  I am using these rf modules:

http://www.sparkfun.com/products/8946 (transmitter)
http://www.sparkfun.com/products/8949 (reciever)

which are analog

Thanks
Logged

0
Offline Offline
Newbie
*
Karma: 1
Posts: 38
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This should be in the coding section. For wireless, you'll want to check out the XBee radios for arduino:

http://www.arduino.cc/playground/Shields/Xbee01

If you don't need wireless, and are fine with just a few wires between the systems, you can use a simple connection.

Connect board 1 pin 0 to board 2 pin 1
Connect board 1 pin 1 to board 2 pin 0
Connect the two grounds together.

You'll then want to look at the Serial library:

http://arduino.cc/en/Reference/Serial
Logged

0
Offline Offline
Newbie
*
Karma: 1
Posts: 28
Sort of...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, All i need to know is how to make it so that the arduinos will communicate without being hooked up to the computer.. I have it set up already so that they will transmit data, however they only do it when connected to computer (as if their talking to it) i am transmitting from one to the other, not bi directional, would that effect it?

Thanks
« Last Edit: January 25, 2011, 11:12:59 pm by x11kjm » Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6137
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What does your code look like?  There's no reason the hardware should need to be connected to your PC unless you are doing something wrong in the code.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

0
Offline Offline
Newbie
*
Karma: 1
Posts: 28
Sort of...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

okay for the Duemilanove i am using this -

Code:
int gTempCmd  = 0b00000011;
int gHumidCmd = 0b00000101;

int shiftIn(int dataPin, int clockPin, int numBits)
{
  int ret = 0;
  int i;

  for (i=0; i<numBits; ++i)
  {
    digitalWrite(clockPin, HIGH);
    delay(10);  // I don't know why I need this, but without it I don't get my 8 lsb of temp
    ret = ret*2 + digitalRead(dataPin);
    digitalWrite(clockPin, LOW);
  }

  return(ret);
}

void sendCommandSHT(int command, int dataPin, int clockPin)
{
  int ack;

  // Transmission Start
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  digitalWrite(dataPin, HIGH);
  digitalWrite(clockPin, HIGH);
  digitalWrite(dataPin, LOW);
  digitalWrite(clockPin, LOW);
  digitalWrite(clockPin, HIGH);
  digitalWrite(dataPin, HIGH);
  digitalWrite(clockPin, LOW);

  // The command (3 msb are address and must be 000, and last 5 bits are command)
  shiftOut(dataPin, clockPin, MSBFIRST, command);

  // Verify we get the coorect ack
  digitalWrite(clockPin, HIGH);
  pinMode(dataPin, INPUT);
  ack = digitalRead(dataPin);
  if (ack != LOW)
    Serial.println("Ack Error 0");
  digitalWrite(clockPin, LOW);
  ack = digitalRead(dataPin);
  if (ack != HIGH)
    Serial.println("Ack Error 1");
}

void waitForResultSHT(int dataPin)
{
  int i;
  int ack;

  pinMode(dataPin, INPUT);

  for(i= 0; i < 100; ++i)
  {
    delay(10);
    ack = digitalRead(dataPin);

    if (ack == LOW)
      break;
  }

  if (ack == HIGH)
    Serial.println("Ack Error 2");
}

int getData16SHT(int dataPin, int clockPin)
{
  int val;

  // Get the most significant bits
  pinMode(dataPin, INPUT);
  pinMode(clockPin, OUTPUT);
  val = shiftIn(dataPin, clockPin, 8);
  val *= 256;

  // Send the required ack
  pinMode(dataPin, OUTPUT);
  digitalWrite(dataPin, HIGH);
  digitalWrite(dataPin, LOW);
  digitalWrite(clockPin, HIGH);
  digitalWrite(clockPin, LOW);

  // Get the lest significant bits
  pinMode(dataPin, INPUT);
  val |= shiftIn(dataPin, clockPin, 8);

  return val;
}

void skipCrcSHT(int dataPin, int clockPin)
{
  // Skip acknowledge to end trans (no CRC)
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);

  digitalWrite(dataPin, HIGH);
  digitalWrite(clockPin, HIGH);
  digitalWrite(clockPin, LOW);
}

void setup()
{
  Serial.begin(2400); // open serial
}

void loop()
{
  int theDataPin  = 10;
  int theClockPin = 11;
  char cmd = 0;
  int ack;

  int val;
  int temp;

  sendCommandSHT(gTempCmd, theDataPin, theClockPin);
  waitForResultSHT(theDataPin);
  val = getData16SHT(theDataPin, theClockPin);
  skipCrcSHT(theDataPin, theClockPin);
  temp = -40.0 + 0.018 * (float)val;
  Serial.print("t");
  Serial.println(temp, HEX);         
}


And for the Mega I am using this (just to simply receive it) -
Code:
int incomingByte = 0; // for incomi
boolean temp = false;
boolean readFirst = false;
boolean readSecond = false;
int readValue = -1;

void setup() {
  Serial.begin(2400); // opens serial port, sets data rate to 9600 bps
}

void loop() {
  // send data only when you receive data:
  if (Serial.available() > 0) {
    incomingByte = Serial.read(); // read the incoming byte
    if (!readFirst) { // if we haven't read the first byte of the triad, then we are now
      char incomingChar = (char) incomingByte; // make it a character
      Serial.println(incomingChar); // print it to serial
      if (incomingChar == 't') { // if it's 't'
        temp = true; // then we are reading a temperature
      } else { // otherwise, it's an 'h'
        temp = false; // and we don't have temperature
      }
      readFirst = true; // we have read in the first of a triad
    } else if (!readSecond) { // if we haven't read the second byte, then we are now
      readValue = (incomingByte - 48) * 10; // so make the readValue the ASCII digit it represents * 10
      readSecond = true; // and mark that we read the second byte
    } else { // we must be reading the third byte
      readValue += (incomingByte - 48); // so add in the next ASCII digit
      processData(); // process the data
      readFirst = false; // and clear both flags
      readSecond = false; // so we read another value
    }
  }
}

void processData() {
  // do something with the data
}

Thanks
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8474
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Don't know if I'm missing something but you seem to be transmitting using synch and receiving using async.

Is that the case or did I not pay enough attention to the code?

Quote
wirelessly communicate

Where does the wireless come into it?

Also you are doing Serial.prints, if the PC is not connected these will hang waiting to clear the TX buffer (I'm pretty sure that's the case).
______
Rob

« Last Edit: January 26, 2011, 11:53:49 pm by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

0
Offline Offline
Newbie
*
Karma: 1
Posts: 28
Sort of...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I dont understand what you mean by "transmitting using synch and receiving using async."  However the wireless comes in with the second piece of code.  I am using basic rf modules, so there is no programming needed for those.

Thanks for reply
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8474
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The first code uses "shiftin" which is sync (ie uses a clock signal), but I don't see any "shiftOuts" in the other code. So how are they talking to each other.

Quote
Also you are doing Serial.prints, if the PC is not connected these will hang waiting to clear the TX buffer (I'm pretty sure that's the case).

 
Logged

Rob Gray aka the GRAYnomad www.robgray.com

0
Offline Offline
Newbie
*
Karma: 1
Posts: 28
Sort of...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am pretty new to arduino, and no i do not use "shiftOuts."  However, they are not talking to each other, one is sending the other the data, and it takes it from there, but does not send anything back. (Transmitter --> Reciever)  I am using Serial.prints, so how would i fix that?

Thanks for the help
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8474
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

OK, I see now, you are sending with Serial, the shiftIn stuff is for the sensor or whatever.

I'll have a closer look at th ecode.
______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8474
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I think your main loop is really prone to errors with all the state variables, partly because the

Serial.println(temp, HEX);

line will also send CR LF at the end of the two HEX chars, and as the data is a constant stream you could easily get out of sync.

I think something simpler may work, and if not should be easier to debug.

Code:
void loop() {
   
  if (Serial.available() > 0) {
    if (Serial.read() == 't') {
    while (Serial.available() < 2) {}; // wait for two bytes
  readValue = (Serial.read() - 48) * 10;
  readValue += (Serial.read() - 48);
    processData(); // process the data
    }
  }
}

Also consider sending the data ad a binary # so you don't have to do the -48 stuff.

   
Code:
readValue = (Serial.read() << 8);
  readValue += (Serial.read());
   

______
Rob

Logged

Rob Gray aka the GRAYnomad www.robgray.com

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 525
Posts: 26538
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I think what is really needed is to use VirtualWire between the 2 arduinos.
http://www.open.com.au/mikem/arduino/VirtualWire.pdf
Works great between those 2 devices, I use it as a remote control to send button pushes to a receiver.
It puts in some bytes for the receiver to recognize something is coming, has some encoding also to ensure data integrity.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

0
Offline Offline
Newbie
*
Karma: 1
Posts: 28
Sort of...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried to use that code and it didn't seem to do anything (rx/tx lights not on, or serial output).  However I did try a code (i forget where from) and it worked without being connected to my computer.  What code would i be able to use with just the arduinos+transmitters/recievers (no buttons or anything) just to see if it works?

Thanks
« Last Edit: January 28, 2011, 02:42:02 pm by x11kjm » Logged

Pages: [1] 2   Go Up
Jump to: