Go Down

Topic: How to properly pass inputs into a function? Help? (Read 919 times) previous topic - next topic

88

im trying to mess with the time alarm example code :
Code: [Select]
#include <Time.h>
#include <TimeAlarms.h>

void setup()
{
  Serial.begin(9600);   
  Serial.println("TimeAlarms Example");
  Serial.println("Alarms are triggered daily at 8:30 am and 17:45 pm");
  Serial.println("One timer is triggered every 15 seconds");
  Serial.println("Another timer is set to trigger only once after 10 seconds");
  Serial.println();
 
  setTime(8,29,40,1,1,10); // set time to 8:29:40am Jan 1 2010

  Alarm.alarmRepeat(8,30,0, MorningAlarm);  // 8:30am every day
  Alarm.alarmRepeat(17,45,0,EveningAlarm);  // 5:45pm every day

  Alarm.timerRepeat(15, RepeatTask);            // timer for every 15 seconds   
  Alarm.timerOnce(10, OnceOnlyTask);            // called once after 10 seconds
}

void MorningAlarm()
{
  Serial.println("Alarm: - turn lights off");   
}

void EveningAlarm()
{
  Serial.println("Alarm: - turn lights on");           
}

void RepeatTask()
{
  Serial.println("15 second timer");         
}

void OnceOnlyTask()
{
  Serial.println("This timer only triggers once"); 
}

void  loop()

  digitalClockDisplay();
  Alarm.delay(1000); // wait one second between clock display
}

void digitalClockDisplay()
{
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.println();
}

void printDigits(int digits)
{
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}


I want to pass values into the setTime(); function but cannot figure out a way that actually works.
Anyone, anyone?

PaulS

Quote
I want to pass values into the setTime(); function but cannot figure out a way that actually works.

You need to explain this better. You can't set the time correctly? You can't get code that sets the time to compile?

88

The code compiles just fine but when i run it using the serial monitor, only the hours are programmed, the minutes have the same value as the hour and it does not increment at all.

I have global variables named : int sethour, int setmin, int setsec, etc... 
im using an IR remote to program the hour, min, sec, etc...

the code for that looks like
Code: [Select]
void  loop() {
 
  int buttonValue = digitalRead(RECV_PIN);
 
  while (irrecv.decode (&results)) {
  Serial.println("Input the hour using the remote. (Military time 1-24) \n For example: Three am would be 0 followed by 3, 1 pm would be 1 followed by 3");
  Serial.println("\n");
  if (results.value == zero){
    sethour = 0;
    cycle = false;
  }
  else if (results.value == one){
    sethour = 1;
    cycle = false;
  }
etc....
needs revision but used as is for the moment



and toward the end of void loop() there is:
              setTime(sethour,setmin,setsec,setday,setmonth,setyear);

originally the header file for the time code states the parameters in the setTime(); function as :
             setTime(int hr,int min,int sec,int day, int month, int yr);

I posted a topic called "loop repeats, something something, whats going on?" with the code im using.

PaulS

Quote
and toward the end of void loop() there is:
              setTime(sethour,setmin,setsec,setday,setmonth,setyear);

Towards the end of loop there should be:
Code: [Select]
Serial.print("Hours: ");Serial.println(sethour);
Serial.print("Minutes: ");Serial.println(setmin);
Serial.print("Seconds: ");Serial.println(setsec);
Serial.print("Day: ");Serial.println(setday);
Serial.print("Month: ");Serial.println(setmonth);
Serial.print("Year: ");Serial.println(setyear);
setTime(sethour,setmin,setsec,setday,setmonth,setyear);

And you should show us the output and the resulting time.

88

the output looks like:


Hours: 2
Minutes: 2
Seconds: 0
Day: 2
Month: 2
Year: 0
2:02:00 2 2 2000


I pressed the 2 button once on the remote yet 2 was passed into minutes, day, and month.  I left out seconds and year in the code incase you where wondering why 2 wasnt passed into those as well.

88

Here is the code im using:
Code: [Select]
#include <Time.h>
#include <TimeAlarms.h>
#include <IRremote.h>

int RECV_PIN = 9;

const int RELAY_0 = A0;
const int RELAY_1 = A1;
const int RELAY_2 = A2;
const int RELAY_3 = A3;
const int RELAY_4 = A4;
const int RELAY_5 = A5;



int sethour;
int setmin;
int setsec = 00;
int setday;
int setmonth;
int setyear;

int count;

IRrecv irrecv(RECV_PIN);

decode_results results;

#define one 0x801
#define two 0x802
#define three 0x803
#define four 0x804
#define five 0x805
#define six 0x806
#define seven 0x807
#define eight 0x808
#define nine 0x809
#define zero 0x800

int hoursdone;
int minsdone;
int daysdone;
int monthsdone;
int on = 0;
unsigned long last = millis();
boolean cycle = true;  //auto-cylce mode


void setup()
{
  pinMode(RELAY_0, OUTPUT);
  pinMode(RELAY_1, OUTPUT);
  pinMode(RELAY_2, OUTPUT);
  pinMode(RELAY_3, OUTPUT);
  pinMode(RELAY_4, OUTPUT);
  pinMode(RELAY_5, OUTPUT);
  pinMode(13, OUTPUT);
  irrecv.enableIRIn();  // start the receiver
  Serial.begin(9600); 
  }
 


/*
void MorningAlarm()
{
  Serial.println("Alarm: - turn lights off");   
}

void EveningAlarm()
{
  Serial.println("Alarm: - turn lights on");           
}

void RepeatTask()
{
  Serial.println("15 second timer");         
}

void OnceOnlyTask()
{
  Serial.println("This timer only triggers once"); 
}
*/
void  loop() {
 
  int buttonValue = digitalRead(RECV_PIN);
 
  if (irrecv.decode(&results) ) {         
  if (hoursdone == 0){     //haven't done hours yet
      Serial.println("Enter the Hour.\n");
       if (results.value == zero){
           sethour = 0;
           cycle = false;
           hoursdone=1;
           }
        else if (results.value == one){
           sethour = 1;
           cycle = false;
           hoursdone=1;
           }
           else if (results.value == two){
           sethour = 2;
           cycle = false;
           hoursdone=1;
           }
           else if (results.value == three){
           sethour = 3;
           cycle = false;
           hoursdone=1;
           }
           else if (results.value == four){
           sethour = 4;
           cycle = false;
           hoursdone=1;
           }
           else if (results.value == five){
           sethour = 5;
           cycle = false;
           hoursdone=1;
           }
    irrecv.resume(); // Receive the next value
 
  }  else if (hoursdone == 1){    //do the minutes if hours have been done already
       Serial.println("Enter the minute.\n");
           }
           if(minsdone == 0){
           if (results.value == zero){
           setmin = 0;
           cycle = false;
           minsdone=1;
           }
           else if (results.value == one){
           setmin = 1;
           cycle = false;
           minsdone=1;
           }
           else if (results.value == two){
           setmin = 2;
           cycle = false;
           minsdone=1;
           }
           else if (results.value == three){
           setmin = 3;
           cycle = false;
           minsdone=1;
           }
           else if (results.value == four){
           setmin = 4;
           cycle = false;
           minsdone=1;
           }
           else if (results.value == five){
           setmin = 5;
           cycle = false;
           minsdone=1;
           }
irrecv.resume(); // Receive the next value
           
        }  else if(minsdone == 1){
             Serial.println("Enter the Day.\n");
           }
           if (daysdone == 0){
              if (results.value == zero){
           setday = 0;
           cycle = false;
           daysdone=1;
           }
           else if (results.value == one){
           setday = 1;
           cycle = false;
           daysdone=1;
           }
           else if (results.value == two){
           setday = 2;
           cycle = false;
           daysdone=1;
           }
           else if (results.value == three){
           setday = 3;
           cycle = false;
           daysdone=1;
           }
           else if (results.value == four){
           setday = 4;
           cycle = false;
           daysdone=1;
           }
           else if (results.value == five){
           setday = 5;
           cycle = false;
           daysdone=1;
           }
         irrecv.resume();
           }  else if(daysdone == 1) {
             Serial.println("Enter the Month. \n");
           }
             if(monthsdone == 0){
               if (results.value == zero){
           setmonth = 0;
           cycle = false;
           monthsdone=1;
           }
           else if (results.value == one){
           setmonth = 1;
           cycle = false;
           monthsdone=1;
           }
           else if (results.value == two){
           setmonth = 2;
           cycle = false;
           monthsdone=1;
           }
           else if (results.value == three){
           setmonth = 3;
           cycle = false;
           monthsdone=1;
           }
           else if (results.value == four){
           setmonth = 4;
           cycle = false;
           monthsdone=1;
           }
           else if (results.value == five){
           setmonth = 5;
           cycle = false;
           monthsdone=1;
           }
           irrecv.resume();
             } else if(monthsdone == 1){
               setyear = 11;
             }
               
  }
   


/*
  Alarm.alarmRepeat(8,30,0, MorningAlarm);  // 8:30am every day
  Alarm.alarmRepeat(17,45,0,EveningAlarm);  // 5:45pm every day

  Alarm.timerRepeat(15, RepeatTask);            // timer for every 15 seconds   
  Alarm.timerOnce(10, OnceOnlyTask);            // called once after 10 seconds
*/

  setTime(sethour,setmin,setsec,setday,setmonth,setyear); // set time
 
  digitalClockDisplay();
  Alarm.delay(1000); // wait one second between clock display
}


void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year());
  Serial.println();
}
void printDigits(int digits)
{
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}


I tried placing the programmable part in void setup() but it didnt make a difference, it actually made things worse.

And this is what the output looks like :

0:00:00 31 12 1999
0:00:00 0 0 2000
0:00:00 0 0 2000
0:00:00 0 0 2000
0:00:00 0 0 2000
Enter the Hour.

0:00:00 0 0 2000
0:00:00 0 0 2000
Enter the Hour.

2:02:00 2 2 2000
2:02:00 2 2 2000
2:02:00 2 2 2000
Enter the minute.

Enter the Day.

Enter the Month.

2:02:00 2 2 2011
Enter the minute.

Enter the Day.

Enter the Month.

2:02:00 2 2 2011
Enter the minute.

Enter the Day.

Enter the Month.

2:02:00 2 2 2011

it continues to repeat and the time does not increment.  I only pressed the #2 on the remote once yet 2 was set into minutes, day and month.

Senso

So, from where do you have 12 bits data comming from?
All those defines are a bit strange, they are a good practice, but really, 12 bits is a odd number of data to come down the Serial port in one go, in fact its impossible to receive 12 bits of data in one go.

PaulS

I think you need to create some functions - defineHourTen() defineHourOne, defineMinTen(), defineMinOne(), etc.

In addition, create a function to get a remote input, and return an int (the value that corresponds to the button press.

When you get a value from the remote, call the appropriate function. This will allow you to print the values after each remote press, to verify that only the single correct value is being set.

Go Up