Show Posts
Pages: 1 2 3 [4] 5 6 ... 8
46  Using Arduino / Programming Questions / Re: VirtualWire receiver never gets its message on: May 18, 2013, 05:39:18 pm
Instead of doing posts with a fraction of the program, attach the whole file. At the bottom when you write your post, "Additional options" ...
Then we can see the problem.
47  Using Arduino / Programming Questions / Lcd Clock almost finished. on: May 18, 2013, 05:09:52 pm
my project is almost there. Here is the complete code. Just need some help with the following.

I can navigate the menu but when I break out of the menu to change any of the RTC parameters the encoder just navigates the menu again. I can only change parameters of the RTC using the encoder push button.  What I want is to change the parameters using the encoder.

Code:
#include <Wire.h>
#include <Rtc_Pcf8563.h>
#include <MenuBackend.h> //MenuBackend library - copyright by Alexander Brevig
#include <LiquidCrystal.h> //this library is included in the Arduino IDE

const int encoderPushButton = 45; // pin for the Enter button
const int buttonPinEsc = 42; // pin for the Esc button

int encoder0PinA = 2;
int encoder0PinB = 3;
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = HIGH;

int menuRoot = true;// A global flag. true = root (top) menu and display clock. false = don't display clock

int lastButtonPushed = 0;
int lastButtonEnterState = LOW; // the previous reading from the Enter input pin
int lastButtonEscState = LOW; // the previous reading from the Esc input pin
long lastEnterDebounceTime = 0; // the last time the output pin was toggled
long lastEscDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 150; // the debounce time

LiquidCrystal lcd(7,8,9,10,11,12);

Rtc_Pcf8563 rtc;//init the real time clock

// Time and Date

byte sec;
byte min;
byte hour;
byte day;
byte weekday;
byte month;
byte year;
byte century;

//Menu variables i.e. whats in the menu
MenuBackend menu = MenuBackend(menuUsed,menuChanged);

//initialize menuitems
MenuItem menuSetClock = MenuItem("menuSetClock"); // 2nd menu item
MenuItem menuItemHr = MenuItem("menuItemHr"); // sub menu
MenuItem menuItemMin = MenuItem("menuItemMin"); //sub menu
MenuItem menuExit = MenuItem("menuExit"); // 6th menu item



void setup()
{
  lcd.begin(16, 2);
  pinMode(encoderPushButton, INPUT);
  pinMode(buttonPinEsc, INPUT);
  pinMode (encoder0PinA,INPUT);
  pinMode (encoder0PinB,INPUT);



  //How the menu will look.
  lcd.setCursor(0,0);
  menu.getRoot().addRight(menuSetClock); // set main menu items
  menuSetClock.add(menuItemHr).addRight(menuItemMin); // add first sub menu then addright additional sub menues for that menu
  menuExit.add(menuExit);
  menu.toRoot();
}



void loop()
{
  if (menuRoot == true)
  {
    lcd.setCursor(4, 0);
    lcd.print(rtc.formatTime());
    lcd.setCursor(3, 1);
    lcd.print(rtc.formatDate());

  }
  readButtons(); //I splitted button reading and navigation in two procedures because
  readEncoder();
  navigateMenus(); //in some situations I want to use the button for other purpose (eg. to change some settings)
  {
  }
}


void  readButtons() //read buttons status

  int reading;
  int buttonEnterState=LOW; // the current reading from the Enter input pin
  int buttonEscState=LOW; // the current reading from the input pin


  //ENTER BUTTON DEBOUNCE

  reading = digitalRead(encoderPushButton); // read the state of the switch into a local variable:

  if (reading != lastButtonEnterState) // If the switch changed, due to noise or pressing:
  {
    lastEnterDebounceTime = millis(); // reset the debouncing timer
  }
  if ((millis() - lastEnterDebounceTime) > debounceDelay) // whatever the reading is at, it's been there for longerthan the debounce delay, so take it as the actual current state:
  {         
    buttonEnterState=reading;
    lastEnterDebounceTime=millis();
  }
  lastButtonEnterState = reading; // save the reading.  Next time through the loop, it'll be the lastButtonState:


  //ESC BUTTON DEBOUNCE           

  reading = digitalRead(buttonPinEsc); // read the state of the switch into a local variable:

  if (reading != lastButtonEscState) // If the switch changed, due to noise or pressing:
  {
    lastEscDebounceTime = millis(); // reset the debouncing timer
  }
  if ((millis() - lastEscDebounceTime) > debounceDelay)// whatever the reading is at, it's been there for longerthan the debounce delay, so take it as the actual current state:
  {
    buttonEscState = reading;
    lastEscDebounceTime=millis();
  }
  lastButtonEscState = reading; // save the reading.  Next time through the loop, it'll be the lastButtonState:

  //Records which button has been pressed
  if (buttonEnterState==HIGH){
    lastButtonPushed=encoderPushButton;
  }
  else if(buttonEscState==HIGH){
    lastButtonPushed=buttonPinEsc;
  }
  else{
    lastButtonPushed=0;
  }                 
}


