Using a potentiometer to send a keyboard stroke?

Status
Not open for further replies.

ubermick

New member
Howyeh boffins,

So I'm working on a spot of code for a control panel for a space sim, using the Teensy++ 2.0. Thought I'd ask this question here, rather than on the Arduino forum, since you folks seem nice and friendly, whereas… well, they seem to be grumpy bastards over there!

So I'm using a potentiometer to control engine speed, say. So when you increase the potentiometer, reads the change in voltage (0-1023) and if the change is beyond a set amount (50) then it send the command. Increase sends a +, decrease sends a -:

Code:
const int potPin = 0;

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


void loop() {
   
  static int oldPotState = analogRead(potPin);
  val = analogRead(potPin);
  
  if (val > (oldPotState + 50) || val < (oldPotState - 50)) {    
    if (val < (oldPotState - 50)) {
        Keyboard.println("-");
    } else if (val > (oldPotState + 50)) {
        Keyboard.println("+");
    }
    oldPotState = val;
  }
}

Straightforward enough. But I have a second potentiometer that needs to do a similar job, but this time with three commands instead of two. Basically need to check the voltage and have it send a 1 if it dips below 200, 2 if it changes between 201 and 824, and 3 over 825. Obviously only sending the keystroke if it moves outside those thresholds. So what I came up with was:

Code:
const int potPinB = 1;

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

void loop() {
   
 static int oldPotStateB = analogRead(potPinB);
 
if (oldPotStateB < 200) {
  oldRange = "1";
} else if (oldPotStateB > 825) {
  oldRange = "3";
} else {
  oldRange = "2";
}

 val = analogRead(potPinB);

if (oldPotStateB < 200) {
  newRange = "1";
} else if (oldPotState > 825) {
  newRange = "3";
} else {
  newRange = "2";
}
  
  if (oldRange != newRange) {    
   if (val < 200) {
     Keyboard.print("1");
     oldRange = "1";
   } else if (val > 824) {
     Keyboard.print("3");
     oldRange = "3";
   } else {
     Keyboard.print("2");
     oldRange = "2";
   }
    
    oldPotState = val;
  }

Basically trying to say that if the current voltage reading doesn't equal the predefined thresholds, then send the keystroke, but otherwise don't bother. Buuuuut, not working.

Anyone feel like lending a hand?! :)
 
Hi, just having a quick read of the code, have you declared what 'oldRange' and 'newRange' and 'val' are? I guess you have as you would have received an error, but just a quick question. In the second else if set, there is also a variable 'oldPotState' which should be 'oldPotStateB'? also, it looks like you are setting both newRange and oldRange to the value oldPotState rather than comparing the two analogRead values. If the first analogRead value is oldPotStateB and the second analogueRead value is val, val should set the newRange value rather then oldPotStateB.

I havent plugged this code into the compiler to check, but thats how it reads. Hope that helps a little.
 
Status
Not open for further replies.
Back
Top