Show Posts
Pages: 1 [2] 3 4 ... 8
16  Using Arduino / Programming Questions / Re: help needed with code on: July 14, 2013, 03:59:33 am
bump smiley-cry
17  Using Arduino / Programming Questions / Re: lcd.print or lcd.write on: June 05, 2013, 05:07:07 am
That's what I call an excellent explanation. Thank you I now understand. smiley-grin

karma awarded. smiley-cool
18  Using Arduino / Programming Questions / lcd.print or lcd.write on: June 05, 2013, 02:12:06 am
lcd.print or lcd.write, Both seem to do the same thing but when would you use one over the other
19  Using Arduino / Programming Questions / Serial to Lcd on: June 04, 2013, 05:02:22 pm
how do I convert this Sketch to use an LCD instead of using Serial.

Code:
/*
CHANGE TIME SKETCH
 
 */

#include <Wire.h>
const int Pcf_RTC = 0x51;

const char* days[] =
{
  "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

const char* months[] =
{
  "January", "February", "March", "April", "May", "June", "July", "August",
  "September", "October", "November", "December"};

byte result;
byte second = 0;
byte minute = 0;
byte hour = 0;
byte monthday = 0;// 1-31
byte weekday = 0; // 1-7 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
byte month = 0; // "January", "February", "March", ect
byte year = 0;
byte second_old;
byte minute_old;
byte hour_old;
byte day;
byte century;

void setup() {
  Wire.begin();
  Serial.begin(9600);
  delay(2000);
  // Serial.print("The current date and time is: ");
  // printTime();
  Serial.println("To set the date and time please select Newline ending to continue.");
  Serial.println("Would you like to set the date and time now? Y/N");
  while (!Serial.available()) delay(10);
  if (Serial.read() == 'y' || Serial.read() == 'Y') {
    Serial.read();
    setTime();
    // Serial.print("The current date and time is now: ");
    printTime();
  }
  Serial.println("Goodbye.");
}


void loop() {
  printTime();
}


byte decToBcd(byte val) {
  return ((val/10*16) + (val%10));
}
byte bcdToDec(byte val) {
  return ((val/16*10) + (val%16));
}


void setTime() {

  second = 0;

  Serial.print("Please enter the current minute, 0-59. - ");
  minute = readByte();
  Serial.println(minute);

  Serial.print("Please enter the current hour in 24hr format, 0-23. - ");
  hour = readByte();
  Serial.println(hour);

  Serial.print("Please enter the current day of the month, 1-31. - ");
  monthday = readByte();
  Serial.println(monthday);

  Serial.println("Please enter the current day of the week, 1-7.");
  Serial.print("1 Sun | 2 Mon | 3 Tues | 4 Weds | 5 Thu | 6 Fri | 7 Sat - ");
  weekday = readByte();
  Serial.println(days[weekday-1]);

  Serial.print("Please enter the current month, 1-12. - ");
  month = readByte();
  Serial.println(months[month-1]);

  Serial.print("Please enter the current year, 00-99. - ");
  year = readByte();
  Serial.println(year);


  Serial.println("Thank you.");


  Wire.beginTransmission(Pcf_RTC);
  Wire.write(byte(0));
  Wire.write(byte(0));
  Wire.write(byte(0));
  Wire.write(decToBcd(second));
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));
  Wire.write(decToBcd(monthday));
  Wire.write(decToBcd(weekday));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));
  Wire.write(byte(0));
  Wire.endTransmission();
}


byte readByte() {
  while (!Serial.available()) delay(10);
  byte reading = 0;
  byte incomingByte = Serial.read();
  while (incomingByte != '\n') {
    if (incomingByte >= '0' && incomingByte <= '9')
      reading = reading * 10 + (incomingByte - '0');
    else;
    incomingByte = Serial.read();
  }
  Serial.flush();
  return reading;
}


