need help with subroutine

Hi, I've never done any programming before, but trying to learn a little.

I have a Sharp IR sensor, and I'm trying to write a piece of code that takes a measurement, and if the distance is below 30, it turns on a LED. I did this successfully.

Then I tried to modify the code by using a subroutine to do the measure part. But my distance value inside the 'void loop' seems to have lost the measured value, and always goes to 0.

I'd appreciate any help.

Thanks,

Greg.

#define LED 13

int IRpin = 1;                                    // analog pin for reading the IR sensor

float distance;


void setup()
 {
   pinMode(LED, OUTPUT);                            //sets the digital pin as output
   Serial.begin(9600);                             // start the serial port
 }
 
 
 
void measure() 
{
  float volts = analogRead(IRpin)*0.0048828125;   // value from sensor * (5/1024)
  float distance = 65*pow(volts, -1.10);          // worked out from graph 65 luckylarry.co.uk
  Serial.println(distance);                       // print the distance
}



void loop()
{
  measure();
  Serial.println("Dist = ");
  Serial.println(distance);
  Serial.println("end");
  if (distance < 30.00) {
    digitalWrite(LED, HIGH);}
    else
    {
      digitalWrite(LED, LOW);}
      
  delay(5000);

  digitalWrite(LED, LOW);
  
  delay(5000);
      
}

When posting code, please put code tags around it (press the "#" symbol in the post editor.)

Inside of your function, you redefine the variable "distance" which is also a global variable. I don't think you meant to do that. I think you wanted to modify the global variable "distance" in which cause, you wouldn't redefine it with "float" inside of your function.

I would add a Serial.print (volts); and that will tell you what your analogRead is getting.
Add the print to this part:

void measure()
{
  float volts = analogRead(IRpin)*0.0048828125;   // value from sensor * (5/1024)
  Serial.println(volts);
  float distance = 65*pow(volts, -1.10);          // worked out from graph 65 luckylarry.co.uk
  Serial.println(distance);                       // print the distance
}

You are speeding a lot of time waiting on the Led to go high then low. You can not take readings or do anything while waiting on a delay. For that part I would suggest using the blink with out delay example.
Here is your led part that you might want to change.

  if (distance < 30.00) {
    digitalWrite(LED, HIGH);}
    else
    {
      digitalWrite(LED, LOW);}
     
  delay(5000);

  digitalWrite(LED, LOW);
 
  delay(5000);
     
}

Sorry about the way I posted the code. I didn't know how to do that.

The volts is taking readings ok. I can make the code work, if I don't use a subroutine.

The following code is ok:

#define LED 13

int IRpin = 1;                                    // analog pin for reading the IR sensor

float distance;


void setup()
 {
   pinMode(LED, OUTPUT);                            //sets the digital pin as output
   Serial.begin(9600);                             // start the serial port
 }
 



void loop()
{
  float volts = analogRead(IRpin)*0.0048828125;   // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
  Serial.println(volts);
  float distance = 65*pow(volts, -1.10);          // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk
  Serial.println(distance);                       // print the distance

  if (distance < 30.00) {
    digitalWrite(LED, HIGH);}
    else
    {
      digitalWrite(LED, LOW);}
      
  delay(5000);

  digitalWrite(LED, LOW);
  
  delay(5000);
      
}/code]

But when I try and use 'void measure', as in my original post, it stops working.

As James C4S said, get rid of the word "float".

Change:

  float distance = 65*pow(volts, -1.10);          // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk

to:

   distance = 65*pow(volts, -1.10);          // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk

You have made two variables called distance the way you did it.

Nick, Thanks very much. It's working now.

Greg.

Why is distance a global variable? You would be much better off learning how to pass values to, and return values from functions.

#define LED 13

int IRpin = 1;
void setup()
{
   pinMode(LED, OUTPUT);                            //sets the digital pin as output
   Serial.begin(9600);                             // start the serial port
}
 
float measure() 
{
  float volts = analogRead(IRpin)*0.0048828125;   // value from sensor * (5/1024)
  float distance = 65*pow(volts, -1.10);          // worked out from graph 65 luckylarry.co.uk
  return distance;
}

void loop()
{
  float distance = measure();
  Serial.println("Dist = ");
  Serial.println(distance);
  Serial.println("end");
  if (distance < 30.00)
  {
    digitalWrite(LED, HIGH);
  }
  else
  {
    digitalWrite(LED, LOW);
  }
      
  delay(5000);

  digitalWrite(LED, LOW);
  
  delay(5000);    
}

I agree with that in principle, but his ISR has to be able to get the most recent distance variable, it doesn't have time to calculate it on the spot. Hence the global variable.

I agree with that in principle, but his ISR has to be able to get the most recent distance variable, it doesn't have time to calculate it on the spot. Hence the global variable.

Huh? What ISR?

Oops. Got confused with an earlier question. I withdraw my comment. :slight_smile: