Analog value received via 433MHZ module and what to do with it? "[solved]"

Hi,
Can anyone help, I have a project where I have successfully transmitted and received a remote Load Cell value via 433MHz modules (FS1000A and XD-RF)

However the value arriving at the receiver is packaged in a Buffer,(buf[0]) I can read the value (buf[0]); on the serial monitor and it is perfect, but I need to work with the number within the buffer, can anyone explain how to extract the number so that I can continue to work with it in the weighing project.
Thank you, any help would be appreciated.

Answer was SIMPLE :

create: int rawVal; //This is the converted serial value from 433MHz receiver module.

in your code:

rawVal = (buf[0]);
  //rawVal = rawVal/2;               // you can divide, multiply and so on.
  //rawVal = rawVal*3;
  Serial.println(rawVal)

;

It will help us to help you if you show the transmitter and receiver codes; so we understand what you exactly do.

Just in case , please read https://forum.arduino.cc/index.php?topic=148850.0 and pay special attention to point #7 about posting code using code tags.

It sounds like you need to convert the character string representation of the number into an actual number.

As previously said, show us your code.

Hi This is my Tx and Rx and my Project, I have inserted the standard ‘Radio Head’ library but as I mentioned without the converted transmission I can’t move forward.

Receiver Code:

#include <SD.h>
#include <SPI.h>
#include <Wire.h>  //new RadioHead stuff
#include <RH_ASK.h>  //new RadioHead stuff
#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 7, 5, 6, 3, 2);

File myFile;
RH_ASK driver; //new RadioHead stuff
int data;      //new RadioHead stuff
int pow_pin = 8;
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int numReadings = 100;
const int Beep = A1;
const int EnterKey = A5;         // Dat enter push button.
int sensorValue = 0;        // value read from the pot
int TotalPinState = 1;         // variable for reading the pushbutton status
const int led = 9;                // Data transfered LED
const int TareButton = A4;     /// the number of the Tare/Zero pushbutton pin
int buttonState = 0;         /// variable for reading the pushbutton status
int TareValue;
int FinalWt;
int Pounds;
int Ounces;
int ZeroVal;
int Weight;
int Maths1;
int Maths2;
int Maths3;
long id = 1;
int readings[numReadings];      // the readings from the analog input
int index = 0;                  // the index of the current reading
int total = 0;                  // the running total

void setup()
{
  pinMode(led, OUTPUT); //led on pin 9  ...needs to be set otherwise its very dull.
  pinMode(Beep, OUTPUT);
  pinMode(TareButton, INPUT);
  pinMode(EnterKey, INPUT);
  driver.init(); //new RadioHead stuff
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.setCursor(2, 0);
  lcd.print("Remote");
  lcd.setCursor(4, 1);
  lcd.print("Scales");
  delay (3000);

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // initialize all the readings to 0:
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0; //Totalising setup of Indexing
}

