[SOLVED] Can Variable in SETUP be Changed?

I’m using the TimeAlarms library to repeat some actions every few minutes in my sketch. I want the ability to change duration at which Alarm.timerRepeat occurs while the sketch is running by pressing a button. I’m using the R3 Uno with a 1602 LCD/keypad shield programming from windows arduino 1.6.5. I have a lot of other hardware in this project but it’s not necessary to area in which I need help.

Alarm.timerRepeat must be called in setup in order to run properly (As far as I’ve been able to muster). I am trying to figure out if there is any possible way to move it to loop so that it can be changed while the sketch is running. Is this even possible or must Alarm.timerRepeat always be located in setup per its library?

My sketch exceeds the post character limit so I’ll include the portions that have the pertinent code. I will attach my full sketch as well.

#include <LiquidCrystal.h>
#include <DFR_Key.h>
#include <DHT.h>
#include <Time.h>
#include <TimeAlarms.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
DFR_Key keypad;

byte UP[8] = {
  B00000,
  B00100,
  B01110,
  B11111,
  B11111,
  B00000,
  B00000,
  B00000, };

byte DOWN[8] = {
  B00000, 
  B00000,
  B00000,
  B11111,
  B11111,
  B01110,
  B00100,
  B00000, 
};

#define DHTTYPE DHT11   // DHT 11 
const byte sensePin = 2; 
int CurrentButton = 0; //Current button pressed
byte CurrentDisplay = 0; //Keeps track of what screen is currently being displayed. 0 = temp, 1 = humidity, 3 = Watering.
byte TempDisplay = 1;
byte HumidDisplay = 0;
byte LEDDisplay = 0;
byte CleanDisplay = 0;
byte VentDisplay = 0;
byte setTemp = 70; //Default to set the desired temp
byte TempMax = 99;
byte TempMin = 32;
byte setHumid = 85; //Defualt to set the desired humidity
byte HumidMax = 100;
byte HumidMin = 0;
int UV_Duration = 300;
//int UV_DurationMax = 10800;
int UV_DurationMin = 300;
const byte comp = A1; //Output pin to compressor relay 
const byte neb = A2; //Output pin to nebulizer relay 
const byte UV = 12; //Output pin to UV relay
const byte clean = 11; //Output pin to internal cleaning UV lamp
const byte vent = 3; //Output pin to  ventilation fan, CANNOT BE ON MUX
const byte circulation = A4; //Output pin to circulation fan, MUST BE PMW
//const byte waterpump = 11; //Output pin to water pump, MUST BE PMW
const byte LED = 13; //Output pin to maturation LEDs
//const byte levelsensor = A4; //Input pin from water level sensor, CANNOT BE ON MUX
const byte beep = A5;
boolean UVState = LOW;
boolean VentState = LOW;
DHT dht(sensePin, DHTTYPE);

void setup() 
{ 
  pinMode(comp, OUTPUT);
  pinMode(neb, OUTPUT);
  pinMode(UV, OUTPUT);
  pinMode(vent, OUTPUT);
  pinMode(circulation, OUTPUT);
//  pinMode(waterpump, OUTPUT);
  pinMode(LED, OUTPUT);
  pinMode(beep, OUTPUT);
  pinMode(clean, OUTPUT);
//  pinMode(levelsensor, INPUT);
  digitalWrite(LED, LOW);
  analogWrite(circulation, 5);
   
  Alarm.timerRepeat(UV_Duration, UV_On); //Must be in setup to run correctly
 
  lcd.begin(16, 2);
  lcd.createChar(0, UP);
  lcd.createChar(1, DOWN);
  lcd.setCursor(0, 0);
  dht.begin();
}

void  loop()
  {  
   Alarm.delay(0); 
   everything();
  }
  
