Code

Hello everyone, I’m trying to use gesture control for my PC with 2 ultrasonic sensors. I’m not done with the code but I got an error.

Code:

const int trigger1 = 2; // 1st sensor
const int echo1 = 3;
const int trigger2 = 4; //2nd sensor
const int echo2 = 5;

void setup() {
Serial.begin(9600);

pinMode(trigger1, OUTPUT);
pinMode(echo1, INPUT);
pinMode(trigger1, OUTPUT);
pinMode(echo1, INPUT);
}

void calculate_distance(int trigger,int echo) {
digitalWrite(trigger, LOW);
delayMicroseconds(5);
digitalWrite(trigger, HIGH);
delayMicroseconds(10);
digitalWrite(trigger, LOW);

double time_taken = pulseIn(echo, HIGH);
double distance = 0.034 * time_taken / 2;
if (distance > 50) {
distance = 50;
}
calculate_distance(trigger1, echo1);
double distanceLeft = distance;
calculate_distance(trigger2, echo2);
double distanceRight = distance;
}

void calculate_newDistance(int trigger,int echo) {
digitalWrite(trigger, LOW);
delayMicroseconds(5);
digitalWrite(trigger, HIGH);
delayMicroseconds(10);
digitalWrite(trigger, LOW);

double time_taken = pulseIn(echo, HIGH);
double newDistance = 0.034 * time_taken / 2;

if (newDistance > 50) {
newDistance = 50;
}
calculate_newDistance(trigger1, echo1);
double newDistanceLeft = newDistance;
calculate_newDistance(trigger2, echo2);
double newDistanceRight = newDistance;
}

void loop() {

if (distanceLeft <= 25 && distanceLeft >= 20) {
delay(100);
calculate_distance(trigger1, echo1);
if (distanceLeft <= 25 && distanceLeft >= 20) {
Serial.println(“Left Locked”);
while (distanceLeft <= 40) {
calculate_distance(trigger1, echo1);
if (distanceLeft < 20) {
calculate_newDistance(trigger1, echo1);
for(; newDistanceLeft = distanceLeft - 0.1; calculate_newDistance(trigger1, echo1)) {
Serial.println(“Mouse Down”);
distanceLeft = newDistanceLeft;
}
delay(300);
}
else if (distanceLeft > 25) {
calculate_newDistance(trigger1, echo1);
for(; newDistanceLeft = distanceLeft + 0.1; calculate_newDistance(trigger1, echo1)) {
Serial.println(“Mouse Up”);
distanceLeft = newDistanceLeft;
}
delay(300);
}
else {
Serial.println(“Hold Mouse”);
}
}
}
}
if (distanceLeft >= 25 || distanceLeft <= 20) {
delay(100);
calculate_distance(trigger1, echo1);
if (distanceLeft >= 25 || distanceLeft <= 20) {
Serial.println(“Left Locked”);
calculate_distance(trigger1, echo1);
while (distanceLeft < 50) {
Serial.println(“Hovering”);
calculate_distance(trigger1, echo1);
if (distanceLeft > 50) {
Serial.println(“Left Click”);
delay(300);
}
}
}
}
}

Error:

C:\Users\Tele\Documents\Arduino\Gesture_Controlled_PC\Gesture_Controlled_PC.ino: In function ‘void loop()’:

Gesture_Controlled_PC:56:7: error: ‘distanceLeft’ was not declared in this scope

