SparkFun Forums 

Where electronics enthusiasts find answers.

General project discussion / help
Did you make a robotic coffee pot which implements HTCPCP and decafs unauthorized users? Show it off here!
By ryemac3
#165298
I’m using Sparkfun’s SEN-08554 IR receiver and remote to control a clock that I’m making. However, I’m having a little trouble implementing the code for the IR receiver.

I’m using Ken Sherriff’s library and the code that Sparkfun provided:
Code: Select all
#include <IRremote.h>

int RECV_PIN = 46; //pin 46 for the Mega2560
IRrecv irrecv(RECV_PIN);
decode_results results;
#define A 0x10EFF807

void setup()
{
  Serial.begin(115200);
  irrecv.enableIRIn(); // Start the receiver
}

void loop() 
{
  if (irrecv.decode(&results)) 
  {
    if (results.value == A) 
    {
      Serial.println("A");    
    }
    irrecv.resume();
  }
}
For the ease of reading, I trimmed the code down to just the one “A” button, but all buttons are in the code. Running that code on my Mega works great. So all the hardware is fine. It’s when I add it to my clock sketch that the problems arise.

What I’m trying to do is display my clock code in the main loop (time and date displayed from a DS3231 RTC), and then bring up a settings menu with a button press on the remote. It doesn’t seem to work.

What I’d like is for the menu function to be called when button “A” is pressed, but otherwise run the rest of the code in my main loop:
Code: Select all
void loop() 
{
  if (irrecv.decode(&results)) 
  {
    if (results.value == A) 
    {
      Serial.println("A");  
	buttonAPressed():  //call this function when “A” is pressed
    }
    irrecv.resume();
  }

//no IR codes rec’d, continue displaying main clock display
	//regular clock display code goes here
	//blah
	//blah
	//blah
}

void buttonAPressed {

//display settings menu
	//blah
	//blah
	//blah
}  
However, if I do it like that, my clock code/display shows fine, but buttonAPressed is never called. I can’t figure out why.

If I place my clock display code within “if (irrecv.decode(&results))”, the remote works, but the clock display code is only displayed when a button is pressed. I need that code to run when nothing is pressed! It's like I get what's either in “if (irrecv.decode(&results))” or the main loop, but not both.
Code: Select all
void loop() 
{
  if (irrecv.decode(&results)) 
  {
    if (results.value == A) 
    {
      Serial.println("A");  
	buttonAPressed():  
    }
    irrecv.resume();
  
//regular clock display code here
//blah
//blah
//blah
 }
}

void buttonAPressed {

//display settings menu
	//blah
	//blah
	//blah
}  
I've tried sticking "irrecv.resume();" in various places, but it doesn't seem to make a difference.

I even tried handling the remote in a separate function like this
Code: Select all
void loop() {
  if (irrecv.decode(&results))   {
    handleRemote(&results):  //I think I pass "&results" here?
    }
    irrecv.resume();
  }

//regular clock display code here
//blah
//blah
//blah
 }

void handleRemote() { //or is it void handleRemote(&results)? I think I tried various things without success
if (results.value == A) 
    {
      Serial.println("A");  
	buttonAPressed():  
    }
}
But that still doesn't get called. What am I doing wrong? I've been at this for days and I just can't get the loop to run as normal, but step into another function when a button is pressed.

Here's my full sketch will everything:
Code: Select all
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <DS3231.h>
#include <glcd.h>
#include <fonts/SystemFont5x7.h>
#include <fonts/Arial14.h>             
#include <fonts/Arial_bold_14.h>       
#include <fonts/fixednums7x15.h>       
#include <fonts/fixednums8x16.h>
#include <fonts/fixednums15x31.h>
#include <IRremote.h>

DS3231 RTC; //Create the DS3231 object

//remote
int RECV_PIN = 46; //pin 46 is for the Mega per Timer5 in the IR library
IRrecv irrecv(RECV_PIN);
decode_results results;
#define POWER 0x10EFD827 
#define A 0x10EFF807 
#define B 0x10EF7887
#define C 0x10EF58A7
#define UP 0x10EFA05F
#define DOWN 0x10EF00FF
#define LEFT 0x10EF10EF
#define RIGHT 0x10EF807F
#define SELECT 0x10EF20DF

