I have a code that works fine with board D1, but with Arduino Ono the number of the counter becomes negative or a weird number comes out.
How do I match the definition of the variables to a setting that would fit Arduino Ono?
const int ledPin = 13;// the number of the LED pin
int MainLinePin = 3; // Pin turns off main review and turns on after 3 seconds
int RiversPin = 4;// Pin turns off the inverter circuit or turns it on
boolean start_timer = true;
//Opening settings
int ledState = LOW; // Opening the LED mode
int MainLinePin_State = LOW; // Opening the switch mode
int RiversPin_State = HIGH; // Opening the inverter circuit
unsigned long previousMillis = 0; // Opening of Remembrance Hour
long timer_nowe = 0;
const long interval1 = 1000*60*1;
const long interval2 =interval1 + 1000*60*1;
const long interval3 =interval2 + 1000*60*1;
const long interval4 =interval3 + 1000*60*1;
const long interval5 =interval4 + 1000*60*1;
const long interval6 =interval5 + 1000*60*1;
const long interval7 =interval6 + 1000*60*1;
const long interval8 =interval7 + 1000*60*1;
const long interval9 =interval8 + 1000*60*1;
const long interval10 =interval9 + 1000*60*1;
int State = 0; // Situational variable - When basic conditions are met, the process will start working
int blink_limit = 1000;
int blink_timer = 0;
void setup() {
pinMode(ledPin, OUTPUT); // Set the LED review port
ledState = HIGH; // Active control light
pinMode(MainLinePin, OUTPUT); // Set the port of the main control switch
pinMode(RiversPin, OUTPUT); // Set the output of the inverter circuit
Serial.begin(9600);
}
void loop() {
unsigned long currentMillis = millis(); // Get the current time
timer_nowe = currentMillis - previousMillis;
// Check if the basic conditions for entering the process have been reached
if (start_timer == true){
if(State==0 && timer_nowe >= interval1){
Serial.println("Case number 1");Serial.print("Because");Serial.print(timer_nowe);Serial.print(" It's more than"); Serial.println(interval1);
State = 1; // The situation changes to a number ....
}
if(State==1 && timer_nowe >= interval2){
Serial.println("Case number 2");Serial.print("Because");Serial.print(timer_nowe);Serial.print(" It's more than"); Serial.println(interval2);
State = 2; // The situation changes to a number ....
}
if(State==2 && timer_nowe >= interval3){
Serial.println("Case number 3");Serial.print("Because");Serial.print(timer_nowe);Serial.print(" It's more than");Serial.println(interval3);
State = 3; // The situation changes to a number ....
}
if(State==3 && timer_nowe >= interval4){
Serial.println("Case number 4");Serial.print("Because"); Serial.print(timer_nowe);Serial.print(" It's more than");Serial.println(interval4);
State = 4; // The situation changes to a number ....
}
if(State==4 && timer_nowe >= interval5){
Serial.println("Case number 5"); Serial.print("Because"); Serial.print(timer_nowe);Serial.print(" It's more than");Serial.println(interval5);
State = 5; // The situation changes to a number ....
}
if(State==5 && timer_nowe >= interval6){
Serial.println("Case number 6"); Serial.print("Because"); Serial.print(timer_nowe);Serial.print(" It's more than");Serial.println(interval6);
State = 6; // The situation changes to a number ....
}
if(State==6 && timer_nowe >= interval7){
Serial.println("Case number 7"); Serial.print("Because"); Serial.print(timer_nowe);Serial.print(" It's more than");Serial.println(interval7);
State = 7; // The situation changes to a number ....
}
if(State==7 && timer_nowe >= interval8){
Serial.println("Case number 8"); Serial.print("Because"); Serial.print(timer_nowe);Serial.print(" It's more than");Serial.println(interval8);
State = 8; // The situation changes to a number ....
}
if(State==8 && timer_nowe >= interval9){
Serial.println("Case number 9"); Serial.print("Because"); Serial.print(timer_nowe);Serial.print(" It's more than");Serial.println(interval9);
State = 9; // The situation changes to a number ....
}
if(State==9 && timer_nowe >= interval10){
Serial.println("Case number 10"); Serial.print("Because"); Serial.print(timer_nowe);Serial.print(" It's more than");Serial.println(interval10);
State = 10; // The situation changes to a number ....
State = 0; //The situation changes to a number ....
previousMillis = currentMillis;blink_timer=0;
start_timer = false; //Switching off the meter
// Serial.println("Reset performed");
}
// delay(25);
//------------------------------------------------אם -- אז----------------------------------------------------------------------------
if (State == 0){
//LED flashes to indicate "0" mode
if (timer_nowe + blink_limit >= blink_timer){ // will enter activity if the target time has elapsed
blink_timer=timer_nowe+blink_limit+blink_limit; // Immediately sets the next target time
if (ledState == HIGH){
Serial.println("LED off");
//MainLinePin_State = LOW;
// pinMode(MainLinePin, OUTPUT);
digitalWrite(MainLinePin, LOW);
ledState = LOW;
}else{
Serial.println("LED on");
digitalWrite(MainLinePin, HIGH);
//MainLinePin_State =HIGH;
ledState = HIGH;
}
}
}
}
//go!
digitalWrite(ledPin, ledState); // Send the command to LED
digitalWrite(MainLinePin, MainLinePin_State); //Send command to main switch
digitalWrite(RiversPin, RiversPin_State); //Send a command to an inverter circuit
}
Usually, such problems are because the ESP32 and ESP8266 have 32-bit 'int' while the Arduno UNO has 16-bit 'int'. Look for any numbers or calculations that may go over 32767 (the largest that fits in a 16-bit signed int). In your case: 1000 * 60 was the problem.
Friends - you are something special !!!
Well done!
Maybe someone in the future could benefit from it.... I hope you agree to accept the revised code or comment on it [there is always something to improve!]:
const int ledPin = 13;// the number of the LED pin
int MainLinePin = 3; // Pin turns off main review and turns on after 3 seconds
int RiversPin = 4;// Pin turns off the inverter circuit or turns it on
boolean start_timer = true;
//Opening settings
int ledState = LOW; // Opening the LED mode
int MainLinePin_State = LOW; // Opening the switch mode
int RiversPin_State = HIGH; // Opening the inverter circuit
unsigned long previousMillis = 0; // Opening of Remembrance Hour
unsigned long timer_nowe = 0UL;
const unsigned long interval1 = 1000UL*60*0.5; // Half a minute
const unsigned long interval2 =interval1 + 1000UL*60*0.2; //7 seconds
const unsigned long interval3 =interval2 + 1000UL*60*1; // Minute
const unsigned long interval4 =interval3 + 1000UL*60*0;
const unsigned long interval5 =interval4 + 1000UL*60*1;
const unsigned long interval6 =interval5 + 1000UL*60*0.5;
const unsigned long interval7 =interval6 + 1000UL*60*0;
const unsigned long interval8 =interval7 + 1000UL*60*0.3; //20 seconds
const unsigned long interval9 =interval8 + 1000UL*60*1;
const unsigned long interval10 =interval9 + 1000UL*60*1;
int State = 0; // Situational variable - When basic conditions are met, the process will start working
int blink_limit = 780;
unsigned long Next_visit_to_blink =0UL;
void setup() {
pinMode(ledPin, OUTPUT); // Set the LED review port
ledState = HIGH; // Active control light
pinMode(MainLinePin, OUTPUT); // Set the port of the main control switch
pinMode(RiversPin, OUTPUT); // Set the output of the inverter circuit
Serial.begin(9600);
}
void loop() {
unsigned long currentMillis = millis(); // Get the current time
timer_nowe = currentMillis - previousMillis;
// Check if the basic conditions for entering the process have been reached
if (start_timer == true){
if(State==0 && timer_nowe >= interval1){
Serial.print("Case number 1");Serial.print(" Because ");Serial.print(timer_nowe);Serial.print(" is more than "); Serial.println(interval1);
State = 1; // The situation changes to a number ....
}
if(State==1 && timer_nowe >= interval2){
Serial.print("Case number 2");Serial.print(" Because ");Serial.print(timer_nowe);Serial.print(" is more than "); Serial.println(interval2);
State = 2; // The situation changes to a number ....
}
if(State==2 && timer_nowe >= interval3){
Serial.print("Case number 3");Serial.print(" Because ");Serial.print(timer_nowe);Serial.print(" is more than ");Serial.println(interval3);
State = 3; // The situation changes to a number ....
}
if(State==3 && timer_nowe >= interval4){
Serial.print("Case number 4");Serial.print(" Because "); Serial.print(timer_nowe);Serial.print(" is more than ");Serial.println(interval4);
State = 4; // The situation changes to a number ....
}
if(State==4 && timer_nowe >= interval5){
Serial.print("Case number 5"); Serial.print(" Because "); Serial.print(timer_nowe);Serial.print(" is more than ");Serial.println(interval5);
State = 5; // The situation changes to a number ....
}
if(State==5 && timer_nowe >= interval6){
Serial.print("Case number 6"); Serial.print(" Because "); Serial.print(timer_nowe);Serial.print(" is more than ");Serial.println(interval6);
State = 6; // The situation changes to a number ....
}
if(State==6 && timer_nowe >= interval7){
Serial.print("Case number 7"); Serial.print(" Because "); Serial.print(timer_nowe);Serial.print(" is more than ");Serial.println(interval7);
State = 7; // The situation changes to a number ....
}
if(State==7 && timer_nowe >= interval8){
Serial.print("Case number 8"); Serial.print(" Because "); Serial.print(timer_nowe);Serial.print(" is more than ");Serial.println(interval8);
State = 8; // The situation changes to a number ....
}
if(State==8 && timer_nowe >= interval9){
Serial.print("Case number 9"); Serial.print(" Because "); Serial.print(timer_nowe);Serial.print(" is more than ");Serial.println(interval9);
State = 9; // The situation changes to a number ....
}
if(State==9 && timer_nowe >= interval10){
Serial.print("Case number 10"); Serial.print(" Because "); Serial.print(timer_nowe);Serial.print(" is more than ");Serial.println(interval10);
State = 10; // The situation changes to a number ....
State = 0; //The situation changes to a number ....
previousMillis = currentMillis;
timer_nowe=0;
Next_visit_to_blink = 0;
// start_timer = false; //Switching off the meter
Serial.println("Reset performed");
}
//---------------------------------------------------------------- if -- then -----------------------------------------
if (State == 0){
//LED flashes to indicate "0" mode
// delay(450);
if (timer_nowe >= Next_visit_to_blink){
Next_visit_to_blink = timer_nowe + blink_limit;
if (ledState == HIGH){
Serial.println("LED off");
ledState = LOW;
}else{
Serial.println("LED on");
ledState = HIGH;
}
}
}
}
//go!
digitalWrite(ledPin, ledState); // Send the command to LED
digitalWrite(MainLinePin, MainLinePin_State); //Send command to main switch
digitalWrite(RiversPin, RiversPin_State); //Send a command to an inverter circuit
}
Anywhere where you start numbering variables, start thinking arrays. Note that uint32_t is the same as unsigned long, just less typing and architecture independent.
You can use NUM_ELEMENTS to determine the number of elements in an array of any type (characters, integers, structs, classes etc). Typical usage would be