Pages: [1]   Go Down
Author Topic: Mapping(Not Resolved)  (Read 362 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
« Last Edit: November 05, 2012, 09:37:19 am by MonorailOrange » Logged

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 22
Posts: 1675
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It does very much! Thank You Duane!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So I adjusted my loop to this:
Code:
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!
Logged

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 22
Posts: 1675
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

potValue =

before analogRead

Duane B
Logged


Pages: [1]   Go Up
Jump to: