RF433MHz some basic examles and traffic ligths one

RF 433 MHz-OT-Forum-po

I bought RF433Mhz set of STX882 and SRX882 and I studied at first this type
of wireless communcation with a simply applications of them, with Arduinos or Espressif.
I studied many web instructions about this RF communication ,
the most important and working are from Github website

and I started with this Simple example for receiving, too.
And I found on the web next good or better applications with this RF433MHz communication.

So I started with Arduinos or with Arduino Uno R3 or Arduino Nano Clone,
the cheap and reliable modules.
Instead of I tried to use ESP8266 NodeMcu modules and with very similar schetches,
but in the end I used Arduinos with more reliable function and I let ESP8266 for another application for example for their excelent ESP-now function, what is very similar to RF one . Instead of I programmed one RF433 tester for NodeMcu V0.9 .

So I started with RF-scanner example, what is the best practice to understand
to some basic principles os RF433 MHz technics.

And then I planned to create simple muster for next applications according to this RF technics
and I used to for them similar name to IOT - as RFOT .

I created as example system for traffic lights (or called semafors in my and next countries)
and here I public for this Forum my realization.

I erase my codes here and I add improved ones again

I am sorry for my problems with code inserting again , but I hope today results in txt codes are for you more acceptable as in my previous one …

and … Heureka … I found, that I can copy my photos and paste them here by right button on my mouse .. :slight_smile:

Here are not some photos from a realisation of my examples … :frowning:

Maybe you have a question in there somewhere.

Please do sort all you are trying to show us so we can read or see it.

But you might want to post a brief description of exactly what kind of issue you have.

a7

I do not understend what you mean .. is here some problem with my Project RF433 .... ?

I do not see the matter .. I think I published all my work for Forum well instead of my small (I think) problems with code insertion again .

Here are name of code examples I have published for Forum :

I created as examle system for traffic lights (or called semafors in my and next countries)
and here I public for this Forum my realization.

RF Scanner code :
RF433-rc-switch-scaner--po-5.ino

RF Sender - tester code via serial monitor :
RF-433MHz-STX882-tester-en-po-1 for Arduinos
RF-433MHz-STX882-tester-en-po-2 for NodeMcu V0.9

RF Receiver for my semafor :
RF433-OT-semafor-receiver-po-6

RF sender with buttons for control this one :
RF-OT-433-STX882-semafor-sender-en-po-4

Is it now clear?

Pavel Ou

Since you posted in the “General Guidance” part of the forum, are you offering general guidance for others to read or are you asking for general guidance for this project of yours?

I see … my english is not excelent, I am sorry, but I know that every english word has more meanings .. so I must to use sometime my english dictionary, what I do sure .. and I am sorry again, but this forum is not simply in a meanings and as never has time to read all instruction, I must to follow my normal expectations what to do here or there .. so again back to meaning of the work Guidance so my dictionary translates it and in red rectangle is what a meaning I choosed for my post :

and here is back translation from my language to english one :

So is my choose for my post OK ? If not, I see no problem to change a category for one, maybe RF communication will be more suitable .

Yes, english is easy … :slight_smile:

There is a communication difficulty. You might want to translate my comments to your native language.

This category of the forum is usually for asking for guidance. But are you offering guidance? Perhaps a sort of tutorial on how to use 433 MHz tools?

Do all your sketches compile and work correctly?

As I see, I am offering guindance according to my previous post .. but if there is another more suitable forum for this RF matter , I do not see the problem to change one.

I tested all my RF sketches on Arduino Uno R3 or Arduino Nano Clone with set of RF modules STX882 and SRX882 (exceptionally I use NodeMCU V0.9 for tester one) and all these schetches work well .. but there can be a problem with pasting them or copy them from forum .. but when this problem will occur, I am here and I can it discuss or correct here .. or explain it .. if I could explain it ...

I read abou General Guidance yet and I thing I am in right forum category (my opinion) :

About the General Guidance category

Discussion about a project as a whole, both hardware and code

This category is for topics where both the hardware and code of a project will be discussed.

For discussions where only code is directly relevant, use the Programming category.

For discussions about hardware alone, use the category most relevant to the hardware:

Thanks. It might have been better to post in the Tutorial section. But wherever the thread is located you do need to first get all five sketches into the required form.They are hard to read, and would be difficult to work with.

