Hello good people of Arduino land.
First of all, apologies if the answer to this is simple, but I'm long in the tooth and new to this.
What I am trying to achieve, is to move a machine tool over a distance, based on an input value.
The method I have chosen to input my values is by using rotary switches, connected to potential dividers, to send varying voltage signals to the analogue input pins.
The problem with this method is that for each switch position, the voltage signal will never be an absolute value, but will fall within a close range of values.
Now, what I cannot get my ageing head around, is how to tell the Arduino to assign an absolute variable based on an upper and lower range of values at the input.
For example: for switch position 5, the analogue input could be seeing anything between 2.4 volts and 2.6 volts, and in this case, I need the assigned variable value to be integer 5. The table below should make things clearer (hopefully)
SWITCH POSITION ANALOGUE VOLTAGE VARIABLE GENERATED
1 0.3 to 0.6 1
2 0.8 to 1.2 2
3 1.4 to 1.8 3
4 1.9 to 2.2 4
[/table]
There will be two switches, each with ten positions (I've not filled in the whole table).
It may seem like a strange way to achieve the required result. I could use a matrix keypad, but then I would also need a display of some sort, and I only have a Duemilanove clone. Therefore not enough I/O. (plus my way is cheap and I thought would be simple!)
int adcReading = analogRead(A0); // Read the switch array
int button = 0; // Button pressed
// You need to start at the highest reading first and work down to the lowest in order
if (adcReading < 389) button = 4;
if (adcReading < 286) button = 3;
if (adcReading < 164) button = 2;
if (adcReading < 61) button = 1;
It could be put in a function that when called will return the button pressed or zero if no button pressed.
The biggest hurdle in any science is vocabulary; in this case, what you are doing is called a "resistor ladder". Once you have the terminology, you have a flood of examples via Google.
int adcReading = analogRead(A0); // Read the switch array
int button = 0; // Button pressed
// You need to start at the highest reading first and work down to the lowest in order
if (adcReading > 389) button = 4;
if (adcReading > 286) button = 3;
if (adcReading > 164) button = 2;
if (adcReading > 61) button = 1;
It could be put in a function that when called will return the button pressed or zero if no button pressed.
Wouldn't that example always result in the button= 1; ? I think those > need to be <