Having trouble with an if statement

Hello im having trouble figuring out how the if statement should be defined. What im doing is reading a pot and if the pot has been moved by 2 or more values either + or - then the if statement is true. I need it so if the pot stops on a bad position it doesnt print 2 values for example 1 2 1 2 1 2 1 2. To ignore that i want it to trigger the Serial.write command only when its moved by 2 or more
/var/State is an int to save the previous state.
If i remove the 2nd if statement for all pots (if /var/ = /varstate/ + 2 || /var/ = /varstate/ -2) all pots work.
With this code all pots arent working
Here is my code.

#include <AnalogMuxDeMux.h>
AnalogMux amux(13, 12, 11, A1);
#define NO_PINS 8
int vHigh = 0;
int vHighState =0;
int vMid = 0;
int vMidState =0;
int vBass = 0;
int vBassState =0;
int vHigh1 = 0;
int vHigh1State=0;
int vMid1 = 0;
int vMid1State=0;
int vBass1 = 0;
int vBass1State=0;
int lastbuttonState =0;
int lastbuttonState1 = 0;
byte CC=176;
byte ON=144;
byte OFF=128;
byte switch1=1;
byte switch2=2;
byte pot1=3;
byte pot2=4;
byte pot3=5;
byte pot4=6;
byte pot5=7;
byte pot6=8;
byte on=127;
byte off=0;

void setup() {
  pinMode (13, OUTPUT);
  pinMode (12, OUTPUT);
  pinMode (11, OUTPUT);
  pinMode (A1, INPUT);
  Serial.begin(31250);
}

void loop() {
  
  int sw = amux.AnalogRead (0) / 8;
int sw1 = amux.AnalogRead(1) / 8;
    int vHigh = amux.AnalogRead(2) / 8;
    int vMid = amux.AnalogRead(3) / 8;
int vBass = amux.AnalogRead(4) / 8;
     int vHigh1 = amux.AnalogRead(5) / 8;
    int vMid1 = amux.AnalogRead(6) / 8;
int vBass1 = amux.AnalogRead(7) / 8;


if (sw != lastbuttonState) {
if (sw == 127)  {
 Serial.write(ON);
 Serial.write(switch1);
 Serial.write(on);
}
}

if (sw == 0 && lastbuttonState == 127){
 Serial.write(OFF);
 Serial.write(switch1);
 Serial.write(off);
}
  lastbuttonState=sw;
  
  if (sw1 != lastbuttonState1) {
  if (sw1 == 127)  {
 Serial.write(ON);
 Serial.write(switch2);
 Serial.write(on);
}
}
  if (sw1 == 0 && lastbuttonState1 == 127){
 Serial.write(OFF);
 Serial.write(switch2);
 Serial.write(off);
 }
 lastbuttonState1=sw1;
  
 if (vHigh != vHighState) {
 if ((vHigh == vHighState +2)||(vHigh == vHighState -2)) {
Serial.write(CC);
Serial.write(pot1);
Serial.write(vHigh); 
}
}
  vHighState=vHigh;
  
  if (vMid != vMidState) {
  if ((vMid == vMidState +2)||(vMid == vMidState -2)) {
Serial.write(CC);
Serial.write(pot2);
Serial.write(vMid);   
}
}
  vMidState=vMid;
 
  if (vBass != vBass1State) {
  if ((vBass == vBassState +2)||(vBass == vBassState -2)) {
Serial.write(CC);
Serial.write(pot3);
Serial.write(vBass);   
}
}
vBassState=vBass;

   if (vHigh1 != vHigh1State) {
   if ((vHigh1 == vHigh1State+2)||(vHigh1 == vHigh1State -2)) {
Serial.write(CC);
Serial.write(pot4);
Serial.write(vHigh1);    
}
}
vHigh1State=vHigh1;
   if (vMid1 != vMid1State) {
   if ((vMid1 == vMid1State +2)||(vMid1 == vMid1State -2)) {
Serial.write(CC);
Serial.write(pot5);
Serial.write(vMid1);
}  
}
vMid1State=vMid1;

  if (vBass1 != vBass1State) {  
  if ((vBass1 == vBass1State +2)||vBass1 == (vBass1State -2)) {
Serial.write(CC);
Serial.write(pot6);
Serial.write(vBass1);
  }
}
   vBass1State=vBass1;
   
   
   
}
#include <stdlib.h>

int pot = 0, pot_before = 0; 

void loop(){
pot = analogRead(A1);

// Do your stuff here

if (abs(pot - pot_before) > 2) {
   //do what you want to do here if the difference is bigger than 2 
}

pot_before = pot;
}//end loop

ok thank you can you explain me what does stdlib contain and why do i need it and why do i use abs

spoocter:
ok thank you can you explain me what does stdlib contain and why do i need it and why do i use abs

http://www.cplusplus.com/reference/clibrary/cstdlib/

The #include isn't necessary because the it's included anyway, but it contains abs() which gives you the absolute value of a number. When you need to get the distance between two numbers, you take the absolute value of their difference. That's really just a basic algebra concept.

hm... replaced it like you said for all the pots, still same thing Serial.write isnt triggered... :confused:

