RF module: if a character is 1 do this if 2 do that

Hi all.

Please keep in mind that my coding experience until now is copying and pasting code, modifying it to my taste and then try to combine all i have.

So that is what i have done now. I have a rf module transmitting a message. In one case it will send the character 1 out, in the other case it wil send out the message 2.

The code i have now for the receiver is as soon it receives any character it will blink a led. What i want it to do is when the character 1 is received is light up the led. if character 2 is received blink the led.

First things first, stap by step i want to try to make the led light up when character 1 is received and not “any” character.

This is the code i have now, can you help me change it so if character 1 is recieved it will light up the led?

#include <RH_ASK.h>
#include <SPI.h> // Not actualy used but needed to compile
 
// tx/rx configuration
const int txSpeed = 512;
const int rxPin = 5;
const int txPin = 6;
const int pttPin = 7;
 
RH_ASK driver(txSpeed, rxPin, txPin, pttPin);
 
void setup()
{
  Serial.begin(9600); // Debugging only
  if (!driver.init())
    Serial.println("init failed");
    pinMode(LED_BUILTIN, OUTPUT);
    
}
 
void loop()
{
  uint8_t buf[RH_ASK_MAX_MESSAGE_LEN] = {0};
  uint8_t buflen = sizeof(buf);
 
  if (driver.recv(buf, &buflen)) // if message received, save it
  {
    Serial.println((char*)buf); // print received message
    digitalWrite(LED_BUILTIN, HIGH);
    }
}
    Serial.println((char*)buf); // print received message

What do you see printed ?

uint8_t buf[RH_ASK_MAX_MESSAGE_LEN] = {0};

Does buf have to be a byte (uint8_t) array?

UKHeliBob:

    Serial.println((char*)buf); // print received message

What do you see printed ?

I see 1 because is told the transmitter to send character 1

groundFungus:

uint8_t buf[RH_ASK_MAX_MESSAGE_LEN] = {0};

Does buf have to be a byte (uint8_t) array?

Euhm, it is not really needed to send characters. But that's the only code i could find so i thought i'll try to modify that. Send character 1 and 2 in different situations and then on the receiver side figure out what character was send and turn on the led i want

It can also just be command to to set a specific led pin high of low

Bringamosa:
I see 1 because is told the transmitter to send character 1

In your original post you said

What i want it to do is when the character 1 is received is light up the led. if character 2 is received blink the led.

So what do you see printed if you send a 2 ?
Could you write some code to test the received value and act differently based on what is received ?

Where are you stuck ?
Have you looked at Using millis() for timing. A beginners guide and the BlinkWithoutDelay example in the IDE ?

So i found some code and changed my part. Now it became:

#include <RH_ASK.h>
#include <SPI.h> // Not actualy used but needed to compile

#define LEDLEEG 13
#define LEDVOL 12
 
// tx/rx configuration
const int txSpeed = 512;
const int rxPin = 5;
const int txPin = 6;
const int pttPin = 7;
 
RH_ASK driver(txSpeed, rxPin, txPin, pttPin);
 
void setup()
{
  Serial.begin(9600);  // Debugging only
  if (!driver.init())
  Serial.println("init failed");
  pinMode(LEDLEEG, OUTPUT);
  pinMode(LEDVOL, OUTPUT);
}
 
void loop()
{
  uint8_t buf[RH_ASK_MAX_MESSAGE_LEN] = {0};
  uint8_t buflen = sizeof(buf);
 
  if (driver.recv(buf, &buflen)) // if message received, save it
{
  if(buf[0]=='1')
{
  digitalWrite(LEDLEEG, HIGH);
  digitalWrite(LEDVOL, LOW);
}  
  if(buf[0]=='2')
{
  digitalWrite(LEDLEEG, LOW);
  digitalWrite(LEDVOL, HIGH);
  }
  }
}

So something is not quite right, when 1 is send the led on pin 13 is lit, when 2 is send led on pin 12 is lit. However when nothing is send the last lit led will keep lit.

Maybe my transmitter code will clarify a bit:

// libraries voor trasnmitter
#include <RH_ASK.h>
#include <SPI.h> // Not actually used but needed to compile

// ultrasoon modulle
#define echoPin 12 // Echo Pin
#define trigPin 9 // Trigger Pin
#define LEDROOD 13 // Onboard LED
#define BLINK_COUNT 20 //hoe vaak de led knipperd als tank leeg is

int maximumRange = 65; ////afstand waarop de tank leeg is ////
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance

// transmitter en reciever module 
const int txSpeed = 512;
const int rxPin = 5;
const int txPin = 6;
const int pttPin = 7;
#define LEDTX 8
 
RH_ASK driver(txSpeed, rxPin, txPin, pttPin);





//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!AFSTAND AANPASSEN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


 
void setup(void) 
{
  Serial.begin(9600);
  if (!driver.init())
    Serial.println("rf driver init failed");
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(LEDROOD, OUTPUT);//Change to output my pins
  pinMode(LEDTX, OUTPUT);
  digitalWrite(LEDROOD,HIGH);//Turn on red LED  
}

void loop(void) 
{
 /* The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it. */ 
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 
 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;
 
 if (distance >= maximumRange || distance <= minimumRange)
{
 Serial.println("-1");
 digitalWrite(LEDROOD, HIGH); 
 Serial.println("leeg");
 const char *msg = "1"; //stuur message 1 naar RX
 digitalWrite(LEDTX, HIGH); //LEDTX gaat branden omdat de tank vol is
 driver.send((uint8_t *)msg, strlen(msg));
 driver.waitPacketSent();
 delay(200);
}
 
 else 
{
 /* Send the distance to the computer using Serial protocol, and
 turn LED OFF to indicate successful reading. */
 Serial.println(distance);
 digitalWrite(LEDROOD, LOW); 
 digitalWrite(LEDTX, LOW);
}
 
 if (distance <= 25)    /// afstand waarop de tank vol is!!!///
{
 Serial.println("vol");
 const char *msg = "2"; //stuur message 1 naar RX
 digitalWrite(LEDTX, HIGH); //LEDTX gaat branden omdat de tank vol is
 driver.send((uint8_t *)msg, strlen(msg));
 driver.waitPacketSent();
 delay(200);

for ( int i=0; i < BLINK_COUNT; i++ )
{
 digitalWrite(LEDROOD, HIGH);
 delay(200);                  
 digitalWrite(LEDROOD, LOW);    
 delay(200);
}
}
 
 


 
  delay(1000);
  
}

So i found some code

A much better approach would be to spend some time learning the programming language, so that you actually understand how to do what you want. That way you avoid all this frustration of trying things that don't work.

Start with the simple projects that come with the Arduino and work your way through them.

Allright so i guess the code is fixed i added a 3th character for both leds off. works too :slight_smile: