Pages: [1]   Go Down
Author Topic: question on saving times  (Read 720 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

when using the code below or a simular code when you set the time, is there a way to store what time you just set it at as an interger so that you can use that time later in the program? if so how? I am trying to make sort of a intermatic timer, so that a user can use the push buttons on the LCD to set 1,2,or 3 diffrent times and when one of those times = the main time it will then run a step motor.

any help will be great, thanks

#include <LiquidCrystal.h> 
LiquidCrystal lcd(8,9,4,5,6,7);

#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68

int a=0;
byte secondminutehourdayOfWeekdayOfMonthmonthyear;

// define custom characters for up/down/left/right arrow
byte ua[8] = {  B00100,  B01110,  B11111,  B10101,  B00100,    B00100,  B00100,  B00100};
byte da[8] = {B00100,    B00100,  B00100,  B00100B10101,B11111,B01110,  B00100};
byte la[8] = {B00011B00110B01100B11111B11111B01100B00110B00011};
byte ra[8] = {B11000B01100B00110B11111B11111B00110B01100B11000 };  

void setup()
{
  
lcd.begin(162);
  
Wire.begin();
  
lcd.createChar(0,ua);
  
lcd.createChar(1,da);
  
lcd.createChar(2,la);
  
lcd.createChar(3,ra);
}

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (
val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (
val/16*10) + (val%16) );
}


int readButtons()
// returns the button number pressed, or zero for none pressed
{
  
int b,0;
  
c=analogRead(0); // get the analog value
  
if (c>1000)
  {
    
b=0// buttons have not been pressed
  

  else
    if (
c>470 && c<485)
    {
      
b=1// button 1 pressed
    

    else
      if (
c<135 && c>120)
      {
        
b=2// button 2 pressed
      

      else
        if (
c>295 && c<315)
        {
          
b=3// button 3 pressed
        

        else
          if (
c<10)
          {
            
b=4// button 4 pressed
          

          else
            if (
c<730 && c>715)
            {
              
b=5// button 5 pressed
            
}
  return 
b;
}

// Gets the date and time from the ds1307
void getDateDs1307(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
  
// Reset the register pointer
  
Wire.beginTransmission(DS1307_I2C_ADDRESS);
  
Wire.send(0);
  
Wire.endTransmission();
  
Wire.requestFrom(DS1307_I2C_ADDRESS7);

  
// A few of these need masks because certain bits are control bits
  
*second     bcdToDec(Wire.receive() & 0x7f);
  *
minute     bcdToDec(Wire.receive());
  *
hour       bcdToDec(Wire.receive() & 0x3f);  // Need to change this if 12 hour am/pm
  
*dayOfWeek  bcdToDec(Wire.receive());
  *
dayOfMonth bcdToDec(Wire.receive());
  *
month      bcdToDec(Wire.receive());
  *
year       bcdToDec(Wire.receive());
}

void setDateDs1307(byte second,        // 0-59
byte minute,        // 0-59
byte hour,          // 1-23
byte dayOfWeek,     // 1-7
byte dayOfMonth,    // 1-28/29/30/31
byte month,         // 1-12
byte year)          // 0-99
{
  
Wire.beginTransmission(DS1307_I2C_ADDRESS);
  
Wire.send(0);
  
Wire.send(decToBcd(second));    // 0 to bit 7 starts the clock
  
Wire.send(decToBcd(minute));
  
Wire.send(decToBcd(hour));     
  
Wire.send(decToBcd(dayOfWeek));
  
Wire.send(decToBcd(dayOfMonth));
  
Wire.send(decToBcd(month));
  
Wire.send(decToBcd(year));
  
Wire.send(00010000); // sends 0x10 (hex) 00010000 (binary) to control register - turns on square wave
  
Wire.endTransmission();
}

void showTime()
{
  
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  
lcd.clear(); // clear LCD screen
  
lcd.setCursor(0,0);
  
lcd.print("    ");
  
lcd.print(hourDEC);
  
lcd.print(":");
  if (
minute<10)
  {
    
lcd.print("0");
  }
  
lcd.print(minuteDEC);
  
lcd.print(":");
  if (
second<10)
  {
    
lcd.print("0");
  }
  
lcd.print(secondDEC);
}

void setTime()
{
  
int escape=0;
  
int h=0;
  
int m=0;
  do
  {
    
escape=readButtons();
    
delay(100); // for debounce
    
if (escape==1)
    {
      --
m;
      if (
m<0)
      {
        
m=59;
      }
    } else
    if (
escape==2)
    {
      
h++;
      if (
h>23)
      {
        
h=0;
      }
    } else
    if (
escape==3)
    {
      --
h;
      if (
h<0)
      {
        
h=23;
      }
    } else 
    if (
escape==4)
    {
      
m++;
      if (
m>59)
      {
        
m=0;
      }
    }
    
lcd.clear();
    
lcd.setCursor(0,0);
    
lcd.write(0);
    
lcd.write(1);
    
lcd.print(":h ");
    
lcd.write(2);
    
lcd.write(3);
    
lcd.print(":m Sel=OK");
    
lcd.setCursor(5,1);
    if (
h<10)
    {
      
lcd.print("0");
    }
    
lcd.print(h);
    
lcd.print(":");
    if (
m<10)
    {
      
lcd.print("0");
    }
    
lcd.print(m);
  } while (
escape<5);
  
setDateDs1307(0mhdayOfWeekdayOfMonthmonthyear);
}
    
  

void loop()
{
  
lcd.clear();
  
showTime();
  
a=readButtons(); 
  if (
a==5// someone pressed 'select'
  
{
    
delay(200); // for debounce
    
setTime();
  } 
  
delay(75); // delay to stop LCD flicker
}
Logged

Leighton Buzzard, UK
Offline Offline
Edison Member
*
Karma: 21
Posts: 1339
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

crikey
try wrapping it in code markers, please. like this:


Code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(8,9,4,5,6,7);

#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68

int a=0;
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;

// define custom characters for up/down/left/right arrow
byte ua[8] = {  B00100,  B01110,  B11111,  B10101,  B00100,    B00100,  B00100,  B00100};
byte da[8] = {B00100,    B00100,  B00100,  B00100, B10101,B11111,B01110,  B00100};
byte la[8] = {B00011, B00110, B01100, B11111, B11111, B01100, B00110, B00011};
byte ra[8] = {B11000, B01100, B00110, B11111, B11111, B00110, B01100, B11000 };  

void setup()
{
  lcd.begin(16, 2);
  Wire.begin();
  lcd.createChar(0,ua);
  lcd.createChar(1,da);
  lcd.createChar(2,la);
  lcd.createChar(3,ra);
}

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (val/16*10) + (val%16) );
}


int readButtons()
// returns the button number pressed, or zero for none pressed
{
  int b,c = 0;
  c=analogRead(0); // get the analog value
  if (c>1000)
  {
    b=0; // buttons have not been pressed
  }
  else
    if (c>470 && c<485)
    {
      b=1; // button 1 pressed
    }
    else
      if (c<135 && c>120)
      {
        b=2; // button 2 pressed
      }
      else
        if (c>295 && c<315)
        {
          b=3; // button 3 pressed
        }
        else
          if (c<10)
          {
            b=4; // button 4 pressed
          }
          else
            if (c<730 && c>715)
            {
              b=5; // button 5 pressed
            }
  return b;
}

// Gets the date and time from the ds1307
void getDateDs1307(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
  // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0);
  Wire.endTransmission();
  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  *second     = bcdToDec(Wire.receive() & 0x7f);
  *minute     = bcdToDec(Wire.receive());
  *hour       = bcdToDec(Wire.receive() & 0x3f);  // Need to change this if 12 hour am/pm
  *dayOfWeek  = bcdToDec(Wire.receive());
  *dayOfMonth = bcdToDec(Wire.receive());
  *month      = bcdToDec(Wire.receive());
  *year       = bcdToDec(Wire.receive());
}

void setDateDs1307(byte second,        // 0-59
byte minute,        // 0-59
byte hour,          // 1-23
byte dayOfWeek,     // 1-7
byte dayOfMonth,    // 1-28/29/30/31
byte month,         // 1-12
byte year)          // 0-99
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0);
  Wire.send(decToBcd(second));    // 0 to bit 7 starts the clock
  Wire.send(decToBcd(minute));
  Wire.send(decToBcd(hour));    
  Wire.send(decToBcd(dayOfWeek));
  Wire.send(decToBcd(dayOfMonth));
  Wire.send(decToBcd(month));
  Wire.send(decToBcd(year));
  Wire.send(00010000); // sends 0x10 (hex) 00010000 (binary) to control register - turns on square wave
  Wire.endTransmission();
}

void showTime()
{
  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  lcd.clear(); // clear LCD screen
  lcd.setCursor(0,0);
  lcd.print("    ");
  lcd.print(hour, DEC);
  lcd.print(":");
  if (minute<10)
  {
    lcd.print("0");
  }
  lcd.print(minute, DEC);
  lcd.print(":");
  if (second<10)
  {
    lcd.print("0");
  }
  lcd.print(second, DEC);
}

void setTime()
{
  int escape=0;
  int h=0;
  int m=0;
  do
  {
    escape=readButtons();
    delay(100); // for debounce
    if (escape==1)
    {
      --m;
      if (m<0)
      {
        m=59;
      }
    } else
    if (escape==2)
    {
      h++;
      if (h>23)
      {
        h=0;
      }
    } else
    if (escape==3)
    {
      --h;
      if (h<0)
      {
        h=23;
      }
    } else
    if (escape==4)
    {
      m++;
      if (m>59)
      {
        m=0;
      }
    }
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.write(0);
    lcd.write(1);
    lcd.print(":h ");
    lcd.write(2);
    lcd.write(3);
    lcd.print(":m Sel=OK");
    lcd.setCursor(5,1);
    if (h<10)
    {
      lcd.print("0");
    }
    lcd.print(h);
    lcd.print(":");
    if (m<10)
    {
      lcd.print("0");
    }
    lcd.print(m);
  } while (escape<5);
  setDateDs1307(0, m, h, dayOfWeek, dayOfMonth, month, year);
}
    
  

void loop()
{
  lcd.clear();
  showTime();
  a=readButtons();
  if (a==5) // someone pressed 'select'
  {
    delay(200); // for debounce
    setTime();
  }
  delay(75); // delay to stop LCD flicker
}

now what was the question? smiley
Logged

there are only 10 types of people
them that understands binary
and them that doesn't

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Have you checked the time library - http://www.arduino.cc/playground/Code/Time - it would make your project quite easy as you just have to know the offset inseconds of the three times you need. (if I understood the question correctly)

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok sorry about that, still quite new to the site.

anyway, I am trying to make a type of intermatic timer, right now I have the arduino LCD keypad that I am using and the code above alows you to set the time using the buttons on the LCD. what I am wodering is how can I have a user enter the main time and have that saved. then have them also be able to set 1-3 more diffrent times that would alow you to save them and when the main time = a set time then a step motor runs.

kinda like this in a way:
if main time == feed time
    run step motor
   else if main time != feed time
       do nothing

so I guess I am wondering is there a way to save the diffrent feed times as intergers like feed time 1, feed time 2, ect. or how would I be able to do this?

sorry for so long and prob. confusing
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I will check out that library also .

thanks
Logged

Pages: [1]   Go Up
Jump to: