Last Updated:19/03/2024
Functions Lesson - map() with neopixels
Functions >> Functions Lesson - map() with neopixels
What does map() do?
Map allows you to find a value in a range of numbers based on a value in another range of numbers.
When we look in the Arduino reference the syntax is: map(value, fromLow, fromHigh, toLow, toHigh)
For this example we will start with two ranges. With simple values this may seem quicker with a simple equasion but when ranges become complicated or are dynamically changing the map function can save a lot of brain ache. |
|
Now lets move to a real world example. So for this example we have a pixel range of 0 - 479 while the touch system is working in the opposite direction with a starting offset of 20. screenPostion is the true screen position, touchPos would be the value between 1236 and 20 that the touch system feeds back. |
Circuit
Circuit:
For the first 2 examples I just used an UNO.
For example 3 with the neopixels the neopixels signal pin is connected to the UNO pin 8.
GND to GND
5v - 5V
I am only using 5 neopixels. If you use a longer string you will need a seperate 5v power supply.
Example 1: mapFunctionv1.ino
Click to Download code:mapFunctionv1.ino
This example shows a few different maps being used with their outputs
/* mapFunctionv1
*/
unsigned long currentMillis;
unsigned long timerMillis;
int timerLength = 500;
int inputValue;
long outputValue;
void setup() {
Serial.begin(9600);
Serial.println("mapFunctionv1");
//Simple Start
inputValue = 20;
outputValue = map(inputValue, 0, 100, 0, 10000);
Serial.print("inputValue: ");
Serial.println(inputValue);
Serial.print("outputValue = map(inputValue, 0, 100, 0, 10000) Gives: ");
Serial.println(outputValue);
Serial.println(" ");
inputValue = 5;
outputValue = map(inputValue, 0, 100, 0, 10000);
Serial.print("inputValue: ");
Serial.println(inputValue);
Serial.print("outputValue = map(inputValue, 0, 100, 0, 10000) Gives: ");
Serial.println(outputValue);
Serial.println(" ");
//Now the toLow and toHigh are more complex values
inputValue = 5;
outputValue = map(inputValue, 0, 100, 280, 229821);
Serial.print("inputValue: ");
Serial.println(inputValue);
Serial.print("outputValue = map(inputValue, 0, 100, 280, 229821) Gives: ");
Serial.println(outputValue);
Serial.println(" ");
//Now the fromLow and fromHigh and toLow and toHigh are more complex values
inputValue = 286;
outputValue = map(inputValue, 225, 336, 280, 229821);
Serial.print("inputValue: ");
Serial.println(inputValue);
Serial.print("outputValue = map(inputValue, 225, 336, 280, 229821) Gives: ");
Serial.println(outputValue);
Serial.println(" ");
//WARNING OUT OF RANGE NUMBERS
inputValue = 10;
outputValue = map(inputValue, 100, 200, 280, 229821);
Serial.print("inputValue: ");
Serial.println(inputValue);
Serial.print("map(inputValue, 100, 200, 280, 229821) Gives: ");
Serial.println(outputValue);
Serial.println(" ");
//This shows how numbers still get calculated outside the range
inputValue = 10;
outputValue = map(inputValue, 100, 200, 1000, 2000);
Serial.print("inputValue: ");
Serial.println(inputValue);
Serial.print("outputValue = map(inputValue, 100, 200, 1000, 2000); Gives: ");
Serial.println(outputValue);
Serial.println(" ");
//Practical uses Touch screen
//X reversed and different start finish and reversed
//screen is 0-479... 480 pixels
//often the touch screen will read from something like 1236 - 20
//so how would our map look
inputValue = 60;//the point being touched
outputValue = map(inputValue, 1236, 20, 0, 479);
Serial.println("Touch screen example");
Serial.print("inputValue: ");
Serial.println(inputValue);
Serial.print("outputValue = map(inputValue, 1236, 20, 0, 479); Gives: ");
Serial.println(outputValue);
Serial.println(" ");
inputValue = 1230;//the point being touched
outputValue = map(inputValue, 1236, 20, 0, 479);
Serial.println("Touch screen example 2");
Serial.print("inputValue: ");
Serial.println(inputValue);
Serial.print("outputValue = map(inputValue, 1236, 20, 0, 479); Gives: ");
Serial.println(outputValue);
Serial.println(" ");
}
void loop() {
}
Example 2: mapFunctionv2Neopixel.ino
This sketch uses 3 map() function to allow the system to fade between different RGB states of the neopixel.
Click to Download code:mapFunctionv2Neopixel.ino
/* mapFunctionv2Neopixel
*/
//Library needs to be included
#include "Adafruit_NeoPixel.h"
// Set the pin that is connected to the D0 on the Neopixel string
#define PIN 8
#define NUMPIXELS 5 // Number of Neopixels
// see Adafruit strandtest example for more information on possible values.
//sets up an Object called "pixels" that our instructions will be sent to
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
unsigned long currentMillis;
unsigned long timerMillis;
//int timerLength = 50;//slower fade
int timerLength = 10;//faster fade
int inputValue;//keeps track of the number of steps in the fade
int routputValue;
int goutputValue;
int boutputValue;
//Red values
int rfromLow;
int rfromHigh;
int rtoLow;
int rtoHigh;
//green values
int gfromLow;
int gfromHigh;
int gtoLow;
int gtoHigh;
//blue values
int bfromLow;
int bfromHigh;
int btoLow;
int btoHigh;
void setup() {
Serial.begin(9600);
Serial.println("mapFunctionv2Neopixel");
//This line starts by initialising "pixels"
pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
pixels.clear(); // Set all pixel colors to 'off'
//Nothing changes until this line is sent
pixels.show(); // Send the updated pixel colors to the hardware.
setTargetValues();//put some initial values
}
void loop() {
currentMillis = millis();
if (currentMillis - timerMillis >= timerLength) {
timerMillis = currentMillis;
//use the map function to find the next colour settings to change the neopixel to for the R G B channels
routputValue = map(inputValue,0,50,rtoLow,rtoHigh);
goutputValue = map(inputValue,0,50,gtoLow,gtoHigh);
boutputValue = map(inputValue,0,50,btoLow,btoHigh);
//send the values to the neopixels
pixels.setPixelColor(0, pixels.Color(routputValue, goutputValue, boutputValue));
pixels.show();//change the neopixels values...display the new values
inputValue++;
if(inputValue > 49){//0-50 steps so reset
inputValue = 0;
setTargetValues();//Set new targets
}
}
}
//This function sets a random values for each R,G,B element of the neopixel.
void setTargetValues(){
rtoLow = rtoHigh;//red set the start value as the old target
gtoLow = gtoHigh;//green set the start value as the old target
btoLow = btoHigh;//blue set the start value as the old target
rtoHigh = random(0,255);//Set random target Red
gtoHigh = random(0,255);//Set random target Green
btoHigh = random(0,255);//Set random target Blue
}
Example 3: mapFunctionv3Neopixel.ino
This sketch uses 3 map() function to allow the system to fade between different RGB states of the neopixel.
Click to Download code:mapFunctionv3Neopixel.ino
/* mapFunctionv3Neopixel
fade moving up a row of neopixels
*/
//Library needs to be included
#include "Adafruit_NeoPixel.h"
// Set the pin that is connected to the D0 on the Neopixel string
#define PIN 8
#define NUMPIXELS 5 // Number of Neopixels
// see Adafruit strandtest example for more information on possible values.
//sets up an Object called "pixels" that our instructions will be sent to
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
unsigned long currentMillis;
unsigned long timerMillis;
int timerLength = 50;//slower fade
//int timerLength = 10;//faster fade
int inputValue;
//As I have 5 neopixels I use a 5 element array for each channel R,G,B
int routputValue[5];
int goutputValue[5];
int boutputValue[5];
//Red values
int rfromLow;
int rfromHigh;
int rtoLow;
int rtoHigh;
//green values
int gfromLow;
int gfromHigh;
int gtoLow;
int gtoHigh;
//blue values
int bfromLow;
int bfromHigh;
int btoLow;
int btoHigh;
void setup() {
Serial.begin(9600);
Serial.println("mapFunctionv3Neopixel");
//This line starts by initialising "pixels"
pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
pixels.clear(); // Set all pixel colors to 'off'
//Nothing changes until this line is sent
pixels.show(); // Send the updated pixel colors to the hardware.
setTargetValues();//put some initial values
}
void loop() {
int q;
currentMillis = millis();
if (currentMillis - timerMillis >= timerLength) {
timerMillis = currentMillis;
for(q = 4;q > 0; q--){
routputValue[q] = routputValue[q-1];
goutputValue[q] = goutputValue[q-1];
boutputValue[q] = boutputValue[q-1];
}
routputValue[0] = map(inputValue,0,50,rtoLow,rtoHigh);
goutputValue[0] = map(inputValue,0,50,gtoLow,gtoHigh);
boutputValue[0] = map(inputValue,0,50,btoLow,btoHigh);
//the fade process will work in 50 steps
for(q = 0;q < NUMPIXELS;q++){
pixels.setPixelColor(q, pixels.Color(routputValue[q], goutputValue[q], boutputValue[q]));
}
pixels.show();//change the neopixels values
//inputValue++;
inputValue += 10;
if(inputValue > 49){//0-50 steps so reset
inputValue = 0;
setTargetValues();//Set new targets
}
}
}
void setTargetValues(){
rtoLow = rtoHigh;//red set the start value as the old target
gtoLow = gtoHigh;//green set the start value as the old target
btoLow = btoHigh;//blue set the start value as the old target
rtoHigh = random(0,255);//Set random target Red
gtoHigh = random(0,255);//Set random target Green
btoHigh = random(0,255);//Set random target Blue
}
Additional Resource Links
Don't forget to use the Reference in you Arduino IDE for more help on this lesson see map()
Comments
This site has been designed to be child friendly, this means that comments cannot be added to videos or directly to the site. To add a comment or ask a question please email the address in this image: and use Functions Lesson - map() with neopixels as a reference.