Your text and code comments are sometimes hard to understand. Try writing them again in you native language and translating, to see if that improves understanding. And use a spell-checker!

Or, more radically, you could just summarise it something like this:
”Here are five examples of 433 MHz RF sketches covering scanning, transmitting and receiving. I’ve included brief explanations on their source and intended use. Feedback would be welcomed.”

Thank you for your advices .. what can I do is … I can read once more my comments in my sketches and then to get them here in right code fields .. but if there will be an interest to create from ones a tutorial, I let it from another forum member this next work with pleasure …

Let's see what other feedback you get. But for a start you could take the advice I offered about communication. Various tools are available for non-english posters to maximise readabilty in an english forum. Here is what I think you meant to say:

"Thank you for your advice. What I can do is read through the comments in my sketches again and then post them here using the proper code formatting. If anyone is interested in turning them into a tutorial, I would be happy to let another forum member take on that task."

Kept scrolling down....

RF scanner :

//Arduino-RF433-rc-switch-scaner
// version for Arduino Nano
/*
Based on library and examles from  https://github.com/sui77/rc-switch/
Reprogrammed by PavelOu
*/

#include <RCSwitch.h>

RCSwitch mySwitch = RCSwitch();

String sketchName = "Arduino-RF433-rc-switch-scaner";
const int ledPin = 4;
const int clearLedButton = 5;



static const char* bin2tristate(const char* bin);
static char * dec2binWzerofill(unsigned long Dec, unsigned int bitLength);

void output(unsigned long decimal,
             unsigned int lengthOfWord,
             unsigned int delayOfPulse,
             unsigned int *raw,
             unsigned int protocol) 
 {
  const char* binar = dec2binWzerofill(decimal, lengthOfWord);
  Serial.print(" Protocol: ");
  Serial.println(protocol);
  Serial.print("Decimal: ");
  Serial.print(decimal);
  Serial.print(" (");
  Serial.print( lengthOfWord );
  Serial.print("Bit) Binary: ");
  Serial.print( binar );
  Serial.println();
  Serial.print("Tri-State: ");
  Serial.print( bin2tristate(binar) );
  Serial.println(" PulseLength: ");
  Serial.print(delayOfPulse);
  Serial.print(" microseconds");
  Serial.println();
  Serial.print("Raw data: ");
  for (unsigned int i=0; i<= lengthOfWord*2; i++) 
   {
    Serial.print(raw[i]);
    Serial.print(",");
   }
  Serial.println();
  digitalWrite(ledPin,HIGH);
  Serial.println();
}

static const char* bin2tristate(const char* bin) 
 {
  static char returnValue[50];
  int pos = 0;
  int pos2 = 0;
  while (bin[pos]!='\0' && bin[pos+1]!='\0') 
   {
    if (bin[pos]=='0' && bin[pos+1]=='0') 
     {
      returnValue[pos2] = '0';
     }
    else 
     if (bin[pos]=='1' && bin[pos+1]=='1') 
      {
      returnValue[pos2] = '1';
      }
     else
      if (bin[pos]=='0' && bin[pos+1]=='1') 
       {
        returnValue[pos2] = 'F';
       }
      else 
       {
        return "not applicable";
       }
    pos = pos+2;
    pos2++;
  }
 returnValue[pos2] = '\0';
 return returnValue;
}

static char * dec2binWzerofill(unsigned long Dec,
                               unsigned int bitLength) 
 {
  static char bin[64]; 
  unsigned int i=0;
  while (Dec > 0) 
   {
    bin[32+i++] = ((Dec & 1) > 0) ? '1' : '0';
    Dec = Dec >> 1;
   }
  for (unsigned int j = 0; j< bitLength; j++) 
   {
    if (j >= bitLength - i) 
     {
      bin[j] = bin[ 31 + i - (j - (bitLength - i)) ];
     }
    else 
     {
      bin[j] = '0';
     }
   }
  bin[bitLength] = '\0';  
  return bin;
}

void setup() 
 {
  Serial.begin(9600);
  delay(1000);
  pinMode(clearLedButton,INPUT_PULLUP);
  pinMode(ledPin,OUTPUT);
  digitalWrite(ledPin,HIGH);
  delay(1000);
  digitalWrite(ledPin,LOW);
  Serial.println();
  Serial.println(sketchName);
  Serial.println("Waiting for RF 433 Mhz data ... ");
  mySwitch.enableReceive(0);  // Receiver on interrupt 0 => that is pin #2
 }