Also i got this from the reference site

Warning
Because of the way the abs() function is implemented, avoid using other functions inside the brackets, it may lead to incorrect results.
abs(a++);   // avoid this - yields incorrect results

a++;          // use this instead -
abs(a);       // keep other math outside the function

How do i make it like this?

spoocter:
How do i make it like this?

What you're doing is fine. The difference is that a++ modifies a. The way abs() is implemented, it would case a++ to happen twice, which would mean a is one bigger than it should be. The subtraction you're doing is fine, though.

If you really want to know:

void loop(){
  pot = analogRead(A1);

  // Do your stuff here
  int potdiff = pot - pot_before;
  potdiff = abs(potdiff);
  if (potdiff > 2) {
    //do what you want to do here if the difference is bigger than 2 
  }

  pot_before = pot;
}

Tried separated, still doesnt work :astonished:
Am i approaching it the wrong way? Any ideas?

WizenedEE:

void loop(){

pot = analogRead(A1);

// Do your stuff here
  int potdiff = pot - pot_before;
  potdiff = abs(potdiff);
  if (potdiff > 2) {
    //do what you want to do here if the difference is bigger than 2
  }

pot_before = pot;
}

That code is broken as it will never notice slow movement of the wiper when the difference between successive samples is less than 2 - you mustn't update pot_before unless you've noticed a significant change.

#define THRESHOLD 2          // #define the threshold for easy code maintainance
int pot_before = -2*THRESHOLD ; // initialize with a value that forces the if true first time round

void loop()
{
  int pot = analogRead(A1);

  // Do your stuff here
  if (abs (pot - pot_before) > THRESHOLD) {
    //do what you want to do here if the difference is bigger than the THRESHOLD

    pot_before = pot ;
  }

}

