# Map function not working

Hey guys! I need to map a potvalue to dmx (witch takes 0 to 255). With this piece of code i try to make a timer for a smokemachine. (thats why the variables have to do with fire)
But my my firetimer is all over the place.
When Pv6 is 255, the firetimer = 0, for some reason.
When PV6 is 0, the firetimer = 0
when PV6 is 55, the firetimer = 4294960818.

can someone help me?
This is the piece of code you’ll need.

``````   val = analogRead(P6);
val = map(val, 0, 1023, 0, 255);
Pv6 = val;

if(Pv6 < 250 && Pv6 > 10){
val = Pv6;                              //save tijd bij vorige fire, vergelijk met verwachte tijd ivm pot
val = map(val, 0, 255, 30000, 600000); //30000 millisec --> 600000 millisecconds
firetimer = val;
nxtfire = lastfire + firetimer;
``````
``````#include <DmxSimple.h>
#define P1 A0
#define P2 A1
#define P3 A2
#define P4 A3
#define P5 A4
#define P6 A5
#define L1 4
#define L2 5
#define R1 6
#define R2 7
int Pv1;
int Pv2;
int Pv3;
int Pv4;
int Pv5;
int Pv6;
int ledState = LOW;
unsigned long firetimer;
unsigned long lastfire;
unsigned long nxtfire;
unsigned long previousMillis = 0;

void setup() {
DmxSimple.usePin(3);
DmxSimple.maxChannel(11);
Serial.begin(9600);

}

void loop() {
DmxSimple.write(1, Pv5);
DmxSimple.write(2, Pv1);
DmxSimple.write(3, Pv2);
DmxSimple.write(4, Pv3);
DmxSimple.write(9, Pv4);
if(Pv6 > 250){
digitalWrite(R1, HIGH);
digitalWrite(L1, HIGH);
}else{
digitalWrite(R1, LOW);
digitalWrite(L1, LOW);
}
if(Pv6 < 250 && Pv6 > 10){
int time = millis();
if(time >= nxtfire){
digitalWrite(R1, HIGH);
Serial.println("FIRE IN THE HOLE");
delay(10000);
digitalWrite(R1, LOW);
lastfire = millis();
}}

}

void ReadValue(){ //map all potvalues to dmx values

val = map(val, 0, 1023, 0, 255);
Pv1 = val;

val = map(val, 0, 1023, 0, 255);
Pv2 = val;

val = map(val, 0, 1023, 0, 255);
Pv3 = val;

val = map(val, 0, 1023, 0, 255);
Pv4 = val;

val = map(val, 0, 1023, 0, 255);
Pv5 = val;

val = map(val, 0, 1023, 0, 255);
Pv6 = val;

if(Pv6 < 250 && Pv6 > 10){
val = Pv6;                              //save tijd bij vorige fire, vergelijk met verwachte tijd ivm pot
val = map(val, 0, 255, 30000, 600000); //30000 millisec --> 600000 millisecconds
firetimer = val;
nxtfire = lastfire + firetimer;

unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= blinkinterval) {
previousMillis = currentMillis;

if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
digitalWrite(L1, ledState);
}
}

Serial.print("P1: ");
Serial.println(Pv1);
Serial.print("P2: ");
Serial.println(Pv2);
Serial.print("P3: ");
Serial.println(Pv3);
Serial.print("P4: ");
Serial.println(Pv4);
Serial.print("P5: ");
Serial.println(Pv5);
Serial.print("P6: ");
Serial.println(Pv6);

Serial.print("firetimer: ");
Serial.println(firetimer);
Serial.print("nxtfire: ");
Serial.println(nxtfire);
Serial.print("Millis(): ");
Serial.println(millis());
}
``````

`val = map(val, 0, 255, 30000, 600000);` Which processor/ board are you using?

``````    val = map(val, 0, 255, 30000, 600000); //30000 millisec --> 600000 millisecconds
``````

"val" is an int, and cannot exceed 32767 on an AVR based Arduino.

It’s an Arduino Uno, that declares a lot. I’ll make it an unsigned long I guess?

Or calculate in seconds or desciseconds and then multiply up with a unsigned long.

Also you should never add times...

``````   firetimer = val;
nxtfire = lastfire + firetimer;
``````

Only ever subtract...

``````fireinterval = val;
....
if (time - lastfire >= fireinterval) {
``````

Why could i not add timers?

Why could i not add timers?

There are issues with millis() rollover.

See this discussion
https://www.gammon.com.au/millis