//temp
int tempC;
int tempF;

//initial condition for menu display
boolean enterNormalMode = true;
boolean enterSettingsMode = false;
boolean enterAlarmMode = false;
boolean chimeOnHour = false;
boolean alarmIsActive = false;

//settings menu
int settingsModeButtonCount;
boolean gmtSelected = false;
boolean dlsSelected = false;
boolean chimeSelected = false;

//alarm menu
int alarmModeButtonCount;
boolean aTimeSelected = false;
boolean activeSelected = false;

//compensations
int TZAdjust; 
int newHours;
int DaylightSavings;
boolean DLSinEffect = false;

//year, month, date, hour, min, sec and week-day(starts from 0 and goes to 6)
//writing any non-existent time-data may interfere with normal operation of the RTC.
//Take care of week-day also.
//DateTime dt(2013, 11, 2, 22, 03, 00, 6);

int hhRTC;
int hhAdj;
int mmRTC;
int ssRTC;
int dayRTC;
int dowRTC;
int monthRTC;
int yearRTC;

int hhGPS;
int newHourGPS;
int mmGPS;
int ssGPS;
int dayGPS;
int monthGPS;
int yearGPS;
int linkStatus;

// If you're using a GPS module:
// Connect the GPS Power pin to 5V
// Connect the GPS Ground pin to ground
// If using software serial (sketch example default):
//   Connect the GPS TX (transmit) pin to Digital 3
//   Connect the GPS RX (receive) pin to Digital 2
// If using hardware serial (e.g. Arduino Mega):
//   Connect the GPS TX (transmit) pin to Arduino RX1, RX2 or RX3
//   Connect the GPS RX (receive) pin to matching TX1, TX2 or TX3

// If you're using the Adafruit GPS shield, change 
// SoftwareSerial mySerial(3, 2); -> SoftwareSerial mySerial(8, 7);
// and make sure the switch is set to SoftSerial

// If using software serial, keep these lines enabled
// (you can change the pin numbers to match your wiring):
//SoftwareSerial mySerial(3, 2);

//Adafruit_GPS GPS(&mySerial);
// If using hardware serial (e.g. Arduino Mega), comment
// out the above six lines and enable this line instead:
Adafruit_GPS GPS(&Serial1);

// Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
// Set to 'true' if you want to debug and listen to the raw GPS sentences. 
#define GPSECHO  true

// this keeps track of whether we're using the interrupt
// off by default!
boolean usingInterrupt = false;
void useInterrupt(boolean); // Func prototype keeps Arduino 0023 happy

int enablePin = 7;

void setup()  
{
  pinMode(enablePin, OUTPUT);
  digitalWrite(enablePin, HIGH); //enable GPS

  // connect at 115200 so we can read the GPS fast enough and echo without dropping chars
  // also spit it out
  Serial.begin(115200);
  Serial.println("Adafruit GPS library basic test!");

  // 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
  GPS.begin(9600);

  // uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  // uncomment this line to turn on only the "minimum recommended" data
  //GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
  // For parsing data, we don't suggest using anything but either RMC only or RMC+GGA since
  // the parser doesn't care about other sentences at this time

  // Set the update rate
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);   // 1 Hz update rate
  // For the parsing code to work nicely and have time to sort thru the data, and
  // print it out we don't suggest using anything higher than 1 Hz

  // Request updates on antenna status, comment out to keep quiet
  GPS.sendCommand(PGCMD_ANTENNA);

  // the nice thing about this code is you can have a timer0 interrupt go off
  // every 1 millisecond, and read data from the GPS for you. that makes the
  // loop code a heck of a lot easier!
  useInterrupt(true);

  delay(1000);
  // Ask for firmware version
  // mySerial.println(PMTK_Q_RELEASE);

  digitalWrite(enablePin, LOW); //disable GPS

  // Initialize the GLCD 
  GLCD.Init();
  Wire.begin(); //I think enabeling the wire library casues the problem
  RTC.begin();
  irrecv.enableIRIn(); // Start the IR receiver

  //unhide to force time setting of the RTC
  // RTC.adjust(dt); //Adjust date-time as defined 'dt' above 
}

