- define variables, `ugly` initialisation -

Hello Arduinios! :wink:
I'm new here (three days) but the virus is planted - Arduino will not go out of my life... :wink:

To not to interrupt the loop() I do not place delay-statements.
Instead, I take the current micros in the loop, and then check in the subroutines/functions, if the timestamp of the last execution has passed a certain amount of time before I reexecute the function.
This amount of time I store in a variable of datatyp long. Because I'd like to quickli see how long the delay will be, I begann to notate the initialisation as follows:

static long WhatEver = 1000 * 1000 * 60   // the main-loop is made to take micros() - this way I see, that this function should run ca. every 60sec

After some hours of debugging I discovered why my programm didn't run as expected:

  void test(long testit) {
  long Delay1 = 1000000;
  long Delay2 = 10 * 100000;
  long Delay3 = 100 * 10000;    // not a good idea...
  long Delay4 = 1000 * 1000;    // not a good idea...
  long Delay5 = 10000 * 100;    // not a good idea...
  long Delay6 = 100000 * 10;
  long Delay7 = 1000000 * 1;
  long Delay8 = 1000000 * testit;
  long Delay9 = 100000 * testit;
  long DelayA = 10000 * testit;
  long DelayB = 1000 * testit;
  long DelayC = 100 * testit;
  long DelayD = 10 * testit;
  long DelayE = 1 * testit;
 
 
  
  long DelayF = testit * 10000;
  long DelayG = testit * 1000;
  long DelayH = testit * 100;
  long DelayI = testit * 10;
  long DelayJ = testit * 1;
 
  long a1 = 1;
  long a2 = 10;
  long a3 = 100;
  long a4 = 1000;
  long a5 = 10000;

  long DelayK = 10000 * a1;
  long DelayL = 1000 * a2;
  long DelayM = 100 * a3;
  long DelayN = 10 * a4;
  long DelayO = 1 * a5;
 
 Serial.print("----Delay 1 : ");
 Serial.println(Delay1);
 Serial.print("----Delay 2 : ");
 Serial.println(Delay2);
 Serial.print("----Delay 3 : ");
 Serial.println(Delay3);
 Serial.print("----Delay 4 : ");
 Serial.println(Delay4);
 Serial.print("----Delay 5 : ");
 Serial.println(Delay5);
 Serial.print("----Delay 6 : ");
 Serial.println(Delay6);
 Serial.print("----Delay 7 : ");
 Serial.println(Delay7);
 Serial.print("----Delay 8 : ");
 Serial.println(Delay8);
 Serial.print("----Delay 9 : ");
 Serial.println(Delay9);
 Serial.print("----Delay A : ");
 Serial.println(DelayA);
 Serial.print("----Delay B : ");
 Serial.println(DelayB);
 Serial.print("----Delay C : ");
 Serial.println(DelayC);
 Serial.print("----Delay D : ");
 Serial.println(DelayD);
 Serial.print("----Delay E : ");
 Serial.println(DelayE);
 Serial.print("----Delay F : ");
 Serial.println(DelayF);
 Serial.print("----Delay G : ");
 Serial.println(DelayG);
 Serial.print("----Delay H : ");
 Serial.println(DelayH);
 Serial.print("----Delay I : ");
 Serial.println(DelayI);
 Serial.print("----Delay J : ");
 Serial.println(DelayJ);
 Serial.print("----Delay K : ");
 Serial.println(DelayK);
 Serial.print("----Delay L : ");
 Serial.println(DelayL);
 Serial.print("----Delay M : ");
 Serial.println(DelayM);
 Serial.print("----Delay N : ");
 Serial.println(DelayN);
 Serial.print("----Delay O : ");
 Serial.println(DelayO);
  
}

The arduino seams to interpret my 1000 * 1000 as BIN (or whatever) and calculates the result not to what I tried to do...