void loop() 
 {
  if (mySwitch.available()) 
   {
    output(mySwitch.getReceivedValue(), 
           mySwitch.getReceivedBitlength(),
	   mySwitch.getReceivedDelay(),
           mySwitch.getReceivedRawdata(),
	   mySwitch.getReceivedProtocol());
           mySwitch.resetAvailable();
   }
  else
   {
     int clearLed = digitalRead(clearLedButton);
     if (clearLed == 0 )
       {
        digitalWrite(ledPin,LOW);
       }
   }     
 }

RF tester with Arduino :

//RF-433MHz-STX882-tester-1
//Arduino Uno R3 or Nano Clone as sender
/*
Sketch for sending RF data readed from serial monitor
Data are coupled with special (or crypto) pre-defined code
Examples : special code is set on 56000 here
           command code switching a led on can be 101, off 100 etc.
Aketch is based on library and examles from  https://github.com/sui77/rc-switch/
Reprogrammed by PavelOu
*/

// RF 433 library
#include <RCSwitch.h>

String sketchName = "RF-433MHz-STX882-tester-1.ino";
const int ledPin = 13;
byte protocolNr = 2;
long int specialCode = 56000;
byte nrBits = 32;
int intervalDat = 1000; // ms


// creating objekt senderRF from library
RCSwitch senderRF = RCSwitch();

void setup() 
 {
  Serial.begin(9600);
  pinMode(ledPin,OUTPUT);
  digitalWrite(ledPin,HIGH);
  delay(1000);
  digitalWrite(ledPin,LOW);
  Serial.println();
  Serial.println(sketchName);
  Serial.println("RF-OT 433 Mhz sender is starting ...");
  // start of sending on pin D10
  senderRF.enableTransmit(10);
  
  // optional set of protocol on nr.2
  // senderRF.setProtocol(2);
  // optional set of protocol nr.5
  // senderRF.setProtocol(5);
  // optional 
  // senderRF.setRepeatTransmit(5);

  protocolNr = 5;
  Serial.print("Protocol: ");
  Serial.println(protocolNr);
  //testData = 22;
  //Serial.print("Tested data: ");
  //Serial.println(testData);
  Serial.print("special data set: ");
  Serial.println(specialCode);
  nrBits = 32;
  Serial.print("Used nr of bits: ");
  Serial.println(nrBits);
  intervalDat = 3000;
  Serial.print("Interval dat: "); //optional
  Serial.println(intervalDat);
  
  senderRF.setProtocol(protocolNr);  
}

void loop() 
 {
  if (Serial.available() > 0) 
   {
    // time from start of sketch
    unsigned long casLong = millis() ;
    // reading and command for sending - example 101 for set on a virtual Led
    String textToSend = Serial.readString();
    long int textInt = textToSend.toInt();
    Serial.print("Sending Int Data: ");
    Serial.println(textInt);
    Serial.print("Sending special Code + Data: ");
    Serial.println(specialCode+textInt);
    float cas = (float)casLong / 1000;
    // vypsani aktualniho casu s delkou
    //Serial.print(" time in sec: "); // cas (cz) is time (en)
    //Serial.println(cas);
    Serial.print(" time (long) in millisec: ");
    Serial.println(casLong);
    Serial.print(" time (long)/ 1000 sec: ");
    Serial.println((float)casLong/1000);
    senderRF.send(specialCode+textInt,nrBits);
    // pause before new reading of next data
    digitalWrite(ledPin,HIGH);
    delay(1000);
    digitalWrite(ledPin,LOW);
    delay(intervalDat);
   }  
 }

RF Tester with NodeMcu V0.9 :

//RF-433MHz-STX882-tester-2
//NodeMcu V0.9 (V1)  as sender
/*
Sketch for sending  data readed from serial monitor
Data are coupled with special (or crypto) pre-defined code
Examples : special code is set on 56000 here
           command code switching a led on can be 101, off 100 etc.
          
Sketch is based on library and examles from  https://github.com/sui77/rc-switch/
Reprogrammed by PavelOu
*/

// RF 433 library
#include <RCSwitch.h>

String sketchName = "RF-433MHz-STX882-tester-2.ino";
const int ledPin = D3;
const int txPin = D5;
byte protocolNr = 2;
long int specialCode = 56000;
byte nrBits = 32;
int intervalDat = 1000; // ms

long int textInt = 0;

byte dataMode = 0;
char markRx = 0;

// creating objekt senderRFe from library
RCSwitch senderRF = RCSwitch();

void setup() 
 {
  Serial.begin(9600);
  while (!Serial)
   {
    delay(1000);
   }
  pinMode(ledPin,OUTPUT);
  digitalWrite(ledPin,HIGH);
  delay(1000);
  digitalWrite(ledPin,LOW);
  Serial.println();
  Serial.println(sketchName);
  Serial.println("RF-OT 433 Mhz sender is starting ...");
  // start of sending on txPin
  senderRF.enableTransmit(txPin);
  
  // optional set of protocol on nr.2
  // senderRF.setProtocol(2);
  // optional set of protocol nr.5
  // senderRF.setProtocol(5);
  // optional
  // senderRF.setRepeatTransmit(5);
  protocolNr = 5;
  Serial.print("Protocol: ");
  Serial.println(protocolNr);
  //testData = 22;
  //Serial.print("Tested data: ");
  //Serial.println(testData);
  Serial.print("special data set: ");
  Serial.println(specialCode);
  nrBits = 32;
  Serial.print("Used nr of bits: ");
  Serial.println(nrBits);
  intervalDat = 3000;
  Serial.print("Interval dat: "); //optional
  Serial.println(intervalDat);
  dataMode = false;
  markRx = 0;
  senderRF.setProtocol(protocolNr);  
}

void loop() 
 {
  if (dataMode == 0)
    {
     Serial.println();
     Serial.println(sketchName);
     Serial.println("New setings:");         
     Serial.println("Px = Protocol, Bxx = Bits, Cint = specCode, Dint = data");
     dataMode = 1;
     markRx = 0;
     digitalWrite(ledPin,LOW);  // turn the LED off
    }
 if (Serial.available())
    {
     markRx = Serial.read();
     Serial.print("choosed: ");
     Serial.println(markRx);
     Serial.flush();
     if ( markRx == 66 )    // B
          {
            String Bstring = Serial.readString();
            Bstring.trim();
            nrBits = Bstring.toInt();
            Serial.print("Bits: ");
            Serial.println(nrBits);
            dataMode = 0;          
          }
     if ( markRx == 67 )    // C
          {
            String Cstring = Serial.readString();
            Cstring.trim();
            specialCode = Cstring.toInt();
            Serial.print("Special code: ");
            Serial.println(specialCode);
            dataMode = 0;
          }
     if ( markRx == 68 )   // D
          {
            // reading of command for sending - example 101 for set on a Led
            String textToSend = Serial.readString();
            textInt = textToSend.toInt();
            Serial.print("Sending data: ");
            Serial.println(textInt);
            digitalWrite(ledPin, HIGH);  // turn the LED on
            dataMode = 2;
          }
      if (markRx == 80)  // P
        {
            String Pstring = Serial.readString();
            Pstring.trim();
            protocolNr = Pstring.toInt();
            Serial.print("Protocol: ");
            Serial.println(protocolNr);
            senderRF.setProtocol(protocolNr);
            dataMode = 0;  
           }
     markRx = 0;  
     Serial.flush();
     if (dataMode == 2 )
      {
        // time from start of loop
        unsigned long timeLong = millis() ;
        Serial.print("Sending special Code + Data: ");
        Serial.println(specialCode+textInt);
        float casTime = (float)timeLong / 1000;
        Serial.print(" time (long) in millisec: ");
        Serial.println(timeLong);
        Serial.print(" time (long)/ 1000 sec: ");
        Serial.println((float)timeLong/1000);
        senderRF.send(specialCode+textInt,nrBits);
        // pause before new reading of next data
        delay(1000);
        digitalWrite(ledPin,LOW);
        delay(intervalDat);
        dataMode = 0;
      }  
   }
}

RF 433 MHz application - traffic lights (semafor) sender:

//RF-OT-433-STX882-semafor-sender
//Arduino Uno R3 or Nano Clone as sender
/*
RF433-OT = RF Of Things example
semafor (cz) = traffic lights (en)
Sketch for sending of data from buttons for green, yellow and red Leds on semafor 
Time for pre-defined changing of traffic light is set from serial monitor
Example : specialized code is set for my semafor to 56000 (and in receiver too)
          code for switching of green led on = 101, yellow led = 201, red led = 301
          specialized code is added with code for switching on leds

Sketch is based on library and examles from  https://github.com/sui77/rc-switch/
Reprogrammed by PavelOu
*/

// RF 433 library
#include <RCSwitch.h>

// creating objekt senderRFe from library
RCSwitch senderRF = RCSwitch();

String sketchName = "RF-OT-433-STX882-semafor-sender.ino";
const int ledPin = 4;
const int buttonPinR = 5;
const int buttonPinY = 6;
const int buttonPinG = 7;

int buttonR = 1;
int buttonY = 1;
int buttonG = 1;

byte protocolNr = 2;
long int specialCode = 56000;
byte nrBits = 32;
int intervalDat = 1000; // ms
int rfData = 100;

long int semaforInterval = 0;
int semaforTime = 0;
bool semaforTimeRedOn = false;
bool semaforTimeGreenOn = false;

byte dataMode = 0;

void setup() 
 {
  Serial.begin(9600);
  while (!Serial) 
   {
     delay(1000);     
     // wait for serial port to connect. Needed for native USB port only
   }
  pinMode(ledPin,OUTPUT);
  pinMode(buttonPinR,INPUT_PULLUP);
  pinMode(buttonPinY,INPUT_PULLUP);
  pinMode(buttonPinG,INPUT_PULLUP);
  digitalWrite(ledPin,HIGH);
  delay(1000);
  digitalWrite(ledPin,LOW);
  Serial.println();
  Serial.println(sketchName);
  Serial.println("RF-OT 433 Mhz sender for semafor is starting ...");
  // start of sending on pin D10
  // optional set of protocol on nr.2
  // senderRF.setProtocol(2);
  // optional set of protocol nr.5
  // senderRF.setProtocol(5);
  protocolNr = 2;
  Serial.print("Protocol: ");
  Serial.println(protocolNr);
  //testData = 22;
  //Serial.print("Tested data: ");
  //Serial.println(testData);
  Serial.print("special data set: ");
  Serial.println(specialCode);
  nrBits = 32;
  Serial.print("Used nr of bits: ");
  Serial.println(nrBits);
  intervalDat = 1000;
  Serial.print("Interval dat: "); //optional
  Serial.println(intervalDat);
  semaforTime = 0;
  semaforTimeRedOn = true; // semafor on interval start with Red light always
  semaforTimeGreenOn = false;
  buttonR = 1;
  buttonY = 1;
  buttonG = 1;


  dataMode = 0;
  Serial.println("Press one of buttons for Green, Yellow or Red Light on your semafor ...");
  senderRF.setProtocol(protocolNr);  
  senderRF.enableTransmit(10);  
}