// Interrupt is called once a millisecond, looks for any new GPS data, and stores it
SIGNAL(TIMER0_COMPA_vect) {
  char c = GPS.read();
  // if you want to debug, this is a good time to do it!
#ifdef UDR0
  if (GPSECHO)
    if (c) UDR0 = c;  
  // writing direct to UDR0 is much much faster than Serial.print 
  // but only one character can be written at a time. 
#endif
}

void useInterrupt(boolean v) {
  if (v) {
    // Timer0 is already used for millis() - we'll just interrupt somewhere
    // in the middle and call the "Compare A" function above
    OCR0A = 0xAF;
    TIMSK0 |= _BV(OCIE0A);
    usingInterrupt = true;
  } 
  else {
    // do not call the interrupt function COMPA anymore
    TIMSK0 &= ~_BV(OCIE0A);
    usingInterrupt = false;
  }
}

void loop() {
  //remote
  if (irrecv.decode(&results)) {
    if (results.value == POWER) 
    {
      Serial.println("POWER");  
      syncGPSTime();  
    }
    if (results.value == A) 
    {
      Serial.println("A");  
      buttonAPressed();
    }
    if (results.value == B) 
    {
      Serial.println("B"); 
      buttonBPressed();
    }
    if (results.value == C) 
    {
      Serial.println("C");
      buttonCPressed();
    }
    if (results.value == UP) 
    {
      Serial.println("UP");
      buttonUPPressed();
    }
    if (results.value == DOWN) 
    {
      Serial.println("DOWN");
      buttonDownPressed();      
    }
    if (results.value == LEFT) 
    {
      Serial.println("LEFT");
      buttonLeftPressed();
    }
    if (results.value == RIGHT) 
    {
      Serial.println("RIGHT");
      buttonRightPressed();
    }
    if (results.value == SELECT) 
    {
      Serial.println("SELECT");  
      buttonSelectPressed();
    }
    irrecv.resume();
  }
  //////////////////////////////////////////////////////////////
  //RTC
  DateTime now = RTC.now();
  tempC = RTC.getTemperature();
  tempF = (tempC * 9.0)/ 5.0 + 32.0; // Convert C to F

  hhRTC = now.hour(); //used to check AM/PM status
  mmRTC = now.minute();
  ssRTC = now.second(); 

  dowRTC = now.dayOfWeek();
  dayRTC = now.date();
  monthRTC = now.month();
  yearRTC = now.year();

  //pass RTC values into a new variable -
  //this way one variable stores time in 24 hour mode and another that can be converted to 12 hour mode
  //time is monitored 'in the background' in 24 hour mode using the RTC, but is displayed in an adjusted 12-hr format
  hhAdj = hhRTC;

  //convert to 12 hr mode for display
  if (hhRTC < 1) {
    hhAdj = 12;
  }

  if ((hhRTC > 12) && (hhRTC < 24)) {
    hhAdj = hhRTC - 12;
  }

  //DLS compensation
  if (DLSinEffect == false) {
    DaylightSavings = 0; //DSL not in effect, don't add an additional hour
  } 
  else {
    DaylightSavings = 1; //DSL in effect, add an additional hour
  }

  newHours = ((hhAdj) + TZAdjust + DaylightSavings);

  ////////////////////////////////////////////////////////////////////////////////
  //not in menu mode
  //time from RTC
  if (enterNormalMode == true) {
    //hour
    GLCD.SelectFont(fixednums15x31);
    GLCD.CursorTo(0,0);
    if (newHours < 10) {
      GLCD.print("0"); 
    } 
    GLCD.print(newHours);

    //minute
    if (mmRTC < 10) {
      GLCD.print(":0"); 
    } 
    else {
      GLCD.print(":");
    }
    GLCD.print(mmRTC);

    //second
    GLCD.SelectFont(fixednums7x15);
    if (ssRTC < 10) {
      GLCD.print(":0"); 
    } 
    else {
      GLCD.print(":");
    }
    GLCD.print(ssRTC);

    // AM/PM indicator
    GLCD.SelectFont(SystemFont5x7);
    if ((hhRTC > 12) && (hhRTC < 22)) {
      //PM
      GLCD.print(" PM");
    } 
    else {
      //AM
      GLCD.print(" AM"); 
    }

    //second line
    //date
    GLCD.CursorTo(0,4);
    GLCD.SelectFont(Arial14);
    switch(dowRTC){
    case 0: 
      GLCD.print("Sun  ");
      break;
    case 1: 
      GLCD.print("Mon  ");
      break;
    case 2: 
      GLCD.print("Tue  ");
      break;
    case 3: 
      GLCD.print("Wed  ");
      break;
    case 4: 
      GLCD.print("Thu  ");
      break;
    case 5: 
      GLCD.print("Fri  ");
      break;
    case 6: 
      GLCD.print("Sat  ");
      break;
    }

    GLCD.SelectFont(fixednums7x15);
    GLCD.print(monthRTC);
    GLCD.print("/");
    GLCD.print(dayRTC);
    GLCD.print("/");
    GLCD.print(yearRTC - 2000);

    //temp
    GLCD.SelectFont(SystemFont5x7);
    GLCD.print("  ");
    GLCD.SelectFont(fixednums8x16);
    GLCD.print(tempF);
    GLCD.SelectFont(SystemFont5x7);
    GLCD.print("F"); 

    //draw line
    GLCD.DrawLine(0,51,128,51,BLACK);

    //GPS status
    linkStatus = (GPS.fixquality);
    GLCD.SelectFont(SystemFont5x7);
    GLCD.CursorTo(0,7);

    if (linkStatus < 1) {
      GLCD.print("GPS:NO ");
    } 
    else {
      GLCD.print("GPS:YES");
    }

    //alarm
    GLCD.print("  ALARM:");
    if (alarmIsActive == true) {
      GLCD.print("6:30AM");
    } 
    else {
      GLCD.print("OFF");
    }
  }

  ////////////////////////////////////////////////////////////////////////////////
  //display settings menu
  if (enterSettingsMode == true) {
    GLCD.ClearScreen();

    //settings menu header
    GLCD.SelectFont(SystemFont5x7);
    GLCD.CursorTo(0,0);
    GLCD.print("     "); 
    GLCD.SelectFont(Arial_bold_14);
    GLCD.print("SETTINGS"); 
    //draw line
    GLCD.DrawLine(0,12,128,12,BLACK);

    if (settingsModeButtonCount == 3) { //no option highlighted
      gmtSelected = false;
      dlsSelected = false;
      chimeSelected = false;

      GLCD.SelectFont(Arial14);

      //time zone
      GLCD.CursorTo(0,1);
      GLCD.print("GMT:"); 
      GLCD.SelectFont(SystemFont5x7);
      GLCD.print(" "); 
      GLCD.SelectFont(Arial14);
      GLCD.print(TZAdjust); 

      //daylight savings 
      GLCD.CursorTo(0,2);
      GLCD.print("DLS:"); 
      GLCD.SelectFont(SystemFont5x7);
      GLCD.print(" "); 
      GLCD.SelectFont(Arial14);
      if (DLSinEffect == true) {
        GLCD.print("YES");
      } 
      else {
        GLCD.print("NO");
      }

      //chime on hour
      GLCD.CursorTo(0,3);
      GLCD.print("CHIME:");
      GLCD.SelectFont(SystemFont5x7);
      GLCD.print(" "); 
      GLCD.SelectFont(Arial14);
      if (chimeOnHour == true) {
        GLCD.print("YES");
      } 
      else {
        GLCD.print("NO");
      }
    }

    if (settingsModeButtonCount == 2) { //GMT highlighted
      gmtSelected = true;
      dlsSelected = false;
      chimeSelected = false;
      GLCD.SelectFont(Arial_bold_14);

      //time zone
      GLCD.CursorTo(0,1);
      GLCD.print("GMT:"); 
      GLCD.SelectFont(SystemFont5x7);
      GLCD.print(" "); 
      GLCD.SelectFont(Arial_bold_14);
      GLCD.print(TZAdjust); 

      GLCD.SelectFont(Arial14);
      //daylight savings 
      GLCD.CursorTo(0,2);
      GLCD.print("DLS:"); 
      GLCD.SelectFont(SystemFont5x7);
      GLCD.print(" "); 
      GLCD.SelectFont(Arial14);
      if (DLSinEffect == true) {
        GLCD.print("YES");
      } 
      else {
        GLCD.print("NO");
      } 

      //chime on hour
      GLCD.CursorTo(0,3);
      GLCD.print("CHIME:");
      GLCD.SelectFont(SystemFont5x7);
      GLCD.print(" "); 
      GLCD.SelectFont(Arial14);
      if (chimeOnHour == true) {
        GLCD.print("YES");
      } 
      else {
        GLCD.print("NO");
      }

    }

    if (settingsModeButtonCount == 1) { //DLS highlighted
      gmtSelected = false;
      dlsSelected = true;
      chimeSelected = false;
      GLCD.SelectFont(Arial14);

      //time zone
      GLCD.CursorTo(0,1);
      GLCD.print("GMT:"); 
      GLCD.SelectFont(SystemFont5x7);
      GLCD.print(" "); 
      GLCD.SelectFont(Arial14);
      GLCD.print(TZAdjust); 

      GLCD.SelectFont(Arial_bold_14);
      //daylight savings 
      GLCD.CursorTo(0,2);
      GLCD.print("DLS:"); 
      GLCD.SelectFont(SystemFont5x7);
      GLCD.print(" "); 
      GLCD.SelectFont(Arial_bold_14);
      if (DLSinEffect == true) {
        GLCD.print("YES");
      } 
      else {
        GLCD.print("NO");
      }

      //chime on hour
      GLCD.SelectFont(Arial14);
      GLCD.CursorTo(0,3);
      GLCD.print("CHIME:");
      GLCD.SelectFont(SystemFont5x7);
      GLCD.print(" "); 
      GLCD.SelectFont(Arial14);
      if (chimeOnHour == true) {
        GLCD.print("YES");
      } 
      else {
        GLCD.print("NO");
      }
    }

    if (settingsModeButtonCount == 0) { //CHIME highlighted
      gmtSelected = false;
      dlsSelected = false;
      chimeSelected = true;
      GLCD.SelectFont(Arial14);

      //time zone
      GLCD.CursorTo(0,1);
      GLCD.print("GMT:"); 
      GLCD.SelectFont(SystemFont5x7);
      GLCD.print(" "); 
      GLCD.SelectFont(Arial14);
      GLCD.print(TZAdjust); 

      //daylight savings 
      GLCD.CursorTo(0,2);
      GLCD.print("DLS:"); 
      GLCD.SelectFont(SystemFont5x7);
      GLCD.print(" "); 
      GLCD.SelectFont(Arial14);
      if (DLSinEffect == true) {
        GLCD.print("YES");
      } 
      else {
        GLCD.print("NO");
      }

      //chime on hour
      GLCD.SelectFont(Arial_bold_14);
      GLCD.CursorTo(0,3);
      GLCD.print("CHIME:");
      GLCD.SelectFont(SystemFont5x7);
      GLCD.print(" "); 
      GLCD.SelectFont(Arial_bold_14);
      if (chimeOnHour == true) {
        GLCD.print("YES");
      } 
      else {
        GLCD.print("NO");
      }
    }

    //add delay to lower refresh rate on the display (remove blink)
    delay(200);
  }

  ////////////////////////////////////////////////////////////////////////////////
  //display alarm menu
  if (enterAlarmMode == true) {
    GLCD.ClearScreen();

    //settings menu header
    GLCD.SelectFont(SystemFont5x7);
    GLCD.CursorTo(0,0);
    GLCD.print("     "); 
    GLCD.SelectFont(Arial_bold_14);
    GLCD.print("  ALARM"); 
    //draw line
    GLCD.DrawLine(0,12,128,12,BLACK);

    if (alarmModeButtonCount == 2) {//no option highlighted
      aTimeSelected = false;
      activeSelected = false;
      GLCD.SelectFont(Arial14);

      //alarm
      GLCD.CursorTo(0,1);
      GLCD.print("TIME:"); 
      GLCD.SelectFont(SystemFont5x7);
      GLCD.print(" "); 
      GLCD.SelectFont(Arial14);
      GLCD.print("6:30 AM"); 

      //alarm status
      GLCD.CursorTo(0,2);
      GLCD.print("ACTIVE:"); 
      GLCD.SelectFont(SystemFont5x7);
      GLCD.print(" "); 
      GLCD.SelectFont(Arial14);
      if (alarmIsActive == true) {
        GLCD.print("YES");
      } 
      else {
        GLCD.print("NO");
      }
    }

    if (alarmModeButtonCount == 1) {//TIME option highlighted
      aTimeSelected = true;
      activeSelected = false;
      GLCD.SelectFont(Arial14);

      //alarm
      GLCD.CursorTo(0,1);
      GLCD.SelectFont(Arial_bold_14);
      GLCD.print("TIME:"); 
      GLCD.SelectFont(SystemFont5x7);
      GLCD.print(" "); 
      GLCD.SelectFont(Arial_bold_14);
      GLCD.print("6:30 AM"); 

      //alarm status
      GLCD.CursorTo(0,2);
      GLCD.SelectFont(Arial14);
      GLCD.print("ACTIVE:"); 
      GLCD.SelectFont(SystemFont5x7);
      GLCD.print(" "); 
      GLCD.SelectFont(Arial14);
      if (alarmIsActive == true) {
        GLCD.print("YES");
      } 
      else {
        GLCD.print("NO");
      }
    }

    if (alarmModeButtonCount == 0) {//ALARM option highlighted
      aTimeSelected = false;
      activeSelected = true;
      GLCD.SelectFont(Arial14);

      //alarm
      GLCD.CursorTo(0,1);
      GLCD.SelectFont(Arial14);
      GLCD.print("TIME:"); 
      GLCD.SelectFont(SystemFont5x7);
      GLCD.print(" "); 
      GLCD.SelectFont(Arial14);
      GLCD.print("6:30 AM"); 

      //alarm status
      GLCD.CursorTo(0,2);
      GLCD.SelectFont(Arial_bold_14);
      GLCD.print("ACTIVE:"); 
      GLCD.SelectFont(SystemFont5x7);
      GLCD.print(" "); 
      GLCD.SelectFont(Arial_bold_14);
      if (alarmIsActive == true) {
        GLCD.print("YES");
      } 
      else {
        GLCD.print("NO");
      }
    }

    //add delay to lower refresh rate on the display (remove blink)
    delay(200);
  }

} //end of loop

