alarm clock with jostick

Can someone please help me code my project. I am trying to turn a light on and off at specific times. I know how to do this using the time.h and timealarm.h libraries. What I cant figure out is how to set the alarms and time with a grove joystick.

Post your code, and we'll help you with it.

I don't have anything yet. this will be part of a bigger project for controlling the environment for a bearded dragon. I'm having a hard time figuring out what libraries to use.

Sorry, I don't do life-support systems.

This is the easy way to do it. but I would like to be able to change time and alarms using a joystick.

#include <Time.h>
#include <TimeAlarms.h>

#define RELAY_PIN 12

void setup()
{
setTime(16,30,0,1,23,16); // set time hh/mm/ss MM/DD/YY
// create the alarms
Alarm.alarmRepeat(7,00,00, MorningAlarm); // 7:00am every day
Alarm.alarmRepeat(21,00,00,EveningAlarm); // 9:00pm every day

pinMode(RELAY_PIN, OUTPUT);
}

void loop(){
}

// functions to be called when an alarm triggers:
void MorningAlarm(){
digitalWrite(RELAY_PIN, HIGH);
}

void EveningAlarm(){
digitalWrite(RELAY_PIN, LOW);
}

So, tell us about your idea for a joystick user interface.

I was thinking hold joystick button down to enter menu. menu will contain: set time, set alarm 1, and set alarm 2. pressing the joystick will act as a select button and the direction controls would be used to navigate and change times.

Why not just use a rotary encoder? Couple bucks and are insanely easy to implement with the right library (don't even need interrupts if that's your thing).

After warily googling bearded dragon, and finding nothing obscene or scary, I see now why you won’t use a rotary encoder. It’s claws wouldn’t be able to operate it. I’m still curious how it would be able to handle a joystick though.

I bought a grove kit that came with a joystick. I assumed it should be easy to use but I have no idea where to start.

The joystick is just meant to adjust the time and clock. A lot of people use buttons but I am using grove components and was trying to use what I got in my kit.

Is this what you have?

It only have X and Y axes are two ~10k potentiometers which control 2D movement .
http://www.seeedstudio.com/wiki/Grove_-_Thumb_Joystick

/*
  Thumb Joystick demo v1.0
by:http://www.seeedstudio.com
 connect the module to A0&A1 for using;
 */
 
void setup() {
  Serial.begin(9600);
}
 
void loop() {
  int sensorValue1 = analogRead(A0);
  int sensorValue2 = analogRead(A1);
  Serial.print("The X and Y coordinate is:");
  Serial.print(sensorValue1, DEC);
  Serial.print(",");
  Serial.println(sensorValue2, DEC);
  Serial.println(" ");
  delay(200);
}

johnsonsteven1986:
I was thinking hold joystick button down to enter menu. menu will contain: set time, set alarm 1, and set alarm 2. pressing the joystick will act as a select button and the direction controls would be used to navigate and change times.

No joystick button.

yes that's what I have. The joystick and be pressed in and it gives a different value.

Here are the values I get with the joystick.

center: x = 501
y = 509
left: x = 501
y = 270
right: x = 501
y = 743
up: x = 267
y = 509
down: x = 750
y = 509
pressed: x = 1023
y = 509

There is a 5 pins joystick button that have button, but you have only 4 pins type, so no button.

johnsonsteven1986:
yes that's what I have. The joystick and be pressed in and it gives a different value.

When you press the joystick in 2D movement, it gives a different value on the x & y potentiometers.

Here are some menu code not tested.

// Button defination for Keypad shield
#define btnRIGHT  0		// Okay
#define btnUP     1		// inc
#define btnDOWN   2		// dec
#define btnLEFT   3		// Select
#define btnSELECT 4		// Menu
#define btnNONE   5

/*
center:   x = 501    y = 509
left:     x = 501    y = 270
right:    x = 501    y = 743
up:       x = 267    y = 509
down:     x = 750    y = 509
pressed:  x = 1023   y = 509
*/

// define variables
int adc0_key_in = 0;
int adc1_key_in = 0;
int lastDay = 0;
int lastMonth = 0;
int lastYear = 0;
int lastHour = 0;
int lastMinute = 0;
int movementTimer = 0;
int menuOptions = 4;
int menuOption = 0;
int alarmHours = 0;
int alarmMinutes = 0;
bool alarmSet = 0;

