I'm trying to control the volume of my Macbook with a potentiometer hooked up to the Arduino. Every time the potentiometer get between a certain interval, the Arduino should send a volume control message to my mac. It should send ONE volume message per interval, for example between the values 0-64, 65-124 and so on.
The problem: The Arduino sends multiple messages per interval. What can I do to make sure it only sends one volume message per interval?
a = analogRead(A5);
void loop() {
for (int i=0; i <16; i++) {
if (a>= i*0 && a <=i*64 && (a-value) > 5) { //if pot is turned left, send volume up
//UP();
Serial.print("UP");
Serial.println(i);
value = a;
}
if (a>= i*0 && a <=i*64 && (a-value) < -5) { //if pot is turned right, send volume down
//DOWN();
Serial.print("DOWN");
Serial.println(i);
value = a;
}
}
}
I have tried that (I think..) I've tried it with a boolean for every interval, but this doesn't work either. I think I am doing something wrong with this variable/boolean but I don't know what.
for( int i=0; i> 16; i++) {
if (a>= i*0 && a <=i*64 && (a-value) > 5 && boolean[i]) {
//UP();
value = a;
boolean[i] = false;
}
if (a>= i*0 && a <=i*64 && (a-value) < -5 && !boolean[i]) {
//DOWN();
value = a;
boolean[i] = false;
}}
I just noticed in the snippet in your original post that the potentiometer is only read once - that will not be much good.
What about this pseudo code
void loop() {
newPotVal = analogRead(potPin);
if (newPotVal != prevPotVal) {
// code here to send value to Mac
prevPotVal = newPotVal;
delay(100); // so as not to send too often
}
}
Well, he's breaking it up into ranges 64/1024ths of the analog range each (ie, 16 blocks).
int prevPotValRange;
void loop() {
int newPotVal = analogRead(potPin);
int newPotValRange=newPotVal>>6 //drop the low 6 bits, dividing it by 64; could also divide by 64 directly
if (newPotValRange != prevPotValRange) {
// code here to send value to Mac
prevPotValRange = newPotValRange;
delay(100); // so as not to send too often
}
}
void loop() {
int a = analogRead(A5);
int b = a/64;
for (int i=0; i <16; i++) {
if ( b == i && (a-value) > 5 && b != c) {
UP();
value = a;
c = b;
}
if ( b == i && (a-value) < -5 && b!= c) {
DOWN();
value = a;
c = b;
}
}
}