void readEncoder() //read the encoder and set lastButtonPushed to values used by navigateMenu()
{
  n = digitalRead(encoder0PinA);// n is current state of encoder0PinA pin
  if ((encoder0PinALast == HIGH) && (n == LOW)) // check if encoder0PinA pin has changed state
  {
    if (digitalRead(encoder0PinB) == LOW)
    {
      lastButtonPushed = 1; //if it has changed and its now low decrement  encoder0Pos;
    }
    else
    {
      lastButtonPushed = 2; // if it has changed and its now high, increment encoder0Pos
    }
  }
  encoder0PinALast = n; // set the variable holding the previous state to the value n read above
}


void menuChanged(MenuChangeEvent changed){

  MenuItem newMenuItem=changed.to; //get the destination menu

  lcd.setCursor(0,0); //set the start position for lcd printing to the second row

  if(newMenuItem.getName()==menu.getRoot()){
    lcd.clear();
    menuRoot = true;   
  }
  //MENU SET CLOCK
  else if(newMenuItem.getName()=="menuSetClock"){
    menuRoot = false;
    lcd.clear();
    lcd.print("Set Clock");
  }
  else if(newMenuItem.getName()=="menuItemHr"){
    lcd.clear();
    lcd.setCursor(0,1);
    lcd.print("Hr");
  }
  else if(newMenuItem.getName()=="menuItemMin"){
    lcd.clear();
    lcd.setCursor(0,1);
    lcd.print("Min");
  }
}


void menuUsed(MenuUseEvent used){ //                  **if you have a function call it here**
  if ( used.item == menuItemHr ){
    lcd.clear();//etc.
    lcd.setCursor(0, 0);
    lcd.print(rtc.formatTime());
    changeHour();
  }
  else if ( used.item == menuItemMin ){
    lcd.clear();//etc.
    lcd.setCursor(0, 0);
    lcd.print(rtc.formatTime());
    changeMin();
  }

  if ( used.item == menuExit ){
    menu.toRoot();//etc.
  }
}


void navigateMenus()
{
  MenuItem currentMenu=menu.getCurrent();

  switch (lastButtonPushed)
  {
  case encoderPushButton: // enter pin
    if(!(currentMenu.moveDown()))//if the current menu has a child and enter has been pressed  then navigate menu to item below
    { 
      menu.use();
    }
    else //otherwise, if menu has no child and enter has been pressed---- the current menu is used
    {
      menu.moveDown();
    }
    break;
  case buttonPinEsc:
    lcd.clear();
    menu.toRoot();  //back to main
    break;
  case 1: //Encoder right/buttonPinRight --:
    menu.moveRight();

    break;     
  case 2: //Encoder left/buttonPinLeft ++:
    menu.moveLeft();
    break;     
  }
  lastButtonPushed=0; //reset the lastButtonPushed variable
}


void changeMin(){

  //min=rtc.getMinute();
  if(digitalRead(encoderPushButton) == HIGH)
  {
    min = ++min % 60; //  this is better than // min += 1;as it rolls overfrom 59 to 00
    //if (incrementMinuteButtonPressDectected) min = ++min % 60 // increment and rollover past 59
    //if (decrementMinuteButtonPressDectected) min = (--min + 60) % 60 // decrement and rollunder past 0
  }
  rtc.setTime(hour, min,sec);
}

void changeHour(){

  // hour=rtc.getHour();

  if(digitalRead(encoderPushButton) == HIGH)
  {
    hour = ++hour % 24; //  this is better than // hour += 1;as it rolls overfrom 59 to 00
    //if (incrementMinuteButtonPressDectected) hour = ++hour % 60 // increment and rollover past 59
    //if (decrementMinuteButtonPressDectected) hour = (--hour + 60) % 60 // decrement and rollunder past 0
  }
  rtc.setTime(hour, min,sec);
  rtc.setTime(sec, min, hour);
}


