Author Options:

Rotary potentiometer to pulse output. Answered

Hello everyone, I'm having a challenge!

I'm working on a code for my Arduino UNO.

In this project, I will be using a rotary potentiometer to emit a keyboard signal for a simulation. when the potentiometer is being rotated I want to change the speed in the simulation, this normally works with a single keypress. So when it is rotated, the output I want is only ONE press when rotated a certain value. I want the Arduino to give a single pulse when the variable of the analog input has a certain change in value.

Example: If the value changes from 0 to 1, I want to have ONE signal pulse to use as a keyboard signal.
Example: if the value changes from 7 to 6, I want to have ONE signal pulse to use as a keyboard signal.

I hope you can help me with this :)

This is the code i use to separate the value into 30 units instead of 1023:

const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output

void setup() { // initialize serial communications at 9600 bps: Serial.begin(9600); }

void loop() {

// read the analog in value:
sensorValue = analogRead(analogInPin); // map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 30); // change the analog out value:

// print the results to the Serial Monitor:
Serial.print("sensor = ");
Serial.print("\t output = "); Serial.println(outputValue);
delay(2); }


The forums are retiring in 2021 and are now closed for new topics and comments.

1 year ago

There is libraries available for these rotary encoders in the Arduino playground ;)
Use a suitable one and you get the type of output you need - reliable.
You can even define different actions for left and right turn or based or how quick you spin the knob.
Far easier then developing your own code if you ask me ;)


1 year ago

1.: Store initial value (0..30)
2.: Delay
3.: get new value (0..30)
4.: Calculate absolute of difference (abs(old_value - new_value))
4.: For(int i = 0; i <= difference; i++) {