void loop()
{
   uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
  uint8_t buflen = sizeof(buf);
  Serial.println(buf[0]);

  if (driver.recv(buf, &buflen)) // Non-blocking
  {
   }
  //tarebutton code
  //If tarebutton is pressed.
  if ( digitalRead(TareButton) == LOW) {
    digitalWrite(led, HIGH);    //shows switchstate is HIGH *****
    int TareValue = analogRead(A0); //analogRead(A0);
    digitalWrite(Beep, HIGH);
    ZeroVal = TareValue;
    Serial.print("Tare Value = ");
    Serial.println (TareValue);
    buttonState = LOW; // *** need to reset this once you've done whats needed ****
    delay (1);
    lcd.clear();
    delay (1);
    lcd.setCursor (3, 1);
    lcd.print ("NET ZERO'D");
  }

  //if tarebutton released
  if ( digitalRead(TareButton) == HIGH) {
    digitalWrite(led, LOW); // shows the process is finished
    digitalWrite(Beep, LOW);
  }
  {
    buttonState == digitalRead(EnterKey);
    if (buttonState == HIGH)
      digitalWrite(EnterKey, HIGH); //shows switchstate is HIGH *****
    // digitalWrite(led, HIGH);
    delay (10);
  }
  digitalWrite(EnterKey, LOW);   //shows switchstate is HIGH *****
  digitalWrite(led, LOW);
  {
  }
  {
    delay(10);

    lcd.display();

    if (digitalRead(EnterKey) == LOW)
    {
      digitalWrite(led, HIGH);    //shows switchstate is HIGH *****
      digitalWrite(Beep, HIGH);   //beeps when Tare or Enter buttons pressed.

      FinalWt = (analogRead(0) - ZeroVal);

      Serial.print("Raw AnalogValue = ");
      Serial.println (analogRead(0));

      Serial.print ("RawVal-Tare = FinalWt = ");
      Serial.println(FinalWt);

      //Weight Calculation for SD and LCD
      Pounds = (FinalWt * 6 / 28.34952 / 16);
      Maths1 = (FinalWt * 6);
      Serial.println(Maths1);
      Maths2 = (Pounds * 28.34952 * 16);
      Serial.println(Maths2);

      Maths3 = (Maths1 - Maths2);
      Serial.println(Maths3);
      Ounces = (Maths3 / 28);
      Serial.println(Ounces);

      lcd.setCursor(5, 1); //Position of first digit
      lcd.print(Pounds);  //Position of Pb Wt Value
      lcd.setCursor(11, 1); //Position of second letter
      lcd.print(Ounces);  //Position of Oz Wt Value

      String dataString = String(id); //This is the number of fish caught count
      lcd.setCursor (0, 1); //Botton Line
      lcd.print (id);     // Count Value

      //Serial Monitor for checking code.
      Serial.print("weight = " );
      Serial.print(Pounds);
      Serial.print("Lbs  ");
      Serial.print(Ounces);
      Serial.print("oz");

      //SD Card writing too.
      myFile = SD.open("Results.txt", FILE_WRITE);
      if (myFile)
        myFile.println(FinalWt);
      myFile.close();

      myFile = SD.open("Weights.txt", FILE_WRITE);
      if (myFile)
        String dataString = String(id);
      myFile.print(id);
      myFile.print("  ");
      myFile.print(Pounds);
      myFile.print("Lb  ");
      myFile.print(Ounces);
      myFile.print("oz");
      myFile.println("");
      myFile.close();
      delay (100);
      digitalWrite(led, LOW); // shows the process is finished and turns off LED
      digitalWrite(Beep, LOW); //Turns pff beep
      delay (5000);
      lcd.clear();

      //TotalPinState = digitalRead(TotalPin);
      if (TotalPinState == LOW)
        //subtract the last reading;
        total = total - readings[index];
      // read from the sensor:
      readings[index] = (FinalWt);
      sensorValue = (FinalWt);
      // add the reading to the total:
      total = total + readings[index];
      // advance to the next position in the array:
      index = index + 1;
      // if we're at the end of the array...
      if (index >= numReadings)
        // ...wrap around to the beginning:
        index = 0;


      // calculate the average:
      //average = total / numReadings;
      // send it to the computer as ASCII digit
      Serial.print("Weight="  );
      Serial.println(FinalWt);
      //Serial.println(inputPin);
      //Serial.println(readings[index]);
      Serial.print("Total=");
      Serial.println(total);
      //Weight Calculation for SD and LCD
      Pounds = (total * 6 / 28.34952 / 16);
      Maths1 = (total * 6);
      Serial.println(Maths1);
      Maths2 = (Pounds * 28.34952 * 16);
      Serial.println(Maths2);

      Maths3 = (Maths1 - Maths2);
      Serial.println(Maths3);
      Ounces = (Maths3 / 28);
      Serial.println(Ounces);

      //Serial Monitor for checking code.
      Serial.print("Total weight = " );
      Serial.print(Pounds);
      Serial.print("Lbs  ");
      Serial.print(Ounces);
      Serial.print("oz");
      Serial.println();

      lcd.setCursor(0, 0); //Total Page generated at end of weigh sequence
      lcd.print("Total");

      lcd.setCursor(7, 0);
      lcd.print("Lbs");
      lcd.setCursor(12, 0);
      lcd.print("Ozs");

      lcd.setCursor(0, 1);
      lcd.print("No.");
      lcd.setCursor(3, 1);
      lcd.print (id);     // Count Value

      lcd.setCursor(7, 1);
      lcd.print(Pounds);
      lcd.setCursor(12, 1);
      lcd.print(Ounces);
      id++; //This INDEX command must stay here otherwise it will add +1 to the total caught value!!!

      delay(500); // delay in between reads for stability

    }
    {
      delay(500);
      lcd.display();

      // set up the LCD's number of columns and rows:
      lcd.begin(16, 2);
      lcd.setCursor(0, 0);
      lcd.print("No.");
      //lcd.setCursor(6,0);
      //lcd.print("Weight");

      lcd.setCursor(5, 0);
      lcd.print("Lb");
      lcd.setCursor(11, 0);
      lcd.print("oz");

      {
      }
    }
  }
}

Does that code compile without errors?

Also you have a lot of these:

 {
  }

That do absolutely nothing except clutter your code. Some of them actually kill your code, like this one:

 if (driver.recv(buf, &buflen)) // Non-blocking
  {
   }

That says, "if message received, do nothing. if message not received, do nothing".

You never posted your TX code. Did you, "successfully transmitted and received a remote Load Cell value via 433MHz modules" with the posted code, or some other code?

You need to get the RadioHead example code working for both TX and RX, and understand how it works, before adding radio code to a more complicated project.

In addition to the serious error pointed out in the last post, the three lines below simply define a buffer somewhere in memory, and then print out the first byte, which will be random nonsense. It does not "receive" anything from the radio.

   uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
  uint8_t buflen = sizeof(buf);
  Serial.println(buf[0]);