void changeDay(){

  // Yet to be done
  if(digitalRead(encoderPushButton) == HIGH)
  {
    day = ++day % 32; //  this is better than // day += 1;as it rolls overfrom 59 to 00
    //if (incrementMinuteButtonPressDectected) dayn = ++day % 31 // increment and rollover past 59
    //if (decrementMinuteButtonPressDectected) day = (--day + 31) % 31 // decrement and rollunder past 0
  }
  rtc.setDate(day,weekday, month, century, year);
}
48  Using Arduino / Programming Questions / Re: For loop on: May 18, 2013, 04:39:09 pm
Thanks for taking the time to give an example. Mine was very contrived but I'm trying to get a better understanding of the for loop. Your example has helped me understand how it all comes together..  Thanks all of you for the help.
49  Using Arduino / Programming Questions / Re: For loop on: May 18, 2013, 03:38:07 pm
thanks. what would tell me if it was red. which part.... this is just hypothetical

for (int a=0;      a <= 5;         a++)
50  Using Arduino / Programming Questions / Re: For loop on: May 18, 2013, 03:33:29 pm
to keep is very basic if I wanted 5apples. when I have 5 in a bag i then pay for them Is that kinda a "for loop" idea.
51  Using Arduino / Programming Questions / For loop on: May 18, 2013, 03:22:29 pm
I have read about the "For loop" statement but could someone explain why you would use such a statement.
I have read the explanations on the web and in books and as I understand it counts through a loop.

Why would you use it. I have seen examples but dont understand "when" it would be used.
52  Using Arduino / Programming Questions / Switch case on: May 09, 2013, 07:07:36 pm
I'm using a rotary encoder to navigate a menu and use "switch" statements to control the 3 functions of the encoder.

Code:
void navigateMenus()
{
  MenuItem currentMenu=menu.getCurrent();

  switch (lastButtonPushed)
  {
  case encoderPushButton: // enter pin
    if(!(currentMenu.moveDown()))//if the current menu has a child and enter has been pressed  then navigate menu to item below
    {  
      menu.use();
    }
    else //otherwise, if menu has no child and enter has been pressed---- the current menu is used
    {
      menu.moveDown();
    }
    break;
  case buttonPinEsc:
    lcd.clear();
    menu.toRoot();  //back to main
    break;
  case 1: //Encoder right rotation
    menu.moveRight();
    break;      
  case 2: //Encoder left rotation
    menu.moveLeft();
    break;      
  }
  lastButtonPushed=0; //reset the lastButtonPushed variable
}

To get this far I have the rotary encoder set up as below

Code:
void readEncoder() //read the encoder and set lastButtonPushed to values used by navigateMenu()
{
  n = digitalRead(encoder0PinA);
  if ((encoder0PinALast == HIGH) && (n == LOW))
  {
    if (digitalRead(encoder0PinB) == LOW)
    {
      lastButtonPushed = 1;
    }
    else
    {
      lastButtonPushed = 2;
    }
  }
  encoder0PinALast = n;
}

What I would like to know is can I also use the same rotary action to increase a parameter instead of moving Right (case 1) or Left (case 2) through a menu. If it is possible, how would it be done?

I have a lot of views but no one has responed. can anyone offer any help on this matter
53  Using Arduino / Programming Questions / Lcd clock and Rotary encoder. on: May 06, 2013, 12:11:29 pm
I am making a clock using a Pcf 8563p as the (RTC) chip. The clock displays on an LCD and I can change the time and date using a Rotary encoder. But TWO things are bothering me and would like some advice on how to solve the following.

1, The date displays as mm/dd/yyyy and I would like dd/mm/yyyy

2, I use a rotary encoder to move through the menu. But, when I want to change either the Time or Date I have to keep pressing the encoder button to increment the digits. Also when I come out of the variable I have set. The digits I had just set increment by an additional 1. so for example if I set the hours to 10 by pressing down on the encoder button then Rotate the encoder to the Minutes the Hour becomes 11.

How do I stop this additional digit increment, and what do I change in my code to increment the digits using "Rotary action" of the encoder instead of the "Push button action" of the encoder.

Heres the code attached below

54  Using Arduino / Programming Questions / Re: changing clock/date setting on Real Time Clock pcf8563 on: January 21, 2013, 02:37:06 pm
Thanks to all. I have it working now.

