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 -:
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:
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?!
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?!