Several arguments under one condition (button box) : VERY IMPORTANT !!!

Bonjour je suis en train de crée une “button box” qui touche à son terme.J’ai donc cablé en série les switch avec des résistance pour pouvoir mesurer une tension différente entre chaque. Comme ce sont des soudure les valeurs retourné en bit ne sont pas toujours précises et changent . Donc je voulais savoir comment dire a mon arduino que si le nombre retourné en bit est entre x et x alors appuis sur la touche x, sans utiliser cette argument dans la condition :
if ( analogRead(sw>515) && (sw<510) ) car il fonctionne mal avec les switchs on off on présents dans ma boite.
Voici mon code que je veux changer : ci- dessous

Hello, I’m creating a “button box” that’s coming to an end, so I’ve wired the switches in series with resistors so that I can measure a different voltage between each one. As they are solder the values returned in bit are not always accurate and change. So I wanted to know how to tell my arduino that if the number returned in bit is between x and x then press the x key, without using this argument in the condition :
if ( analogRead(sw>515) && (sw<510) ) because it doesn’t work well with the on off on switches in my box.
Here is my code that I want to change :

 #include <Keyboard.h>

   
 #define sw0 A0
 #define sw1 A1
 #define sw2 A2
 #define sw3 A3 
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  
}

void loop() {


Serial.println(analogRead(sw1));
//off ligne basse
 if ( analogRead(sw0) == 1023 )
  Keyboard.print("q");
  delay(100);

 if ( analogRead(sw0) == 511 )
  Keyboard.print("b");
  delay(10);

  if ( analogRead(sw0) == 341)
  Keyboard.print("c");
  delay(10);

  if ( analogRead(sw0) == 255 )
  Keyboard.print("d");
  delay(10);

  if ( analogRead(sw0) == 204)
  Keyboard.print("e");
  delay(10);





  //on ligne basse
  if ( analogRead(sw1) == 1023) 
  Keyboard.print("f");
  delay(10);

  if ( analogRead(sw1) == 511 )
  Keyboard.print("g");
  delay(10);

  if ( analogRead(sw1) == 339)
  Keyboard.print("h");
  delay(10);

  if ( analogRead(sw1) == 254 )
  Keyboard.print("i");
  delay(10);

  if ( analogRead(sw1) == 202)
  Keyboard.print("j");
  delay(10);
   

}

Yes i have correct in my code this little mistake but here's not the problem

felix240306:
Yes i have correct in my code this little mistake but here's not the problem

Then, please post the corrected, most recent code. In a new reply, please... don't edit the original one.

Oh, excuse but it's did

You mean something like this? This would print a “d” for all values between 200 and 255, inclusive.

  if ( analogRead(sw0) >= 200 && analogRead(sw0) <= 255 ) Keyboard.print("d");

Thank’s it works perfect

ToddL1962:
You mean something like this? This would print a “d” for all values between 200 and 255, inclusive.

  if ( analogRead(sw0) >= 200 && analogRead(sw0) <= 255 ) Keyboard.print("d");

But just do the analogRead() once and save the value - something like this

value = analogRead(switchPin);
if (value < 300) {
   Serial.println("LOW");
}
else if (value <  700) {
   Serial.println("MIDDLE");
}
else {
    Serial.println("HIGH");
}

…R

You should only read once and then test multiple times with the saved value:

  int swValue = analogRead(sw0);
...
  if ( swValue >= 200 && swValue <= 255 ) Keyboard.print("d");
...

Ok but ToddL1962 give the êrfect answer

felix240306:
Ok but ToddL1962 give the êrfect answer

No, that answer would be red marked on any code exam. I think Todd knows, but doesn't want to get bogged down in details.

felix240306:
Ok but ToddL1962 give the êrfect answer

aarg is right. I was in a hurry. Better to read once, especially since it is an analog. On some systems reading analogs that fast back to back results in weirdness. Either way, it is best to read once, otherwise you may be testing two different values.

aarg:
No, that answer would be red marked on any code exam. I think Todd knows, but doesn't want to get bogged down in details.

Actually I was just being careless. I'm glad you pointed it out.

But i try and my final project works i can active switch in myu flight simualtor, it works and arduino don't dsays error message . So it's good and thank's for all.

felix240306:
But i try and my final project works i can active switch in myu flight simualtor, it works and arduino don’t dsays error message . So it’s good and thank’s for all.

Why don’t you listen for good advice? that’s stupid. if the value changes during you test, then you might end up in weird state.

is it that difficult to write instead:

int sv0Value = analogRead(sw0);
if ((sv0Value >= 200) && (sv0Value <= 255)) Keyboard.print("d");

and your code will run faster too…

ok i will test but it's the same who's toddl1962 was say, no?

J-M-L:

if ((sv0Value >= 200) && (sv0Value <= 255)) Keyboard.print("d");

and your code will run faster too…

It will run even faster if each test only needs to check one value - as in the code in Reply #6

…R

Thank's it works better!!!!!!!!!!!!!!!!

felix240306:
ok i will test but it's the same who's toddl1962 was say, no?

it's the same test structure, but you don't call the function to read the voltage twice. you get the value once and then use that value to perform the comparisons.

felix240306:
ok i will test but it's the same who's toddl1962 was say, no?

No. Not the same. I would recommend changing it.