Show Posts
Pages: 1 ... 4 5 [6] 7 8 ... 48
76  Using Arduino / Programming Questions / Re: Serial read more than one thing on: July 07, 2014, 09:54:48 pm
A couple of ideas:

I think the code in your for loop would be easier to read if you used a switch:

    switch (myData) {

      case '1':
        Serial.println("LED on");

      case '2'
        Serial.println("LED off");
        Serial.println("Shouldn't be here...");

Of course, that's personal preference. Second, use toupper() to convert all the characters to upper case, then do a substring search for " ON" or " OFF", and act accordingly. That way, the user has some flexibility in the way they pass the input to your program. That is, "TURN LED ON", "LED ON", "POWER LED ON", etc. all produce the desired result. Notice the leading space so some words with "ON" in them don't trigger a false response.
77  Using Arduino / Programming Questions / Re: Problem with variable declaration on: July 07, 2014, 07:00:13 pm
No, your use of exp() is fine. I believe the library uses a float as its argument.

I have not looked at the assembler for the floating point calculations, but usually integer math is faster than floating point math. So I would leave things as long int.

As you know, assigning a smaller data type into a larger data type (e.g., char into int) does not require a cast since the compiler performs a silent cast for you. However, assigning a larger data type into a smaller data type (e.g., int into char) is like trying to pour 2 bytes of information into a 1 byte bucket. An explicit cast is required. That said, I always use a cast even in the silent cast situations as it documents your intentions better than a silent shows you're aware of the data conversion taking place. Just persomal preference.
78  Using Arduino / Programming Questions / Re: Problem with variable declaration on: July 07, 2014, 03:45:53 pm
Paul's right, there's nothing wrong with it, but the code is performing a silent casts for you. You can document your intent and make it more clear if you use an explicit cast. In the expression:

vout = vin * (1 - exp(-time/tao));

I assume tao is a float, time is a long, and vout and vin are longs. I've also assumed exp is your own function that takes a float.  If so, consider explicit casts:

vout = vin * (long) ( (1.0 - exp(-(float) time / tao)));

The code converts everything in the expression:  (1.0 - exp(-(float) time / tao)) to a float and then casts it to a long for the actual assignment.
79  Using Arduino / Programming Questions / Re: Extracting a variable from a buffer on: July 07, 2014, 03:14:53 pm
PaulS is right. There are three levels of scope:

Global scope, where a data item is defined outside of any function. That data item is "visible" everywhere in the source file.

Function scope, where a data item is defined within the opening and closing parentheses, but not within a statement block. Some people also call this local scope. The data item is visible everywhere within the function, but not outside the function.

Block scope, where a data item is defined within the opening and closing parenthesis of a keyword (e.g., for, if, while, etc) statement block. The data item is visible only within the statement block.

int myGlobla;      // Global scope

int myFunction()
   int i;      // Function scope.

  for(i=0;i < sizeof(buff); i++){
   int t = buff[i];             // block scope
  }                     // closing parenthesis for statement block

because you defined t within the for statement block, it has block scope. Because t "dies" (i.e., goes out of scope) when the closing parenthesis of the for loop is reached, t is no loner available for use. As PaulS pointed out, this should draw a compiler error unless you have another t defined at function or global scope...a really bad idea.
80  Using Arduino / Programming Questions / Re: Populating Global Arrays on: July 07, 2014, 03:00:33 pm
@luisilva: Your English is fine. The problem is that the vast majority of programmers fail to see the distinction between define and declare. While it may seem to be a trivial difference, there is a significant difference between the two terms. Understanding the distinction makes teaching some topics much easier if the student already knows the difference (e.g., explaining what extern means, the purpose of function prototypes, or object instantiation).
81  Using Arduino / Programming Questions / Re: Populating Global Arrays on: July 07, 2014, 02:06:21 pm
luisilva illustrates the differences quite nicely. However, the statement:
int LedNumber[120];
is a definition, not a declaration. Definition form an attribute list for the data item (e.g., an int array name LedNumber with 120 element) and allocates memory for it. A definition may appear outside of a function, in which case it has global scope.

A data declaration, on the other hand, also creates an attribute list, but does not allocate memory for it. For example:
extern int myGlobal;
Function prototypes are another common example of a data declaration. Data declarations may also appear outside a function.

Assignments, like:
LedNumber[1] = 3;
must appear within a function defintion and may have function or block scope.

Finally, a statement like:
int ledNumber[120] = { 10, 12, 14, 18, ..    } ;  // 120 numbers
is not an assignment even though is uses the assignment operator. Rather the statement is an itinitalization list. You can initialize non-aggregate data types as part of the definition.
82  Using Arduino / Programming Questions / Re: Problem with creating a string from serial data on: July 07, 2014, 01:00:55 pm
First, welcome to the Forum. You may want to read the first two posts on this Forum as those tell you the proper way to post things so you get maximum response.

Second, get rid of the String object and switch to a char array. Your statement:

objectID = canArray[0];

is actually assigning the first character of the canArray String object into objectID. Because you made objectID a float, but canArray[0] is a character, the code assigns 43 into objectID. 43 is the ASCII code for the plus sign ('+'); probably not what you want to do.

You should change canArray to something like::

char canArray[15];

and then investigate the function strtok() to see how to pick the substrings out of the character array once it is read. From there is should be pretty easy to get things right.
83  Using Arduino / Programming Questions / Re: Rotary Encoder Skipping Pulses on: July 06, 2014, 02:23:51 pm
Yep...several flat forehead mistakes: volatile plus I forgot that byte is unsigned. Fixed those and all is well. Thanks guys...the help is sincerely appreciated.
84  Using Arduino / Programming Questions / Re: If statement takes 1kb of memory on: July 06, 2014, 12:45:22 pm
I can't compile the code, but try removing the ".0" that follows any numeric constant. I think you'll find it lowers the byte count.
85  Using Arduino / Programming Questions / Rotary Encoder Skipping Pulses on: July 06, 2014, 12:29:05 pm
I'm working with a cheap rotary encoder:

and am trying to sense whether it's turning CW or CCW. My test code is as follows:
#include <rotary.h>

#define ENC_A   A0
#define ENC_B   A1
int flag, state;
int sum;

Rotary myEncoder(A0, A1);

void setup() {

  pinMode(ENC_A, INPUT);           // set pin to input
  digitalWrite(ENC_A, HIGH);       // turn on pullup resistors
  pinMode(ENC_B, INPUT);           // set pin to input
  digitalWrite(ENC_B, HIGH);       // turn on pullup resistors

  PCICR |= ( 1<<PCIE1 );  // ISR for encoder
  PCMSK1 |= ((1 << PCINT8)|(1 << PCINT9));  // look on A0 and A1
  sum = state = 0;
  flag = 0;

void loop() {
  // put your main code here, to run repeatedly:

  switch (flag) {
    case -1:    // CCW
      Serial.println("  CCW");
    case 0:      // Do nothing
    case 1:      // CW
      Serial.println("  CW");
      Serial.print("  Shouldn't be here: flag = ");
  flag = 0;
  unsigned char result = myEncoder.process();
  switch (result) {
    case 0:                                 // Nothing done...
      flag = 0;
    case DIR_CCW:                 // Turning Clockwise, higher frequencies
      flag = -1;

    case DIR_CW:            // Turning Counter-Clockwise, lower frequencies   
      flag = 1;
      flag = result;
       // Should never be here


Every once and a while, I can "feel" the detent change, but see no output on the serial monitor. Also, I will occasionally fall into the default case in loop() and print either -1 or -255 which really puzzles me since -1 is a case condition. It's probably a flat-forehead mistake, but I'm not seeing it.
86  Using Arduino / Programming Questions / Re: What programming language to learn ?! on: July 06, 2014, 10:19:13 am
Before you buy, go to Amazon and read the reviews for each of the books you are considering AND pay attention to whether the reviewers give you an idea of their level of prior programming experience. Prior programming experience makes a huge difference in how one perceives a book.  Obviously I'm biased, but I think the Beginning C for Arduino does a good job of hand-holding the beginner who is trying to learn C as a first language. The K and R book is a great reference, but often tough sledding for a beginner. Read it after you have some experience with the language...then you'll really appreciate the nuggets therein.
87  Using Arduino / Programming Questions / Re: How to convert numbers from Binary to Decimal? Please Coment!:D on: July 06, 2014, 10:09:33 am
This should also work:

void setup() {
  // put your setup code here, to run once:

  byte booleanArray[8] = {1, 0, 1, 0, 1, 0, 1, 0};
  byte decimalValue = 0;
  int j, temp;

  for (int i = sizeof(booleanArray) / sizeof(booleanArray[0]) - 1, j = 0; i > -1; i--) {
    decimalValue += booleanArray[j++] << i;

void loop() {
  // put your main code here, to run repeatedly:

However, I agree with PeterH, this seems a weird way to do something, unless it's a homework assignment.
88  Using Arduino / Programming Questions / Re: What programming language to learn ?! on: July 06, 2014, 09:40:15 am
...naturally it's not limited in what you can do with it, but it is more limited in syntax smiley. It would be wiser to learn an OO language so you can also write classes for your arduino, and understand the sketches that do so, than to stick to strict C and have to roll your own OO like structures and being puzzled when encountering C++ stuff

My guess is that you've never taught programming to a total beginner. I've written 18 programming texts and taught programming for almost 40 years and this is just plain wrong. "Limited syntax" is exactly what someone with no programming experience needs. Learning OOP from the get-go is fine if there is no alternative, but that's not the case here. C is a great avenue to ease into C++, which is native to the Arduino IDE.  Finally, how can you possible poffer Java as a solution when pointers are so integral to practically all of the Arduino libraries.

I would agree with you about Java as a good first Arduino language to learn, but then we'd both be wrong.
89  Using Arduino / Programming Questions / Re: Split Number into single digits on: July 05, 2014, 11:08:00 am
Something like:

int val = 26;
int tens;
int ones;

tens = val / 10;      // tens now = 2
ones = val % 10;      // ones now = 6
90  Using Arduino / Programming Questions / Re: serial problem on: July 03, 2014, 10:24:06 pm
Several things:
1. Read the two posts at the start of this forum. It tells you how to post your code for use on this forum. You need to post your code using the code tag (i.e., the # sign icon).

2. As PaulS told you, always post all of your code so we can try to compile it.

3. When your code tries to write to the Serial object via the print method, where do you think it is writing the data?
Pages: 1 ... 4 5 [6] 7 8 ... 48