Show Posts
Pages: 1 ... 4 5 [6] 7 8 ... 32
76  Using Arduino / Programming Questions / Re: Need this boolean to read correctly. Stuck on this on: March 26, 2014, 08:36:30 am
You need to get a grasp on the concept of scope. Scope refers to the lifetime and visibility of a data item. As a starter, consider:
1) Global scope -- data items define outside of any function. These are visible to any part of the program and live as long as the program is running.
2) Function scope -- data items defined within the starting ({) and end (}) braces of a function. They come to life when program execution enters the function and they "die" (i.e., go out of scope) when control leaves the function.
3) Block scope -- data items defined within a statement block. The come to life (i.e., are in scope) at the start of the statement block and die when the block ends. For example:
Code:
                    for (int i = 0; i < MAXNUM; i++) {
                            sum += val[i];
                      }
                      average = sum / i;     // ERROR: i is no longer in scope
         Variable i comes alive (into scope) when its definition is read as expression1 of the for loop. It dies when the closing brace of the for loop is reached. Therefore, the statement outside the braces draws a compiler error because i no longer exists.
4) static -- Using the keyword static allows you define a data item, and have it retain its value even when program control goes outside its scope level. For example, if you have
Code:
                   void myFunction() {
                           static int myVariable = 10;
                           // more code...
                     }  // end of function
the next time you enter myFunction(), myVariable will still have the same value it did after the previous call to the function ended. Data items defined with the static type specifier are allocated on the heap, which means 1) only one of them is defined for the program, and 2) the data item persists regardless of its scope level.
77  Using Arduino / Programming Questions / Re: Help in while, if & else on: March 25, 2014, 10:33:45 pm
With your cursor in the Arduino source window, press Ctrl-T to automatically format your code. Always do that before you post your code...makes it much easier to read.
78  Using Arduino / Programming Questions / Re: Convert a float to binary on: March 25, 2014, 11:55:48 am
You can always look for the decimal point in the array and throw it away, splice the two pieces together and then convert to a long.
79  Using Arduino / Programming Questions / Re: What does - digitalWrite(OutPin, correct ? HIGH : LOW); Mean/do ? on: March 25, 2014, 10:34:27 am
The statement is using the ternary operator, which has the form:

Code:
expression1 = expression2 ? expression3 : expression4;

expression2 is usually a conditional operator and evalutates to True or False. If expression2 evaluates to logic True, expression3 is evaluated and its result assigned into expression1. If expression2 evaluates to logic False, expression4 is evaluated and its outcome is assigned into expression1.
80  Using Arduino / Programming Questions / Re: Can i program in C# rather than C? on: March 25, 2014, 10:29:06 am
@PaulS: Going to have to disagree with you on this one. Visual Studio is a nice environment in which to teach and learn any of its supported languages. I taught programming using everything from the editor-compiler-assembler-linker days to VS and, as far as a student learning something, the VS environment let me spend more time teaching the language rather than the steps necessary to move from editor to EXE. The only downside of the VS enviroment is that students tend to use the "throw-this-at-the-bug-and-see-if-it-works" approach to program development and debugging. It was a lot different when you had a 6" stack of IBM cards, had to walk a half-mile to submit it for compilation, get a job number passed back through a little slot in the wall, call on the phone to see which job numbers were ready, walk back over to get it, only to find the comma in column 70 should be in column 71. Students don't seem to learn a disciplined way to debug a program. Throw a stray pointer at them and they go comatose.
81  Using Arduino / Programming Questions / Re: Convert a float to binary on: March 25, 2014, 10:16:22 am
You're probably running into a floating point precision problem. Because the float data type uses only 4 bytes, there are a limited number of significant digits it can represent. Generally, a 4-byte float has 6 digits of precision. While it can display more digits than that, any digits after 6 (its precision) are simply the compiler's best guess as to what the data really are. You might have more luck converting it to an unsigned long data type.
82  Using Arduino / Programming Questions / Re: looping inside a made function on: March 25, 2014, 10:10:18 am
Whatever show() is expected to do, you wrote it passing in the necessary int data for it to complete its task. If it doesn't need that information passed in, then show() shouldn't have an argument.

Also, the code:

Code:
int show(int p){
 if (p == 1) { page1(); }
       else  { page2(); }
 if(p >= 3)  { p=1;     }
 return p;
}

would be easier to read if you place the cursor in the source code window and pressed Ctrl-T, which automatically reformats your code. Also, to me, it would be easier to read if it were written:

Code:
int show(int p ){
   switch (p) {
      case 1:
         page1();
        break;
      case 2:
         page2();
         break;
      default:
         p = 1;
         break;
   }
   return p;
}

