1

I am working on an assignment that converts a reading of force into pounds and then finds the maximum and average of all the values received

This is my code:

void setup()
{

 // put your setup code here, to run once:

 Serial.begin(9600);

  // declare the outputs and inputs
  pinMode( 0 , INPUT); 

  //figure out what pin the reading is going to be input to 

  fsrReading = analogRead( 0 ) ; 
  //the # is the same pin used to get input

  //or use analogRead ( # ), it depends if the pin input is on the analog side of the arduino

  // should this be under the while loop or here?

  //list functions here
  long ForceInPounds(long);
  long FindMax(long);
  long FindAvg(long);

  //declaring variables of functions
  FM = FindMax(new long);
  FA = FindAvg(new long);

        Serial.println("The max value is:");
        Serial.println(FM); //calls FindMax function

        Serial.println("The average value is:");
        Serial.println(FA); //calls FindAvg function

  }

void loop()
{
  // put your main code here, to run repeatedly 

 int counter1= 0; 

  while (counter1< 100) 
  //runs test 100 times

  //should I just make this a function? instead of embedding it in the void loop

 {
      Serial.print("Analog reading = ");

      Serial.println(fsrReading);

      //declaring variable of function used in this segment
      FiP = ForceInPounds(new long);

      Serial.print("Force in Newtons:");
      Serial.println(FiP); //calls ForceinPounds function

      counter1 ++;
  }

}

//FUNCTIONS

long ForceInPounds(const long)
// converts recorded voltage to resistance to conductance to force to pounds
{
 fsrReading = fsrVoltage;

 if (fsrVoltage== 0) 
 {
   Serial.print("No pressure");
 }
 else
 {
   fsrResistance = 5000.0 - fsrVoltage; 
   //convert voltage to resistance
   fsrResistance *= 10000.0;
   fsrResistance /= fsrVoltage;
   fsrConductance = 1000000.0 ; 

   //convert resistance to conductance
   fsrConductance /= fsrResistance;
   if (fsrConductance <=1000.0) 
   //convert resistance to conductance
   {
     fsrForce = fsrConductance/80.0;
   }
   else 
   {
     fsrForce = fsrConductance - 1000.0;
     fsrForce /= 30.0;
   }
   fsrPounds = fsrForce/4.45; 

   //convert force to pounds
 }

 return fsrPounds;
}

long FindMax(long)
{
  int counter2 = 0;

  while (counter2 < 100);
  {
    if ( fsrPounds > maximum)
    {
          maximum = fsrPounds;
          counter2++;
    }
    else 
    {
          maximum = maximum;
          counter2++;
    }

  }
  return maximum;

}

long FindAvg (long)
{
  long start = 0; 
  //variable to begin avg function
  long avg;
  int counter3 = 0;

  while (counter3 < 100);
  {
    start = start + fsrPounds ; 
    counter3 ++;
  }

  avg = start/100;
  return avg;
}

What am I doing wrong? This is my first time working with Arduino. Thanks

3
  • There are several problems with the code, and I can't reproduce the errror you gave with the code you gave. Is this the actual/real code you are getting this error with? Are you using the Arduino IDE? Aug 28 '17 at 23:34
  • Please post your entire code, or at least a Small Self-Contained Compilable Example.
    – Majenko
    Aug 29 '17 at 12:38
  • I just added the rest of the code. Thank you. And yes, I am using the Arduino IDE. Aug 29 '17 at 15:52
1

This is most likely not what you want to be doing

//declaring variables of functions
FM = FindMax(new long);
FA = FindAvg(new long);

And it is entirely incompatible with these definitions

//list functions here
long ForceInPounds(long);
long FindMax(long);
long FindAvg(long);

With the result that you pass a pointer to a long (ie, its address in memory), to a function which is instead expecting the value of a long.

However, it's going to be hard to propose a solution until you edit the body of the functions into your question.

There's probably no reason to use new here at all - let the functions allocate their own temporary variables on the stack, since they seem to return a value rather than a pointer. Most likely even if this worked, the new long you create would be abandoned - ie, a memory leak.

1
  • Thank you Chris, I just added the rest of the code for review. Thank you. Aug 29 '17 at 15:52
1

There are several fundamental problems with the code. It appears your understanding of function declaration, definition, and usage are not accurate.

This part:

  //list functions here
  long ForceInPounds(long);
  long FindMax(long);
  long FindAvg(long);

Is unneccesary.

setup() is not the place to declare function variables:

  //declaring variables of functions
  FM = FindMax(new long);
  FA = FindAvg(new long);

If your functions use variables internally, the place to declare them is within the function definition.

In your FUNCTIONS section, you start off defining this function:

long ForceInPounds(const long)

The first long declares that the function will return a long, and the (const long) would normally declare what variable types your function accepts as parameters, as well as the name(s) of said parameters. You have only provided a type long, and a modifier const, but no name. It doesn't make sense to pass in a const variable anyway, as the const indicates the content of the variable will never change from the initial value. In your code, not only is the variable not named, it is not given an initial value.

Within that ForceInPounds() function, you start with:

  fsrReading = fsrVoltage;

which overwrites your initial fsrReading = analogRead(0); with an undefined value, as you have neither declared a type for fsrVoltage, nor given it an initial value.

Also, you call analogRead(0) once in setup, so it only captures the value once. The fsrReading variable is not continually updated with the analog reading. You have to keep calling analogRead(0) and storing it in a variable each time through your loop.

There are other problems with the code, but these are a few of the fundamental problems. There is no simple/quick fix to your code.

If you don't come from a C or C++ background, you may want to read a quick overview of them, at a minimum. Start with some of the Arduino example sketches and get a good understanding of how functions are defined and how they work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.