void printTime() {
  Wire.beginTransmission(Pcf_RTC);
  Wire.write(0x02); // Start address
  result = Wire.endTransmission();
  //#ifdef DEBUG
  //  Serial.print("Result of asking for date and time: ");
  //  Serial.println(result, DEC);
  //#endif
  // if (result) SetError(ERROR_RTC_GET);

  Wire.requestFrom(Pcf_RTC, 1);
  second = Wire.read();
  // if (second & 0x80) SetError(ERROR_CLOCK_INTEGRITY);
  second = BcdToDec(second & 0b01111111);
  if (second != second_old) // Cycle begins only when it has changed
  {
    second_old = second;
    if (second == 0) // If second is zero I need to ask for the minute
    {
      Wire.requestFrom(Pcf_RTC, 1);
      minute = BcdToDec(Wire.read());
      if (minute != minute_old) // Cycle begins only when it has changed
      {
        minute_old = minute;
        if (minute == 0) // If minute is zero I need to ask for the hour
        {
          Wire.requestFrom(Pcf_RTC, 1);
          hour = BcdToDec(Wire.read());
          if (hour != hour_old) // Cycle begins only when it has changed
          {
            hour_old = hour;
            if (hour == 0) // If hour is zero I need to ask for other elements
            {
              Wire.requestFrom(Pcf_RTC, 4);
              day = BcdToDec(Wire.read());
              weekday = BcdToDec(Wire.read());
              month = Wire.read();
              century = (month & 0x80);
              month = BcdToDec(month & 0b01111111);
              year = BcdToDec(Wire.read());
            }
          }
        }
      }
    }
    Serial.print(days[weekday]);
    Serial.print(""); //(");
    //Serial.print(weekday, DEC);
    //Serial.print(")");
    Serial.print(" ");
    if (monthday < 10) Serial.print("0");
    Serial.print(monthday, DEC);

    Serial.print("-");
    if (month < 10) Serial.print("0");
    Serial.print(month,DEC);
    Serial.print("-");

    if (century)
      Serial.print("21");
    else
      Serial.print("20");
    if (year < 10) Serial.print("0");
    Serial.print(year, DEC);

    Serial.print(" ");
    if (hour < 10) Serial.print("0");
    Serial.print(hour,DEC);

    Serial.print(":");
    if (minute < 10) Serial.print("0");
    Serial.print(minute, DEC);

    Serial.print(":");
    if (second < 10) Serial.print("0");
    Serial.println(second, DEC);
  }
}

// Converts a BCD (binary coded decimal) to decimal
byte BcdToDec(byte value)
{
  return ((value / 16) * 10 + value % 16);
}

// Converts a decimal to BCD (binary coded decimal)
byte DecToBcd(byte value){
  return (value / 10 * 16 + value % 10);
}




void readTime() {
  Wire.beginTransmission(Pcf_RTC);
  Wire.write(byte(0));
  Wire.endTransmission();
  Wire.requestFrom(Pcf_RTC, 7);
  second = bcdToDec(Wire.read());
  minute = bcdToDec(Wire.read());
  hour = bcdToDec(Wire.read());
  monthday = bcdToDec(Wire.read());
  weekday = bcdToDec(Wire.read());
  month = bcdToDec(Wire.read());
  year = bcdToDec(Wire.read());
}

20  Using Arduino / Programming Questions / help needed with code on: June 03, 2013, 04:10:29 am
From power up the LCD shows Time and Date

I can navigate the menu but seem to be always stuck in case 1: or case 2: I cant seem to change the Rtc parameters as I just get put back into the menu.

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

I then navigate the menus using the encoder

Code:

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
}

Then using case 1; or case 2 of the void navigateMenus, I move Left/Right through the menu till I get to for example "Min" from void menuChanged.

Code:

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");
  }
}

 Now that I have "Min" displayed on the LCD. I select it by pressing the encoder push button on pin 45.

Code:

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.
  }
}

This now shows the LCD displaying the clock. because of the function changeMin() called from void menuUsed above
Code:

void changeMin(){

  {
    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);
}

At this point If I use the encoder case 1 or case 2, it does not change the Mins on the clock. It simply navigates the menu left or right.

but if I press the encoder push button, the minutes change... hope this helps.  I have included attachment of the Full code below.

