execute only one function between an interval

Hi,

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;    
    } 
  }
}

with kind regards,
Bas

You need a variable to record the fact that the data has been sent so that your code will know not to send it twice.

...R

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;
    }}

with kind regards,
Bas

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
   }
}

...R

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
   }
}

it's working, thanks guys!

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; 
    } 
  }
}