2

I'm playing around with a Neopixel strip and want to be able to enter the amount of minutes I want an LED to do something rather than in milli seconds. So for example,

void loop() {

colorWipe(strip.Color(0, 0, 255), 50);
delay(sConversion(5));

colorWipe(strip.Color(0, 0, 0), 50);
delay(sConversion(5));

In my mind this should then delay the code by 5 minutes instead of doing

void loop() {
colorWipe(strip.Color(0, 0, 255), 50);

delay(300000);

The function I wrote is

sConversion(int minutes) {
    int milli = minutes * 60000;

    return milli
};

When I write this same function in JS it returns the correct value, 300000. But when I run it in the Arduino IDE the blue light never changes.

I have this function outside my void loop() and void setup(). Is that the correct place to put it? I have no errors returned.

Full code sample

void setup() {


strip.begin();
strip.show(); 

}

int sConversion(int minutes) {
    int milli = minutes * 60000;

return milli;
}

void loop() {


colorWipe(strip.Color(0, 0, 255), 50); // Blue
delay(sConversion(1));



colorWipe(strip.Color(102, 204, 0), 50); // Green
delay(300000);
}

void colorWipe(uint32_t c, uint8_t wait) {
for (uint16_t i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
    };
};

Is this the reason?

No other reading of sensors, mathematical calculations, or pin manipulation can go on during the delay function, so in effect, it brings most other activity to a halt.

https://www.arduino.cc/en/Reference/Delay

3 Answers 3

2

Your sConversion returns an int as argument. You need to declare it as unsigned long.

This is what you are lookin for:

unsigned long conv(int x) {
  return x * 600000L;
}
4
  • Perhaps this is backwards. The question was about converting minutes (int) into milliseconds (unsigned). You should put the long as the return type, and keep the int as the parameter type. Also, multiply by 60000, rather than divide. Another commonly accepted type, that is platform neutral is uint16_t, which is an unsigned 16-bit integer. This bypasses the ambiguity of "long", which can by whatever number of bytes the compiler for that architecture decides. Commented Aug 8, 2017 at 18:22
  • @josecanuc My bad. I corrected it.
    – user31481
    Commented Aug 8, 2017 at 18:30
  • @LookAlterno This worked. I did not know about unsigned longs. New to C. Thanks!
    – Alex
    Commented Aug 8, 2017 at 18:55
  • Take a look at the numeric data types in arduino.cc/en/Reference/HomePage and see what each one can hold. Keep that limits in mind when dealing with numeric values.
    – user31481
    Commented Aug 8, 2017 at 19:01
0

Function for converting milliseconds to minutes?

1 min has 60 seconds and 1s has 1000 me.

So take the ms and divide it by 60k you will get its equivalent min.

1
  • While you are rigth, using a function avoids stupid mistakes like writing an extra '0' (or missing one '0'). In a large program, where you have to do that conversion in many places, that is a likely error to occurs, and a nightmare to debug.
    – user31481
    Commented Aug 8, 2017 at 19:44
0
int sConversion(int minutes) {
    int milli = minutes * 60000;

return milli;
}

If minutes is greater than 1, you will overflow the int and not get the right answer. Use unsigned long here instead.

unsigned long sConversion(int minutes) {
    unsigned long milli = minutes * 60000ul;

return milli;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.