Show Posts
Pages: 1 [2] 3 4 ... 48
16  Using Arduino / Programming Questions / Re: How to find number of "rows" in an array of strings? on: July 28, 2014, 07:18:53 pm
Quote
p.s. you know you write a lot of code when you type using a semicolon as a period.

Opps...
17  Using Arduino / Programming Questions / Re: How to find number of "rows" in an array of strings? on: July 28, 2014, 06:29:17 pm
No, sizeof() returns the number of bytes allocated to the object. For your calculation to tell how many elements there are, you need to use:

Code:
number  = sizeof(myString) / sizeof(char *);

Each element of the array is a pointer, not a string. Therefore the first sizeof() expression returns the total number of bytes allocates for all of the pointers (e.g., 8 bytes). To get the element count, you need to divide by the size of one element, which is the size of a single pointer (e.g., 2 bytes);
18  Using Arduino / Programming Questions / Re: New to Arduino - help converting float on: July 28, 2014, 04:05:59 pm
Look at dtostrf() for help.

Code:
void setup() {
  char buff[10];
  Serial.begin(115200);

  int sensorValue = 512;
  float voltage = (float) sensorValue * (5.0 / 1023.0);
  Serial.println(voltage);
  dtostrf(voltage, 8, 6, buff);
  Serial.println(buff);
 
}

void loop() {}

If you need to send it one byte at a time, try:

Code:
int index = 0;
while (buff[index]) {
   ble_write( buff[index++]);
}

I haven't tested the code, but it should work.
19  Using Arduino / Programming Questions / Re: Do I need to reset variables to 0 ? on: July 28, 2014, 11:53:57 am
What's wrong with using strtok()?
20  Using Arduino / Programming Questions / Re: Question about grammar for the boolean function on: July 27, 2014, 10:00:33 pm
One way to figure this out is to supply sample values for a and b (e.g., a = .003, b = .0029) and keep in mind that the type specifier for the function is boolean. Once you see what those values return, make them smaller until they approach DELTA. You should be able to see what the function does.
21  Using Arduino / Programming Questions / Re: what is the difference between char and int and why can you not convert them? on: July 27, 2014, 03:48:29 pm
Quote
I have read many many many books on pointers and chars and integers but it doesn't make sense with vw_send(); nothing wants to work and I don't know why

You just haven't read the right book yet. Read chapters 8 and 9 in Beginning C for Arduino, which are about pointers, then come back here with your question.
22  Using Arduino / Programming Questions / Re: What to do next? on: July 27, 2014, 11:45:48 am
I'm still puzzled why A0 didn't compile for you. As MAS3 mentions, pins 0 and 1 are used by the Serial object so I'd stay away from using 0 for analog pin 0  even if analogRead() is smart enough to make the right call.  If nothing else, using A0 helps to document your code. The fact that your code doesn't recognize A0 would make me a little worried.

Another thing to keep in mind is that most Arduino boards make the external interrupt routines available on pin 2 and 3.  Because you may need to add an ISR sometimes, I always change:

Code:
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

to

Code:
LiquidCrystal lcd(12, 11, 7,6,5,4);

so I won't impinge on any potential ISR down the road.
23  Using Arduino / Programming Questions / Re: Displaying random char string on: July 27, 2014, 11:29:23 am
@luisilva: No, I don't mean to seem like I'm pointing out errors in yours or anyone else's code. I'm simply trying to show how alternatives exist that might be useful. I'm a retired professor who used to teach programming, and I can't seem to get out of the habit.
24  Using Arduino / Programming Questions / Re: Displaying random char string on: July 27, 2014, 11:00:44 am
I forgot to address your wasted memory question. The program I just posted used 4124 bytes, with a good chunk being wasted because of the "300" element size. The following line

Code:
const char string[3][300] = { "abc", "def", "ghi"};

is wasteful. You can use Purdum's Right-Left Rule to verbalize this definition as: "string is an array of 3 elements each holding 300 chars which are constants".

Now write the same line as:

Code:
const char *string[] = { "abc", "def", "ghi"};

and the code size shrinks to 3238 bytes, a savings of 886 bytes. The reason it is smaller is because the compiler now allocates only enough space for each string constant. Note that you don't have to supply the element size when you have a complete initializer list.  (You could have written your line as: const char string[][300] = { "abc", "def", "ghi"};) This definition is verbalized with the Right-Left Rule as: "string is an array of pointers to char which are constants."
25  Using Arduino / Programming Questions / Re: Displaying random char string on: July 27, 2014, 10:45:20 am
There are a couple of issues here:

