Skip to main content
Fixed syntax highlighting, capitalization.
Source Link
VE7JRO
  • 2.5k
  • 19
  • 28
  • 31

I have a simplest code to turn on and off a led. varibaleThe variable holding time definition is onTime, using Arduino Nano.

whenWhen it is written as: onTime=1000*30, all is good.

whenWhen onTime is greater than this, if loop does not return true value when needed ( see code below ).

BUT when writing it implicit as onTime=60000  , and not onTime=1000*60 code run OK.

I'm guessing that value of 30*1000 - has to be something with int value.

Appreciate your assistance, Guy

CODE:

int enbPin = 10;
bool onState = true;
unsigned long onPeriod = 60*1000; // <---- 
unsigned long startTime = millis();
void setup() {
  // put your setup code hee, to run once:
  Serial.begin(9600);
  pinMode(enbPin, OUTPUT);
  digitalWrite(enbPin, onState);
  Serial.println("START");
}

void loop() {
  // put your main code here, to run repeatedly:
  if (millis() - startTime >= onPeriod ) {
    onState = !onState;
    startTime = millis();
    digitalWrite(enbPin, onState);
    Serial.println(onState);
  }
}
int enbPin = 10;
bool onState = true;
unsigned long onPeriod = 60*1000; // <---- 
unsigned long startTime = millis();
void setup() {
  // put your setup code hee, to run once:
  Serial.begin(9600);
  pinMode(enbPin, OUTPUT);
  digitalWrite(enbPin, onState);
  Serial.println("START");
}

void loop() {
  // put your main code here, to run repeatedly:
  if (millis() - startTime >= onPeriod ) {
    onState = !onState;
    startTime = millis();
    digitalWrite(enbPin, onState);
    Serial.println(onState);
  }
}

I have a simplest code to turn on and off a led. varibale holding time definition is onTime, using Arduino Nano.

when is written as: onTime=1000*30, all is good.

when onTime is greater than this, if loop does not return true value when needed ( see code below ).

BUT when writing it implicit as onTime=60000  , and not onTime=1000*60 code run OK.

I'm guessing that value of 30*1000 - has to be something with int value.

Appreciate your assistance, Guy

CODE:

int enbPin = 10;
bool onState = true;
unsigned long onPeriod = 60*1000; // <---- 
unsigned long startTime = millis();
void setup() {
  // put your setup code hee, to run once:
  Serial.begin(9600);
  pinMode(enbPin, OUTPUT);
  digitalWrite(enbPin, onState);
  Serial.println("START");
}

void loop() {
  // put your main code here, to run repeatedly:
  if (millis() - startTime >= onPeriod ) {
    onState = !onState;
    startTime = millis();
    digitalWrite(enbPin, onState);
    Serial.println(onState);
  }
}

I have a simplest code to turn on and off a led. The variable holding time definition is onTime, using Arduino Nano.

When it is written as: onTime=1000*30, all is good.

When onTime is greater than this, if loop does not return true value when needed ( see code below ).

BUT when writing it implicit as onTime=60000, and not onTime=1000*60 code run OK.

I'm guessing that value of 30*1000 - has to be something with int value.

CODE:

int enbPin = 10;
bool onState = true;
unsigned long onPeriod = 60*1000; // <---- 
unsigned long startTime = millis();
void setup() {
  // put your setup code hee, to run once:
  Serial.begin(9600);
  pinMode(enbPin, OUTPUT);
  digitalWrite(enbPin, onState);
  Serial.println("START");
}

void loop() {
  // put your main code here, to run repeatedly:
  if (millis() - startTime >= onPeriod ) {
    onState = !onState;
    startTime = millis();
    digitalWrite(enbPin, onState);
    Serial.println(onState);
  }
}
Source Link
guyd
  • 1k
  • 2
  • 26
  • 62

Problem in defining a variable as multiplication

I have a simplest code to turn on and off a led. varibale holding time definition is onTime, using Arduino Nano.

when is written as: onTime=1000*30, all is good.

when onTime is greater than this, if loop does not return true value when needed ( see code below ).

BUT when writing it implicit as onTime=60000 , and not onTime=1000*60 code run OK.

I'm guessing that value of 30*1000 - has to be something with int value.

Appreciate your assistance, Guy

CODE:

int enbPin = 10;
bool onState = true;
unsigned long onPeriod = 60*1000; // <---- 
unsigned long startTime = millis();
void setup() {
  // put your setup code hee, to run once:
  Serial.begin(9600);
  pinMode(enbPin, OUTPUT);
  digitalWrite(enbPin, onState);
  Serial.println("START");
}

void loop() {
  // put your main code here, to run repeatedly:
  if (millis() - startTime >= onPeriod ) {
    onState = !onState;
    startTime = millis();
    digitalWrite(enbPin, onState);
    Serial.println(onState);
  }
}