Void Loop not picking Global variable

In the attached sketch I am trying to access the global variable " offtimE" from within the “Void Loop”.

Within the " Remote loop’ the variable is correct but within the “Void loop” it is always “0”.

I know t I am failing to see the obvious but could someone please point out my error?

Thanks

//...   TX SENDER   ............
#include <SPI.h>
#include <LoRa.h>
//...    RTC   DS 3231   ........
#include <Wire.h>
#include <ds3231.h>
struct ts t;

//...   SET UP IR REMOTE   ......
#include <IRremote.h>
const int RECV_PIN = 12;
IRrecv irrecv(RECV_PIN);
decode_results results;
//...   LORA ADDRESSES   .........
byte localAddress = 0xAA;
byte destinationAddress = 0xBB;
//...   GLOBAL VARIABLES   .......
long lastSendTime = 0;
long lastSendTime1 = 0;
unsigned int interval = 10000;  //.......MILLIS INTERVAL................................
unsigned int interval1 = 0;
int  staTM = 0;   //    .................Manually Activated Station.....................
int runtimE = 2;  //    .................RUN TIME FOR MANUAL STATION IN MINUTES.........
int offtimE = 0;  //    .................Off Time for MANUAL STATION TURNOFF................
int staT = 0;
int daY      = 0; int houR     = 0; int miN      = 0; int seC      = 0;

void setup() {
  Serial.begin(9600);
  //...     IR REMOTE      ........
  irrecv.enableIRIn(); // Start the receiver
  irrecv.blink13(true);
  //...     RTC START         ............

  Wire.begin();
  DS3231_init;
  //...      SET TIME   ...........
  // t.hour = 19; t.min = 33; t.sec = 5; t.mday = 1; t.mon = 2; t.year = 2021;
  //DS3231_set(t);
  //............Print Date at Startup................................
  DS3231_get(&t);
  daY      = (t.mday); houR     = (t.hour); miN      = (t.min); seC      = (t.sec);
  Serial.print(daY); Serial.print(":"); Serial.print(houR); Serial.print(":"); Serial.print(miN); Serial.print(":"); Serial.println(seC);


  //...   STARTING LORA   ................
  Serial.println("Start LoRa duplex  TX  Sender");
  if (!LoRa.begin(915E6)) {
    Serial.println("LoRa init failed. Check your connections.");
    while (true) {}
  }
}
//...   READING REMOTE   ...........
void remotE() {
  DS3231_get(&t);
  // Serial.println(results.value, HEX);




  if (results.value == 0x807F8976) {
    Serial.println("TANK FILL - FRESH"); digitalWrite(23, HIGH); staT = 230;
    staTM = 230; int offtimE = ((t.min) + runtimE);
    if (offtimE >= 60) {
      offtimE = (runtimE - 60);
    }

    Serial.print("   STATM  is   "); Serial.print(staTM); Serial.print("   OFFTIME  IS   "); Serial.println(offtimE);
  }


}
void loop() {

  //...   GET TIME   ...........
  DS3231_get(&t);
  daY      = (t.mday); houR     = (t.hour); miN      = (t.min); seC      = (t.sec);
  //... CHECK REMOTE INPUT   ............
  if (irrecv.decode(&results)) {
    results.decode_type == NEC; remotE(); irrecv.resume(); // Receive the next value
  }
  //............................................................................................... CHECK IF REMOTE OFF IS DUE
  Serial.print("                             VOID  LOOP          TMIN  IS  "); Serial.print(t.min); Serial.print("    OFFTIME is   "); Serial.println(offtimE);
  if (t.min == offtimE) {
    staT = (staTM + 1);
  }
}

Farticus:

void remotE() {

if (results.value == 0x807F8976) {
    Serial.println(“TANK FILL - FRESH”); digitalWrite(23, HIGH); staT = 230;
    staTM = 230; int offtimE = ((t.min) + runtimE);  // << == !!

In the marked line, a new and local variable offtimE is declared, because you wrote “int offtimE”. That’s why the global variable of the same name is out of scope, for the duration of the function remotE() !

Thank you so much for your explanation.

It was a stupid " Cut and Paste " error on my part. :slight_smile:

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