if (distanceLeft <= 25 && distanceLeft >= 20) {
^

Gesture_Controlled_PC:65:17: error: ‘newDistanceLeft’ was not declared in this scope

for(; newDistanceLeft = distanceLeft - 0.1; calculate_newDistance(trigger1, echo1)) {
^

Gesture_Controlled_PC:73:17: error: ‘newDistanceLeft’ was not declared in this scope

for(; newDistanceLeft = distanceLeft + 0.1; calculate_newDistance(trigger1, echo1)) {
^

Gesture_Controlled_PC:85:7: error: ‘distanceLeft’ was not declared in this scope

if (distanceLeft >= 25 || distanceLeft <= 20) {
^

exit status 1
‘distanceLeft’ was not declared in this scope

The code is just for one sensor so far.
Please what do you think I should do?

Gesture_Controlled_PC.ino (2.75 KB)

You declare distanceLeft in setup() hence it can only be accessed within that function

        for (; newDistanceLeft = distanceLeft - 0.1; calculate_newDistance(trigger1, echo1))I don't know whether this mess was intentional or accidental. Check how to write a for loop

Ooh, but distance in calculate_distance(...), not setup()

And concerning the for loop, I made some changes, but which part exactly

but distance in calculate_distance(…), not setup()

Sorry, but I don’t understand what you mean

concerning the for loop, I made some changes, but which part exactly

Do you mean that you have made changes since I posted my first reply. If so then please post your new code. Please explain exactly what the for loop is supposed to do

OK thanks..

First of all

Now I removed:
calculate_distance(trigger1, echo1);
double distanceLeft = distance;
calculate_distance(trigger2, echo2);
double distanceRight = distance;

calculate_newDistance(trigger1, echo1);
double newDistanceLeft = newDistance;
calculate_newDistance(trigger2, echo2);
double newDistanceRight = newDistance;

and put it outside all the loops, and this is what I get:

Gesture_Controlled_PC:45:19: error: expected constructor, destructor, or type conversion before '(' token

calculate_distance(trigger1, echo1);
^

Gesture_Controlled_PC:46:24: error: 'distance' was not declared in this scope

double distanceLeft = distance;
^

distance(_InputIterator __first, _InputIterator __last)
^

Gesture_Controlled_PC:47:19: error: expected constructor, destructor, or type conversion before '(' token

calculate_distance(trigger2, echo2);
^

Gesture_Controlled_PC:48:24: error: 'distance' was not declared in this scope

double distanceRight = distance;
^
......

exit status 1
expected constructor, destructor, or type conversion before '(' token

Read about scope.....

and as you post the new code, Please correct your post above and add code tags around your code:

[code]

[color=blue]// your code is here[/color]

[/code]

.

It should look like this:

// your code is here

(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

That is quite a long program for someone who can’t program.
Start of simply, load in the blink LED example, build up your knowlage about code and variables.

Your response to the guidance you received shows you do not know what you are doing in the slightest. Use the help menu in the IDE to make sure you get the syntax of the instructions correct.

And…concerning the for loop, using an ultrasonic sensor, I want to scroll down with my mouse from a particular range when my hand moves towards the sensor and to scroll up as my hand moves away from the sensor.

Code:

// your code is here
const int trigger1 = 2;
const int echo1 = 3;
const int trigger2 = 4; //2nd Sesnor
const int echo2 = 5;

void setup() {
  Serial.begin(9600);

  pinMode(trigger1, OUTPUT);
  pinMode(echo1, INPUT);
  pinMode(trigger1, OUTPUT);
  pinMode(echo1, INPUT);
}

void calculate_distance(int trigger, int echo) {
  digitalWrite(trigger, LOW);
  delayMicroseconds(5);
  digitalWrite(trigger, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger, LOW);

  double time_taken = pulseIn(echo, HIGH);
  double distance = 0.034 * time_taken / 2;
  if (distance > 50) {
    distance = 50;
  }
}

void calculate_newDistance(int trigger, int echo) {
  digitalWrite(trigger, LOW);
  delayMicroseconds(5);
  digitalWrite(trigger, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger, LOW);

  double time_taken = pulseIn(echo, HIGH);
  double newDistance = 0.034 * time_taken / 2;

  if (newDistance > 50) {
    newDistance = 50;
  }
}

calculate_distance(trigger1, echo1);
double  distanceLeft = distance;
calculate_distance(trigger2, echo2);
double distanceRight = distance;

calculate_newDistance(trigger1, echo1);
double newDistanceLeft = newDistance;
calculate_newDistance(trigger2, echo2);
double newDistanceRight = newDistance;

void loop() {

  if (distanceLeft <= 25 && distanceLeft >= 20) {
    delay(100);
    calculate_distance(trigger1, echo1);
    if (distanceLeft <= 25 && distanceLeft >= 20) {
      Serial.println("Left Locked");
      while (distanceLeft <= 40) {
        calculate_distance(trigger1, echo1);
        if (distanceLeft < 20) {
          calculate_newDistance(trigger1, echo1);
          for (; distanceLeft > newDistanceLeft + 0.1; distanceLeft = newDistanceLeft + 0.1) {
            Serial.println("Mouse Down");
            calculate_newDistance(trigger1, echo1);
          }
          delay(300);
        }
        else if (distanceLeft > 25) {
          calculate_newDistance(trigger1, echo1);
          for (; distanceLeft < newDistanceLeft - 0.1; distanceLeft = newDistanceLeft - 0.1) {
            Serial.println("Mouse Up");
            calculate_newDistance(trigger1, echo1);
          }
          delay(300);
        }
        else {
          Serial.println("Hold Mouse");
        }
      }
    }
  }
  if (distanceLeft >= 25 || distanceLeft <= 20) {
    delay(100);
    calculate_distance(trigger1, echo1);
    if (distanceLeft >= 25 || distanceLeft <= 20) {
      Serial.println("Left Locked");
      calculate_distance(trigger1, echo1);
      while (distanceLeft < 50) {
        Serial.println("Hovering");
        calculate_distance(trigger1, echo1);
        if (distanceLeft > 50) {
          Serial.println("Left Click");
          delay(300);
        }
      }
    }
  }
}

That’s what I’ve thought of so far.

Let me spell it out.
You can’t have code outside a function. You can have variable decelerations outside a function to make those variables global in scope.
This code is outside a function

calculate_distance(trigger1, echo1);
double  distanceLeft = distance;
calculate_distance(trigger2, echo2);
double distanceRight = distance;

calculate_newDistance(trigger1, echo1);
double newDistanceLeft = newDistance;
calculate_newDistance(trigger2, echo2);
double newDistanceRight = newDistance;

Look up how to write a for loop before posting again.