void loop() 
 {
  if (dataMode == 0) 
   {
    //semafor is waiting for commands from serial link or from buttons
    //or semafor will blinking with yellow led
   }
  if (Serial.available())
   {
      String serialData = Serial.readString();
      semaforInterval = serialData.toInt();
      if (semaforInterval > 29)   // interval has to be longer then half minute
       {
        Serial.print("Interval of semafor: ");
        Serial.print(semaforInterval);
        Serial.println(" sec");
        dataMode = 5;
        semaforTimeRedOn = true;
       }
      else
       {
        Serial.print("Minimal time interval for semafor (your have set ");
        Serial.print(semaforInterval);
        Serial.println(") is 30 sec !!");
        semaforInterval  = 0;
        dataMode = 0;
       }
   }
  // test of buttons
  buttonR = digitalRead(buttonPinR);
  if (buttonR == 0)
     {
       Serial.println();
       Serial.println("button for semafor red light was pressed ...");
       rfData = 301;
       dataMode = 1;     
       delay(1000);     
     }
  buttonY = digitalRead(buttonPinY);
  if (buttonY == 0)
     {
      Serial.println();
      Serial.println("button for semafor yellow light was pressed ...");
      rfData = 201;
      dataMode = 1;
      delay(1000);     
     }
  buttonG = digitalRead(buttonPinG);
  if (buttonG == 0)
     {
      Serial.println();
      Serial.println("button for semafor green light was pressed ...");
      rfData = 101;     
      dataMode = 1;     
      delay(1000);     
     }
 // accepting choosed command from buttons - example 101 was set for green Led  
 if (dataMode == 1)
   {  
    digitalWrite(ledPin,HIGH);
    delay(1000);
    dataMode = 2;
   }
  if (dataMode == 2)
   {  
    while ((buttonR == 0)||(buttonY == 0)||(buttonG == 0))
     {
      buttonG = digitalRead(buttonPinG);
      buttonY = digitalRead(buttonPinY);
      buttonR = digitalRead(buttonPinR);
      delay(500);
     }
    digitalWrite(ledPin,LOW);
    delay(1000);
    Serial.print("RF command data: ");
    Serial.println(rfData);
    Serial.print("Special Code + RF Data: ");
    Serial.println(specialCode+rfData);
    unsigned long casLong = millis() ;
    float cas = (float)casLong / 1000; //cas (cz) = time (en)
    // actual cas (time) in milisec and in sec
    Serial.print(" time (long) in millisec: ");
    Serial.println(casLong);
    Serial.print(" time (long)/ 1000 in sec: ");
    Serial.println((float)casLong/1000);
    senderRF.send(specialCode+rfData,nrBits);
    dataMode = 3;
   }
  if (dataMode == 3)
   {  
    // pause before new reading of next data
    digitalWrite(ledPin,HIGH);
    delay(1000);
    delay(intervalDat);
    digitalWrite(ledPin,LOW);
    delay(1000);
    //delay(intervalDat);
    dataMode = 0;
   }  
  if (dataMode == 5)  // set on RF data for traffic lights
   {
    if (semaforTimeRedOn)
     { 
      buttonR = 1;
      buttonY = 0;
      buttonG = 0;
      rfData = 301;
     } 
    if (semaforTimeGreenOn)
     { 
      buttonR = 0;
      buttonY = 0;
      buttonG = 1;
      rfData = 101;
     } 
    semaforTime = 0; 
    Serial.println(specialCode+rfData);
    unsigned long casLong = millis() ;
    float cas = (float)casLong / 1000;  // cas = time
    // vypsani aktualniho casu s delkou
    Serial.print(" time in sec: ");
    Serial.println(cas);
    /*
      Serial.print(" cas (long) in millisec: ");
      Serial.println(casLong);
      Serial.print(" cas (long)/ 1000 sec: ");
      Serial.println((float)casLong/1000);
      */
      senderRF.send(specialCode+rfData,nrBits);
      dataMode = 6;
   }
  if (dataMode == 6) // counting time for change in traffic lights
   {
     semaforTime += 1;
     if (semaforTime > semaforInterval)
      {
       dataMode = 7;
      }
     else
      {
        delay(1000); 
      }
   }
  if (dataMode == 7) // there is time on for change in traffic lights
   {
    if (semaforTimeRedOn)
     {
       semaforTimeRedOn = false;
       semaforTimeGreenOn = true;
     }
    else
     {
       semaforTimeRedOn = true;
       semaforTimeGreenOn = false;
     }
    dataMode = 5; 
   }
}

RF 433 MHz application traffic lights (semafor) - receiver

//RF433-OT-semafor-receiver
//version for Arduino UNO R3 or Nano Clone
/*
  RF433-OT = RF Of Things example
  semafor (cz) = traffic lights (en)
  based on library and examles from  https://github.com/sui77/rc-switch/
  transformed from ReceiveDemo_Simple
  reprogrammed by PavelOu
*/

#include <RCSwitch.h>

RCSwitch mySwitch = RCSwitch();

String sketchName = "RF433-OT-semafor-receiver";
const int ledPinG = 5; // green Led
const int ledPinY = 6; // yellow Led
const int ledPinR = 7; // red Led

bool ledYellow = false;
bool ledYellowBlink = false;
bool semaforDataOn = false;

unsigned long rfValue = "";
byte rfLength = 0;
byte rfProtocol = 0;

byte protocolNr = 2;
long int specialCode = 56000;
long int rfCode = 0;
long int rfResult = 0;
byte nrBits = 32;

unsigned long decimal;
unsigned int lengthOfWord;
unsigned int delayOfPulse;
unsigned int *raw;
unsigned int protocol; 


byte dataMode = 0;
bool ledRedOn = false;
bool ledGreenOn = false;

