Hi I'm brand new to Arduino and coding stuffs..
and am trying to make these kind of different reactions according to different ranges I set.
Found quite similar debates in this forum but couldn't actually adapt it to my project such as else statement, rangebound and etc.
Can anyone give me a quick solution to my codes.!
Would really appreciate some.
#include <Mouse.h>
int smoke01 = A0;
int smoke02 = A3;
int smoke03 = A5;
int sensorThres = 400;
void setup() {
pinMode(smoke01, INPUT);
pinMode(smoke02, INPUT);
pinMode(smoke03, INPUT);
Serial.begin(9600);
}
void loop() {
char xDir = analogRead(smoke02);
char yDir = analogRead(smoke03);
char wheel = 0;
if ( 80 > analogRead(smoke01) > 60)
{ Mouse.press();}
if ( 85 > analogRead(smoke01) > 80)
{ Mouse.release(); }
if ( 100 > analogRead(smoke01) > 85)
{ Mouse.press();}
if ( 105 > analogRead(smoke01) > 100)
{ Mouse.release(); }
if ( 150 > analogRead(smoke01) > 105)
{ Mouse.press();}
if (analogRead(smoke01) < 60)
{ Mouse.release(); }
if(250 > analogRead(smoke02) > 200)
{ Mouse.move(xDir/5, 0, wheel); }
if(300 > analogRead(smoke02) > 250)
{ Mouse.move(-xDir/5, 0, wheel); }
if(350 > analogRead(smoke02) > 300)
{ Mouse.move(xDir/6, 0, wheel); }
if(400 > analogRead(smoke02) > 350)
{ Mouse.move(-xDir/6, 0, wheel); }
if(450 > analogRead(smoke02) > 400)
{ Mouse.move(xDir/7, 0, wheel); }
if(500 > analogRead(smoke02) > 450)
{ Mouse.move(-xDir/7, 0, wheel); }
if(analogRead(smoke02) < 200)
{ Mouse.move(0, 0, wheel); }
if(250 > analogRead(smoke03) > 200)
{ Mouse.move(0, yDir/5, wheel); }
if(300 > analogRead(smoke03) > 250)
{ Mouse.move(0, -yDir/5, wheel); }
if(350 > analogRead(smoke03) > 300)
{ Mouse.move(0, yDir/6, wheel); }
if(400 > analogRead(smoke03) > 350)
{ Mouse.move(0, -yDir/6, wheel); }
if(450 > analogRead(smoke03) > 400)
{ Mouse.move(0, yDir/7, wheel); }
if(500 > analogRead(smoke03) > 450)
{ Mouse.move(0, -yDir/7, wheel); }
if(analogRead(smoke03) < 200)
{ Mouse.move(0, 0, wheel); }
delay(10);
}
Read it into a variable, and test the variable twice, with an and condition
This syntax is not how you do it in C++, or to be precise technically it will compile but it is not doing what you think it is doing
oh.. sorry. Is there a function I could use instead..?
I would do something like this
int smokeValue01 = analogRead(smoke01);
if (smokeValue01 > 60 && smokeValue01 < 80)
{
Mouse.press();
}
else if (smokeValue01 > 80 && smokeValue01 < 85)
{
Mouse.release();
}
etc
Apart from anything else, I really don't like comparisons with the value first as they do not read so easily for me
J-M-L
January 25, 2022, 12:40pm
6
no just do 2 tests with a logical AND (&&, and) in between
if ((x <= 150) && (x > 105)) { ....}
you have to split it into 2 statements
if (a > b && b > c)
This is basics, learn to walk first
Noted, but this is a personal preference and there is no reason impose it on newbies.
great thanks and sorry for the inconvenience.
Yup after this project I will start over from the basics since I'm finding it really fun to learn. Kinda jumped straight in and this happened. and many thanks to ur reply
Will do . Thank you J-M-L
Hence me stating my personal preference
with no attempt to impose it on anyone
I don't like value first comparisons either- it sounds odd if you read it out loud. I have seen it touted as a useful way to get the compiler to help you avoid mixing = with ==, but that advantage isn't enough to overcome my distaste.
I may not read code out load, but I do read code to myself as a series of sentences and it certainly sounds odd
J-M-L
January 25, 2022, 1:21pm
15
agreed. I dislike it a well
@wildbill @J-M-L be careful, otherwise you will be accused of imposing your preferences on newbies
Triggered. There is exactly 0 usefulness for the OP about your personal preference as it doesn’t address the fundamental problem with their if statement. But being new they might take it as this is a big deal since it is mentioned in this context, which is not. Now you are trying to downplay it like it is nothing with your attempted sarcasm, but maybe you don’t remember when you started, all this sinks in for someone trying to learn new things. When I commented I did so that OP won’t pay too much attention to it and not because I wanted to somehow get at you. But you seem to take it quite personally. I’m sorry, it wasn’t my intent.
If it compiles it is, ipso facto, valid C++ syntax.
You know precisely what I meant