Arduino Programming – Map and smooth values

Re-map a value from one range to another and smooth analog input values.

Level : beginner with Arduino. ( Basic knowledge of programming principles like if/else and variables ).

Download the Arduino ( .pde ) files : arduino_tutorial_map-and-smooth.zip

Map.pde :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
  Mapping example

  Re-map a number from one range to another.

  created 01-12-2009 by kasperkamperman.com
*/


const int sensorPin = 0;    // the pin that the potmeter is attached to
int sensorValue     = 0;    // store the value coming from the sensor
int mappedValue;            // store the new value

void setup() {
  // opens serial port, sets data rate to 57600 bits per second
  Serial.begin(57600);
}

void loop() {
  // read the value from the sensor
  // the range from an analog input is 0 - 1023
  sensorValue = analogRead(sensorPin);  

  // print the value
  Serial.print("sensorValue                                 : ");
  Serial.println(sensorValue);  

  // map(value, fromLow, fromHigh, toLow, toHigh)
  mappedValue = map(sensorValue, 0, 1023, 0, 255);

  Serial.print("map to 0 - 255                              : ");
  Serial.println(mappedValue);  

  // invert a value is possible too
  mappedValue = map(sensorValue, 0, 1023, 255, 0);

  Serial.print("map to 255 - 0                              : ");
  Serial.println(mappedValue);  

  // or negative values
  mappedValue = map(sensorValue, 0, 1023, -127, 128);

  Serial.print("map to -127 - 128                           : ");
  Serial.println(mappedValue);  

  // only watch a certain part of the sensor values
  // for example when using a sensor that doesn't cover the whole
  // range
  mappedValue = map(sensorValue, 512, 712, 0, 100);
  Serial.print("map 512 - 712 to 0 - 100                    : ");
  Serial.println(mappedValue);

  // constraining the sensorValue can be usefull to prevent negative values
  sensorValue = constrain(sensorValue, 512, 712);  
  Serial.print("constrained sensorValue                     : ");
  Serial.println(sensorValue);

  mappedValue = map(sensorValue, 512, 712, 0, 100);
  Serial.print("map 512 - 712 to 0 - 100 ( with constrain ) : ");
  Serial.println(mappedValue);

  // division between next read
  Serial.println("------------------------------------------------");

  // wait for 1000 milliseconds ( 1 second ) to read every second
  delay(1000);
}

Analog_smooth.pde :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
  Smoothing example

  Read the analog input and smooth out the result by averaging
  the result with past values of the input.

  The more samples to longer it takes to reach the final value.
  4/5 samples give a smooth and reactive enough result.

  Simpler algorithm than the smoothing included with the Arduino
  examples.

  based on : Analog Smoothing Algorithm by Tom Igoe
  <http://www.tigoe.net/pcomp/code/category/arduinowiring/41>

  created 05-01-2010 by kasperkamperman.com
*/


const int sensorPin = 0;    // the pin that the potmeter is attached to

int sensorVal       = 0;    // store the value coming from the sensor
int smoothedVal     = 0;    // smoothed result
int samples         = 4;    // amount of samples

void setup() {
  Serial.begin(57600);
}

void loop() {

  // read the value from the sensor
  // the range from an analog input is 0 - 1023
  sensorVal = analogRead(sensorPin);

  smoothedVal = smoothedVal + ((sensorVal - smoothedVal)/samples);

  // print the original value and the smoothedValue
  Serial.print(sensorVal);
  Serial.print(", ");
  Serial.println(smoothedVal);

  // slow down amount of readings and prints
  delay(100);

}