Show Posts
Pages: 1 ... 7 8 [9] 10
121  Forum 2005-2010 (read only) / Syntax & Programs / Re: #define vs. const variable on: June 06, 2010, 07:01:29 pm
Well, it's been more than 6 months since I last read this post and I am learning a lot. smiley-sweat I'm learning more on the use of preprocessor commands, and #defining macros. Apparently you can do quite a few very useful things with #define. It's not just for defining constants! smiley-eek I've learned so much from this forum as well as the playground. Another thing I have found to be an invaluable method for learning how to achive specific tasks, and even better, tasks I had not even thought of, is to open and study all the library files. Most of them are very well commented, explaining every step of the way through the code. Even if you don't understand some of the concepts being used when you first read through it, you will remember seeing it before and now it makes sense.

Once again, thanks to everyone on this forum for their contributions smiley-grin,
DJ   
122  Forum 2005-2010 (read only) / Syntax & Programs / Re: #define vs. const variable on: June 02, 2010, 04:25:21 pm
I understand a little better now. As far as checking the assembly code to see which is better, I, like PaulS,  am not qualified to do. :-/

Thanx again for the info. guys,
DigitalJohnson
123  Forum 2005-2010 (read only) / Syntax & Programs / #define vs. const variable on: June 02, 2010, 01:17:43 pm
I've read somewhere that it's better to use a const variable rather than use #define. However, I also read that #define uses no program memory or RAM after compiling. So now I'm not sure which is better to use, or if one is better than the other in different cases. If any Admins or God Members read this, please post a reply I'm very interested hearing the facts.

Thanks, DigitalJohnson
124  Forum 2005-2010 (read only) / Syntax & Programs / Re: A question about file extentions of tabs in IDE. on: May 20, 2010, 08:36:19 am
I never thought of it that way. So, as an intermediate programmer, what I'm really looking for is more examples of use (the experience of others (people like you)). smiley-razz

I'm off to work now. Thanks PaulS,
DigitalJohnson
125  Forum 2005-2010 (read only) / Syntax & Programs / Re: A question about file extentions of tabs in IDE. on: May 20, 2010, 07:56:44 am
Thanks for the quick replies PaulS.
I'm pretty new to C, but I think I'm picking it up fairly quickly. If I understand you correctly, I put my function definitions  into an .h file. Then my use of those functions into a .c/.cpp file. Is that a correct way of putting it? Or am I misunderstanding what you mean by implementation? What I've been doing is to put related function definitions and as much of the use of those functions as I can into a header file only.

Thanks again for taking the time to explain things to me. Is there any literature on this that is easy to understand. I pick things up quickly, but I've noticed when trying to research some new aspect of the C language, there's alot of stuff for beginers (very basic) or advanced users (very technical/unfamiliar terms used). I'm somewhere in the middle.

Again thanks for any help.
DigitalJohnson
126  Forum 2005-2010 (read only) / Syntax & Programs / Re: A question about file extentions of tabs in IDE. on: May 20, 2010, 05:48:37 am
Thanks for the reply PaulS.
  I need to clarify. I understand the use of .h as tab ext's. What is the use of/how do I use .c or .cpp as a tab ext. Just to see what would happen, I changed the extention of a tabbed file from .h to c. and changed the #ifndef, #define, #endif and #include to match and got errors(as I expected to).
127  Forum 2005-2010 (read only) / Syntax & Programs / A question about file extentions of tabs in IDE. on: May 19, 2010, 11:40:21 pm
  I'm starting to write larger sketches and finding out how important the organization of code is the larger a program becomes. I'm using the Arduino IDE to compsose my code and started using tabs to break the code into semi-managable chunks. When I named the tabs I didn't give any extention to the names (didn't even know I could). But the IDE would treat the code as if it were just one big file.

  Then I learned a little about making them into header files and "#include"ing them into the main sketch which enables me to isolate the different sections of code and localize the variables within them. Very useful!

  Somewhere in the learning section on the Arduino site  I recently came accross this -

Tabs, Multiple Files, and Compilation:

Allows you to manage sketches with more than one file (each of which appears in its own tab). These can be normal Arduino code files (no extension), C files (.c extension), C++ files (.cpp), or header files (.h).

  My question is, what is the difference/use of using the .c or .cpp extentions in the tabs of the Arduino IDE. Is there any benifit in doing that. I know that libraries have to have both .h and .c files. Would I be better off learning how to write libraries. Which, by the way, is a little confusing. I can't seem to find much about how to write libraries that doesn't asume the reader has a fair knowledge of C language.

A little babbling about myself.
  I've only been using C for 5-6 months. I used to write programs in machine language on my Commodore 64 (yes I'm that old). About the time windows 95 came out I started using PC's and there was so much software already available I didn't see the need to write programs anymore. But, I've been into electronics since I was 9. So when I came across the Arduino while browsing the internet the geek in me had to have one. Now I've got some ideas I'd like to develope, maybe even make a buck or two. So, now I have to start programming again  :smiley.

P.S. I give thanks to the Arduino team for the devices they've created, which allow the rest of us to more easily and very affordably create our own devices of the future. (And thanks to everyone in the Open-Source movement)
128  Forum 2005-2010 (read only) / Syntax & Programs / Re: need help or suggestion on syntax. on: February 23, 2010, 08:24:07 pm
Thanks for the rapid response guys.
I tried your (AWOL and PaulS) suggestions and got compile errors. After tinkering with the syntax I was able to compile and upload. But I got some bizzare results. i.e. buttons seemed to be switched around randomly (button1 became button4, etc.) also the digits being displayed were not incrementing correctly (1, 9, 5, 2, etc.).
I do have a working sketch, I was just trying to simplify my code.

I was wondering if anyone could explain my bizzare results. Thanks.

I am using an Arduino Mega and IDE v18.
Here is my sketch:

I remarked out the bad code just above the working code.

Code:
/*
 * Electronic Door Lock.
 * Created 2/23/10 by Corey Johnson
*/

#include <avr/sleep.h>
#include <TimedAction.h>
#include <Streaming.h>
#include <Bounce.h>

#define ledPower 7
#define button1 6
#define button2 5
#define button3 4
#define button4 3
#define button5 2

/*
  * This code does not work correctly. Why?
 * Bounce bounce[5] = {bounce[0] = Bounce(button1, 15), bounce[1] = Bounce(button2, 15), bounce[2] = Bounce(button3, 15), bounce[3] = Bounce(button4, 15), bounce[4] = Bounce(button5, 15)};
 */

// This code works correctly.
Bounce bounce1 = Bounce(button1, 15);
Bounce bounce2 = Bounce(button2, 15);
Bounce bounce3 = Bounce(button3, 15);
Bounce bounce4 = Bounce(button4, 15);
Bounce bounce5 = Bounce(button5, 15);

TimedAction timedAction = TimedAction(100, 60000, sleep);

// This is the Unlock code (in HEX) to unlock the door.
byte code[4] = {0x31, 0x32, 0x33, 0x34}; // Correct ode is "1234".
byte digit[4]; // This array holds the current digits as seen on the LED display.

byte sleepStatus = 0;

void setup() {
  
  Serial.begin(115200); // For serial monitor (for debugging and future features).
  Serial1.begin(9600); // For sending data to serial 7-segment display.
  digitsReset();
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(button4, INPUT);
  pinMode(button5, INPUT);
  pinMode(ledPower, OUTPUT);
  digitalWrite(button1, HIGH);
  digitalWrite(button2, HIGH);
  digitalWrite(button3, HIGH);
  digitalWrite(button4, HIGH);
  digitalWrite(button5, HIGH);
  digitalWrite(ledPower, HIGH);
}

