Mapping analog voltage to multiple digital output pins/momentary latching

Hello all,

I just started programming with Arduino a few days ago. I have written some code that will map 10 voltages (derived from a 10 stage resistor ladder with an n.o. momentary switch on each rung) to digital pins 0-9. I am quite sure my code is not all that well written or efficient, but it accomplishes that task. What I want to happen, and I have not been able to figure out yet, is for each button to latch on, and only one at a time (one button in the on state, all others off). In the schematic I posted below, the inputs float when no momentary is pressed, since there is no pull down/pull up. I'm guessing if the switches are latching on, this might not matter. Ignoring that, I'm wondering what code I need to insert so each of the 10 momentary switches will latch on. Here's my crappy code:) and the schematic - and thank you in advance!

int ledPin0 = 0; // LED connected to digital pin 0 int ledPin1 = 1; // LED connected to digital pin 1 int ledPin2 = 2; // LED connected to digital pin 2 int ledPin3 = 3; // LED connected to digital pin 3 int ledPin4 = 4; // LED connected to digital pin 4 int ledPin5 = 5; // LED connected to digital pin 5 int ledPin6 = 6; // LED connected to digital pin 6 int ledPin7 = 7; // LED connected to digital pin 7 int ledPin8 = 8; // LED connected to digital pin 8 int ledPin9 = 9; // LED connected to digital pin 9

int analogPin = 0; // potentiometer connected to analog pin 0

int val = 0; // variable to store the read value

void setup()

{

pinMode(ledPin0, OUTPUT); // sets the pin as output pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(ledPin3, OUTPUT); pinMode(ledPin4, OUTPUT); pinMode(ledPin5, OUTPUT); pinMode(ledPin6, OUTPUT); pinMode(ledPin7, OUTPUT); pinMode(ledPin8, OUTPUT); pinMode(ledPin9, OUTPUT);

}

void loop()

{

{ int val = analogRead(0); val = map(val, 10, 1013, 0, 9);

if(val == 0) { digitalWrite(ledPin0, HIGH); digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, LOW); digitalWrite(ledPin3, LOW); digitalWrite(ledPin4, LOW); digitalWrite(ledPin5, LOW); digitalWrite(ledPin6, LOW); digitalWrite(ledPin7, LOW); digitalWrite(ledPin8, LOW); digitalWrite(ledPin9, LOW);

} if(val == 1) { digitalWrite(ledPin0, LOW); digitalWrite(ledPin1, HIGH); digitalWrite(ledPin2, LOW); digitalWrite(ledPin3, LOW); digitalWrite(ledPin4, LOW); digitalWrite(ledPin5, LOW); digitalWrite(ledPin6, LOW); digitalWrite(ledPin7, LOW); digitalWrite(ledPin8, LOW); digitalWrite(ledPin9, LOW);

} if(val == 2) { digitalWrite(ledPin0, LOW); digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, HIGH); digitalWrite(ledPin3, LOW); digitalWrite(ledPin4, LOW); digitalWrite(ledPin5, LOW); digitalWrite(ledPin6, LOW); digitalWrite(ledPin7, LOW); digitalWrite(ledPin8, LOW); digitalWrite(ledPin9, LOW);

} if(val == 3) { digitalWrite(ledPin0, LOW); digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, LOW); digitalWrite(ledPin3, HIGH); digitalWrite(ledPin4, LOW); digitalWrite(ledPin5, LOW); digitalWrite(ledPin6, LOW); digitalWrite(ledPin7, LOW); digitalWrite(ledPin8, LOW); digitalWrite(ledPin9, LOW);

} if(val == 4) { digitalWrite(ledPin0, LOW); digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, LOW); digitalWrite(ledPin3, LOW); digitalWrite(ledPin4, HIGH); digitalWrite(ledPin5, LOW); digitalWrite(ledPin6, LOW); digitalWrite(ledPin7, LOW); digitalWrite(ledPin8, LOW); digitalWrite(ledPin9, LOW);

} if(val == 5) { digitalWrite(ledPin0, LOW); digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, LOW); digitalWrite(ledPin3, LOW); digitalWrite(ledPin4, LOW); digitalWrite(ledPin5, HIGH); digitalWrite(ledPin6, LOW); digitalWrite(ledPin7, LOW); digitalWrite(ledPin8, LOW); digitalWrite(ledPin9, LOW);

} if(val == 6) { digitalWrite(ledPin0, LOW); digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, LOW); digitalWrite(ledPin3, LOW); digitalWrite(ledPin4, LOW); digitalWrite(ledPin5, LOW); digitalWrite(ledPin6, HIGH); digitalWrite(ledPin7, LOW); digitalWrite(ledPin8, LOW); digitalWrite(ledPin9, LOW);

} if(val == 7) { digitalWrite(ledPin0, LOW); digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, LOW); digitalWrite(ledPin3, LOW); digitalWrite(ledPin4, LOW); digitalWrite(ledPin5, LOW); digitalWrite(ledPin6, LOW); digitalWrite(ledPin7, HIGH); digitalWrite(ledPin8, LOW); digitalWrite(ledPin9, LOW);

} if(val == 8) { digitalWrite(ledPin0, LOW); digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, LOW); digitalWrite(ledPin3, LOW); digitalWrite(ledPin4, LOW); digitalWrite(ledPin5, LOW); digitalWrite(ledPin6, LOW); digitalWrite(ledPin7, LOW); digitalWrite(ledPin8, HIGH); digitalWrite(ledPin9, LOW);

} if(val == 9) { digitalWrite(ledPin0, LOW); digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, LOW); digitalWrite(ledPin3, LOW); digitalWrite(ledPin4, LOW); digitalWrite(ledPin5, LOW); digitalWrite(ledPin6, LOW); digitalWrite(ledPin7, LOW); digitalWrite(ledPin8, LOW); digitalWrite(ledPin9, HIGH);

}

} }

I borrowed the schematic from an earlier post that uses 5 switches, but the principle is the same::

/* * * analogPin +5 V * | | * | \ * ---------------- / * | \ 1K * | / * | \ * |____ _| * | SW1 | * | \ * | / * | \ 1K * | / * | \ * |_ _| * | SW2 | * | | * | \ * | / * | \ 1K * | / * | \ * |_ _| * | SW3 | * | | * | \ * | / * | \ 1K * | / * | \ * |_ ___| * | SW4 | * | | * | \ * | / * | \ 1K * | / * | \ * | | * |/\/\/_| * | * | * _____ * ___ ground * _ * */

First off read the how to use the forum sticky post to see how to post code correctly.

You can not have any software that makes an input switch latch.

You should not let inputs float because they can read anything, see:- http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

Your code is very long winded, you need to use arrays:- http://www.thebox.myzen.co.uk/Tutorial/Arrays.html

In the schematic I posted below, the inputs float when no momentary is pressed, since there is no pull down/pull up

You only have one input and it is not floating. It is pulled to GND by the resistor at the bottom of the diagram.