//////////////////////////////////////////////////////////////
//remote functions

//GPS
void syncGPSTime() {

  digitalWrite(enablePin, HIGH);

  // if a sentence is received, we can check the checksum, parse it...
  if (GPS.newNMEAreceived()) {
    // a tricky thing here is if we print the NMEA sentence, or data
    // we end up not listening and catching other sentences! 
    // so be very wary if using OUTPUT_ALLDATA and trytng to print out data
    //Serial.println(GPS.lastNMEA());   // this also sets the newNMEAreceived() flag to false

    if (!GPS.parse(GPS.lastNMEA()))   // this also sets the newNMEAreceived() flag to false
      return;  // we can fail to parse a sentence in which case we should just wait for another
  }

  Serial.print("\nTime: ");
  Serial.print(GPS.hour, DEC); 
  Serial.print(':');
  Serial.print(GPS.minute, DEC); 
  Serial.print(':');
  Serial.print(GPS.seconds, DEC); 
  Serial.print('.');
  Serial.println(GPS.milliseconds);
  Serial.print("Date: ");
  Serial.print(GPS.day, DEC); 
  Serial.print('/');
  Serial.print(GPS.month, DEC); 
  Serial.print("/20");
  Serial.println(GPS.year, DEC);
  Serial.print("Fix: "); 
  Serial.print((int)GPS.fix);
  Serial.print(" quality: "); 
  Serial.println((int)GPS.fixquality); 
  if (GPS.fix) {
    Serial.print("Location: ");
    Serial.print(GPS.latitude, 4); 
    Serial.print(GPS.lat);
    Serial.print(", "); 
    Serial.print(GPS.longitude, 4); 
    Serial.println(GPS.lon);

    Serial.print("Speed (knots): "); 
    Serial.println(GPS.speed);
    Serial.print("Angle: "); 
    Serial.println(GPS.angle);
    Serial.print("Altitude: "); 
    Serial.println(GPS.altitude);
    Serial.print("Satellites: "); 
    Serial.println((int)GPS.satellites);
  }

newHourGPS = ((hhGPS) + TZAdjust + DaylightSavings);

//day of week is hard coded until a calculation can be added
//can I get this from the Time library?
//http://playground.arduino.cc/Code/Time
DateTime dt(yearGPS, monthGPS, dayGPS, newHourGPS, mmGPS, ssGPS, 6);
  
  //add compensation here
  //do not force update if data is invalid
  //
  //
  
  RTC.adjust(dt); 
  delay(10);
  GLCD.ClearScreen();
  
 //turn off GPS module
 digitalWrite(enablePin, LOW);
}

