Hello guys.
I have a problem with the use of my sensor and its use.
I want to measure the distance between a lens and an object with this sensor. Then I would like to apply two-three formulas to transform this distance into a number of motor steps to translate a lens in my system.
In my system, I'm using: Quimat Nema 17 Stepper Motor (QD06), HC-SR04 (ultrasonic sensor), two lenses, Potentiometer 10K, LCD 1602 Module, Driver A4988 (for the stepper motor).
3 problems:
-I can not take such a variable (distance lens - object) and use it.
-Error while compiling is in the loop (line 2 of the loop) while saying many conditions in an "if"
-When it's measuring a distance > 12 cm, it doesn't enter the program.
How to use a value measured by the ultrasonic sensor in a program?
Here the final code.
// On apporte les différentes bibliothèques
#include <Keypad.h>
#include <Stepper.h>
#include <LiquidCrystal.h>
#include "SR04.h"
// On initialise les différentes variables du prgm
int tempPin = 0;
int DistTransLent = 0;
int StepsMotor = 0;
int nombreDePas = 200;
int DistLentObj;
// Commande Moteur pas-à-pas
Stepper monMoteur (nombreDePas,3,4);
// Commande écran LCD
// BS E D4 D5 D6 D7
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
// Commande capteur-ultrasons
#define TRIG_PIN 3
#define ECHO_PIN 2
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN); // Ports de mesure
long DistLenObj; // est la distance mesurée par le capteur US
void setup() {
Serial.begin(9600);
delay(1000);
lcd.begin(16, 2);
}
void loop() {
DistLentObj=sr04.Distance();
if ((DistLentObj > 300) || (DistLentObj == 0) ||(DistLentObj == 6))
{ // La distance mesurée ne rentre pas dans le prgm car trop grande
}
else
{ // La distance mesurée rentre dans le prgm
int tempReading = analogRead(tempPin);
// Initialisation et Affichage Distance en cm sur LCD
lcd.setCursor(0, 0);
lcd.print("Dist cm ");
lcd.setCursor(6, 0);
lcd.print(DistLentObj);
DistTransLent = -6*DistLentObj/(DistLentObj-6);
// La distance de translation est transformée en nombre de Pas-Moteur
StepsMotor = DistTransLent*1019.87;
monMoteur.setSpeed(50);
monMoteur.step(StepsMotor);
delay(10000);
}
}
Please post your code and not pictures of your code
Did you read this before posting a programming question ?
Ardginner:
How can i post it ? i can only insert picture of it...
Read the thread that I linked to.
Basically you copy the code from the IDE and paste it here, preferably between code tags after Auto formatting it. The linked thread explains it
UKHeliBob:
Read the thread that I linked to.
Basically, you copy the code from the IDE and paste it here, preferably between code tags after Auto formatting it. The linked thread explains it
Got it
now everyone can understand
evanmars:
Oops
= is not ==
Oops, a basic error thanks!
(and also there was the other error while saying "DisLentObj was not declared in the scope"
It's a simple error too ^^ : i declared DistLentObj not DisLentObj.... lol)
But I still have one problem: When it's measuring a distance > 12 cm, it doesn't enter the program.
Is it because of the conditions? When the ultrasonic sensor measures a distance called "DistLentObj", I want it to not be equal to 0cm, 6cm and to not be > 300 cm.
evanmars:
!= is not equal
Yes.
I know that, but I prefer an "IF...ELSE" structure.
Do you know why a distance > 12 cm isn't entering my program?
(When I say isn't entering my program, my stepper motor isn't turning)
How can DistLenObj be >300, == 0, and == 6 at the same time?
Maybe you want || instead of &&
evanmars:
How can DistLenObj be >300, == 0, and == 6 at the same time?
Maybe you want || instead of &&
Right! "||" is "or inclusive"?
but it's not changing the fact that a distance > 12 cm isn't entering my program?
DistTransLent = -6 / (DistLentObj - 6);
At 12cm, this equates to
DistTransLent = -6 / (12 - 6)
DistTransLent = -6 / 6
DistTransLent = -1
For any value greater than 12cm, (DistLentObj - 6) will be greater than 6, so the equation will give a fractional value less than 1, and since DistTransLent is an integer the fraction part gets dropped and DistTransLent will be 0.
You will also have problems with values of DistLentObj below 12, because the fractions part is always dropped and only the whole number portion of the answer is used for DistTransLent.
For any value greater than 12cm, (DistLentObj - 6) will be greater than 6, so the equation will give a fractional value less than 1, and since DistTransLent is an integer the fraction part gets dropped and DistTransLent will be 0.
You will also have problems with values of DistLentObj below 12, because the fractions part is always dropped and only the whole number portion of the answer is used for DistTransLent.
You right.
The formula is exactly -6*DistLentObj/(DistLentObj-6)
It's working !