void loop() {
  timedAction.check();
  boolean enterCode = buttonScan(); // scan  all buttons and return with button5 state.
  digitsOut();
  if (enterCode == LOW) {
    boolean yesNo = checkCode();
    if (yesNo == HIGH) {
      digitalWrite(ledPower, LOW);
      Serial << "Code is correct. \n";
      delay(500);
      Serial << "Door is Unlocked! \n";
      delay(6000);
      /*
       * This is where the code to control the lock will go.
      */
      Serial << "Door is now Locked! \n";
      digitsReset();
    }
    else {
      digitalWrite(ledPower, LOW);
      Serial << " \n" << "Incorrect Code!!! \n" << " \n";
      delay(500);
      Serial << "Door Remains Locked! \n";
      delay(2000);
      digitsReset();
    }
  }
}

void digitsReset() {
  for (byte i = 0; i <= 3; i++) {
    digit[i] = 0x30;
  }
  digitalWrite(ledPower, HIGH);
  delay(100);
  digitsOut();
  timedAction.reset();
  return;
}

void digitsOut() {
  for (byte i = 0; i <= 3; i++) {
    Serial1 << byte(digit[i]);
  }
  return;
}

boolean buttonScan() {
  boolean digitVal;
/*
 *
 * ?? This is the code that is not working correctly. Why?
 *
 * for (byte i = 0; i <= 4; i++) {
 * bounce[i].update();
 * digitVal = bounce[i].read();
 * if (i < 5) {
 *   if (digitVal == LOW) {
 *     if (digit[i - 1] == 0x39) {
 *       digit[i - 1] = 0x30;
 *     }
 *     else {
 *       digit[i - 1] = (digit[i - 1] + 0x01);
 *     }
 *   }
 * }
 * delay(100);
 *
*/

// This code works correctly.
  bounce1.update();
  digitVal = bounce1.read();
  if (digitVal == LOW) {
    if (digit[0] == 0x39) {
      digit[0] = 0x30;
    }
    else {
      digit[0]++;
    }
  }
  bounce2.update();
  digitVal = bounce2.read();
  if (digitVal == LOW) {
    if (digit[1] == 0x39) {
      digit[1] = 0x30;
    }
    else {
      digit[1]++;
    }
  }
  bounce3.update();
  digitVal = bounce3.read();
  if (digitVal == LOW) {
    if (digit[2] == 0x39) {
      digit[2] = 0x30;
    }
    else {
      digit[2]++;
    }
  }
  bounce4.update();
  digitVal = bounce4.read();
  if (digitVal == LOW) {
    if (digit[3] == 0x39) {
      digit[3] = 0x30;
    }
    else {
      digit[3]++;
    }
  }
  bounce5.update();
  digitVal = bounce5.read();
  delay(250);
  return digitVal;
}

// Function to compare entered code with Unlock code.
boolean checkCode() {
  boolean goodBad;
  if ((((digit[0] == code[0]) && (digit[1] == code[1])) && (digit[2] == code[2])) && (digit[3] == code[3])) {
    goodBad = HIGH;
  }
  else {
    goodBad = LOW;
  }
  digitsReset();
  return goodBad;
}

// Fuction to put the arduino into sleep mode.
void sleep() {
  digitalWrite(ledPower, LOW);
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  attachInterrupt(0, wakeUp, LOW);
  sleep_mode(); // Arduino Enters sleep mode at this Point.
  /*
   * Sleeping - ZzZzZzZzZz...
   * Pressing button 5 will wake-up arduino.
  */
  sleep_disable(); // Arduino Exits sleep mode at this point.
  detachInterrupt(0);
  digitalWrite(ledPower, HIGH);
}

void wakeUp() {
}
129  Forum 2005-2010 (read only) / Syntax & Programs / need help or suggestion on syntax. on: February 23, 2010, 03:55:32 pm
I am reading 5 buttons with the Bounce function (bounce1, bounce2, etc...). I would like to read them with a 'for loop'. Is there a way to do this with say a 'bounce(x).read' or something like that?
130  Forum 2005-2010 (read only) / Development / Re: AVRProjectIDE - Editor for AVR/Arduino Projects on: May 20, 2010, 05:02:25 am
Hi Frank,
First, GREAT WORK! I love the look and feel of your IDE. I think I'm really going to like using it over the Arduino IDE. It has way more features, and I always like more features. It was easy to get it going with my ArduinoMEGA. I have encountered a problem though, but I'm not sure if it's your IDE. I loaded a sketch I wrote with the arduino IDE that has multiple header files as tabs in that IDE. It compiles and runs fine with the arduino IDE. When I try to compile with AVRprojectIDE I get a bunch of 'multiple definition of' errors occuring in the library files I've included. The strange thing is, the line number of the first definition is the same line number of the multiple definition error in every occurence of that type of error. Next, I'm getting some 'was not declared in this scope' errors. I get some 'This file has been moved to' errors. Some 'Initialization makes integer from pointer without a cast' errors, 'Only initialized variables can be placed into program memory area' errors, and one 'comparison between signed and unsigned integer expression' error. The scope errors are from function declarations in tabbed header files I wrote. All other errors are occuring inside included libraries (Tone & wiring). I have several other libraries included also, no errors with them. I've heard of, but not used the IDE's you've based yours on, so I'm not sure if these are bugs or if I have to change my syntax for this IDE.

  Anyway, I still think I'll like your IDE better.

             Thanks, DigitalJohnson
131  Forum 2005-2010 (read only) / Development / Re: Develop arduino without arduino (simulator?) on: December 30, 2010, 12:29:14 am
@ Bean Head
@ PualS

I believe that name calling or derogatory remarks toward others shows bad character. This forum, or any other for that matter, can do without that sort of thing. Don't you agree?

@ scarecrow47
@ Bean Head

I don't see  what PaulS wrote that was so offensive to you. He asked a simple question.  ...And now we've all gotten off topic, haven't we? :'(
DJ
132  Forum 2005-2010 (read only) / Development / I2C 8/16- bit I/O Expander Library on: December 28, 2010, 05:17:52 am
The 'i2c_iox' Library. This library is for both 8-bit and 16-bit I2C I/O expander devices. I posted the library on google here:http://code.google.com/p/i2c-iox-library/downloads/list

I've only tested the library with a Texas Instraments PCA9539 (It's the only device I have at this time). The library has been working with the tests I've ran so far. I do welcome any feedback from others who'd like to test it as well. In particular I could use feedback from people with 8-bit devices, or even better, those with both 8 and 16 bit devices. The example sketch is setup to use one 8-bit device and one 16-bit device at the same time. See the included readme.txt file for information and instructions on how to use the library. I hope others will find this library usefull.

Enjoy! [smiley=thumbsup.gif],
DigitalJohnson

update:

It's been a while since I posted this library and I was hoping to get some feedback on it. smiley-mad The web page shows 17 downloads to date (I was one of them(DL test)). I know that's not many. smiley-sad However, would one of the 16 people that downloaded it, please tell me what you think. I'm always looking to improve my coding skills. Maybe you'd like some feature added or can suggest an improvement to my code. The way I see it, everyone here can learn something from everyone else on this forum at some point. So speak up! smiley-yell

Thanks to those who DL'd my library. smiley
I hope it's useful to you. smiley-grin
133  Forum 2005-2010 (read only) / Development / Re: Need help with an error in my library on: December 22, 2010, 07:42:16 pm
@ robtillaart

I'm going to do more testing today. I only have a mega, but the I2C interface should be the same on other arduinos, correct? They just use different pins as I understand it. Anyway, after I've tested the library I'll write an example sketch with some documentation and put it on the playground for everyone. I'm pretty busy right now (X-mas and all) so I should have it posted in a day or two.

Thanks for everyones input,
DJ
134  Forum 2005-2010 (read only) / Development / Re: Need help with an error in my library on: December 21, 2010, 01:33:46 am
I found my "other" mistake. I didn't change the variable names in the function declarations :smiley. Thanks for the help guys, it works fine now! smiley

I'm glad there's a forum for this stuff,
DJ
135  Forum 2005-2010 (read only) / Development / Re: Need help with an error in my library on: December 21, 2010, 01:07:07 am
Perhaps it's the IDE I'm using. I used AVR Project IDE. I'll try the library with the arduino IDE and let you know.

DJ
Pages: 1 ... 7 8 [9] 10