Controlling a computer using a variable resistor

What and why?

Recently, I read an article by Mikhail Sannikov “ Connecting the encoder to a computer ”. And it so happened that the other day they sent me an Arduino board, so I wanted to repeat this simple design or do something similar.

First, a little about what an encoder is and why it is needed at all. An encoder is an angle-to-code converter whose output signal is a Gray code. There are input devices built on encoders that look like a massive washer, rotating which, for example, you can rotate the model in a 3D editor.

image

(the picture is from here )

However, I didn’t have an encoder, I didn’t want to go to the store, so instead it was decided to adapt an old 150 KΩ variable resistor lying around in a box with different rubbish.

So, we had to do the following:
  1. Connect a variable resistor to Arduino;
  2. Make a program for the controller that reads the byte from the analog port and transfers it to the computer;
  3. Write a program for a computer that emulates pressing certain keys depending on the direction of rotation of the variable resistor shaft.

Hard & Soft

An example of Analog In, Out Serial was found in the programming guide on the Arduino website, which completely solves the first and second problems.
The resistor is connected in this way:
image

However, the code had to be slightly changed: remove all lines from Serial.print and replace with one Serial.write (outputValue): The control program for the computer was written in C # in Visual Studio 2010 Express. Main window of the program: Dialog with settings: Note: clicking on the “Button” button is not yet processed. Here it is worth considering in more detail the parameters from the “Misc” group. The first is needed to emulate a keystroke, the rest is needed to eliminate chatter. Latency - the delay in milliseconds between pressing a key and releasing.

const int analogInPin = A0; // Переменный резистор подключен к порту A0
int sensorValue = 0; // значение, считанное из порта

void setup() {
Serial.begin(9600); // Инициализация COM-порта
}

// Чтение из порта A0 и вывод этого значения в COM-порт
void loop() {
sensorValue = analogRead(analogInPin);
outputValue = map(sensorValue, 0, 1023, 0, 255);
Serial.write(outputValue);
delay(10);
}






image



image






Buffer - the number of sequentially read values ​​from the COM port.
Jitter - shows the permissible deviation from the value of the last sample.

The chatter suppression and determination of the direction of rotation of the variable resistor shaft works as follows:
  • a certain number of bytes (Buffer parameter) are read into the buffer from the serial port;
  • if the last byte in the buffer is greater than the sum of the first byte and the Jitter value, then this means that the shaft rotates to the right;
  • if the last byte in the buffer is less than the difference between the first byte and the Jitter value, this means that the shaft rotates to the left.

The disadvantages of this method:
- the delay between the start of the rotation of the shaft and the reaction of the program to it;
- the key is pressed once, regardless of the angle of rotation. That is, you can quickly rotate the resistor shaft (the first byte in the buffer will be, say, 10, and the last - 30), and the program will work it out in one click.

Algorithm code: Project for Visual Studio, a sketch for Arduino and a scheme can be downloaded here: http://sourceforge.net/projects/keyemu/files/KeyEmu-0.1b-source.zip/download The archive contains the bin directory containing the compiled program . To run it, you will need the installed .NET Framework 4.

int[] buffer = new int[(int)conf.buffer]; // Buffer
int bufferTop = (int)conf.buffer - 1;

for (int i = 0; i < conf.buffer; i++)
{
buffer[i] = sp.ReadByte();
}

if (buffer[bufferTop] > (buffer[0] + conf.jitter))
{
// “Нажимаем” клавишу
}
else if (buffer[bufferTop] < (buffer[0] - conf.jitter))
{
// “Нажимаем” клавишу
}





That's all for now. A curious person can modify the program - implement a series of keystrokes instead of one, or “press” different keys, depending on which application is active at a given time, or simply “polish it glitch” to infinity.

Also popular now: