Go Down

Topic: Multiple inputs for one output (Read 150 times) previous topic - next topic

Shadowbuddy518

Does anyone have example code for two inputs for one output? Ex: First input to servo, Using one potentiometer. Second input to same servo, using 2nd potentiometer. The goal is to have the math worked out by the arduino and have it find the mid point between the two potentiometers when both are in use. Right now there is a lot of gittering because well, It's trying two things at once. Thx in advance.

Power_Broker

Right now there is a lot of gittering because well, It's trying two things at once.
Not necessarily. Servos also jitter when not properly powered. How are you powering the servo?


Anyway, is this what you're looking for?

Code: [Select]

int data1 = analogRead(pot1);
int data2 = analogRead(pot2);

float finalData = (data1 + data2) / 2.0;

servo.write(finalData,0,1023,0,180);
"The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind."
   - Nikola Tesla

Robin2

Anyway, is this what you're looking for?
I believe there is a map() missing from that code.

And I would not bother with a float - I can't imagine that the servo would notice any extra detail.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Power_Broker

I believe there is a map() missing from that code.
Oops, yes I forgot to put it in. Here is the revised code:


Code: [Select]

int data1 = analogRead(pot1);
int data2 = analogRead(pot2);

float finalData = (data1 + data2) / 2.0;

servo.write(map(finalData,0,1023,0,180));
"The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind."
   - Nikola Tesla

wvmarle

Just wondering, isn't it possible (and simpler) to wire the two pots together as voltage divider and then read the midpoint directly? That way you need only one input and no additional code.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Go Up