How to minus millis

HI, so I’ve written a code for a little tester project that will be Incorporated into a bigger project. However, I have ran into a bit of a problem.
I want to be able to minus the millis number printed on the LCD when u press a button.
This is what I have so far however it doesn’t seem to be working.

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(14, 15, 16, 17, 18, 19);

int redpin =5; //Pin 5
int greenpin =4; //Pin 4
int bluepin =3; //Pin 3

// set up a constant for the tilt switchPin
const int switchPin = 6;

int hits = 0;

// variable to hold the value of the switchPin
int switchState = 0;

// variable to hold previous value of the switchpin
int prevSwitchState = 0;

void setup() {
// set up the number of columns and rows on the LCD
lcd.begin(16, 2);

// set up the switch pin as an input
pinMode(switchPin,INPUT);
pinMode(redpin, OUTPUT);
pinMode(greenpin, OUTPUT);
pinMode(bluepin, OUTPUT);
setColor(255, 255, 255);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Shoot the”);
lcd.setCursor(0, 1);
lcd.print(“zombies”);
delay(3000);
lcd.clear();
delay(25);
}

void loop() {
// check the status of the switch
switchState = digitalRead(switchPin);

lcd.setCursor(11,0);
lcd.print(“Time:”);
lcd.setCursor(11,1);
lcd.print(millis() / 1000);

lcd.setCursor(0, 0);
lcd.print(“Hits:”);
// compare the switchState to its previous state
if (switchState != prevSwitchState) {
if (switchState == HIGH) {
hits = hits + 1;
lcd.setCursor(11, 1);
lcd.print(millis() /-5000); // this is where the problem is at
lcd.setCursor(0, 1);
lcd.print(hits);
setColor(0, 255, 255);
delay(400);
setColor(255, 255, 255);
}
}
// save the current switch state as the last state
prevSwitchState = switchState;
}
void setColor(int red, int green, int blue)
{
#ifdef COMMON_ANODE
red = -255 - red;
green = -255 - green;
blue = -255 - blue;
#endif
analogWrite(redpin, red);
analogWrite(greenpin, green);
analogWrite(bluepin, blue);
}

Why are you dividing millis() by a negative number? Bear in mind millis returns a unsigned long value.

Use a "ul" after any constant you use with an unsigned long. Like this:

lcd.print(millis() /5000ul); // this is where the problem is at

So I added that into the code, however it now prints a new number next to Hits: when i press the button after its been 5 seconds.

Well, yes. Millis() return value changes every millisecond. What exactly do you expect your code to do?

Please go back and edit your post. Before copying the code from the IDE press ctrl+T. How does that look? Better, ain't it? Now post it within [code ]tags. See the "How to use this forum"-post.

Now back to the problem

SurferTim:
Use a "ul" after any constant you use with an unsigned long. Like this:

Not needed. Because millis() already return a unsigned long the compiler will already use a unsigned long for the calculation.

And what do you want to print? A negative number of millis in seconds?

lcd.print('-');
lcd.print(millis() / 1000);

Or do you want to print millis in seconds minus 5 seconds?

lcd.print(millis() / 1000 - 5);

So be a bit clearer.

miliis() is unsigned long.
-5000 is signed int

The C++ language promotes signed quanitites to unsigned quantities whenever they are compared with each other. It also promotes integers to longs.

I'm not certain in which order these are done. If the conversion to long gets done first, the resulting value will be 4294962296, If the conversion to unsigned gets done first, then the value will be 5000.

In any case:

((long)millis)/-5000

But that will break over time (+-25 days)..

If you really want to do millis() /-5000 but only print it just do millis() / 5000 (which does not break) and print a '-' in front of it...

PaulMurrayCbr:
I'm not certain in which order these are done. If the conversion to long gets done first, the resulting value will be 4294962296, If the conversion to unsigned gets done first, then the value will be 5000.

I believe the rule is "length first" so the -5000 (0xEC78) gets sign-extended to -5000L (0xFFFFEC78L) and then treated as unsigned 4294962296UL.

Even if the signedness was done first the 0xEC78 would be treated as unsigned (60536U) and then extended to 32 bits (60536UL). In neither case do you get 5000 from -5000.