Show Posts
Pages: [1] 2 3 ... 56
1  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.
2  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.
3  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 = ");
4  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.
5  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.
6  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.
7  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.
8  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.
9  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.
10  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.
11  Using Arduino / Programming Questions / Re: How to convert Char* to float? on: September 01, 2014, 09:51:52 pm
If it is a char, I assume it is a digit character, '0' - '9'. If so, you could use:


   char c = '5';
   float x;

   x = (float) (c - '0');

The ASCII code for the '5' digit is 53 and for '0' (i.e., zero) it is 48. Therefore:

'5' - '0'
53 - 48

which is then cast to a float.
12  Using Arduino / Programming Questions / Re: Functions - call a function and send a variable on: September 01, 2014, 08:50:27 pm
I'd like to take Bulldog's example and modify it a bit. I'm not a big fan of global data because everything has access to it. Instead, I try to encapsulate the data and keep it as private as possible. My example, encapsulates the alarm state so if something changes it, it is easier to isolate where the change took place.


void setup()

void loop()
  int output = 200;    //example
  int alarmState;      // the variable is no longer global, so it can only be changed within loop().
  alarmState = setAlarm(output);
  switch (alarmState) {
    case 1:
      Serial.println("Alarm state = 1");
    case 2:
      Serial.println("Alarm state = 2");
    case 3:
      Serial.println("Alarm state = 3");
      Serial.println("I should never see this");

int setAlarm(int value)
  int state;
  if (value < 100)  {
    state = 1;
  }  else if (value < 500)  {
    state = 2;
  }  else   {
    state = 3;
  return state;
13  Using Arduino / Programming Questions / Re: Manipulating sequence numbers received through the serial port. on: September 01, 2014, 01:46:41 pm
First, if you are sure of the format and that it is always terminated with a newline character ('\n', click clicking Enter in the Serial monitor), you can capture all of your data at once:


void loop() {
  byte red, green, blue;
  char buff[10];
  int bytesReceived;

  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    bytesReceived= Serial.readBytesUntil('\n', buff, 9);    // Stay here and read all of the input data
    buff[bytesReceived] = '\0';      // Make it a string
    red = GetAColor(buff);      // Pick off first three bytes...
    green = GetAColor(&buff[3]);      // ...the next three...
    blue = GetAColor(&buff[6]);      // ...the last three...

byte GetAColor(char str[])
    char temp[4];

    strncpy(temp, str, 3);
   return (byte) atoi(temp);

I haven't tested this out, but at least it's a starting point.
14  Using Arduino / Programming Questions / Re: Trouble with serial writing on: September 01, 2014, 01:35:46 pm
The Serial method does read to the specified character, so you code is doing as it should, but not as you want. Try this mod:
  int bytesRead;
   char buffer[6];      // I misspelled this first time...

    if (Serial.available() > 0) {
      bytesRead = Serial.readBytesUntil('\n',buffer,5);
      buffer[bytesRead] = '\0';      // Make it a string

Note that the code reads up to the newline character ('\n'), which is sent when the user clicks Enter or presses the Enter key. If you want to treat a char array as a string, you need to append a null termination character ('\0') to the end of the data. If you need to break out substrings, you can use indexof() and look for 'a'.
15  Using Arduino / Programming Questions / Re: Day-of-Year conversion to Date, how??? on: September 01, 2014, 11:49:56 am
It seems that it does have several formats, I just don't which one applies to your set up.
Pages: [1] 2 3 ... 56