:cold_sweat: Still not working =(. It has an LED to monitor the incoming signal FROM the arduino so when i power it up it flashes, so the first time it loops it works, though when i rotate it fast or slow, doesnt send data. Im starting to think i should not Serial.write(analogRead(ofthepot)) but something modified. I thought something happened to the hardware so i changed it to the buggy state (only vHigh!=vHighState) and it works, tho the sent data is with values 1 2 1 2 3 2 3 4 3 4 5 6 5 6 5 6…
And im thinking i should use unsigned int or something for the analogread, because if i rotate it from 127 towards 0 it would give a negative value (pot - pot before)

Here im putting the code with the things you people said.

#include <AnalogMuxDeMux.h>
#define THRESHOLD 2
AnalogMux amux(13, 12, 11, A1);
#define NO_PINS 8
int vHigh = 0;
int vHighState =0;
int HighEQ = 0;
int vMid = 0;
int vMidState =0;
int MidEQ = 0;
int vBass = 0;
int vBassState = -2*THRESHOLD;
int BassEQ = 0;
int vHigh1 = 0;
int vHigh1State= -2*THRESHOLD;
int HighEQ1 = 0;
int vMid1 = 0;
int vMid1State= -2*THRESHOLD;
int MidEQ1=0;
int vBass1 = 0;
int vBass1State= -2*THRESHOLD;
int BassEQ1=0;
int lastbuttonState =0;
int lastbuttonState1 = 0;
byte CC=176;
byte ON=144;
byte OFF=128;
byte switch1=1;
byte switch2=2;
byte pot1=3;
byte pot2=4;
byte pot3=5;
byte pot4=6;
byte pot5=7;
byte pot6=8;
byte on=127;
byte off=0;

void setup() 
{
  pinMode (13, OUTPUT);
  pinMode (12, OUTPUT);
  pinMode (11, OUTPUT);
  pinMode (A1, INPUT);
  Serial.begin(31250);
}

void loop() 
{

  int sw = amux.AnalogRead (0) / 8;
  int sw1 = amux.AnalogRead(1) / 8;
  int vHigh = amux.AnalogRead(2) / 8;
  int vMid = amux.AnalogRead(3) / 8;
  int vBass = amux.AnalogRead(4) / 8;
  int vHigh1 = amux.AnalogRead(5) / 8;
  int vMid1 = amux.AnalogRead(6) / 8;
  int vBass1 = amux.AnalogRead(7) / 8;


  if (sw != lastbuttonState) 
  {
    if (sw == 127)  {
      Serial.write(ON);
      Serial.write(switch1);
      Serial.write(on);
    }
  }

  if (sw == 0 && lastbuttonState == 127)
  {
    Serial.write(OFF);
    Serial.write(switch1);
    Serial.write(off);
  }
  lastbuttonState=sw;

  if (sw1 != lastbuttonState1) 
  {
    if (sw1 == 127)  {
      Serial.write(ON);
      Serial.write(switch2);
      Serial.write(on);
    }
  }
  if (sw1 == 0 && lastbuttonState1 == 127)
  {
    Serial.write(OFF);
    Serial.write(switch2);
    Serial.write(off);
  }
  lastbuttonState1=sw1;

  if (vHigh != vHighState) 
  {
    Serial.write(CC);
    Serial.write(pot1);
    Serial.write(vHigh); 
  }
  vHighState=vHigh;

  MidEQ = vMid - vMidState;
  MidEQ = abs(MidEQ);

  if (MidEQ > 2) 
  {
    Serial.write(CC);
    Serial.write(pot2);
    Serial.write(vMid);   
  }
  vMidState=vMid;

  if (abs(vBass-vBassState) > THRESHOLD) 
  {
    Serial.write(CC);
    Serial.write(pot3);
    Serial.write(vBass);   
  }
  vBassState=vBass;

  if (abs(vHigh1 - vHigh1State) > THRESHOLD) 
  {
    Serial.write(CC);
    Serial.write(pot4);
    Serial.write(vHigh1);    
  }
  vHigh1State=vHigh1;

  if (abs(vMid1 - vMid1State) > THRESHOLD) 
  {
    Serial.write(CC);
    Serial.write(pot5);
    Serial.write(vMid1);  
  }
  vMid1State=vMid1;

  if (abs(vBass1 - vBass1State) > THRESHOLD) 
  {
    Serial.write(CC);
    Serial.write(pot6);
    Serial.write(vBass1);
  }
  vBass1State=vBass1;



}

If you put each { on a new line, and use the Tools + Auto Format menu item to reformat your code, you'd end up with something much more readable.

I can't believe, either, that it takes all that code, with nary a Serial.print() in sight, to demonstrate your problem.

Yes i know i suck at coding, i dont know how to use properly arrays. im sorry its so long :confused:

EDIT: I formated the code so its easier.

Please comment out all those Serial.write() statements, and add some Serial.print() and Serial.println() statements. We have no idea what values you are getting from amux.AnalogRead() on each of the 8 channels (if that is what the argument represents), and, apparently you don't either. So, print them.

Unplug whatever is connected to the Arduino's serial port. Connect the Arduino to the PC, and open the Serial Monitor. See what the incoming data is.

Then, you can determine if the if statements make sense, and what, exactly is going on.

When i replace all Serial.write with Serial.println, it prints the buttons with their values - either 0 or 1. When i rotate the pots it prints only for pot1 (vHigh, in the code its right after the buttons). When i rotate all other pots nothing happens (im running them through a multiplexer, there is no problem with the multiplexer. If i make all the if statements like the one for pot1 - all pots work, though not the way i want them to.)

What values are being read from the multiplexed pots? THAT is the key question.

If i write Serial.println in the if statements nothing is being read.
If i write them outside the if statements its just 0-1023 / 8 (0-127)
For high 1 its being read 0 - 127

If i write Serial.println in the if statements nothing is being read.

Wrong. The data is still being read from the pots. The data may not be being printed because your if statement(s) does/do not evaluate to true.

If i write them outside the if statements its just 0-1023 / 8 (0-127)

Quit waving your hands and show the code with the Serial.print() statements, and (some of) the output.

For high 1 its being read 0 - 127

This makes no sense.

  1. Yes the if statements are not triggering the Serial.write
  2. Note in my code
  int vHigh = amux.AnalogRead(2) / 8; // Library command to read the pot from the pin on the mux - amux.AnalogRead(pinnumber)
  int vMid = amux.AnalogRead(3) / 8;
  int vBass = amux.AnalogRead(4) / 8;
  int vHigh1 = amux.AnalogRead(5) / 8;
  int vMid1 = amux.AnalogRead(6) / 8;
  int vBass1 = amux.AnalogRead(7) / 8;

Serial.write(vHigh) // or vMid or vBass according to the if statement
the if statement for vHigh is just a simple if the value has changed no matter how - print the value of the pot.
Whatever (except vHigh) pot i turn nothing is getting printed. When i turn the vHigh pot it prints its value from 0 - 127
3.

1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
26
27
28
29
30
29
30
31
32
33
32
33
34
35
36
37
38
39
40
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
116
117
118
119
120
121
122
123
124
125
126
127
126
127
126
125
124
125
124
125
124
123
122
121
120
119
118
117
116
117
116
115
114
115
114
113
112
113
112
111
110
109
108
107
106
105
104
105
104
103
104
103
104
103
104
103
102
101
102
101
100
99
98
97
96
97
96
95
96
95
96
95
94
93
92
91
90
89
88
87
86
85
84
83
82
81
80
79
78
77
76
75
76
75
74
73
72
71
70
69
68
67
66
65
64
63
62
61
60
59
58
57
56
55
54
53
52
51
50
49
48
47
46
45
44
45
44
43
42
41
40
39
38
37
36
35
34
33
32
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
2
1
0

spoocter:
the if statement for vHigh is just a simple if the value has changed no matter how - print the value of the pot.

If you haven’t changed the code since the last time it was posted, that’s not true. The if statement reads “if the value has changed by greater than 2 since the last iteration of loop.”

Loop runs very quickly, so depending on what is being measured, it might never change by 3+ in < 1 mS.

omg im stupid im sorry i meant vHigh not vHigh1. NVM though a friend told me to add the command that saves the state in the if statements big brackets (where serial.write is)

The correct fix is the 1st guy's answer :slight_smile: thank you guys!