Math problem "invalid conversion"

Hi

I have a small probably very simple problem I cant figure out. I do some math in my if statements below, but there is some problem with it. Among others I get the following error message "warning: invalid conversion from 'long unsigned int (*)()' to 'long int' [-fpermissive]
StartMillis = millis;
"

Best regards
Rasmus

#include "MegunoLink.h" // Helpful functions for communicating with MegunoLink.
#include <Wire.h>

// Interval (milliseconds) between sending analog data
const unsigned SendInterval = 20; // [ms]
 
// The plot we are sending data to.
TimePlot MyPlot;

// constants won't change. Used here to set pin numbers:
int ControlPin = 6;      // LED connected to digital pin 9
int buttonPin =  12;      // The number of the pin controlled by the button

// Variables will change:
int val = 25;         // variable to store the value calculated based on time passed since the test was started.
long StartMillis = 0; // Millis value when test was started. 
int Flag = 0; // flag to mark if the test should be ongoing


void setup() {
  Wire.begin();
  Serial.begin(9600);
  pinMode(ControlPin, OUTPUT);  // sets the pin as output
  pinMode(buttonPin, INPUT);  // sets the pin as input
}

void loop() {
if (digitalRead(buttonPin) == LOW) {
      delay(1000);
      Serial.println( "New test initiated");
      Flag = 1;
      StartMillis = millis;
    }

if (Flag == 1) {
      val = floor(((millis - StartMillis) / 500) + 25);  // Calculate val based on time elapsed
      analogWrite(ControlPin, val); // AnalogWrite values from 0 to 255
    }  

if ((millis - StartMillis) > 60000) {
  Flag = 0;
  delay (5000);
  val = 25;
  analogWrite(ControlPin, val); // AnalogWrite values from 0 to 255
  }
}

Please follow the advice given in the link below when posting code. Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Thanks for adding the code tags

millis() returns an unsigned long
You have StartMillis declared as a signed long

Please post the complete error messages (using code tags) copied from the IDE using the "Copy Error message" button

Arduino: 1.8.13 (Windows 10), Board: "Arduino Uno"





















C:\Users\rafb\AppData\Local\Temp\untitled145637832.tmp\sketch_sep15a\sketch_sep15a.ino: In function 'void loop()':

C:\Users\rafb\AppData\Local\Temp\untitled145637832.tmp\sketch_sep15a\sketch_sep15a.ino:32:21: warning: invalid conversion from 'long unsigned int (*)()' to 'long int' [-fpermissive]

       StartMillis = millis;

                     ^~~~~~

C:\Users\rafb\AppData\Local\Temp\untitled145637832.tmp\sketch_sep15a\sketch_sep15a.ino:36:28: warning: pointer to a function used in arithmetic [-Wpointer-arith]

       val = floor(((millis - StartMillis) / 500) + 25);  // Calculate val based on time elapsed

                     ~~~~~~~^~~~~~~~~~~~~

sketch_sep15a:36:43: error: invalid operands of types 'long unsigned int (*)()' and 'int' to binary 'operator/'

       val = floor(((millis - StartMillis) / 500) + 25);  // Calculate val based on time elapsed

                    ~~~~~~~~~~~~~~~~~~~~~~~^~~~~

C:\Users\rafb\AppData\Local\Temp\untitled145637832.tmp\sketch_sep15a\sketch_sep15a.ino:40:13: warning: pointer to a function used in arithmetic [-Wpointer-arith]

 if ((millis - StartMillis) > 60000) {

      ~~~~~~~^~~~~~~~~~~~~

C:\Users\rafb\AppData\Local\Temp\untitled145637832.tmp\sketch_sep15a\sketch_sep15a.ino:40:30: warning: ISO C++ forbids comparison between pointer and integer [-fpermissive]

 if ((millis - StartMillis) > 60000) {

                              ^~~~~

exit status 1

invalid operands of types 'long unsigned int (*)()' and 'int' to binary 'operator/'



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

if ((millis - StartMillis) > 60000)

millis is a function so this should be

if ((millis() - StartMillis) > 60000)

Thanks. That cleared all the errors.

I changed StartMillis to be an unsigned long also, but that may not have been necessary? Will it change automatically when I assign millis() to it?

You are probably OK mixing types as the compiler will promote data types but why not do it right ?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.