//A
void buttonAPressed() {
  enterNormalMode = true;
  enterSettingsMode = false;
  enterAlarmMode = false;
  GLCD.ClearScreen();
}

//B
void buttonBPressed() {
  //enter menu in default state so no options are highlighted
  settingsModeButtonCount = 3; 

  enterNormalMode = false;
  enterSettingsMode = true;
  enterAlarmMode = false;
  GLCD.ClearScreen();
}

//C
void buttonCPressed() {
  //enter menu in default state so no options are highlighted
  alarmModeButtonCount = 2;

  enterNormalMode = false;
  enterSettingsMode = false;
  enterAlarmMode = true;
  GLCD.ClearScreen();
}

//UP
void buttonUPPressed() {
  if(enterSettingsMode == true) {
    settingsModeButtonCount++;

    if(settingsModeButtonCount == 4) { 
      settingsModeButtonCount = 0;
    }
  }
  if(enterAlarmMode == true) {
    alarmModeButtonCount++;

    if(alarmModeButtonCount == 3) {
      alarmModeButtonCount = 0;
    }
  }
}

//DOWN
void buttonDownPressed() {
  if(enterSettingsMode == true) {
    settingsModeButtonCount--;

    if(settingsModeButtonCount < 0) {
      settingsModeButtonCount = 3;
    }
  }
  if(enterAlarmMode == true) {
    alarmModeButtonCount--;

    if(alarmModeButtonCount < 0) {
      alarmModeButtonCount = 2;
    }
  }
}