Code:

const char string[3][300] = { "abc", "def", "ghi"};

printStringWithShift(string[random(0, 4)],100);

First, I didn't see the code for the printStringWithShift(), so I don't really know what it is supposed to do. Second, I don't know what the purpose of the 100 is for the second argument. Third, the random() call should be with the arguments 0, 3 not 0, 4. C arrays start with 0, so only elements 0, 1, and 2 are valid for your example and the upper limit parameter is exclusive for random().

Is this what you're trying to do?

Code:
const char string[3][300] = { "abc", "def", "ghi"};

void printStringWithShift(const char *str) {
  Serial.println(str);
}
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  randomSeed(analogRead(0));
}


void loop() {
  // put your main code here, to run repeatedly:
  int index = random(0, 3);
  Serial.print("Index = ");
  Serial.println(index);
  printStringWithShift(string[index]);
  delay(500);
}
26  Using Arduino / Programming Questions / Re: led control pannel on: July 26, 2014, 02:36:09 pm
I don't see a definition of patternIndex anywhere

Edit:

Also, C is case sensitive, so patternTwo is not the same as PatternTwo.
27  Using Arduino / Programming Questions / Re: What to do next? on: July 26, 2014, 02:33:01 pm
Maybe I don't understand what your're doing. It appears that you have a single switch connected to pin 10, which should read either HIGH or LOW depending on its state. However, you treat it as a potentiometer in the code and map the value returned from it and then use a bunch of if statements to decide which option was selected.  If you're using  a pot, those are usually attached to an analog pin (A0-A5) and the return value is mapped.  So what's really on pin 10, a pot or a switch?
28  Using Arduino / Programming Questions / Re: LCDs and Strings and Substrings Oh My - Need help with my C++ on: July 26, 2014, 11:10:48 am
I don't have an LCD display handy, but the code below uses the char array as suggested by UKHeliBob. Avoid the C++ String class whenever you can.

Code:
#include <String.h>


const int lcdCols = 16; //Number of columns in my lcd
const int lcdRows = 2;//Number of rows in my lcd
const int delayTime = 1000; //Value in milliseconds to delay the scroll

char text[] = "This is example text that should need more than one line to print. Here's more text."; //The actual text

void setup() {
  Serial.begin(115200);
  Serial.println("                |");  // The screen size
}

void loop() {
  printString(text);
}

void printString(char *text) {
  char temp[lcdCols + 1];
  int iterations;
  int len = strlen(text);

  if (len <= lcdCols) {  //Prints the string if its length is
    Serial.println(text);
    return;
  }
  iterations = len / lcdCols + 1;
  for (int counter = 0; counter < iterations; counter++) {
    strncpy(temp, &text[counter * lcdCols], lcdCols);
    temp[lcdCols] = '\0';
    Serial.println(temp);
    delay(delayTime);
  }
  Serial.println();
}

The Serial.println() call in setup() gives you a frame of reference for the width of a 2x16 LCD display.
29  Using Arduino / Programming Questions / Re: LED Binary Converter on: July 25, 2014, 11:43:53 pm
Take a look at this short program for converting from decimal to binary.

Code:
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
 
  int val = 300;
  char result[15];
 
  DecimalToBinary(val, result);
  Serial.println(result);
 
}

void DecimalToBinary(int val, char *result)
{
  int i = 0;
  int j;
  int temp;
  char buff[15];

  while (val > 0) {
    temp = val % 2;
    if (temp == 1)
      buff[i++] = '1';
    else
      buff[i++] = '0';
    val /= 2;
  }
  temp = 0;
  for (j = i - 1; j >= 0; j--) {      // Need this loop because the answer is "backwards"
    result[temp++] = buff[j];
  }
}

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

}
30  Using Arduino / Programming Questions / Re: LED Binary Converter on: July 25, 2014, 11:00:41 pm
Also, the Serial object sends things a byte at a time, and you need to know when it has "arrived" for your use.  You probably need to look at Serial.available() for use in your code. Next, once Serial.available() is greater than 0, I would use Serial. readBytesUntil() and look for the newline character ('\n') which indicates the user has pressed the Enter key and is done entering the number. If you look in the Examples --> Communications menu, you'll find examples that should be helpful.
Pages: 1 [2] 3 4 ... 48