void setup() 
 {
  Serial.begin(9600);
  while (!Serial) 
   {
     delay(1000);     
     // wait for serial port to connect. Needed for native USB port only
   }
  delay(1000);
  pinMode(ledPinG,OUTPUT);
  pinMode(ledPinY,OUTPUT);
  pinMode(ledPinR,OUTPUT);
  digitalWrite(ledPinG,HIGH);
  delay(1000);
  digitalWrite(ledPinG,LOW);
  digitalWrite(ledPinY,HIGH);
  delay(1000);
  digitalWrite(ledPinY,LOW);
  digitalWrite(ledPinR,HIGH);
  delay(1000);
  digitalWrite(ledPinR,LOW);
  Serial.println();
  Serial.println(sketchName);
  Serial.println("RF 433 Mhz receiver as semafor is waiting for data ...");
  bool ledYellow = true;
  bool ledYellowBlink = true;

  dataMode = 0;
  semaforDataOn = false;
  ledRedOn = false;
  ledGreenOn = false;

  // Receiver on interrupt 0 => that is pin #2 for Arduinos
  mySwitch.enableReceive(0);  
 }

void loop() 
 {
  if (dataMode == 0) 
   {
    if (!semaforDataOn)
     {
       if (mySwitch.available())
        {
           decimal = mySwitch.getReceivedValue();
           lengthOfWord = mySwitch.getReceivedBitlength();
           delayOfPulse = mySwitch.getReceivedDelay();
           protocol = mySwitch.getReceivedProtocol();
           mySwitch.resetAvailable();
           Serial.print("Decimal: ");
           Serial.println(decimal);
           Serial.print(" Protocol: ");
           Serial.println(protocol);
           Serial.print("Bits: ");
           Serial.print(lengthOfWord);
           Serial.print(" PulseLength: ");
           Serial.print(delayOfPulse);
           Serial.print(" microseconds");
           Serial.println();
           delay(1000);
           dataMode = 1;
           Serial.println("RF data On !");
        }      
      else
       {
         if (ledYellowBlink == true)
           {
            if (ledYellow == false)
             {
              digitalWrite(ledPinY,HIGH);
              ledYellow = true;
             }
            else
             {
              digitalWrite(ledPinY,LOW);
              ledYellow = false;          
             }
            delay(1000);         
          }     
         
      }
    }   
  } 
 if (dataMode == 1) 
  {
    Serial.println("RF433 control of received data: ");
    dataMode = 2;
  }
 if (dataMode == 2)
  {
    if (protocol != protocolNr)
     {
        Serial.println("Another protocol !"); 
        Serial.println("This is not command for my semafor !");
        Serial.println();
        delay(500);
        dataMode = 0;
     }
    else
     {
      if (lengthOfWord != nrBits)
       {
        Serial.println("Another bits length !"); 
        Serial.println("This is not command for my semafor !");
        Serial.println();
        delay(500);
        dataMode = 0; 
       }
     }
  }
 if (dataMode == 2) 
  {
    rfResult = decimal - specialCode;
    Serial.print("Result command: ");
    Serial.println(rfResult);
    switch(rfResult)
     {
      case 101:
       if (!ledGreenOn)
        {
          ledGreenOn = true;
          ledRedOn = false;
          Serial.println("Green LED On !");        
          digitalWrite(ledPinY,HIGH);
          delay(2000); 
          digitalWrite(ledPinR,LOW);
          digitalWrite(ledPinY,LOW);
          digitalWrite(ledPinG,HIGH);
          delay(1000);
          ledYellowBlink = false;
          semaforDataOn = true;
        }  
       break;       

      case 201:
       Serial.println("Yellow LED blink !");
       digitalWrite(ledPinY,HIGH);
       delay(2000); 
       digitalWrite(ledPinG,LOW);
       digitalWrite(ledPinY,LOW);
       digitalWrite(ledPinR,LOW);
       delay(1000);
       ledYellowBlink = true;
       ledYellow = false;
       semaforDataOn = true;
       break;       

      case 301:
       if (!ledRedOn)
        {
         ledGreenOn = false;
         ledRedOn = true;          
         Serial.println("Red LED On !");
         digitalWrite(ledPinY,HIGH);
         delay(2000); 
         digitalWrite(ledPinG,LOW);
         digitalWrite(ledPinY,LOW);
         digitalWrite(ledPinR,HIGH);
         delay(1000);
         ledYellowBlink = false;
         semaforDataOn = true;
        } 
       break; 

      default:
       Serial.println("Unknown semafor command !");
       Serial.println("This is not command for my semafor ! !");
       delay(500);
     }
    Serial.println();
    if (semaforDataOn == true)
     {
      semaforDataOn = false;
     }
    dataMode = 0;
 }
}