//LEFT
void buttonLeftPressed() {
  if(gmtSelected == true) {
    TZAdjust--;
    //time zone changed - resync with GPS clock
    syncGPSTime();
  }

  if(dlsSelected == true) {
    DLSinEffect = !DLSinEffect;
  }

  if(chimeSelected == true) {
    chimeOnHour = !chimeOnHour;
  }

  if(aTimeSelected == true) {
  }

  if(activeSelected == true) {
    alarmIsActive = !alarmIsActive;
  }
}

//RIGHT
void buttonRightPressed() {
  if(gmtSelected == true) {
    TZAdjust++;
    //time zone changed - resync with GPS clock
    syncGPSTime();
  }

  if(dlsSelected == true) {
    DLSinEffect = !DLSinEffect;
  }

  if(chimeSelected == true) {
    chimeOnHour = !chimeOnHour;
  }

  if(aTimeSelected == true) {
  }

  if(activeSelected == true) {
    alarmIsActive = !alarmIsActive;
  }
}

//SELECT
void buttonSelectPressed() {
  //set totally wrong date and time (so that a GPS sync can be tested)
  DateTime dt(2011, 1, 1, 1, 1, 0, 6);
  RTC.adjust(dt); 
  delay(10);
  GLCD.ClearScreen();
}
Last edited by ryemac3 on Wed Nov 06, 2013 12:03 pm, edited 1 time in total.
User avatar
By Ross Robotics
#165306
Those delays are not doing you any good. If your are pressing the button while during a 'delay' it's not going to register it (especially the delay of 30 secs). I will admit I just skimmed over the code, maybe someone who has experience with RTCs will reply.
By ryemac3
#165307
codlink wrote:Those delays are not doing you any good. If your are pressing the button while during a 'delay' it's not going to register it (especially the delay of 30 secs). I will admit I just skimmed over the code, maybe someone who has experience with RTCs will reply.
Correct. I actually took that already. I was thinking that I'd allow the GPS to stream for 30 seconds to allow it enough time to link, but then I realized that that's dumb because it'll jam up the clock for 30 seconds while it's doing it. But that's not the problem. The problem is that I can't call any function from the remote.
User avatar
By Ross Robotics
#165310
Have you tested the remote to see if it's actually working and triggering code?
By ryemac3
#165313
codlink wrote:Have you tested the remote to see if it's actually working and triggering code?
The remote works fine when it's just the blank sketch provided by Sparkfun. I can see A, B, C, etc in the serial monitor. But when I paste that code into my sketch for the clock, it doesn't work. It only works when I embed all my loop code within the "if (irrecv.decode(&results))" code. But that's useless since none of the clock code will run until a button is pressed on the remote. The clock code should run in the loop until an IR code is received, then "if (irrecv.decode(&results))" should capture the code and fire off the corresponding function.
By Mee_n_Mac
#165316
ryemac3 wrote:It only works when I embed all my loop code within the "if (irrecv.decode(&results))" code. But that's useless since none of the clock code will run until a button is pressed on the remote. The clock code should run in the loop until an IR code is received, then "if (irrecv.decode(&results))" should capture the code and fire off the corresponding function.
Have you put some debug Serial.println("got here"); in places below the if (irrecv.decode(&results))
statement to see that the code is running and getting to all the places it should be ?

