Pages: [1]   Go Down
Author Topic: How to communicate text and numbers over serial  (Read 4079 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, firstly, (i have two questions) I have two arduinos hooked up over serial, the tx pin on a duemilanove wired to the rx pin on a mega.  I plan to do this wirelessly, but when i disconnect them from my computer they do not communicate anymore, even though they do not need the computer to operate... How do i go about making it so that they do not need a computer to use serial?  Also, when I do wire them together, i am outputing a letter and a number like (t79) but on the other arduino there is a bunch of numbers coming out that go up to 255 it seems... How do i decode this or make it so it displays what i am outputting?  I am using this code:

Code:
int incomingByte = 0;
void setup(){
Serial.begin(2400);
}
void loop(){
if (Serial.available() > 0) {
incomingByte = Serial.read();
Serial.println(incomingByte, DEC);
}
incomingByte = 0;
}

Thanks for the help, sorry if any of this is confusing, im new..
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49080
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I have two arduinos hooked up over serial, the tx pin on a duemilanove wired to the rx pin on a mega.
Are the grounds connected, too?

Quote
Also, when I do wire them together, i am outputing a letter and a number like (t79) but on the other arduino there is a bunch of numbers coming out that go up to 255 it seems
We need to see the code on both sides - sender and receiver.

Most likely, the 79 is not being sent as 79, but as '7' and '9'. You are then printing the ASCII code for '7' (55), instead of the character received (the ,DEC is doing that).
Logged

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

Thanks for the reply, i dont understand what you mean about the grounds being connected...  They will be wireless, would that effect it? Anyhow, here is the transmitter code (its kinda big...) -
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);
      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.println(temp, HEX);        
         }
      }
   }
}


Logged

Pages: [1]   Go Up
Jump to: