Show Posts
Pages: 1 2 [3] 4 5 ... 58
31  Using Arduino / Project Guidance / Re: Can't receive data more than 64byte on: September 04, 2014, 11:09:58 pm
I'm guessing:

Look at the HardwareSerial.h header file found in your IDE directory:


and you will see that the transmit and receive buffers are set to 64 bytes.
32  Using Arduino / Programming Questions / Re: Sketch compiles on PC but not on Raspberry Pi on: September 04, 2014, 01:09:18 pm
Yes. Go to the Raspberry Pi site and ask them.
33  Using Arduino / Programming Questions / Re: TFT ILI9341 on: September 04, 2014, 10:39:50 am
What board are you using?
34  Using Arduino / Programming Questions / Re: Help in the code sytax on: September 03, 2014, 01:44:37 pm
First, statements like:

if (val == 1 && val == 2)

don't make sense, because val is either 1 or 2, not both. My guess is that you want to use a logical OR condition, such as:

if (val == 1 || val == 2)

Also, most compilers implement what is called "short circuiting", although I don't know if the Gnu compiler does this. My guess is that it does. Short circuiting is simply a completion of the branch on the first condition that fulfills the branch. For example,

if (val == 1 || val == 2 || num == 10 || num == 13)

With short circuiting, if val equals 1, the rest of the expression is not even evaluated and the if statement block is executed. Therefore, in complex expressions, it sometimes makes sense to put the most likely outcome first in the expression list.
35  Using Arduino / Programming Questions / Re: working with Array (basic code but very strange) on: September 03, 2014, 12:22:27 pm
Isn't all this a bit pointless in the small Arduino environment?

As the programmer you decide what size the array will be so why is there a need to use sizeof().
If you need to use the size of the array in a FOR loop, for example just use
const byte arrayLen = 6;
int myArray[arrayLen];

I disagree strongly. The sizeof operator offers numerous advantages. Using the Array_Size() macro is typeless, so if I want to march through a for loop using an int array, I can use the macro. If I want to traverse a different array of type float, I can use the same macro because it is typeless. Your way requires defining a const for each array used in the program if they have different element counts. Also, if I change the size of the array, I need to remember to edit the length constant, too. The macro obviates such changes. Finally, the macro allows you to get away from "magic numbers" (e.g., 6) in the code.
36  Using Arduino / Programming Questions / Re: 'Beginning C for Arduino by Jack Purdum' book errors? Modified Blink program on: September 02, 2014, 10:11:48 pm
Stick with it and make sure you work the exercises. When you get finished with it, all of this will look like duck's soup.
37  Using Arduino / Programming Questions / Re: Trouble shooting code on: September 02, 2014, 09:35:55 pm
@PhilC: You're right. An optimizing compiler should generate a single constant. A better form for the #define might be:

#define SENSORRATIO  0.00488758      // 0.00488758 = (5.0 / 1023.0)

so they can see where the number came from.
38  Using Arduino / Programming Questions / Re: random from a char array on: September 02, 2014, 09:30:57 pm
How about:

char a[] = {'0','1','2','3','4','5','6','7',
void setup() {

void loop() {
  long rand;
  rand = random(0, 17);
  Serial.print("rand = ");
  Serial.print("   char = ");
39  Using Arduino / Programming Questions / Re: working with Array (basic code but very strange) on: September 02, 2014, 02:03:40 pm
Arrch has it correct. You are confusing the bytes allocated for the array with the number of elements in the array:

myArray[ ? ] = {0,0};

sizeof(myArray());        // =2

sizeof(myArray) return the bytes allocated to myArray[], which is 2 bytes for each element, or 4 bytes total. The macro Arrch showed you takes the memory allocated to the array (4 bytes) and divides it by the amount of memory needed for each element in the array (2 bytes) with the result being the element count for the array (2). Therefore:

long int biggerArray[] = {1,2,3,4,5,6,7,8,9,0};

using the macro would resolve to:

sizeof(biggerArray) / sizeof(biggerArray[0])
                       40       /            4
10 elements in the array, since each long requires 4 bytes of storage.
40  Using Arduino / Programming Questions / Re: working with Array (basic code but very strange) on: September 02, 2014, 01:54:33 pm
The statements below (which are data definitions, not declarations):

// Definition **
int myArrayActual[1];         // Array for all digital inputs
int myArrayLast[2] = {0,0};  // Array to compare with other array

asks for an array with one element, which is no different than a plain int, while the second definition asks for a two element array. The sizeof operator returns the total amount of memory allocated to the data item.  Therefore, your first expression asking for the size of myArrayActual[] should return the size of a single int...2 bytes. The size of myArrayLast[] has two elements, so it returns 4 bytes.
41  Using Arduino / Programming Questions / Re: Trouble shooting code on: September 02, 2014, 12:05:35 pm
One of the slowest expressions you can evaluate is division. Floating point division is even worse. So, everywhere you find an expression similar to:

float voltage0 = flexValue0 * (5.0 / 1023.0);

use something like:

#define SENSORRATIO  0.00488758
// more of your code, then statements like above:

float voltage0 = flexValue0 * SENSORRATIO;
// ...more code...
float voltage1 = flexValue1 * SENSORRATIO;
// ...and so on

It may not make much difference, depending upon how it is used, but it should save at least a few CPU cycles.
42  Using Arduino / Programming Questions / Re: Comparing Values on: September 02, 2014, 07:45:32 am
I absolutely agree with UKHeliBob comment about magic numbers. A common convention is to use upper case letters for such constants so they are easily identified as constants, not variables. So:

const int WATERLOWPOINT= 2500;
const int WATERHIGHPOINT= 6000;
// ...and so on...

if (sensor >= WATERLOWPOINT && sensor <= WATERHIGHPOINT) {
  digitalWrite(LED1, HIGH);
} // ...and so on...

While I don't like using the underscore character in data names, some prefer WATER_LOW_POINT. Either way, using caps makes it easy to identify constants in the code, plus, since most programmers place such constants at the top of the source code file, it tells you to look at the top of the file for the definitions.

You can also use symbolic constants, too:

#define WATERLOWPOINT 2500

The advantage of symbolic constants is that they are "typeless". That is, WATERLOWPOINT could be used in an expression with any other data type without casting. While an advantage in some cases, it does mean you lose the benefits of type checking, should you feel that is necessary.
43  Using Arduino / Programming Questions / Re: Comparing Values on: September 02, 2014, 07:00:00 am
As PaulRB points out, the ternary example:

digitalWrite(LED1, (sensor >=2500 && sensor <= 6000) ? HIGH : LOW);

and the if tests he suggested are not as efficient as the cascading series of if-else statements. In either case, if the first if statement is logic true, you still proceed to execute three more if tests even though you know they cannot be true. While the time slice is small for each test, when placed in a tight loop (e.g., the loop() function) you're talking about wasting uo to several thousand time slices per second. A few thousand here...a few thousand there...pretty soon it starts to add up.
44  Using Arduino / Programming Questions / Re: What is wrong with this code??? Can somebody please help me? on: September 02, 2014, 06:47:57 am
Welcome to the Forum. We note that you have made several posts before, yet have not read the rules for posting to the Forum. The rules are explained in the first two posts at the top of the Forum list. It would help us if you followed those rules, as it makes it easier for us to help you. Always use code tags when posting source code.
45  Using Arduino / Programming Questions / Re: Comparing Values on: September 02, 2014, 06:44:51 am
You might try something like:


if (sensor >= 2500 && sensor <= 6000) {
  digitalWrite(LED1, HIGH);
} else if (sensor >= 8000 && sensor <= 12000) {
  digitalWrite(LED2, HIGH);
} else if (sensor >= 13000 && sensor <= 17000) {
  digitalWrite(LED3, HIGH);
} else if (sensor > 18000) {
  digitalWrite(LED4, HIGH);

Please note that your ranges are not overlapping. For example, what if you read 7000, 12500, 17001?? If you want to leave those gaps, that's fine...just be aware that they exist.
Pages: 1 2 [3] 4 5 ... 58