21  Using Arduino / Programming Questions / Re: Something not right on: May 27, 2013, 06:07:57 pm
Almost the ENTIRE sketch is inside the interrupt service routine.  smiley-eek Interrupts are for extreemly time critical tasks and have to be kept short else it disturbs all other interrupts (like millis() driving servos etc).  smiley-eek-blue They are not a way to avoid writing a proper loop().... smiley-roll

I never was keen on interrupts, the code I was using was lifted from someone Else's sketch, as I could not get my Original code to work. in hind sight I think Help is better focused on my Original problem which is below. I think this is easier to solve and any insight into this would be appreciated. I have awarded karma to all of you for you help so far.

From power up the LCD shows Time and Date

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

I then navigate the menus using the encoder

Code:
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
}

Then using case 1; or case 2 of the void navigateMenus, I move Left/Right through the menu till I get to for example "Min" from void menuChanged.

Code:
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");
  }
}

 Now that I have "Min" displayed on the LCD. I select it by pressing the encoder push button on pin 45.

Code:
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.
  }
}

This now shows the LCD displaying the clock. because of the function changeMin() called from void menuUsed above

Code:
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);
}

At this point If I use the encoder case 1 or case 2, it does not change the Mins on the clock. It simply navigates the menu left or right.

but if I press the encoder push button, the minutes change... I would like the enocder to change Min up/down instead of the encoder push button.
22  Using Arduino / Programming Questions / Re: Something not right on: May 27, 2013, 12:22:59 pm
Quote
I don't see any pull-up or pull-down resistors in the external circuit

They are their, if you click the image it will expand and you can see them.

Quote
Does it provide some way for you to know that the button has been released after being pressed

yes. that is done in the button library. ".waspressed" ".isPressed".

Quote
Does it actually give you the encoder movement events you're expecting when you expect them?

yes it does. The encoder navigates the menu ok but when the lcd displays change colour and I press BUTTONR it should call "void settingsChangeColour()" but nothing happens. I can still rotate the encoder through the menu "empty 1", "empty 2" etc.

23  Using Arduino / Programming Questions / Re: Something not right on: May 27, 2013, 10:02:31 am
Additional link added smiley-cool

http://www.flickr.com/photos/96527043@N02/
24  Using Arduino / Programming Questions / Re: Something not right on: May 27, 2013, 02:23:24 am
Thanks for replying. I have added a photo of the hardware setup picture below. I also swapped the BUTTONR and BUTTON around but I get the same result.

additional link

http://www.flickr.com/photos/96527043@N02/
25  Using Arduino / Programming Questions / Something not right on: May 26, 2013, 03:07:02 pm
When I press BUTTONR on "change colour" it does not switch to "settingsChangeColour();" and run that code. I'm using an arduino mega and BUTTONR is on pin 2 and BUTTON 1 is on pin 3.

Can anyone shed some light on this. If I can get this working I can finish my menu.

Code:
// Include LCD Library Code
#include <LiquidCrystal.h>
// Include Rotary Encoder Library
#include <QuadEncoder.h>
#include <Button.h>

// control
#define ROTENCA 43
#define ROTENCB 45
#define BUTTONR 2 // Intr 0 = Pin 2 encoder push button
#define BUTTON 1 // Intr 1 = Pin 3 push button

// initialize the LCD with the numbers of the interface pins
LiquidCrystal lcd(7,8,9,10,11,12);

// initialize the encoder
QuadEncoder encoder(ROTENCA,ROTENCB); // initialize the encoder
// has the encoder moved on this loop?
boolean moved = false;

Button buttonR (BUTTONR);

void setup() {
  // setup interrupts
  attachInterrupt(BUTTON, displayMenu, RISING); //RISING IS ON THE ENCODER
  // set the LCD's number of cols and lines
  lcd.begin(16, 2);
}

void loop() {
  lcd.setCursor(1,0);
  lcd.print("*** WELCOME! ***");

  //lcd.print("T"); 
}

