#define capteur_force A0 // broche capteur de force
#include "Servo.h"
Servo servo; // création de l'objet "servo"
void setup() {
Serial.begin(9600); // Initialisation du moniteur serie
servo.attach(9); // attache le servo au pin spécifié
}
void loop() {
int force = analogRead(capteur_force); // on lit la valeur venant du capteur de force
Serial.print(force);
if(force > 400){
servo.write(90);
delay(2000);
while(force < 400){
int force = analogRead(capteur_force);
delay(1);
}
}
else{
servo.write(25);
}
delay(200);
}
I'm french so "capteur force" mean pressure detector and force still forceTexte préformaté
Every time you create a new local variable with the name "force", your code uses the most recent variable with that name. Use a SINGLE variable that is global.
Let’s assume that the force value read right when the loop starts is > 400. The if is executed, the servo moves to position 90, and waits 2 seconds. The value of force is still >400, so the while is not executed.
After the if, we wait for another 200msec.
The loop then restarts.
Now let’s assume that the force detected is not >400. The else executes moving the servo to position 25. Wait for 200msec and restart loop.
Besides the effect mentioned by @Paul_KD7HB, the while will never execute.
I had the delay of 2 secondes for release the pressure detector so force is (close) equal to 0 and the loop work until I press it again but it continue.
The while() is using the previously defined "force", then the analogRead is using the newly created variable name "force". You have to learn the difference between global and local variables and see why using the same name for both is a disaster waiting to happen.
You should write in normal words what the wanted functionality is.
This description shall avoid all programming-terms because there might be a mis-conception about how a certain detail of programming works.
With normal words you are an expert in french.
So I recommend that you write this description in french and then let do google-translate the translation to english.
As long as it is unclear what functionality you want almost any suggestion might be wrong.
Give this Code-Version a try. The variable "force" is made global. This means at all places in your code the same variable is used.
This code-version has additional serial printing to make visible what your code is really doing
// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *
// a detailed explanation how these macros work is given in this tutorial
// https://forum.arduino.cc/t/comfortable-serial-debug-output-short-to-write-fixed-text-name-and-content-of-any-variable-code-example/888298
#define dbg(myFixedText, variableName) \
Serial.print( F(#myFixedText " " #variableName"=") ); \
Serial.println(variableName);
#define dbgi(myFixedText, variableName,timeInterval) \
{ \
static unsigned long intervalStartTime; \
if ( millis() - intervalStartTime >= timeInterval ){ \
intervalStartTime = millis(); \
Serial.print( F(#myFixedText " " #variableName"=") ); \
Serial.println(variableName); \
} \
}
#define dbgc(myFixedText, variableName) \
{ \
static long lastState; \
if ( lastState != variableName ){ \
Serial.print( F(#myFixedText " " #variableName" changed from ") ); \
Serial.print(lastState); \
Serial.print( F(" to ") ); \
Serial.println(variableName); \
lastState = variableName; \
} \
}
#define dbgcf(myFixedText, variableName) \
{ \
static float lastState; \
if ( lastState != variableName ){ \
Serial.print( F(#myFixedText " " #variableName" changed from ") ); \
Serial.print(lastState); \
Serial.print( F(" to ") ); \
Serial.println(variableName); \
lastState = variableName; \
} \
}
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *
#define capteur_force A0 // broche capteur de force
#include "Servo.h"
Servo servo; // création de l'objet "servo"
int force;
void setup() {
Serial.begin(9600); // Initialisation du moniteur serie
servo.attach(9); // attache le servo au pin spécifié
}
void loop() {
force = analogRead(capteur_force); // on lit la valeur venant du capteur de force
//Serial.print(force);
dbgc("ToL", force);
if (force > 400) {
Serial.println("force > 400 => servo.write(90)");
servo.write(90);
delay(2000);
Serial.println("delay(2000) finished");
while (force < 400) {
dbgc("while", force);
force = analogRead(capteur_force);
delay(1);
}
}
else {
Serial.println("else force BIGGER than 400");
servo.write(25);
}
delay(250);
Serial.println("delay(250) finished");
}
When the value of variable force is > 400 the execution flow enters the if. When it gets to the while its value has not been modified and is still > 400 so the while does not execute.
int force = analogRead(pressure_detector);
Serial.print(force);
if(force > 400){ // force is bigger than 400
servo.write(90); // execute a servo-command
// that CHANGES the PHYSICAL force
delay(1500); // WAIT 1,5 seconds to let the servo do its move
// read in measured force NEW
force = analogRead(pressure_detector); // read in measured force NEW
// because value was read in NEW force CAN be smaller than 400
// CAUSED through the servo-MOVE
while(force < 400){
delay(200);
force = analogRead(pressure_detector);
}