This last bit solved the issue.
On each pass through loop(), you are testing whether the switch connected to minButton is pressed. If it is, you set the time to 5:30:14, then increment the min variable and return.

You need to move the local variables hour, min, and sec from the function to global space.

Then, you need to call setTime AFTER incrementing min.

Karma awarded.
55  Using Arduino / Programming Questions / Re: changing clock/date setting on Real Time Clock pcf8563 on: January 21, 2013, 01:48:29 am
Even with the call, all that happens is the Time defaults to 18:20:00. I can't get the minutes to increase when the minButton goes HIGH.

Code:
#include <Wire.h>
#include <Rtc_Pcf8563.h>
#include <LiquidCrystal.h>

//init the real time clock
Rtc_Pcf8563 rtc;


LiquidCrystal lcd(7,8,9,10,11,12);

int minButton = 42;

void setup()
{
  lcd.begin(16, 2);
  pinMode (minButton,INPUT);
  //clear out all the registers
  rtc.initClock();
  //set a time to start with.
  //day, weekday, month, century, year
  byte day = 1;
  byte weekday= 1;
  byte month = 1;
  byte century =0;
  byte year = 13;

  day =20;
  weekday =7;
  month =1;
  century =20;
  year =13;
  rtc.setDate(day,weekday, month, century, year);
}

void loop()
{
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 0);
  lcd.print(rtc.formatTime());
  lcd.setCursor(0, 1);
  lcd.print(rtc.formatDate());
  delay(1000);

  if(digitalRead(minButton) == HIGH)
    changeTime();
}

void changeTime(){

  byte hour = 5;
  byte min = 30;
  byte sec = 14;

  hour = 18;
  min = 20;
  sec = 00;
  rtc.setTime(hour, min,sec);

  if(digitalRead(minButton) == HIGH)
  {
    min += 1;  
  }
}
56  Using Arduino / Programming Questions / Re: changing clock/date setting on Real Time Clock pcf8563 on: January 20, 2013, 05:28:53 pm

Code:
if(digitalRead(minButton) == HIGH)
   {
      min += 1;
   }

I managed to reset the time back to the defaults but I cant get the minute to increase based in the snippet you gave.

I changed the code around since then and it does not do anything now.

Code:
#include <Wire.h>
#include <Rtc_Pcf8563.h>
#include <LiquidCrystal.h>

//init the real time clock
Rtc_Pcf8563 rtc;


LiquidCrystal lcd(7,8,9,10,11,12);

int minButton = 42;

void setup()
{
  lcd.begin(16, 2);
  pinMode (minButton,INPUT);
  //clear out all the registers
  rtc.initClock();
  //set a time to start with.
  //day, weekday, month, century, year
  byte day = 1;
  byte weekday= 1;
  byte month = 1;
  byte century =0;
  byte year = 13;

  day =20;
  weekday =7;
  month =1;
  century =20;
  year =13;
  rtc.setDate(day,weekday, month, century, year);
}

void loop()
{
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 0);
  lcd.print(rtc.formatTime());
  lcd.setCursor(0, 1);
  lcd.print(rtc.formatDate());
  delay(1000);
}

void changeTime(){

  byte hour = 5;
  byte min = digitalRead(minButton);
  byte sec = 14;

  hour = 18;
  min = 20;
  sec = 00;
  rtc.setTime(hour, min,sec);

  if(digitalRead(minButton) == HIGH)
  {
    min += 1; 
  }
}
57  Using Arduino / Programming Questions / Re: changing clock/date setting on Real Time Clock pcf8563 on: January 20, 2013, 01:04:26 pm
Write some simple code, in setup():

Code:
byte hour = 5;
byte min = 10;
byte sec = 14;

hour += 3;
min +=27;
sec +=10;

rtc.setTime(hour, min, sec);

What time does the RTC get set to? Read the time in loop(), and print the values.

Tme is set to 08:37:24. in which case I kinda understand.
58  Using Arduino / Programming Questions / Re: changing clock/date setting on Real Time Clock pcf8563 on: January 20, 2013, 12:29:13 pm
Code:
rtc.setTime(00, 30, 11)

In the snippet above, if I change the variables 00,30,11 to the current time. Then compile the sketch, the time on the lcd is updated. I am trying to get away from compiling the sketch each time JUST to update the "Time".

but I dont know how to enter the variables from my sketch AFTER it has been compliled.