// read the buttons from LCD keypad shield
int read_Joy_buttons()
{
  adc0_key_in = analogRead(0);      // read the value from the sensor
  adc1_key_in = analogRead(1);      // read the value from the sensor

  // we add approx 50 to those values and check to see if we are close
  // No button pressed should be x = 501    y = 509
  if (adc0_key_in > 525 && adc0_key_in < 475 && adc1_key_in > 525 && adc1_key_in < 475 ) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
  if (adc0_key_in > 525 && adc0_key_in < 475 && adc1_key_in > 725 && adc1_key_in < 770)   return btnRIGHT; 
  if (adc0_key_in > 245 && adc0_key_in < 295 && adc1_key_in > 525 && adc1_key_in < 475)  return btnUP;
  if (adc0_key_in > 725 && adc0_key_in < 770 && adc1_key_in > 525 && adc1_key_in < 475)  return btnDOWN;
  if (adc0_key_in > 525 && adc0_key_in < 475 && adc1_key_in > 245 && adc1_key_in < 295)  return btnLEFT;
  if (adc0_key_in > 1000 && adc1_key_in > 525 && adc1_key_in < 475)  return btnSELECT;  
  return btnNONE;  // when all others fail, return this...

}


void button_loop()
{
  int button = read_Joy_buttons();
  if (button == btnSELECT)
  {
    selectMenu();
  } 
}

void selectMenu()
{
  int button = 0; 
  menuOption = 1;

  Serial.print("Set/Clear Alarm");  

  while (menuOption <= menuOptions)
  {
    button = read_Joy_buttons();
    if (button == btnSELECT)
    {
      timedBeep(shortBeep,1);   
      menuOption++;

      if (menuOption == 2)
      {

        // clearAlarm feature
        Serial.print("Set/Clear Alarm2");            
      }
      if (menuOption == 3)
      {

        Serial.print("Set Date/Time");            
      }

    } 

    if (button == btnLEFT)
    {
      if (menuOption == 1)
      {

        // clearAlarm feature
        //check for existing alarm
        if (alarmSet)
        {
          clearAlarm();
        }
        else
        {
          setAlarm();
        }
        return;
      }
      if (menuOption == 2)
      {

        // clearAlarm feature
        //check for existing alarm
        if (alarmSet)
        {
          clearAlarm();
        }
        else
        {
          setAlarm();
        }
        return;
      }
      if (menuOption == 3)
      {

        // setDateTime feature
        setDateTime();
        return;
      } 

    }
  }
}  

// clearAlarm feature
void clearAlarm()
{
  int button = 0;
  bool clearIt = true;


  Serial.print("Alarm Set For");

  Serial.print(alarmHours);   
  Serial.print(":");
  Serial.print(alarmMinutes);
  delay(2000);

  Serial.print("Clear Alarm?");

  Serial.print("Yes");  

  while (button != btnSELECT)
  {
    button = read_Joy_buttons();
    if (button == btnUP)
    {

      clearIt = !clearIt;
    }
    if (button == btnDOWN)
    {

      clearIt = !clearIt;
    }
    if (button == btnRIGHT)
    {

      alarmSet = !clearIt;
      if (clearIt)
      {


        Serial.print("Alarm Cleared!");
        delay(2000);
      }
      return; 
    }
    Serial.setCursor(0,1);
    if (clearIt)
    {
      Serial.print("Yes"); 
    }
    else{
      Serial.print("No ");
    }
  }   
}

void setAlarm()
{
  int button = 0;
  // Pass maxCount to getTimerMinutes
  alarmHours = getTimerMinutes("Set Alarm Hour", alarmHours, 23);  
  // Validate alarm hours > 0 and < 24
  if (alarmHours >= 0 && alarmHours < 24)
  {
    // Pass maxCount to getTimerMinutes
    alarmMinutes = getTimerMinutes("Set Minutes", alarmMinutes, 59);
    // allow alarm minutes to be 0
    if (alarmMinutes < 60)
    {


      //display alarm time
      Serial.print(alarmHours);       
      Serial.print(":");
      if (alarmMinutes < 10)
        Serial.print("0");
      Serial.print(alarmMinutes);
      if (button == btnRIGHT)
      {

        alarmSet = true;   

        Serial.print("Alarm Set for");
        delay(1000);
        return;       
      }
      else
      {
        timerCancelled("Alarm");
        return;  
      }   
    }
    else
    {
      timerCancelled("Alarm");     
    }    
  }     
  else
  {
    timerCancelled("Alarm");       
  }
}