void displayMenu() {
  int selected = 3;

  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Turn the knob to");
  lcd.setCursor(0,1);
  lcd.print("select an option");
  // display options
  while (!buttonR.wasPressed()) {
    selected += readEncoder();// selected = encoder
    if (selected<0) selected=3;
    if (selected>3) selected=0;
    if (moved) {
      lcd.clear();
      lcd.setCursor(0,0);
      switch(selected) {
      case 0:
        lcd.print("Change Colour    ");
        break;
      case 1:
        lcd.print("empyt 1");
        break;
      case 2:
        lcd.print("empty 2");
        break;
      case 3:
        lcd.print("Exit Settings   ");
        break;
      }
    }
  }// moving this to the bottom makes menuseltion part work

    // when button is pressed...
  lcd.clear();
  switch(selected) {
  case 0:
    settingsChangeColour();
    break;
  case 1:
    // to be added
    break;
  case 2:
    // to be added
    break;
  }
}

void settingsChangeColour() {
  int selected = 2;
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Select Colour");
  lcd.setCursor(0,1);
  lcd.print("Currently it's ");
  //lcd.write(temperatureUnit);
  while (!buttonR.wasPressed()) {
    selected += readEncoder();
    if (selected<0) selected=2;
    if (selected>2) selected=0;
    if (moved) {
      lcd.setCursor(0,1);
      switch(selected) {
      case 0:
        lcd.print("Red");
        break;
      case 1:
        lcd.print("Yellow");
        break;
      case 2:
        lcd.print("Cancel");
        break;
      }
    }

    // when button is pressed...
    switch(selected) {
    case 0:
      lcd.print ("yes, it's red");
      break;
    case 1:
      lcd.print ("yes, it's yellow");
      break;
    }
  }
}     

// returns 1 for right, -1 for left, or 0 for no movement
int readEncoder() {
  moved = true;
  switch(encoder.hb()) {
  case '>':
    return 1;
  case '<':
    return -1;
  }
  moved = false;
  return 0;
}
26  Using Arduino / Programming Questions / Re: Does this look right. on: May 24, 2013, 03:58:10 pm

Quote
Look at the different interrupt to pin assignments for the Uno and the Leonardo

Yes, I see that. CrossRoads was referring to the UNO

The 1 & 2 in Button 1 & Button 2 refer to D1 and D2.

INT0 is on D2, and INT1 is on D3, if that is what you are after (on '328P boards).

So your both correct. smiley-wink
27  Using Arduino / Programming Questions / Re: Does this look right. on: May 24, 2013, 01:13:35 pm
Quote
When you attach an interrupt, interrupt zero is on Arduino Pin 2, interrupt one is on pin 3.
Except when it's not http://arduino.cc/en/Reference/attachInterrupt

when is the mega pin 2 and pin 3 not intr 0, intr 1, not sure what you mean. smiley-red
28  Using Arduino / Programming Questions / Re: Does this look right. on: May 24, 2013, 01:11:33 pm
Quote
#define BUTTON 3 // Intr 1 = Pin 3 turn knob
Why the hell is the "turn knob" called BUTTON?

thanks changing that solved all my issues...........fail.

I'd value your help more than semantics.
29  Using Arduino / Programming Questions / Re: flowmeters on: May 24, 2013, 01:53:43 am
Quote
Serial.print (Calc, 5); //Prints the number calculated above
  Serial.print (" L/min"); //Prints "L/hour" and returns a  new line
  Serial.print ("   ");
  Serial.print (Vazao, 5);
  Serial.print (" L/min");
  Serial.println ();

Quote
Serial.print (" L/min"); //Prints "L/hour" and returns a  new line

Hour ?

Quote
returns a  new line

 no it wont...........Serial.println  prints on a new line try using that "Between" your code to split the flow meters

 
30  Using Arduino / Programming Questions / Re: Does this look right. on: May 23, 2013, 02:34:47 pm
Thank you all.

Quote
The 1 & 2 in Button 1 & Button 2 refer to D1 and D2.

INT0 is on D2, and INT1 is on D3, if that is what you are after (on '328P boards).

I'm using the mega 2560. and have changed pins to

Code:
#define BUTTONR 2 // Intr 0 = Pin 2 encoder push button
#define BUTTON 3 // Intr 1 = Pin 3

but the button stops working. Even though intr 1 is on Pin3
Pages: 1 [2] 3 4 ... 8