the original sketch is here

Code:
/* Demonstration of Rtc_Pcf8563 Clock on LCD.
 *
 * 
 * This sketch connects a lcd to display the clock output.
 *
 * setup:  see Pcf8563 data sheet.
 *         1x 10Kohm pullup on Pin3 INT
 *         No pullups on Pin5 or Pin6 (I2C internals used)
 *         1x 0.1pf on power
 *         1x 32khz chrystal
 *         1x h44780 LCD
 *
 * Joe Robertson, jmr
 * orbitalair@bellsouth.net
 */

#include <Wire.h>
#include <Rtc_Pcf8563.h>
/* add the lcd support */
#include <LiquidCrystal.h>

//init the real time clock
Rtc_Pcf8563 rtc;

/* initialize the library objects */
LiquidCrystal lcd(7,8,9,10,11,12);

void setup()
{
  // set up the LCD's number of rows and columns:
  lcd.begin(16, 2);

  //clear out all the registers
  rtc.initClock();
  //set a time to start with.
  //day, weekday, month, century, year
  rtc.setDate(14, 6, 3, 0, 10);
  //hr, min, sec
  rtc.setTime(1, 15, 40);
}

void loop()
{
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 0);
  lcd.print(rtc.formatTime());
  lcd.setCursor(0, 1);
  lcd.print(rtc.formatDate());

  delay(1000);

}

I am going to use push buttons to change the "hour" "minute" and "sec". but "rtc.setTime(1, 15, 40);" how will the sketch know what 1,15,40 is. In the library these appear to be

Code:
setTime(byte sec, byte minute, byte hour)

so my thinking is byte sec, byte minute, byte hour tell ME what "(1, 15, 40)" are and therfore I would create a function to change byte sec, byte minute, byte hour byte
59  Using Arduino / Programming Questions / Re: changing clock/date setting on Real Time Clock pcf8563 on: January 20, 2013, 11:05:22 am
Quote
No, you use the setTime method. Lets say you want to set it to 11:30, then you'd do something like this...

Code:
//setTime(byte sec, byte minute, byte hour)
rtc.setTime(00, 30, 11)

I dont think it's as simple as that. It looks like I need to set some sort of pointer to each of the variables "hour", "min","sec".
I found this in the library

Code:
setTime(byte sec, byte minute, byte hour)

which I believe is

Code:
rtc.setTime(00, 30, 11)

So I think I need to interact with/point to, the "byte sec, byte minute, byte hour". If i can manipulate those at the byte level I think it might work?
60  Using Arduino / Programming Questions / Re: changing clock/date setting on Real Time Clock pcf8563 on: January 20, 2013, 07:14:28 am
I have an lcd and a Pcf8563 Real Time Clock. I have the following sketch which is used to set the time and date.

Code:
/* Demonstration of Rtc_Pcf8563 Clock on LCD.
 *
 * I used a Arduino mega 2560,
 * SCL -21, SDA - 20,
 *
 * This sketch connects a lcd to display the clock output.
 *  written by               
 * Joe Robertson, jmr
 * orbitalair@bellsouth.net
 */

#include <Wire.h>
#include <Rtc_Pcf8563.h>
/* add the lcd support */
#include <LiquidCrystal.h>

//init the real time clock
Rtc_Pcf8563 rtc;

/* initialize the library objects */

LiquidCrystal lcd(7,8,9,10,11,12);

void setup()
{
  // set up the LCD's number of rows and columns:
  lcd.begin(16, 2);
 
  //clear out all the registers
  rtc.initClock();
  //set a time to start with.
  //day, weekday, month, century, year
  rtc.setDate(14, 6, 3, 0, 10);
  //hr, min, sec
  rtc.setTime(1, 15, 40);
}

void loop()
{
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 0);
  //lcd.print(rtc.formatTime(RTCC_TIME_HM));
  lcd.print(rtc.formatTime());
  lcd.setCursor(0, 1);
  //lcd.print(rtc.formatDate(RTCC_DATE_ASIA));
  lcd.print(rtc.formatDate());
 
  delay(1000);
   
}

The sketch allows for the time and date to be set before the sketch is compliled. But I want to SET both using push buttons to change time and date variables.
The Libray for this  "Rtc_Pcf8563.h", has the code for changing the time and date. So would the answer lie in the library. Do I cut and past the code from the libray into the sketch above .
Pages: 1 2 3 [4] 5 6 ... 8