Hi, You can probably tell I’m no programmer but I have made this my life’s work! anyway the following is the Tx code straight forward code.
Thank you for looking at this.

Tx Code:

#include <RH_ASK.h>
#include <SPI.h>
RH_ASK driver;

const int PotInputPin = A0;
uint8_t *data;
uint8_t InputValue;

void setup()
{
   Serial.begin(9600);
  driver.init();
}

void loop()
{
 
  InputValue = analogRead(PotInputPin)/4;

  data = &InputValue;

  driver.send(data, sizeof(InputValue));
  driver.waitPacketSent();
  delay(10);
  Serial.println(InputValue);
}[code]

When you have taken care of the problems already pointed out and have questions about the revised code, post the details, along with the revised code.

Hi, i have looked at the RadioHead example for ASK tx and rx and they don'y give me any pointers regarding manipulation of the received buffer.

I have taken out the {} errors as you suggested.

Regards,

geordieboy1:
Hi, i have looked at the RadioHead example for ASK tx and rx and they don'y give me any pointers regarding manipulation of the received buffer.

I have taken out the {} errors as you suggested.

Regards,

but you didn't post the code

I have taken out the {} errors as you suggested.

How about the other errors?

Since the TX code sends only one byte, there is no need to "manipulate" the received byte.

thankyou, but how do i u.
e it as the input value and carry out the 'Tare' etc.

Regards,

I have sent the transmitter code code? but here it is anyway,

#include <RH_ASK.h>
#include <SPI.h>
RH_ASK driver;

const int PotInputPin = A0;
uint8_t *data;
uint8_t InputValue;

void setup()
{
   Serial.begin(9600);
  driver.init();
}

void loop()
{
 
  InputValue = analogRead(PotInputPin)/4;

  data = &InputValue;

  driver.send(data, sizeof(InputValue));
  driver.waitPacketSent();
  delay(10);
  Serial.println(InputValue);
}

Post the revised receiver code.

how do i u.
e it as the input value and carry out the 'Tare' etc.

What is the rest of that code supposed to do? Where did you get it? Did it work before you modified it?

You have given us no information, so please spend a few minutes studying, then follow the instructions in the "How to use the forum" post at the head of every forum topic.

Hello , I have read through the ‘how to use the forum’ as you suggested and I now realise I have not given you the information you need, well this is the background of the project.

I have a ‘weigh belt’ which I measure batches on, but the belt swings to different positions as required, the load call cable fails regularly despite trying every flexable cable known to man! so I decided to try and invent a wireless system.

You asked if the current code compiles and worked before the modification? yes it does and is currently in use on the weigh belt.

You can read in the code I sent that I press a ‘Tare’ button to Tare the empty belt then run product until the ‘Enter’ button is press, this stores the Displays the weight and Batch count and stores it on the SD card. and all this is currently in use.

I have the Nano and Uno boards and a load cell on my desk right now and I can view the data Tx Rx on two serial screens, the data transfer is almost instant when weight is applied.

But in order to use the data, I need as you say to convert it to an integer to allow me to use it in my code, and this is the reason I contacted you.

I hope this helps you and I appologise for any confusion, I have included the codes I’m using to test the TxRX at the moment.
Regards, Gordon

Transmitter Code:

#include <RH_ASK.h>
#include <SPI.h>
RH_ASK driver;

const int PotInputPin = A0;
uint8_t *data;
uint8_t InputValue;

void setup()
{
   Serial.begin(9600);
  driver.init();
}

void loop()
{
 
  InputValue = analogRead(PotInputPin)/4;

  data = &InputValue;

  driver.send(data, sizeof(InputValue));
  driver.waitPacketSent();
  delay(10);
  Serial.println(InputValue);
}

Receiver code:

#include <Wire.h>
#include <RH_ASK.h>
#include <SPI.h>

RH_ASK driver;
int data;

void setup()
{

  Serial.begin(9600);
  driver.init();
}

void loop()
{
  uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
  uint8_t buflen = sizeof(buf);
  Serial.println(buf[0]);

  if (driver.recv(buf, &buflen)) // Non-blocking
  {
  }
}

Thanks for all the information, but you haven't fixed the error I mentioned in reply #4. This is from your "revised" code:

  if (driver.recv(buf, &buflen)) // Non-blocking
  {
  }

This is going to be very slow and drawn out... you may be thinking, why do I have to go through this just to get an answer to my question.

Well the answer is, atoi().

But please don't complain if it doesn't work, because the rest of your program doesn't work! Please consult the RF library examples and documentation to understand how the receive works.

Also please consult some documentation and tutorials about C++ syntax. If you can't see a mistake even when someone points it out, you need some reinforcement there. It's a semantic error, but it's based on a poor understanding of the syntax.

geordieboy1:
This is just to let you know I have been successful in integrating the Rx value and my code is fully functioning!

You should post the code, just for anybody that happens to read the thread later and is looking for the same kind of solution.