Go Down

Topic: Mapping(Not Resolved) (Read 364 times) previous topic - next topic

MonorailOrange

Nov 05, 2012, 03:12 pm Last Edit: Nov 05, 2012, 03:37 pm by MonorailOrange Reason: 1
Hello Team!
 I hope everyone is doing well! I have a question about my program. I have set up a simple program to test a bunch of LED's and Potentiometers that I just purchased. I'd like to make an enhancement to the program:
Code: [Select]
int potPin = A0; // Potentiometer Pin
int led1 = 13; // Green
int led2 = 11; // Green
int led3 = 9; // Green
int led4 = 7; // Yellow
int led5 = 5; // Yellow
int led6 = 3; // Red
int potValue = 0; // variable from the sensor

void setup() {
 pinMode(led1, OUTPUT);
 pinMode(led2, OUTPUT);
 pinMode(led3, OUTPUT);
 pinMode(led4, OUTPUT);
 pinMode(led5, OUTPUT);
 pinMode(led6, OUTPUT);
 Serial.begin(9600);
}

void loop(){
 potValue = analogRead(potPin);
 digitalWrite(led1, HIGH);
 Serial.println(potValue);
 delay(potValue/4);
 potValue = analogRead(potPin);
 digitalWrite(led2, HIGH);
 Serial.println(potValue);
 delay(potValue/4);
 potValue = analogRead(potPin);
 digitalWrite(led3, HIGH);
 Serial.println(potValue);
 delay(potValue/4);
 potValue = analogRead(potPin);
 digitalWrite(led4, HIGH);
 Serial.println(potValue);
 delay(potValue/4);
 potValue = analogRead(potPin);
 digitalWrite(led5, HIGH);
 Serial.println(potValue);
 delay(potValue/4);
 potValue = analogRead(potPin);
 digitalWrite(led6, HIGH);
 Serial.println(potValue);
 delay(potValue/4);
 digitalWrite(led1, LOW);
 Serial.println(potValue);
 delay(potValue/4);
 potValue = analogRead(potPin);
 digitalWrite(led2, LOW);
 Serial.println(potValue);
 delay(potValue/4);
 potValue = analogRead(potPin);
 digitalWrite(led3, LOW);
 Serial.println(potValue);
 delay(potValue/4);
 potValue = analogRead(potPin);
 digitalWrite(led4, LOW);
 Serial.println(potValue);
 delay(potValue/4);
 potValue = analogRead(potPin);
 digitalWrite(led5, LOW);
 Serial.println(potValue);
 delay(potValue/4);
 potValue = analogRead(potPin);
 digitalWrite(led6, LOW);
 Serial.println(potValue);
 delay(potValue/4);
}

Because the analogRead has a range from 0-1023, and because that value is used to determine the speed of the LED chase, I would like to map the values to make the speed transitions more visibly consistent (I think a few of my potentiometers are bad), but I'm not sure on how to use the function correctly. Do I need to repeat the map() function in each step, or is it possible to implement it in the void setup() function so that it repeats itself automatically... I've read the documentation ( http://www.arduino.cc/en/Reference/Map ) on mapping but I'm still unsure of what to do.
Thank You Team!
MonorailOrange

DuaneB

Remove all but the first analogRead in loop and put your map immediatley after this first analogRead, the value will be used throughout loop.

When you exit loop and reenter, you will read the analog input again (analogRead) and then map the value to the range you want to use throughout the rest of loop.

Does that help ?

Duane B

rcarduino.blogspot.com

MonorailOrange

It does very much! Thank You Duane!

MonorailOrange

So I adjusted my loop to this:
Code: [Select]
int potPin = A0; // Potentiometer Pin
int led1 = 13; // Green
int led2 = 11; // Green
int led3 = 9; // Green
int led4 = 7; // Yellow
int led5 = 5; // Yellow
int led6 = 3; // Red
int potValue = 0; // variable from the sensor

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  Serial.begin(9600);
}

void loop(){
  analogRead(potPin);
  potValue = map(potValue, 0, 1023, 0, 200);
  digitalWrite(led1, HIGH);
  Serial.println(potValue);
  delay(potValue);
  digitalWrite(led2, HIGH);
  Serial.println(potValue);
  delay(potValue);
  digitalWrite(led3, HIGH);
  Serial.println(potValue);
  delay(potValue);
  digitalWrite(led4, HIGH);
  Serial.println(potValue);
  delay(potValue);
  digitalWrite(led5, HIGH);
  Serial.println(potValue);
  delay(potValue);
  digitalWrite(led6, HIGH);
  Serial.println(potValue);
  delay(potValue);
  digitalWrite(led1, LOW);
  Serial.println(potValue);
  delay(potValue);
  digitalWrite(led2, LOW);
  Serial.println(potValue);
  delay(potValue);
  digitalWrite(led3, LOW);
  Serial.println(potValue);
  delay(potValue);
  digitalWrite(led4, LOW);
  Serial.println(potValue);
  delay(potValue);
  digitalWrite(led5, LOW);
  Serial.println(potValue);
  delay(potValue);
  digitalWrite(led6, LOW);
  Serial.println(potValue);
  delay(potValue);
}


Now my potValue is constantly reading as 0...   :smiley-eek: What did I do wrong?? Thank you for the help!

DuaneB

Your calling analogRead but not storing the result anywhere, you need to add -

potValue =

before analogRead

Duane B

Go Up