Potentiometer to send keypress

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
ubermick
 
Posts: 8
Joined: Fri May 17, 2013 1:50 pm

Potentiometer to send keypress

Post by ubermick »

Heya folks,

Trying to put together a control panel that'll act as a HID (for a simulator). I'm using momentary pushbuttons, a few toggle switches, and a couple of potentiometers. The pushbuttons and switches are sorted, but one of the potentiometers is giving me fits.

Basically using a slide potentiometer to read the voltage between 0 and 1023. That part I get. Using one slide pot, I can easily enough use it to zoom in and out:

Code: Select all

const int potPin0 = 0;
int val = 0; 

void setup() {
  Keyboard.begin();
}

void loop() {
  static int oldPotState = analogRead(potPin0);
  val = analogRead(potPin0);
  
  if (val > (oldPotState + 100) || val < (oldPotState - 100)) {    
    if (val < (oldPotState - 100)) {
        Keyboard.println("-");
    } else if (val > (oldPotState1 + 100)) {
        Keyboard.println("+");
    }
    oldPotState1 = val;
  }
}
But what I want to do with a second sliding pot is have THREE different commands sent depending on the position. Basically set three states - A < 200, B = 201-825, C > 826. When the pot moves from one state to another, it sends a single keypress. (Enters state A, sends a 1 and waits for the next state change, for example). But for whatever reason, I can't seem to click it.

Any help is appreciated!

User avatar
jigsawnz
 
Posts: 180
Joined: Mon Mar 12, 2012 10:17 pm

Re: Potentiometer to send keypress

Post by jigsawnz »

Hello, how about this.

Code: Select all

#define potPin0 0
uint16_t val = 0;

void setup() {
  Keyboard.begin();
}

void loop() {
  val = analogRead(potPin0);
if ( val > 0 && val <= 200 )
  Keyboard.println("command1");
else if ( val > 200 && val <= 825)
  Keyboard.println("command2");
else 
  Keyboard.println("command3");
}

User avatar
ubermick
 
Posts: 8
Joined: Fri May 17, 2013 1:50 pm

Re: Potentiometer to send keypress

Post by ubermick »

Cheers for that, jigsaw mate. I should have been more clear though, I need the keypress sent just once when the potentiometer hits the required range, rather than having it repeated. So basically need to assign an additional variable to keep track of each one: when I first tried it, this is what I came up with:

Code: Select all

const int potPin0 = 0;
uint16_t val = 0;
String newRange = "";
String oldRange = "";


void setup() {
  Keyboard.begin();
}

void loop() {
static int oldPotState = analogRead(potPin0);
 
if (oldPotState < 200) {
  oldRange = "A";
} else if (oldPotState > 825) {
  oldRange = "C";
} else {
  oldRange = "B";
}

 val = analogRead(potPin0);

if (oldPotState < 200) {
  newRange = "A";
} else if (oldPotState > 825) {
  newRange = "C";
} else {
  newRange = "B";
}
  
  if (oldRange != newRange) {    
   if (val < 200) {
     Keyboard.print("A");
     oldRange = "A";
   } else if (val > 825) {
     Keyboard.print("C");
     oldRange = "C";
   } else {
     Keyboard.print("B");
     oldRange = "B";
   }
    
    oldPotState = val;
  }  
}
But nothing from that.

User avatar
jigsawnz
 
Posts: 180
Joined: Mon Mar 12, 2012 10:17 pm

Re: Potentiometer to send keypress

Post by jigsawnz »

Code: Select all

#define potPin0 0
uint16_t val = 0;
char previous = '?';

void setup() {
  Keyboard.begin();
}

