Help me with analogRead()

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 );
}

Well not sure your code does what you want but one thing seems wrong:

" val = random( 256 ); // Generate a random number "

You don't seem to use 'val' anywhere in your loop. What did you want to use this random generated variable for in each cycle of the loop?

Lefty

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.

So, the question is: how is your pot connected?

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.

Make sure that the LED is connected to a digital pin that supports PWM output e.g. pins 3, 5, 6, 9, 10, and 11

/me

"The problem is with analogRead. The speed does not seem to change when I turn the pot. It always stays the same speed. "

You are connected to the Analog Pin 2, right?

Do you have a multimeter you can verify the voltage going in actually changes when you turn the pot?

“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.”

Serial.Println() doesn’t work for you? It would be really helpful to see what the value of time is getting set to.

with the center pin connected to PIN 2.

Is that analog pin 2, digital pin 2 or AVR chip pin 2?

Thanks for the reply. I’m using a BBB board from Modern Device. Here’s a link: http://www.moderndevice.com/index.shtml

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.

Thanks for the reply. The analog pin would that be something like pin 25 on the ATmga168?

Yes, analog pin 2 is connected to the AVR ATmega168 pin 25.

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.

It makes sense. If you think about them as digital I/Os, it would be I/O 14. But in analogue mode, it is analogue In 0.

And the analogue pins follow the ATmega168’s pin mapping. Whereas the digital I/Os d not follow the patter; but work simply.

/me

Thanks, I'll bet this solves the problem.