I'm not sure that's correct because of the way you wrote the if clause, but you can take it from there.
83  Using Arduino / Programming Questions / Re: looping inside a made function on: March 25, 2014, 09:49:33 am
You wrote the show() function:

Code:
int show(int p){

This line (sometimes called the function's signature) says that it must be called using an int data type as an argument. Yet, in loop(), you call it with the statement:

Code:
  wq = show();

The compiler is complaining because you didn't pass the expected int to the function.
84  Using Arduino / Programming Questions / Re: Casting, pointers, referencing/dereferencing question on: March 25, 2014, 08:34:26 am
If you need to parse complex data definitions, use Purdum's Right-Left Rule:

http://jdurrett.ba.ttu.edu/3345/handouts/RL-rule.html

This should help you figure out how to use (or create) complex data defintions.
85  Using Arduino / Programming Questions / Re: Can i program in C# rather than C? on: March 25, 2014, 08:29:36 am
Other than syntactic idioms, C# and C++ are very much alike. AWOL probably has it right: C# was Microsoft's answer to java before Oracle took it over. Microsoft takes a lot of hits, many of them deserved, but C# in their IDE is a nice environment in which to teach and learn. I, for one, miss their debugger and have played with Visual Micro on more than one occasion. As far as the OP goes, he should finish his C# class and absorb as much OOP as possible, using C in the Arduino IDE in the meantime. If the time comes where OOP makes sense for an Arduino project, the C# programming class he's taking and whatever C experience he picks up on his own, should allow him to transition to C++.
86  Using Arduino / Programming Questions / Re: Can i program in C# rather than C? on: March 25, 2014, 12:23:29 am
I agree with CrossRoads. I've taught C, C++, and C# and I really do like the environment Visual Studio provides, and it supports all three languages. However, the Arduino IDE doesn't use C#, so I'd put that on the back burner for now as far as Arduino work goes. If you already know Object Oriented Programming (OOP), C++ shouldn't be too tough. If you don't know OOP, you might master C and ease into C++, since you can get a lot of stuff done with the Arduino with just straight C, and I think it's easier to learn if you don't have OOP experience.  You'll find it similar to C# in terms of its syntax.

There are plenty of free tutorials on line for both languages. Since you're taking a C# class now, you should be learning OOP and that should make learning C and C++ pretty easy. If you want some help from books, I've written texts on C (Beginning C for Arduino) and C# (Beginning OOP with C#), but that's hardly an unbiased recommendation! There are a ton of choices.
87  Using Arduino / Programming Questions / Re: Can i program in C# rather than C? on: March 24, 2014, 09:20:59 pm
I'm pretty sure that the Arduino IDE is only going to let you use C or C++. I think the Netduino uses Visual Studio and C#, but I'm not positive.
88  Products / Arduino Due / Re: Getting a strange error on: March 24, 2014, 02:37:39 pm
No, the original post is not the code because there is no #include preprocessor directive in it. When I compile the header file you posted, the compiler tells me _usbConfiguration is not defined, so where is its definition?
89  Using Arduino / Programming Questions / Re: How do i put together several programs? on: March 24, 2014, 12:33:48 pm
First, read the sticky notes which are the first two posts on this forum. They lay out the ground rules for using this forum.
Second, format your code in the Arduino IDE using Ctrl-T while your cursor is in the source code window.
Third, then post your code using the Code message icon (#).
Fourth:  the code:

Code:
for(int i = 0; i < 5; i++)       //this is a loop and will repeat eight times
      pinMode(ledPins,OUTPUT); //we use this to set each LED pin to output
  }                                   //the code this replaces is below

isn't going to do much because you aren't stepping through the array. Try:

Code:
for(int i = 0; i < 5; i++)       //this is a loop and will repeat eight times
      pinMode(ledPins[i],OUTPUT); //we use this to set each LED pin to output
  }                                   //the code this replaces is below

and see if that helps.

Fifth, what are you trying to do here??

Code:
{ledPins {oneAfterAnotherLoop()}  //led

Sixth, the code:

Code:
  for(int i = 0; i <= 7; i++){
    digitalWrite(ledPins, HIGH);  //Turns on LED #i each time this runs i
    delay(delayTime);                //gets one added to it so this will repeat
  }                                  //8 times the first time i will = 0 the final

is probably going to be a problem since you only have 5 ledPins. You have the same array issue here, too (ledPin is an array and need an index subscript here). The same problem is repeated later.

Work on these issues for now.
90  Using Arduino / Programming Questions / Re: Using atof on a string on: March 23, 2014, 07:13:21 pm
Try strtok() first to break out each number as a string:

http://www.cplusplus.com/reference/cstring/strtok/

 then use atof() to finish the job.
Pages: 1 ... 4 5 [6] 7 8 ... 32