Mapping analog input voltages to digital output pins and latching.

Hello again,

I will try to make a simpler post than my last one. I am very new to programming, and just started with Arduino a few days ago. I have 10 momentary switches that send different voltages to the analog pin and are mapped to 10 LEDS on digital pins set as outputs. This is working fine.

What I want to do is emulate a rotary switch (NOT encoder). I other words, I press a momentary, it turns on its LED based on mapping the voltage to a digital pin, and the LED STAYS ON after releasing the momentary. If another momentary is pressed, the first LED turns off, the newly selected LED turns on, and stays on after the momentary is released, and so on and so forth. Only one LED can be on at a time.

I've been having a devil of a time trying to figure out how to do this, but I can't get it right. I have tried many different approaches. My 2 questions are:

  1. Is it possible to do this?
  2. Considering I really have put in the effort to learn how to do this, how do I do it? (please be kind).

I'm hoping I don't need to post code here. The operation is pretty standard, I started out mapping a potentiometer on analog input 0 to 0-1023, and was able to turn on each LED on digital pins 0-9, and simply substituted momentaries and voltage dividers to get the same results.

Thank you so much for listening.

It's best to show us your code so we can see what you are doing. Please attach it using code tags.

  1. Is it possible to do this?

Of course it is. The state change detection example shows how.

  1. Considering I really have put in the effort to learn how to do this, how do I do it? (please be kind).

We can’t wee what effort you have put into it. This is not to say that you haven’t put any effort in. It just means that we can’t tell what you tried or what your results are/were.

Post some code, if the hint above is not enough.

Issue resolved, although I don't understand why completely. Firstly, I was mapping the analogRead to digital pins 0-9(1-10), not taking into account the first 2 pins are for serial communication, so I changed to pins 2-11 (3-12). Secondly, I changed the mapping from 0, 1023, 0, 9 to 100, 1000, 2, 11 as shown in the code below. I have a 1k pulldown resistor on the analog input pin, and 10 momentary switches with resistors of varying values that form voltage dividers with the 1k pulldown so that momentary #1 turns on digital pin 2, momentary #2 turns on digital pin 3 etc., down the line to pin 11. The digital pins latch on as well now. In the second example below, I originally mapped 0-1023 to pins 2-11. When I pressed a momentary, it's LED would light up as long as the switch was pressed, but the LED on pin 2 would light on release. Mapping 100-1000 to pins 1-22 got the desired results (each LED latches on). No need for button state code.

Original code:

int val = analogRead(0);
  val = map(val, 0, 1023, 0, 9);

Revised code:

{
    int val = analogRead(0);
  val = map(val, 100, 1000, 2, 11);