// setDateTime feature
void setDateTime()
{
  int button = 0;

  //get month
  int setMonth = getTimerMinutes("Set Month", lastMonth, 12);
  if (setMonth > 0 && setMonth < 13)
  {
    //get day
    // default day and hour settings on set date/time
    int setDay = getTimerMinutes("Set Day", lastDay, 31);
    if (setDay > 0 && setDay < 32)
    {
      //get year
      int setYear = getTimerMinutes("Set Year", lastYear, 2999);
      if (setYear > 2000 && setYear < 3000)
      {
        //get hour
        int thisHour = lastHour;
        // default day and hour settings on set date/time
        int setHour = getTimerMinutes("Set Hour", thisHour, 23);
        if (setHour >= 0 && setHour < 24)
        {
          //get minutes
          int setMinute = getTimerMinutes("Set Minute", lastMinute, 59);
          if (setMinute < 60)
          {
             // RTC.adjust(DateTime(setYear,setMonth,setDay,setHour,setMinute)); // for DS1307
                setTime(setHour, setMinute, 0, setDay, setMonth, setYear); // sec, min, hour, date, month, year


              Serial.print("Saving...     ");
              delay(1000);
              return;       
            }
            else
            {
              timerCancelled("");
              return;  
            }  
          }
          else
          {
            timerCancelled("");     
          }    
        }     
        else
        {
          timerCancelled("");       
        }
      }
      else
      {
        timerCancelled("");     
      }    
    }     
    else
    {
      timerCancelled("");       
    }

}


// Pass maxCount to getTimerMinutes
int getTimerMinutes(char timerText[], int startNum, int maxCount)
{
  int minutes = startNum;
  int button = 0;

  Serial.print(timerText);

  Serial.print(minutes);   

  while (button != btnSELECT)
  {
    button = read_Joy_buttons();
    Serial.println(button);

    if (button == btnLEFT)
    {
      if ((minutes + 10) <= maxCount)
      {

        minutes = minutes + 10;
      }

    }

    if (button == btnUP)
    {
      if (minutes < maxCount)
      {

        minutes++;
      }
      else
      {

		minutes = 0;
      }
    }
    if (button == btnDOWN)
    {
      if (minutes > 0)
      {

        minutes--;
      }
      else
      {

		minutes = maxCount;
      }   
    } 
    if (button == btnRIGHT)
    {

      return minutes; 
    }

    Serial.print(minutes); 
    Serial.print("   ");
  }
  return 0;
}

void setOffAlarm()
{
  int button = 0;
  int i = 0;
  Serial.println(i);
  digitalWrite(backLight, HIGH); // turn backlight on
  while (button != btnSELECT)
  {
    button = read_LCD_buttons();
    lcdClear();
    i++;
    if (i > 50)
    {
      lcdClear();
      Serial.print("Alert Alert");
      Serial.setCursor(0,1);
      Serial.print("     Alert Alert");      
      i = 0;
      timedBeep(shortBeep,3);
    }

  }     
  timerCancelled("Alarm"); 
  alarmSet = false;  
}


void loop(){ 

	for (int i = 0; i < 9000; i++)
		{
			button_loop(); //check for button pushed
		}
}

Thank you. I will try out your code in the morning. I'm feeling sick right now.

tried your code but there were a lot of errors. I also found your code this derived from so I am going to try to compare them and see if I can figure out how to make it work.

would it be easier to get rid of the joystick and just use buttons. I want the user to be able to set the time (date doesn't matter), and what times to turn a light on and off.

Considering I have never seen an alarm clock with a joystick, then yes, buttons would be way easier.

Or at this point, you could have saved some time and energy and gone and bought a $3 alarm clock. Why reinvent the wheel?

the alarm is part of a bigger project. I am making a device that will turn lights on and off at specific times and monitor temp, humidity and uv of a terrarium.