Something very odd is going on so start by confirming that even the obvious "has to work" code is actually working.
By ryemac3
#165317
Mee_n_Mac wrote:Something very odd is going on so start by confirming that even the obvious "has to work" code is actually working.
Yes, I''ve tried that. That's how I know nothing is being called. No message show up on the serial monitor when all the clock code is in the sketch.

Very strange stuff going on. If I do this, and only this, it totally works:
Code: Select all
#include <IRremote.h>
#include <glcd.h>
#include <fonts/allFonts.h>

int RECV_PIN = 46;
IRrecv irrecv(RECV_PIN);
decode_results results;
#define A 0x10EFF807 
#define B 0x10EF7887
#define C 0x10EF58A7
 
void setup() {
  GLCD.Init();
  Serial.begin(115200);
  irrecv.enableIRIn(); // Start the receiver
  
}
 
void loop() {
  GLCD.SelectFont(System5x7);
  
  if (irrecv.decode(&results)) 
  {

    if (results.value == A) 
    buttonA();
    {
      Serial.println("A");    
    }
    if (results.value == B) 
    {
      Serial.println("B");  
      buttonB();
    }
    if (results.value == C) 
    {
      Serial.println("C");
      buttonC();
    }
   
    irrecv.resume();
  }
}

void buttonA() {
  
  GLCD.print("button A pressed");
}