Is that:

  • my fault (shoulden't I initialize a variable this way)?
  • a missinterpretation of the IDE 1.0.1 ?
  • a fault in the arduino leonardo V3?

It's not realy a problem - just a little question in the faszinating univers of Arduino!! ;-))

Thank you
Thomas

ps: sorry about my english....

It's always a good idea to post the full code you're having problems with. :slight_smile:

The maximum integer is 65535 as int's nd int expressions are by default 16 bit. For expressions that are to yield long values you should append a letter L to at least one of the constants:  long Delay3 = 100 * 10000L; 

Otherwise the compiler sees an int value 100, an int value 10000, multiplies then in 16-bit arithmetic, giving a wrong result, and only then converts to a long. The L forces the constant to be long, and int x long gives long so the result is calculated in 32-bit arithmetic correctly.

The compiler treats constants as int by default. If you're using larger types, you will need to designate your constants appropriately. For long, use an 'L' suffix, i.e. 1000L, not 1000. Unsigned long is 'UL'.

Hello MarkT,

Your explanation sounds good - thank you.

The result from my debugging code-snippet is then (without the L ):
----Delay 1 : 1000000
----Delay 2 : 1000000
----Delay 3 : 16960 ->> not what I meant...
----Delay 4 : 16960 ->> not what I meant...
----Delay 5 : 16960 ->> not what I meant...
----Delay 6 : 1000000
----Delay 7 : 1000000

I did another test with your L - and that is was I found out:

int gugus1 = 10000;
int gugus2 = 10;
long microlis1  = 0;
long microlis2  = 0;
long microlis3  = 0;
long microlis4  = 0;
long microlis5  = 0;
long microlis6  = 0;
long microlis7  = 0;


void loop() {
 microlis1 = gugus1 * gugus2 * 1L; 
 microlis2 = gugus1 * 1L * gugus2; 
 microlis3 = 1L * gugus1 * gugus2; 
 microlis4 = long(gugus1 * gugus2); 
 microlis5 = long(gugus1) * long(gugus2); 
 microlis6 = 1L * long(gugus1) * gugus2; 
 microlis7 = 1L * gugus1 * long(gugus2); 
 
  
 Serial.print("GUGUS  1: ");
 Serial.println(gugus1);
 Serial.print("GUGUS  2: ");
 Serial.println(gugus2);
 Serial.print("Microlis 1: ");
 Serial.println(microlis1);
 Serial.print("Microlis 2: ");
 Serial.println(microlis2);
 Serial.print("Microlis 3: ");
 Serial.println(microlis3);
 Serial.print("Microlis 4: ");
 Serial.println(microlis4);
 Serial.print("Microlis 5: ");
 Serial.println(microlis5);
 Serial.print("Microlis 6: ");
 Serial.println(microlis6);
 Serial.print("Microlis 7: ");
 Serial.println(microlis7);
 
 
 delay(1000);
}

The result looks like this:

GUGUS 1: 10000
GUGUS 2: 10
Microlis 1: -31072 // seams that the compiler doesn't look at my L on the third position....
Microlis 2: 100000
Microlis 3: 100000
Microlis 4: -31072 // is as you explained - the long() is explizit called and doesn't change the manner how the calculation within the braces is done
Microlis 5: 100000
Microlis 6: 100000 // same as 3
Microlis 7: 100000 // same as 3

... so, I know that I don't know mutch in programming C - it seams I've to bee very carefull on datatypes and calculations between them...

Greetings from switzerland
Thomas

If you want something to be long, better to declare it long in the first place. However, you can cast if necessary so this:

microlis1 = gugus1 * gugus2 * 1L;

should work like this:

microlis1 = (long)gugus1 * (long)gugus2 * 1L;

Hello to all,

Thank you for your help - I'll redo my source-code... ;-))

Till the next time!
Thomas

The maximum integer is 65535 32767 as int's and int expressions are by default signed 16 bit.

Some minor corrections.

Time variables should be stored as unsigned long. Also make sure you code your time handling to deal with rollover correctly. The following are not equivalent:

// good
if(millis() - lastMillis > interval)
{
    lastMillis = millis();
    ...
// bad
if(millis() > nextMillis)
{
    nextMillis = millis() + interval;
    ...