Problems With Ultrasonic Sensor and Wireless Code

Hello forums,
I am trying to put this code for an ultrasonic sensor:

int vcc = 2; //attach pin 2 to vcc
int trig = 3; // attach pin 3 to Trig
int echo = 4; //attach pin 4 to Echo
int gnd = 5; //attach pin 5 to GND

void setup() {

pinMode (vcc,OUTPUT);
pinMode (gnd,OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}

void loop()
{
digitalWrite(vcc, HIGH);
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;


pinMode(trig, OUTPUT);
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(5);
digitalWrite(trig, LOW);


pinMode(echo,INPUT);
duration = pulseIn(echo, HIGH);

// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();

delay(100);
}

long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{

return microseconds / 29 / 2;
}

into code for wireless communication. This code checks out fine, but when I try to put part of it into the transmitter code, like so

//Source code from https://www.youtube.com/watch?v=e8RhXtst7ME
#include <VirtualWire.h>
int vcc = 2;
int trig = 3;
int echo = 4;
int gnd = 5;
void setup() {
  // put your setup code here, to run once:
vw_setup(2000);
vw_set_tx_pin(7);
pinMode(vcc, OUTPUT);
pinMode(gnd, OUTPUT);
}

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

    char c  ;
    {digitalWrite(vcc, HIGH);
long duration, inches, cm;
pinMode(trig, OUTPUT);
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(5);
digitalWrite(trig, LOW);

// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echo,INPUT);
duration = pulseIn(echo, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
    } ;
    
        vw_send((uint8_t *)c, 1);      
}

it gives the error message “Proto_WiTrinket.ino: In function ‘void loop()’:
Proto_WiTrinket.ino:38:39: error: ‘microsecondsToInches’ was not declared in this scope
Proto_WiTrinket.ino:39:40: error: ‘microsecondsToCentimeters’ was not declared in this scope”
I have tried putting the microsecondsTo… in the setup or above that but it still does not work.

In addition, for the reciever code:

//Source code from http://www.instructables.com/id/Easy-ultrasonic-4-pin-sensor-monitoring-hc-sr04/
//more from video https://www.youtube.com/watch?v=e8RhXtst7ME
#include <VirtualWire.h>
int vcc = 2;
int trig = 3;
int echo = 4;
int gnd = 5;
void setup()
{
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
pinMode (vcc,OUTPUT);
pinMode (gnd,OUTPUT);
// initialize serial communication:
Serial.begin(9600);
vw_setup(2000);
vw_set_rx_pin(7);
vw_rx_start();
}
void loop()
{
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
  uint8_t buf[buflen];
  if(vw_get_message(buf, &buflen))
  {
    for(int i = 0;i < buflen;i++)
    {
      if(buf[i] == '1')
{
  Serial.print(inches);
  Serial.print("in,");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  delay(100);
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}   
    }
    }
  }

it gives the error “Proto_WiUno.ino:42:1: error: a function-definition is not allowed here before ‘{’ token
Proto_WiUno.ino:46:1: error: a function-definition is not allowed here before ‘{’ token”

I have tried erasing and replacing the braces, and searching for a similar problem on the web but I can’t find one.

Thank you in advance, Vince

It looks to me like you need to try matching opening braces with closing braces

Well, there are six open braces, and six closed braces, but the problem seems to be emanating from "long microsecondsToInches" on lines 39 and 43 with the error message "a function-definition is not allowed here before the { token". I don't know if that is what you mean by matching the brackets. Thank you, Vince

I meant match the braces, not brackets, sorry

In the common dialects of C and C++ you can't embed one function inside another, unlike, say, occam

So, to get around this problem, do I have to use different language in the code?

No, you just need to not embed functions inside other functions.

So I should create an array and make a pointer to it? Thanks, Vince

?

It was in an old forum post that I read. But what would be the way you would solve this? Thanks for everything, Vince

I'm posting from a phone, so it's difficult to edit, but it looks like you've got functions inside another function. Sort out the braces, and all should become good.

I changed the code since I realized that I had to make it correspond with the transmitter code. But, even though the if function is gone, the error message still comes up. I have played with the braces and they look fine, so I don’t know if it is the braces around the return function (the problem areas) or somewhere else. I know you’re on a phone, and its fine that you can’t edit right now. I’m in no hurry.
Thank you, Vince
Here’s the new code

//Source code from http://www.instructables.com/id/Easy-ultrasonic-4-pin-sensor-monitoring-hc-sr04/
//more from video https://www.youtube.com/watch?v=e8RhXtst7ME
#include <VirtualWire.h>
int vcc = 2;
int trig = 3;
int echo = 4;
int gnd = 5;
void setup()
{
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
pinMode (vcc,OUTPUT);
pinMode (gnd,OUTPUT);
// initialize serial communication:
Serial.begin(9600);
vw_setup(2000);
vw_set_rx_pin(7);
vw_rx_start();
}
void loop()
{
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
  uint8_t buf[buflen];
  if(vw_get_message(buf, &buflen))
  {
    for(int i = 0;i < buflen;i++)
    
{
  long duration, inches, cm;
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  Serial.print(inches);
  Serial.print("in,");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  delay(100);
}

long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 /2;
}

    }
  }

and the error messages:

Proto_WiUno.ino: In function ‘void loop()’:
Proto_WiUno.ino:30:41: error: ‘microsecondsToInches’ was not declared in this scope
Proto_WiUno.ino:31:42: error: ‘microsecondsToCentimeters’ was not declared in this scope
Proto_WiUno.ino:41:1: error: a function-definition is not allowed here before ‘{’ token
Proto_WiUno.ino:46:1: error: a function-definition is not allowed here before ‘{’ token
Error compiling.

This

long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 /2;
}

    }

shows you've got one function declared inside another function. You cannot do this in C or C++.

I have tried taking out the brackets to take them outside, but it gives the same error message except the brace is substituted for "return". How should I proceed? Thanks, Vince

I have tried taking out the brackets to take them outside

I can't see your code.

Sorry. Here’s the Code

//Source code from http://www.instructables.com/id/Easy-ultrasonic-4-pin-sensor-monitoring-hc-sr04/
//more from video https://www.youtube.com/watch?v=e8RhXtst7ME
#include <VirtualWire.h>
int vcc = 2;
int trig = 3;
int echo = 4;
int gnd = 5;
void setup()
{
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
pinMode (vcc,OUTPUT);
pinMode (gnd,OUTPUT);
// initialize serial communication:
Serial.begin(9600);
vw_setup(2000);
vw_set_rx_pin(7);
vw_rx_start();
}
void loop()
{
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
  uint8_t buf[buflen];
  if(vw_get_message(buf, &buflen))
  {
    for(int i = 0;i < buflen;i++)
    
{
  long duration, inches, cm;
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  Serial.print(inches);
  Serial.print("in,");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  delay(100);
}

long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 /2;
}

    }
  }

And error messages
Proto_WiUno.ino: In function ‘void loop()’:
Proto_WiUno.ino:30:41: error: ‘microsecondsToInches’ was not declared in this scope
Proto_WiUno.ino:31:42: error: ‘microsecondsToCentimeters’ was not declared in this scope
Proto_WiUno.ino:41:1: error: a function-definition is not allowed here before ‘{’ token
Proto_WiUno.ino:46:1: error: a function-definition is not allowed here before ‘{’ token
Error compiling.

long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 /2;
}

    }

This shows that you've got one defined function inside another function. I think I've already pointed this out.

I have tried taking the braces out, so how should I solve this. P.S. I have to be away from my computer now, sorry.

I have to sleep. Sorry.