I'm trying to the read the value from a pot. I have a B10K pot connected to + and - with the center pin connected to PIN 2.
I want the LED to Flash at a random brightness. So there is some extra code in there. As is the LED should turn on and off based on the value read at PIN2. As is there does not seem to be any change in speed. What Am I missing?
int ledPin = 9; // LED connected to digital pin 13
int potPin = 2; // Define a pin for pot
int val = 0; // hold a random value
int time = 1000; // hold the time between loops
void setup() { // run once, when the sketch starts
pinMode( ledPin, OUTPUT ); // sets the digital pin as output
}
void loop() { // run over and over again
val = random( 256 ); // Generate a random number
time = analogRead( potPin ); // Get the value from pot
analogWrite( ledPin, 255 ); // sets the LED brightness to random value
delay( time ); // waits for a second
analogWrite( ledPin, 0 );
delay( time );
}
I want to make the brightness random. But I was having problems with the analogRead(). So I set it up to turn the light on and off with a delay set by the analog read. The problem is that time doesn't seem to be changing.
One end to GND, the other end to +5VDC, and the middle pin to the Analog input, right? It's what you describe, but check and recheck that the pins are correct. Use an ohm meter to verify the resistance changes.
The code I have is almost exactly like the analog read example. Here it is with out the extra items.
int ledPin = 9; // LED connected to digital pin 13
int potPin = 2; // Define a pin for pot
int time = 1000; // hold the time between loops
void setup() { // run once, when the sketch starts
pinMode( ledPin, OUTPUT ); // sets the digital pin as output
}
void loop() { // run over and over again
time = analogRead( potPin ); // Get the value from pot
analogWrite( ledPin, 255 ); // Turn LED on
delay( time ); // wait
analogWrite( ledPin, 0 ); // LED off
delay( time );
}
You’re generating the random value into ‘val’ but not using the value i.e. you’re not writing the value of ‘val’ to the PWM pin.
I think it should be like:
int ledPin = 9; // LED connected to digital pin 13
int potPin = 2; // Define a pin for pot
int val = 0; // hold a random value
int time = 1000; // hold the time between loops
void setup() { // run once, when the sketch starts
pinMode( ledPin, OUTPUT ); // sets the digital pin as output
}
void loop() { // run over and over again
val = random( 0, 255 ); // Generate a random number
time = analogRead( potPin ); // Get the value from pot
analogWrite( ledPin, val ); // sets the LED brightness to random value
delay( time ); // waits for a second
analogWrite( ledPin, val );
delay( time );
}
And it should be a random value from 0 - 255 which is 256 steps.
Thanks for the reply. You are correct, I did not use the random value. The random value is working fine.
The problem is with analogRead. The speed does not seem to change when I turn the pot. It always stays the same speed.
To test this I removed the random value and and made the light blink. Which made it easier to see if the speed was changing.
It would be great if I could test by sending a value back to the arduino software on the computer and see what the value was. It seems to always be the same and doesn't change.
I used the sample code, I found in the Arduino examples. I’m guessing this should be be the pin 2 on the board. Which looks to be physically connected to pin 4 on the chip.
I'm guessing this should be be the pin 2 on the board.
No, it should be pin A2, in the block of analog pins on the left-hand side of the photo. Looks like there are a bunch of power and ground pins there, too. From left to right, the pins are Ground, +5V and analog input 2.
I wish I had read this topic last night. I was having the same issue. It's beyond bizarre to me that the analog pins on the Duemilanove are labeled 1-5 for analogRead purposes but the same pins are 14-19 for digitalWrite purposes. I had hooked an LED up to pin 14=analog 0 and was able to make it light with digitalWrite(14, HIGH), so I was positive I should be able to analogRead(14) and read the voltage of that same pin. But actually, you have to analogRead(0). I refused to believe this until my friend insisted I try using a different pin number for the same pin.