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
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...
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
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;
}
// 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 ;
}
}
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;
}
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.)
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
Yes the if statements are not triggering the Serial.write
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.
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 thank you guys!