void buttonB() {
  
  GLCD.print("button B pressed");
}

void buttonC() {
  
  GLCD.print("button C pressed");
}
Each subsequent button press (A, B, or C) calls a function that displays a text message on the LCD. It works. So it's not a hardware thing. Why do these functions get called, but not once I add all my RTC code?
Last edited by ryemac3 on Thu Nov 07, 2013 5:59 am, edited 1 time in total.
By ryemac3
#165321
Ha! I think I figured out where the problem is. If I eliminate this code from my loop, it works:
Code: Select all
DateTime now = RTC.now();
  tempC = RTC.getTemperature();
  tempF = (tempC * 9.0)/ 5.0 + 32.0; // Convert C to F

  hhRTC = now.hour(); //used to check AM/PM status
  mmRTC = now.minute();
  ssRTC = now.second(); 

  dowRTC = now.dayOfWeek();
  dayRTC = now.date();
  monthRTC = now.month();
  yearRTC = now.year();
But then I don't have data from the DS3231 RTC. So it's something in the Wire library that steps on the IR Receiver library. I tried both SDA/SCL ports on the Mega. Again, not a hardware thing.

I searched for a new IR library and found this.

I popped this code it
Code: Select all
void loop()                    
{
   if (My_Receiver.GetResults(&My_Decoder)) {
       My_Decoder.decode();
          switch(My_Decoder.value) {
            case A:   buttonA();    break;
            case B:   buttonB();    break;
            case C:   buttonC();    break;
          }
     My_Receiver.resume();
    }

void buttonA() {
 Serial.println("A"); 
}

void buttonB() {
  Serial.println("B"); 
}

void buttonC() {
  Serial.println("C"); 
}
And so far my RTC is running on screen, and the IR signals are being received and the corresponding functions are being called. Looks like it's working with the new library. It's not perfect and every now and then a code slips, but it's better than it was.

I think there's something between the IR library and the Wire library that don't work well together. I'm going to try and find a way to pull the RTC clock stuff out of the main loop and see if that helps.

In any case, does anyone know what in the Wire library could interfere with the IR library? I wanted to look at the header file but I can't seem to find where the Wire library is on my Mac.