LED, push button and analog values ain't working they way it should


i am trying to create a small system that will allow me to turn a LED on with a push button and keep it on until i oush it again, but the ''tricky part'' is that i want it to go high with an analog value that is coming from a variable resistor.

reading the resistor value = no problem

lighting the LED with the analog value and coping with the debounce = stuck

can someone help me on the way, telling what to take specially care of?

the circuit is in place, but i can't work out the code

please help

What do you have for code so far?

This is what i got so far

I have the debounceing working great, but i am stuck trying to implement my analog readings as the output value to the LED. any ideas?

i have written the analog reading / mapping part, but i am not sure how to alter the code for my use.

int switchPin = 8; int ledPin = 13; boolean lastButton = LOW; boolean currentButton = LOW; boolean ledOn = false; int analogPin = A0

void setup() { pinMode(switchPin, INPUT); pinMode(ledPin, OUTPUT); }

boolean debounce(boolean last) { boolean current = digitalRead(switchPin); if (last != current) { delay(5); current = digitalRead(switchPin); } return current; }

void loop()

int outputValue = map(analogValue, 0, 1023, 0, 255); int analogValue = analogRead(analogPin);

{ currentButton = debounce(lastButton); if (lastButton == LOW && currentButton == HIGH) { ledOn = !ledOn; } lastButton = currentButton;

digitalWrite(ledPin, ledOn); }

int outputValue = map(analogValue, 0, 1023, 0, 255);   
int analogValue = analogRead(analogPin);

Probably be a good idea to do this AFTER the { for loop, not before.

It would also be a good idea to read the value first, then map it, so you don't look (so much) like an idiot.

Also, what is it you expect to do with this value? How does the analog value correspond to a switch state?

I am new to the entire arduino conecpt, so bare a along with me

but i will remember it for next time ;)

a friend told me this would help against the debounce, and i was trying to implement an analog reading for the LED.

It ain't working, i know that, but i fail to see the solution on how analogewrite to the LED and yet keep it high with the current value.

what would you adivce me to do?

I assume that you’re trying to control whether the led is on or not with the button and how brightly lit it is when it is on with the pot. If so, when you detect a transition and flip the ledOn variable, test that variable and either analogWrite what you read from the pot, or zero if the led is supposed to be off. Get rid of the later digitalWrite.