void everything()
{  
  // ... code omitted for forum post

if (VentDisplay == 1 && UV_Duration == 300)
{
    lcd.setCursor(0, 0);
    lcd.print(F("VENT CYCLES: OFF "));
    lcd.setCursor(0,1);         
    lcd.print(F("Every: "));
    lcd.print(int((UV_Duration / 60)-5));
    lcd.print(F(" mins.  "));
    CurrentDisplay = 4;
}
//****************************************************************************************************
//MENU SYSTEM
//****************************************************************************************************

// Reads which buttons are pressed. 
// No button = 0
// Select button = 1
// Left button = 2
// Up Button = 3
// Down Button = 4
// Right Button = 5

// ... code omitted for forum post

if (CurrentButton == 3 && CurrentDisplay == 4) 
{
    for (int i=0; i<110; i++)
  {
  digitalWrite(beep, HIGH);
   delayMicroseconds(190);
  digitalWrite(beep, LOW);
   delayMicroseconds(75);  
  }
   UV_Duration = UV_Duration + 300;
    lcd.setCursor(0, 0);
    lcd.print(F("PRESS SEL TO SET"));
    lcd.setCursor(0,1);         
    lcd.print(F("Every: "));
    lcd.print(int((UV_Duration / 60)-5));
    lcd.print(F(" mins.  "));
     Alarm.delay(100);
  }

if (CurrentButton == 4 && CurrentDisplay == 4 && UV_Duration > UV_DurationMin) 
{
    for (int i=0; i<110; i++)
  {
  digitalWrite(beep, HIGH);
   delayMicroseconds(190);
  digitalWrite(beep, LOW);
   delayMicroseconds(75);  
  }
   UV_Duration = UV_Duration - 300;
    lcd.setCursor(0, 0);
    lcd.print(F("PRESS SEL TO SET"));
    lcd.setCursor(0,1);         
    lcd.print(F("Every: "));
    lcd.print(int((UV_Duration / 60)-5));
    lcd.print(F(" mins.  "));
     Alarm.delay(100);
  }

if (CurrentButton == 1 && CurrentDisplay == 4) 
{
  Alarm.timerRepeat(UV_Duration, UV_On); //temporarily placed here in an attempt to work it into loop somehow
    lcd.setCursor(0, 0);
    lcd.print(F("VENT CYCLES: SET "));
    lcd.setCursor(0,1);         
    lcd.print(F("Every: "));
    lcd.print(int((UV_Duration / 60)-5));
    lcd.print(F(" mins.  "));
  }    
 } //Ends all the button pressing
} //Ends "void evertyhing"

/*if (digitalRead(levelsensor) == HIGH) 
 {
        analogWrite(waterpump, 60);
  } else 
  {
       digitalWrite(waterpump, LOW);
 */    
  
void UV_On()
 {    
  UVState = !UVState;  
  digitalWrite(UV, UVState);
  Alarm.timerOnce(30, Vent_On);
  Alarm.timerOnce(5*60, Off);
  }
  
void Vent_On()
  {  
     VentState = !VentState;
     digitalWrite(vent, VentState);
  }

void Off()
{
  UVState = !UVState;  
  digitalWrite(UV, UVState);
  VentState = !VentState;
  digitalWrite(vent, VentState);  
}

Incubator_Vent_Cycles.ino (12.6 KB)

alphacheese: I'm using the TimeAlarms library to repeat some actions every few minutes in my sketch. I want the ability to change duration at which Alarm.timerRepeat occurs while the sketch is running by pressing a button. I'm using the R3 Uno with a 1602 LCD/keypad shield programming from windows arduino 1.6.5. I have a lot of other hardware in this project but it's not necessary to area in which I need help.

Alarm.timerRepeat must be called in setup in order to run properly (As far as I've been able to muster). I am trying to figure out if there is any possible way to move it to loop so that it can be changed while the sketch is running. Is this even possible or must Alarm.timerRepeat always be located in setup per its library?

My sketch exceeds the post character limit so I'll include the portions that have the pertinent code. I will attach my full sketch as well.

change your code in setup to capture the Alarm_id

AlarmId_t uvAlarm;
void setup() 
{ 
  uvAlarm = Alarm.timerRepeat(UV_Duration, UV_On); //Must be in setup to run correctly

}

void  loop()
  {  
   Alarm.delay(0); 
   everything();
   if(changeOfAlarmPeriod){
      Alarm.disable(uvAlarm);
      Alarm.write(uvAlarm,newTimeValue);
      changeOfAlarmPeriod = false;
      }

  }

Now that wasn't hard.

Chuck.

Awesome! Thanks for your reply. I'm not very skilled in C so could you help me out just a bit more please :grinning:

I'm going through the library readme and I don't understand how to capture my alarm_id. How do I declare an ID for my uvAlarm?

Compiler spits back: AlarmId_t does not name a type

alphacheese: Awesome! Thanks for your reply. I'm not very skilled in C so could you help me out just a bit more please :grinning:

I'm going through the library readme and I don't understand how to capture my alarm_id. How do I declare an ID for my uvAlarm?

Compiler spits back: AlarmId_t does not name a type

look in the TimeAlarms.h file.

I missed the case of alarmid_t. it is defined here

typedef uint8_t AlarmID_t;

class TimeAlarmsClass{
public: 
  AlarmID_t timerRepeat(time_t value, OnTick_t onTickHandler); // trigger after the given number of seconds continuously
}

the class declaration of timerRepeat() says it returns a AlarmID_t value.

I just got sloppy and did not copy the type name correctly, The compiler likes Exact correct names. one little mistake ;) and nothing works :>

Change my code to

AlarmID_t uvAlarm;

Chuck.

You

are

awesome!

Thank you so much for your help. I really struggle with arduino but I've learned so much in the last year from helpful persons like yourself.

alphacheese: You

are

awesome!

Thank you so much for your help. I really struggle with arduino but I've learned so much in the last year from helpful persons like yourself.

You are welcome for the help, now you Just have to Help someone else.

Pass the knowledge forward!

Chuck.