Arduino Analog input for a digital output

So this week was a bit more challenging in fact I never got it to work exactly the way that I wanted to despite several days of trying and failing.  I guess I just found a whole ton of ways to not make a led get brighter with a variable resistor. Anyways the goal this week was to make a LED brighter by wiring a analog input from a variable resistor.  So anyway maybe there is someone that will read this blog and help me to understand why it didn't work the way that it was suppose to.

So here is the code that I used to make this project try and work.

int led= 13;
int knob = A0;
int output = 0;

void setup() {
  pinMode(led, OUTPUT);
}

void loop() {
  int val = analogRead(knob);
  output = map(val, 0, 1023, 0, 255);
  analogWrite(led, output);
}

Here is a video of it "working"

So as you can see from that video that it didn't work as intended so I thought I would play around with it for a while.  It took me several days of changing the ports trying different configurations etc. one thing that I thought it would be is that the potentiometer was some how broken or something and the board was not reading it right so I wrote some more code to test that idea out. Here is that code.

int led = 13;
int led2 = 12;
int knob = 0;
void setup() {
  pinMode(led, OUTPUT);
}

void loop() {
  int val = analogRead(knob);
  digitalWrite(led, HIGH);
  delay(val);
  digitalWrite(led, LOW);
  delay(val);
  digitalWrite(led2, HIGH);
  delay(1024-val);
  digitalWrite(led2, LOW);
  delay(1024-val);
 
}

So essentially this code takes the input from the variable resistor and translates that to the time that it will be delayed from the blinks in the LEDs so here is this code working like I thought it would.



So you can see that it does read that variable resistor for the right level.  So I can't figure out why the code above can't make the LED brighter and dimmer instead of just turning it on and off. If anyone could help that would be greatly appreciated. 

Overall it was a great project and I did learn quite a bit about the different ports in the Arduino and also about the coding.  I guess just not enough to actually make it work.  I think the dimmer switches is the place were this would show up the most and it made me think that maybe it was just my LED that was messed up because I know that you have to use the right LED bulbs with dimmer switches and so maybe that could be a factor I don't know.  I am just a little tired now after tinkering with this thing for quite a while now.  So anyway I thought I might put a bigger picture of my circuit just in case anyone want to look at it to help me out.    






Comments

Popular posts from this blog

What is social bookmarking?

Motor with sonar