void loop() {
  val = analogRead(potPin0);
if ( (val > 0 && val <= 200) && previous != 'A'  )  {
  Keyboard.println("command1");
  previous = 'A';
}
if ( (val > 200 && val <= 825) && previous != 'B')  {
  Keyboard.println("command2");
  previous = 'B';
if ( (val > 825 && val <= 1024) && previous != 'C')  {
  Keyboard.println("command3");
  previous = 'C';
  }
}
Try this, hopefully this is going the right direction.

User avatar
ubermick
 
Posts: 8
Joined: Fri May 17, 2013 1:50 pm

Re: Potentiometer to send keypress

Post by ubermick »

We are now CERTAINLY in the right direction! Thank you!

The code as provided works nicely on it's own. The new problem is now occurring when I try and use the two potentiometers at the same time. They seem to be conflicting with each other throwing random readings. Assuming that there may have been an electrical issue (possibly wires shorting together) I moved the two analog pins further apart:

Code: Select all

#define potPin1 0
#define potPin2 3

int valp1 = 0; 
uint16_t valp2 = 0;
char previous = '?';

void setup() {
  Keyboard.begin(); 
}
void loop() {
  //pot reading
  static int oldPotState1 = analogRead(potPin1);
  valp1 = analogRead(potPin1);
  
  if (valp1 > (oldPotState1 + 200) || valp1 < (oldPotState1 - 200)) {
    if (valp1 < (oldPotState1 - 200)) {
        Keyboard.print("-");
        
    } else if (valp1 > (oldPotState1 + 200)) {
        Keyboard.print("+");
      }    
      
oldPotState1 = valp1;
  }
  
  //pot reading
  valp2 = analogRead(potPin2);
  
  if ( (valp2 < 199) && previous != 'A')  {
      Keyboard.print("1");
      previous = 'A';
  }
  if ( (valp2 > 200 && valp2 < 824) && previous != 'B')  {
      Keyboard.print("2");
      previous = 'B';
  }
  if ( (valp2 > 826) && previous != 'C')  {
      Keyboard.print("3");
      previous = 'C';
  }
}

User avatar
jigsawnz
 
Posts: 180
Joined: Mon Mar 12, 2012 10:17 pm

Re: Potentiometer to send keypress

Post by jigsawnz »

Code: Select all

#define potPin1 0
#define potPin2 3

uint16_t valp1 = 0; 
uint16_t valp2 = 0;
char previous = '?';
uint16_t oldPotState1 = 0;

void setup() {
  Keyboard.begin(); 
  
  //initialise values
  valp1 = analogRead(potPin1);
  valp2 = analogRead(potPin2);
  oldPotState1 = analogRead(potPin1);
}
void loop() {
  //pot1 reading
  valp1 = analogRead(potPin1);
  
  if (valp1 < (oldPotState1 - 200)) {
    Keyboard.print("-");    
  } 
  else if (valp1 > (oldPotState1 + 200)) {
    Keyboard.print("+");
  }      
  oldPotState1 = valp1;
  
  //pot2 reading
  valp2 = analogRead(potPin2);
  
  if ( (valp2 < 199) && previous != 'A')  {
      Keyboard.print("1");
      previous = 'A';
  }
  else if ( (valp2 > 200 && valp2 < 824) && previous != 'B')  {
      Keyboard.print("2");
      previous = 'B';
  }
  else if ( (valp2 > 826) && previous != 'C')  {
      Keyboard.print("3");
      previous = 'C';
  }
  delay(1000); 
}
I think the problem in your code for pot 1 was:
static int oldPotState1 = analogRead(potPin1);
valp1 = analogRead(potPin1);

these readings are only a few microseconds apart, so both values are going to be pretty much the same. I added a delay to your loop to increase the change of detecting a noticable change between readings.

User avatar
ubermick
 
Posts: 8
Joined: Fri May 17, 2013 1:50 pm

Re: Potentiometer to send keypress

Post by ubermick »

Good shout on that - alas, doesn't solve the problem. Gaaah!

(Appreciate your help on all this, by the way - This thing has been driving me mental for over a week now!)

User avatar
jigsawnz
 
Posts: 180
Joined: Mon Mar 12, 2012 10:17 pm

Re: Potentiometer to send keypress

Post by jigsawnz »

Code: Select all

#define potPin1 0
#define potPin2 3

uint16_t valp1 = 0; 
uint16_t valp2 = 0;
char previous = '?';
uint16_t oldPotState1 = 0;

void setup() {
  Keyboard.begin(); 
  
  //initialise values
  valp1 = analogRead(potPin1);
  valp2 = analogRead(potPin2);
  oldPotState1 = analogRead(potPin1);
}
void loop() {
  //pot1 reading
  analogRead(potPin1);
  delay(10);
  valp1 = analogRead(potPin1);
  
  if (valp1 < (oldPotState1 - 200)) {
    Keyboard.print("-");    
  } 
  else if (valp1 > (oldPotState1 + 200)) {
    Keyboard.print("+");
  }      
  oldPotState1 = valp1;
  
  //pot2 reading
  analogRead(potPin2);
  delay(10);
  valp2 = analogRead(potPin2);
  
  if ( (valp2 < 199) && previous != 'A')  {
      Keyboard.print("1");
      previous = 'A';
  }
  else if ( (valp2 > 200 && valp2 < 824) && previous != 'B')  {
      Keyboard.print("2");
      previous = 'B';
  }
  else if ( (valp2 > 826) && previous != 'C')  {
      Keyboard.print("3");
      previous = 'C';
  }
  delay(1000); 
}
Try that and see if it gives more stable readings. Maybe add Serial.print() for debugging.

User avatar
ubermick
 
Posts: 8
Joined: Fri May 17, 2013 1:50 pm

Re: Potentiometer to send keypress

Post by ubermick »

I have no idea what the issue is - the code looks completely clean, and I can't understand why it's doing this - but it's basically acting like the first potentiometer is constantly sending the "-" press. Occasionally it's throwing a "+" in there as well.

But in a bizarre twist, I dumped the code into the main control script (25 buttons, 9 toggle switches, and a 16x2 LCD) and… it's suddenly working fine! So while I haven't the foggiest what was going on, it looks like it's solved!

So thanks a MILLION for the help. And somewhat punching myself in the face for as straightforward as it turned out to be! (Now just need to convert everything to arrays so my code isn't a thousand lines long!)

User avatar
jigsawnz
 
Posts: 180
Joined: Mon Mar 12, 2012 10:17 pm

Re: Potentiometer to send keypress

Post by jigsawnz »

Oh well, good luck with cleaning up your code. Post a picture when it's finished.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”