Pages: [1]   Go Down
Author Topic: RF modules  (Read 1189 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 102
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everybody, I'm trying to send soem data via the tx and rx pins of the arduino. I used a rf 433.92 tx module connected to the arduino tx and a rf 433.92 rx module connected to the arduino rx. I tried to write and print with the tx, and in the rx tried to read, but I get nothing...

I already used the virtualWire library and worked well, that means I didn't do nothing wrong with the connections. Maybe the problem is the code.

Here's the tx code:

void setup () {
 Serial.begin(4800);
 pinMode(3, OUTPUT);
 }
void loop () {
 Serial.write("ok"); // Already tried print and got nothing

}


And here's the rx code:

String text = String("  ");
      void setup(){
        Serial.begin(4800);
        pinMode(13,OUTPUT);
      }
      void loop(){
        int pos = 0;
        while(Serial.available()>0){
          char c = char(Serial.read());
          text.setCharAt(pos,c);
     pos++;
        }
        if(text.equals("ok")){
          digitalWrite(13,HIGH);
          delay(1000);
          digitalWrite(13,LOW);
          text = String("  ");
        }
      }

Thanks!
Logged

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

Serial data transmission is relatively slow, especially at 4800 baud.

The loop function is called in an endless loop, hundreds of thousands of times per second. The serial data dribbles in, byte by byte.

On every pass through loop, you start over reading data from the serial port.
On one pass, you'll find that there is a character to read, 'o'. You read that, but the 'k' hasn't arrived yet.

So, text does not equal "ok", so the LED does not get lit.

On the next few hundred passes, you set pos back to 0, but there is nothing to read.

Finally, the 'k' arrives. So, it's put in position 0. The string text does not contain "ok", so the LED does not get lit.

You need to include start-of-packet and end-of-packet markers in the sent data (<ok>), and, in the receiver, initialize (not create a new) the String object when the start-of-packet marker ('<') arrives, add characters to it (using concat or +=, not setCharAt) until the end-of-packet marker ('>') arrives.

Only when the end-of-packet marker arrives should you care about the contents of the packet.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 102
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, so I have to verify if the received char is < and then make a for to finish the text? Gonna try it
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 27363
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

Yes, more than likely it is your code :-)
You said virtualwire worked, and now you have a totally simplified sketch that ignores all the things that virtualwire does, running at a speed that is likely at least 2 times too fast.

Serial.begin, serial.write, serial.read, etc use pins 0 & 1.
You appear to be doing somethin with pin 3 on the TX side, I don't know what.

virtualwire would have used 11 & 12 by default. Had you changed them to use 0 & 1?
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
Full Member
***
Karma: 0
Posts: 102
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The virtualWire worked, but I don't wanna use this lib, I want to make my own serial communication, and connected the transmitter on the tx of the arduino that sends the signal and the receiver on the rx pin of the arduino that receives the signal.
Even if I'm receiving something but not the entire text, the arduino should tell me, because I tried a code like this:
if (Serial.available) { Serial.print(Serial.read())} Anything that are received by the arduino should appears on the serial monitor
This should output any value, in case, I tried to send the "o" char, but nothing appears on the serial monitor.
Logged

Lancashire, UK
Offline Offline
Edison Member
*
Karma: 9
Posts: 1991
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The virtualwire library is written at least in part to overcome the severe limitations of these primitive devices, overwise why bother if plain old serial worked ?
Logged


0
Offline Offline
Full Member
***
Karma: 0
Posts: 102
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, so, how to send numbers via virtualWire? I tried but always tell me about wrong conversion from const char to int, something like this
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 27363
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 do it like this. I am reading key presses from a keypad and sending them via virtualwire:

send side in loop:
Code:
  // go read the keypad
  char key = keypad.getKey();                 // reading the keypad
  if(key)                                     // same as if(key != NO_KEY)- did something change?
  {
    msg[0]=key;                               // load the array with the key character
    // msg[1]=NULL;                           // Rx side seems to work without this

    vw_send((uint8_t *)msg, strlen(msg));     // send the character out

//    Serial.println(key);                // for debugging only

    vw_wait_tx();                             // Wait until the whole message is gone

    delay (50);                               // need some delay or seem to miss key presses

receive side in loop
Code:
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;

if (vw_get_message(buf, &buflen)) // Non-blocking
{
  int i;

  // Message with a good checksum received, dump it.
  //Serial.println("Got: ");  // Show on PC for debugging

  //    for (i = 0; i < buflen; i++)
  //    {
  //Serial.print(byte(buf[i]));
  //    }
  //Serial.println(""); // spaces it out for the monitor

  //    
  switch ( buf[0] )
  {    // switch on the first byte in the array for now
// bunch of case:'0', case:'1' statements follow ...


I think you just need to load up the msg[] array the numbers you want.
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.

Pages: [1]   Go Up
Jump to: