element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Attempting to use Switch and Case to Serial.print Date and Time only every 15 minutes
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 8 replies
  • Subscribers 403 subscribers
  • Views 972 views
  • Users 0 members are here
  • errors
  • ds1307
Related

Attempting to use Switch and Case to Serial.print Date and Time only every 15 minutes

Former Member
Former Member over 13 years ago

Hello, I am just starting to  work with the Ardunio Uno Rev 3 hardware and re-learning programing; programing experience is from TI 99/4A Extend Basic days!

 

I am working with the Arduio Uno example sketch from the RTCLib, DS1307.

 

Here is what I have so far:

 

//Arduino 1.0+ Only
//Arduino 1.0+ Only

//  Modified DDT DS1307 RTC.ino

#include "Wire.h"
#define DS1307_ADDRESS 0x68
byte zero = 0x00; //workaround for issue #527

void setup(){
  Wire.begin();
  Serial.begin(9600);
  setDateTime(); //MUST CONFIGURE IN FUNCTION
}

void loop(){
  switch(Minute);
  printDate();
  delay(1000);
}

void setDateTime(){

  byte second =      00; //0-59
  byte minute =    13; //0-59
  byte hour =    8; //0-23
  byte weekDay =     6; //1-7
  byte monthDay =    07; //1-31
  byte month =       12; //1-12
  byte year  =       12; //0-99

  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero); //stop Oscillator

  Wire.write(decToBcd(second));
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));
  Wire.write(decToBcd(weekDay));
  Wire.write(decToBcd(monthDay));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));

  Wire.write(zero); //start

  Wire.endTransmission();

}

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

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

void printDate(){

  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);

  int second = bcdToDec(Wire.read());
  int minute = bcdToDec(Wire.read());
  int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  int monthDay = bcdToDec(Wire.read());
  int month = bcdToDec(Wire.read());
  int year = bcdToDec(Wire.read());
 
 
  
  //print the date EG   3/1/11 23:59:59
  Serial.print(month);
  Serial.print("/");
  Serial.print(monthDay);
  Serial.print("/");
  Serial.print(year);
  Serial.print(" ");
  Serial.print(hour);
  Serial.print(":");
  Switch(minute);
  Serial.print(":");
  Serial.println(second);

}

void Switch(minute){    //  Print only every 15 minutes EG 3/1/11 23:00:00, 23:15:00, 23:30:00, 23:45:00
   
Case 1;
   if (minute) = 00;
   Serial.print(minute);
   break;
Case 2;
   if (minute) = 15;
   Serial.print(minute);
   break;
   Serial.print(minute);
Case 3;
   if (minute) = 30;
   Serial.print(minute);
   break;
Case 4;
   if (minute) = 45;
   Serial.print(minute)
   break;
Default;
   break;
   }

 

 

Verifing the sketch; I have the following errors:

 

sketch_dec08a:15: error: variable or field 'Switch' declared void

sketch_dec08a:15: error: 'minute' was not declared in this scope

sketch_dec08a.ino: In function 'void loop()':

sketch_dec08a:17: error: 'Minute' was not declared in this scope

sketch_dec08a.ino: In function 'void printDate()':

sketch_dec08a:87: error: 'Switch' was not declared in this scope

sketch_dec08a.ino: At global scope:

sketch_dec08a:93: error: variable or field 'Switch' declared void

sketch_dec08a:93: error: 'minute' was not declared in this scope

 

Please, could someone help  to debug this sketch?

 

Thank you.

  • Sign in to reply
  • Cancel
Parents
  • mconners
    0 mconners over 13 years ago

    The way you want to use the switch statement is not as a function, but as follows:

     

    void foo(int bar){

     

         switch(bar){

              case 1:

                   doStuff();

                   break;

              case 2:

                   doOtherStuff();

                   break;

              case 3:

                   doMoreStuff();

                   break;

              default:    

                   break;

         }

    }

     

     

    What that will do is compare the value of bar to the values in your case statements, if bar == 3, doMoreStuff() will  be called. If the value of bar doesn't match any of the cases, the block following default will be executed.

     

    I think you are off to a good start, but I think you are a little bit away from where you want to be.

     

    Good luck and keep learning.

     

    Mike

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • billabott
    0 billabott over 13 years ago in reply to mconners

    Thanks, MikeC.  I totally over looked that; was a Forest - Trees kind of thing.  I appreciate you bringing that to everyone's attention.  It will help greatly in getting this program running correctly.  I want to point out for everyone that the Arduino Language Reference is found here:  http://arduino.cc/en/Reference/HomePage

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mconners
    0 mconners over 13 years ago in reply to billabott

    I always struggle when I see stuff like this. As I've mentioned in other posts I'm a teach a man to fish type of guy, but I also know I could rewrite it for him in about 10 mins, but I think lessons learned on your own are lessons best learned, so I just try to clarify the concepts for folks.

     

    I've seen you offer advice on quite a few posts as well and it is appreciated.

     

    Mike

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • mconners
    0 mconners over 13 years ago in reply to billabott

    I always struggle when I see stuff like this. As I've mentioned in other posts I'm a teach a man to fish type of guy, but I also know I could rewrite it for him in about 10 mins, but I think lessons learned on your own are lessons best learned, so I just try to clarify the concepts for folks.

     

    I've seen you offer advice on quite a few posts as well and it is appreciated.

     

    Mike

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
  • billabott
    0 billabott over 13 years ago in reply to mconners

    I finally got the switch statement to where it compiles without errors:

     

      //print the date EG   3/1/11 23:59:59

      Serial.print(month);

      Serial.print("/");

      Serial.print(monthDay);

      Serial.print("/");

      Serial.print(year);

      Serial.print(" ");

      Serial.print(hour);

      Serial.print(":");

      Swatch();

      Serial.print(":");

      Serial.println(second);

     

    }

     

    void Swatch(){    //  Print only every 15 minutes EG 3/1/11 23:00:00, 23:15:00, 23:30:00, 23:45:00

    switch(minute){  

    case 1:

       if (minute == 00)

       Serial.print(minute);

       break;

    case 2:

       if (minute == 15)

       Serial.print(minute);

       break;

    case 3:

       if (minute == 30)

       Serial.print(minute);

       break;

    case 4:

       if (minute == 45)

       Serial.print(minute);

       break;

    default:

       break;

       }

     

    BUT, that whole series of Serial.print statements need to be placed in a function of their own and it should be called from within the switch construct instead of just   Serial.print(minute);

    If the line of data is only going to print out once every 15 minutes.

     

    On the other hand:  An appropriate "if" statement would get the job done also.

    if ((minute == 00)||(minute==15)||(minute==30)||(minute==45))

              PrintDateTime();

     

    MikeC and everyone:  Have you tried MariaMole IDE for 'Duino?  They are aiming to make it an IDE for advanced users.  I found it not quite ready for prime time.  Color coding of the sketch token/symbols is nice to have.  If you want to help them make improvements, I am sure they welcome bug reports.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 13 years ago in reply to billabott

    Thank you billabott and Michael Conners for getting this to work.  I will continue to progress and learn!

     

    I really appreciate to work and the replies to need for help and fresh eyes!

     

    William

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mconners
    0 mconners over 13 years ago in reply to billabott

    I haven't tried MariaMole, it looks like it's windows only. I'm pretty much Linux only, when I can help it anyway.

     

    TBH,  I've been ignoring arduinos for a while in favor of arm based boards, I'll need to check out the Due soon, although it's a little pricey compared to

    some of the other boards I've purchased.

     

    I would recommend that people master the arduino and the C language prior to trying to move to arm. That is definitely intermediate leaning to advanced territory.

     

     

    Mike

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mconners
    0 mconners over 13 years ago in reply to Former Member

    No problem, good luck. There are a lot of good resources out there on the web.

     

    Make sure you get a good tutorial on C, once you learn to reuse the libraries for the arduino, the hardware is easy to deal with.

     

    Mike

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube