problem in serial communication

Hi i am kind of new to arduino

And i have 2 questions, i hope you can help

1: when i code a transmitter (tx/rx), i cant get it to work without the usb cable( external supply from reciver). If i remove the usb cable, and connect it again, it wont work until it has been programmed again. Arduino RF link using 433MHz Transmitter / Receiver modules

2: i have triet to make my own transmitter and receiver, it looks as it transmits ok, but i cant get any output from the recever. The essential is, i have to be able to send 3 different signals and have 3 different outputs from the recever.

The Transmitter are just as basic as it can be. I change the byte written between (71,72,73) when i program.

// Used Arduino UNO
// This is a simple test app for cheap RF transmitter and receiver hardware.
// 433MHz RF Transmitter: http://electronics-diy.com/product_details.php?pid=250
// 433MHz RF Receiver:    http://electronics-diy.com/product_details.php?pid=251

// Arduino digital pins
#define BUTTON_PIN  2

void setup(){
    pinMode(BUTTON_PIN, INPUT);
    Serial.begin(600);}  // Hardware supports up to 2400, but 1200 gives longer range

void loop(){
    Serial.write (73);
delay(5000);} // Debounce button

The Receiver are supposed to, identify which out of the 3 bytes are send, but as is,
it do not seem to receive much at all

// Used Arduino UNO
// This is a simple test app for cheap RF transmitter and receiver hardware.
// 433MHz RF Transmitter: http://electronics-diy.com/product_details.php?pid=250
// 433MHz RF Receiver:    http://electronics-diy.com/product_details.php?pid=251

int LEDPIN = 2;
int ser;
boolean newData = false;
int light_led = 0;

void setup(){
    pinMode(LEDPIN, OUTPUT);
    digitalWrite(LEDPIN, LOW);
    Serial.begin(600);}  // Hardware supports up to 2400, but 1200 gives longer range

void loop(){
   ser_read ();
if (ser == 71){ // Check to see if we got the 71 test number
light_led = 1;}
if (ser == 72){ // Check to see if we got the 71 test number
light_led = 2;}
if (ser == 73){ // Check to see if we got the 71 test number
light_led = 3;}
newData = false;
// Afhænig af værdien af light_led vælges en af de nedenstående programmer
switch (light_led){//(light_led) {
  case 1:
      digitalWrite(LEDPIN, HIGH);
      delay(1000);
      digitalWrite(LEDPIN, LOW);
      ser=0;
  break;    
  case 2:
      analogWrite (LEDPIN,100);
      delay(1000);
      digitalWrite(LEDPIN, LOW);
      ser=0;
  break; 
  case 3:
      analogWrite (LEDPIN,20);
      delay(1000);
      digitalWrite(LEDPIN, LOW);
      ser=0;
  break;     
}}
void ser_read (){
if (Serial.available() > 0) {
    ser=Serial.read();
    newData = true;}}

To transmit and receive, you need a output with ones and zeroes more or less balanced. You can not use the serial port reliably for that.

I used a code that simply toggled (every second or so) an Arduino output pin connected to the data pin of the transmitter. On the same Arduino I read it back from the receiver and it worked. But at the moment that I started using a button to send a zero or a one, it did not work reliably; the receiver picks up garbage.

To get reliable communication, use the RadioHead library. It comes with examples for the receiver and transmitter. Base your code on that.

RadioHead. This is the successor of the VirtualWire library

With this library, transmitter datapin is connected to pin 12 (Uno); receiver datapin is connected to pin 11 (Uno). Those are the defaults and you can change it. Default rate is 2000 bits per second (can also be changed).

Below is sample code that I currently use for (range) testing. Both transmitter and receiver are basically the examples.

Transmitter

// ask_transmitter.pde
// -*- mode: C++ -*-
// Simple example of how to use RadioHead to transmit messages
// with a simple ASK transmitter in a very simple way.
// Implements a simplex (one-way) transmitter with an TX-C1 module
#include <RH_ASK.h>
//#include <SPI.h> // Not actually used but needed to compile
RH_ASK driver;
// RH_ASK driver(2000, 2, 4, 5); // ESP8266: do not use pin 11


void setup()
{
  Serial.begin(9600);   // Debugging only
  if (!driver.init())
    Serial.println("init failed");

  pinMode(13, OUTPUT);
}
void loop()
{
  static unsigned long prevMillis = 0;
  unsigned long currMillis = millis();

  char msg[24];

  static int count;

  if (currMillis - prevMillis > 500)
  {
    prevMillis = currMillis;
    sprintf(msg, "%d", count++);

    driver.send((uint8_t *)msg, strlen(msg) + 1);
    driver.waitPacketSent();

    digitalWrite(13, !digitalRead(13));

    Serial.print("TX duration (ms): "); Serial.println(millis() - currMillis);
  }
}

The transmitter sends an incrementing number every 500 milliseconds. It displays how long it takes on the serial monitor.

Receiver

// ask_receiver.pde
// -*- mode: C++ -*-
// Simple example of how to use RadioHead to receive messages
// with a simple ASK transmitter in a very simple way.
// Implements a simplex (one-way) receiver with an Rx-B1 module
#include <RH_ASK.h>
//#include <SPI.h> // Not actualy used but needed to compile
RH_ASK driver;
// RH_ASK driver(2000, 2, 4, 5); // ESP8266: do not use pin 11
void setup()
{
  Serial.begin(9600); // Debugging only
  if (!driver.init())
    Serial.println("init failed");
}

void loop()
{
  static int lastCount = 0;
  static int dropCount = 0;

  uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
  uint8_t buflen = sizeof(buf);
  if (driver.recv(buf, &buflen)) // Non-blocking
  {
    int count = atoi((char*)buf);
    if (count != lastCount + 1)
    {
      dropCount++;
      Serial.print("Missed some beats. ");
      Serial.print("Last count: "); Serial.print(lastCount);
      Serial.print(". Rcvd count: "); Serial.print(count);
      Serial.print(". Drop count: "); Serial.println(dropCount);
    }
    lastCount = count;

    
    Serial.println((char*)buf);

    // Message with a good checksum received, dump it.
    //driver.printBuffer("Got:", buf, buflen);
  }
}

The receiver basically displays the received number. It also keeps track how many numbers were missed due to poor communication.

PS
The toggling using one Uno was done a while ago. But I went through the exercise of transmitting data using two Unos only yesterday; I use the same type of modules at 433Mhz.

Also, only use three wires for the transmitter; I had one wire floating (connect to nothing) parallel to the data wire to the transmitter and it did not work (probably picked up rubbish affecting the transmitted data.

In my setup (no antennas connected, roughly 40 cm apart) I get a drop count of around 500 on 15000 packets and the shortest interval between dropped packets is 3. Note that the first packet will always be indicated as dropped because the counter in the receiver start at 0 and the transmitter might already be at another number.

you have to have antennae on, about 17 cm. then it will work with the transmitters/recever

Antennas (antennae) will help, I'm aware of that. Just haven't gotten there yet. Establishing communication was my main goal and that now works. Next step is improving reach; I need 60 to 100 meters and possibly through walls (at least one).