copy value from one void loop to another

Hi guys!

Im noob on programming and i need your help! Im want to copy the "distance" value from void loop to void updateValues. How can i do it ? Thanks!

float distance = fix.location.DistanceKm( SC );
......
float distance = ?????????;

void loop()
{
  while (gps.available( gpsPort )) {
    gps_fix fix = gps.read(); // save the latest

    // When we have a location, calculate how far away we are from the SC location.
    if (fix.valid.location) {
      float distance = fix.location.DistanceKm( SC );

      Serial.print( F("Distance: ") );
      Serial.print( distance );
      Serial.println( F(" meters") );
    } else
      // Waiting...
      Serial.print( '.' );
  }

  updateValues(); // Very important! iBUS protocol is very sensitive to timings.
                    // DO NOT USE ANY delay()! Look at updateValues() method.
                    // It's an example of how to use intervals without delays.

    telemetry.run(); //It must be here. Period.
} // loop


void updateValues()
{

  updateValues(); // Very important! iBUS protocol is very sensitive to timings.
                    // DO NOT USE ANY delay()! Look at updateValues() method.
                    // It's an example of how to use intervals without delays.

    telemetry.run(); //It must be here. Period.
  
    uint32_t currMillis = millis();
    
    float distance = ?????????;

Hi Vasi,

wecome to the forum.

"void" is a datatype not a command. It is the datatype "nodata"

loop() and UpdateValues() are functions.
You haven't posted the whole sketch. I estimate you are using Blynk.

Again: you are welcomed in the forum and are free to ask as many questions as you like.
From your question I can conclude that your picture how programming works is partially wrong.
No problem at all just to improve your programming skills it will be a big advantage to correct your picture.

For doing so I recommend walking through this tutorial

Arduino Programming Course

It is easy to understand and is a good micture between explaining important concepts and codeexample to get you going. So give it a try and report back your opinion about this tutorial

best regards Stefan

Normally you could just make 'distance' a global variable defined at the top of the code outside any function. Then it would be available in loop() and in updateValues() automatically, no copying needed.

But your code is a mess. The first thing you do in updateValues() is call updateValues() i.e. you call the function you're in the middle of. That makes no sense.

Steve

Thank you guys for you reply!
Im working on telemetry code for FlySky ibus and i trying to take a gps distance on screen. So, i have two sketch, one for telemetry and one for gps distance and im trying to compare those two sketchs.
Seperatly those sketchs works fine and the only think that i want is to take the “distance” from the gps sketch to telemetry sketch. Thanks!!

telemetry ino

#include <iBUSTelemetry.h>

#define UPDATE_INTERVAL 500

iBUSTelemetry telemetry(11); // I use only PCINT0 interrupt vector, so you can use D8 to D13 pins.

uint32_t prevMillis = 0; // Necessary to updateValues() method. Look below.
float i = 0;

void setup()
{
    telemetry.begin(); // Let's start having fun!

    telemetry.addSensor(0x01); // You can use sensors definitions from iBUSSensors.h instead of numbers.
                               // Ex.: telemetry.addSensor(IBUS_MEAS_TYPE_TEM);

    telemetry.addSensor(IBUS_MEAS_TYPE_GPS_STATUS);
    telemetry.addSensor(IBUS_MEAS_TYPE_SPE);
    telemetry.addSensor(IBUS_MEAS_TYPE_GPS_LAT);
    telemetry.addSensor(IBUS_MEAS_TYPE_ARMED);
    telemetry.addSensor(IBUS_MEAS_TYPE_FLIGHT_MODE);
    telemetry.addSensor(IBUS_MEAS_TYPE_ALT);
}

void loop()
{
    updateValues(); // Very important! iBUS protocol is very sensitive to timings.
                    // DO NOT USE ANY delay()! Look at updateValues() method.
                    // It's an example of how to use intervals without delays.

    telemetry.run(); //It must be here. Period.
}

void updateValues()
{
    uint32_t currMillis = millis();

    if (currMillis - prevMillis >= UPDATE_INTERVAL) { // Code in the middle of these brackets will be performed every 500ms.
        prevMillis = currMillis;

        telemetry.setSensorValueFP(1, i); // Now, you have two ways to set sensors values. Using floating point variables
                                          // or directly in 32bit integer, but then you have to format variable by yourself.
                                          // Ex.: telemetry.setSensorValueFP(1, 24.45); is equivalent to telemetry.setSensorValue(1, 2445);
                                          // The values differ for different sensors.

        telemetry.setSensorValue(2, telemetry.gpsStateValues(3, 8)); // As GPS status consists of two values,
                                                                     // use gpsStateValues(firstVal, secondVal) to set it properly.

        telemetry.setSensorValue(3, 123 * 10);

        telemetry.setSensorValue(4, 179583647); // You can set LAT / LON using FP metohod, but due to fact floats have only 6 digits precision,
                                                // your values on tx may be corrupted in some cases. It's better to use setSensorValue().

        telemetry.setSensorValue(5, UNARMED); // ARMED / UNARMED or 1 / 0 could be used.

        telemetry.setSensorValue(6, LOITER); // Available flight modes:	   STAB   0
																		// ACRO   1
																		// AHOLD  2
																		// AUTO   3
																		// GUIDED 4
																		// LOITER 5
																		// RTL    6
																		// CIRCLE 7
																		// PHOLD  8
																		// LAND   9

        telemetry.setSensorValueFP(7, 54.87);

        i += 0.1;
        if (i > 50)
            i = 0;

        // These were the most difficult sensors to use. I hope that this library will be useful for you and will make your work easier. :)
    }
} /* updateValues */

GPS distance

 #include <NMEAGPS.h>

#include <AltSoftSerial.h>
AltSoftSerial gpsPort; // 





NMEAGPS gps;

// The base location, negative for West and South
NeoGPS::Location_t SC( 33.687786, 1.032857 );

void setup()
{
  Serial.begin(9600);
  Serial.println( F("Distance example started.") );
  gpsPort.begin(9600);


} // setup

void loop()
{
  while (gps.available( gpsPort )) {
    gps_fix fix = gps.read(); // save the latest

    // When we have a location, calculate how far away we are from the SC location.
    if (fix.valid.location) {
      float distance = fix.location.DistanceKm( SC );

      Serial.print( F("Distance: ") );
      Serial.print( distance );
      Serial.println( F(" meters") );
    } else
      // Waiting...
      Serial.print( '.' );
  }

  updateValues(); // Very important! iBUS protocol is very sensitive to timings.
                    // DO NOT USE ANY delay()! Look at updateValues() method.
                    // It's an example of how to use intervals without delays.

    telemetry.run(); //It must be here. Period.
} // loop

You need to create a new sketch, containing only the relevant portions of the other two.

That requires you to understand how both of those sketches work. That may presently be beyond your capabilities, because you skipped all the beginner programming language tutorials.

If this is for your commercial "bait boat" products (which look great!) and you don't want to take the time to learn, consider posting on the Gigs and Collaborations forum section. You may be asked to pay for the help.

Thank you!
I haven't any problem to pay. Could you suggest me any person or website etc for this?

I haven't any problem to pay. Could you suggest me any person or website etc for this?

I have moved your thread to an appropriate place on the forum