Show Posts
Pages: 1 [2] 3 4 ... 55
16  Using Arduino / Programming Questions / Re: 'Beginning C for Arduino by Jack Purdum' book errors? Modified Blink program on: August 30, 2014, 02:22:17 pm
Quote
Making count a long is an error. It only needs to be 1 byte long.

No, it's not an error. It may be inefficient, but it's not an error, especially if you are writing for people who have no prior programming experience and you haven't discussed all of the data types yet.
17  Using Arduino / Programming Questions / Re: 'Beginning C for Arduino by Jack Purdum' book errors? Modified Blink program on: August 30, 2014, 01:13:36 pm
If you read page 62, first paragraph from the top. it says:

"...where the new variable, counter, is a long data type that is initialized to 0 and defined just after the data definitions for led1 and led2."
18  Using Arduino / Programming Questions / Re: Don't stop believing playing on my arduino. on: August 29, 2014, 01:01:09 pm
el_supremo has you on the right track, but a safer and more portable way is to use:

Code:
#define ARRAYELEMENTCOUNT(x)  (sizeof(x) / sizeof(x[0]))

// Your normal code...

 // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < ARRAYELEMENTCOUNT(melody); thisNote++) {


The logic is the same: take the number of bytes allocated to the entire array and divide it by the number of bytes required for a single element. Pete simply had you divide by the 2 bytes required for each int. However, the macro allows you to get the element count for any type of array and will also work in environments where identical data types are different. For example, a double is 4 bytes on an Uno but 8 bytes on a Due. The macro works in either environment without source code change.
19  Using Arduino / Programming Questions / Re: Serial read more than 64 bytes? on: August 29, 2014, 12:50:09 pm
Nick can give you more complete details, but the hardwareSerial.h header file found at your root directory plus:

hardware\arduino\avr\cores\arduino\

contains #define's for the serial buffer size:

Code:
#define SERIAL_TX_BUFFER_SIZE 64
#define SERIAL_RX_BUFFER_SIZE 64

I don't know how safe it is to increase these sizes, but I'm sure others here can tell you better than I can.
20  Using Arduino / Programming Questions / Re: help with for loop on: August 28, 2014, 10:24:04 am
Get rid of this:

Code:
      switch(asd)
      {
      case true:
        for(int k=0;k<5;k++)
        {
          Serial.print(tagIn[k]);
          Serial.println("\nAuthorize");
        }
        break;
      case false:

        Serial.print("\nnot Authorize\n");
        break;
      }

and replace it with this:

Code:

      if (asd == true) {                                 // A match?
        for(int k=0;k<5;k++)                             // Yes
        {
          Serial.print(tagIn[k]);
          Serial.print('\t');
        }
        Serial.println("\nAuthorize");
      } else {                                           // No
        Serial.print("\nnot Authorize\n");
      }
21  Using Arduino / Programming Questions / Re: Crazy corruption in byte Array when Serial.write on: August 27, 2014, 11:36:01 pm
A data definition like:

Code:
byte dataPacket [4];  // 0-4  --> 5 bytes

specifies the number of elements you want for the array, not index positions. Your definition asks for 4 elements when you really need 5. Change the definition to:

Code:
byte dataPacket [5];  // 0-4  --> 5 bytes
22  Using Arduino / Programming Questions / Re: How to display the text entered via serial monitor on: August 27, 2014, 11:22:36 pm
I'm not a big fan of the String class, as it adds a lot of bloat to most programs. Here's an alternative; my guess is that it's smaller than the alternative:

Code:
#define MAXBUFF  20

const int LEDPIN = 13;
char message[MAXBUFF];

void setup() {
  Serial.begin(9600);
  pinMode(LEDPIN, OUTPUT);
}

void loop() {
  int charsRead;
  if (Serial.available() > 0) {
    charsRead = Serial.readBytesUntil('\n', message, MAXBUFF - 1);   // Gather the input
    message[charsRead] = '\0';      // Make it a C string
    Serial.println(message);
  }
  if (strcmp(message, "on") == 0)      // Did it say on?
  {
    digitalWrite(LEDPIN, HIGH);
  } else {  
    if (strcmp(message, "off") == 0)      // or off
      digitalWrite(LEDPIN, LOW);
  }
}
23  Using Arduino / Programming Questions / Re: Copy multi Array to larger multi array. on: August 27, 2014, 08:36:04 am
I have not used this library, but evidently there is an extended EEPROM library which has a block move function. You can read more about it at:

http://thijs.elenbaas.net/2012/07/extended-eeprom-library-for-arduino

What board are you using? The large array chews up 1250 bytes and that may be a problem on some boards.
24  Using Arduino / Programming Questions / Re: Anyone please help me on: August 27, 2014, 08:23:29 am
It would seem that the displayChar() function could be simplified a bit:

Code:
#define ARRAYSIZE(x) (sizeof(x) / sizeof(x[0]))   // Place near top of sketch

void displayChar(char array[])
{
   int i;

   for (i = 0; i < ARRAYSIZE(array); i++) {
      displayLine(array[i]);
      delay(delayTime);
   }
   displayLine(0);
}

Also, in displayLine(), why define myline? Just use line as it is passed into the function.
25  Using Arduino / Programming Questions / Re: reconstructing 3 byte array to long after EEPROM read on: August 27, 2014, 08:00:13 am
@Paul: I think Mike and I are talking at cross purposes to some degree. I was thinking of exactly what you mentioned as I have run across that problem in the past. While I haven't checked, my guess is the packing/unpacking might have a speed/size advantage, but I think the union approach is easier for a newbie to understand.
26  Using Arduino / Programming Questions / Re: reconstructing 3 byte array to long after EEPROM read on: August 27, 2014, 07:43:26 am
@Mike: Not sure I follow, or perhaps we're talking at cross purposes. Are you suggesting that the packing/unpacking code works for an Uno and a Due for all basic data types without change, and that I can move that same code to a PIC, chipKIT, or other  controller without modification?
27  Using Arduino / Programming Questions / Re: problem with my code :/ on: August 26, 2014, 10:11:41 pm
In loop():
Code:
void loop() {
 // Measure distance
  digitalWrite(TRIGPIN, LOW);                   // Set the trigger pin to low for 2uS
  delayMicroseconds(2);
  digitalWrite(TRIGPIN, HIGH);                  // Send a 10uS high to trigger ranging
  delayMicroseconds(10);
  digitalWrite(TRIGPIN, LOW);                   // Send pin low again
  int distance = pulseIn(ECHOPIN, HIGH);        // Read in times pulse
  distance= distance/58;                        // divide by 58 gives cm.

// Measure distance 2
  digitalWrite(TRIG_PIN2, LOW);                   // Set the trigger pin to low for 2uS
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN2, HIGH);                  // Send a 10uS high to trigger ranging
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN2, LOW);                   // Send pin low again
  int distance2 = pulseIn(ECHO_PIN2, HIGH);        // Read in times pulse
  distance2= distance2/58;                        // divide by 58 gives cm.
  
 // Convert measured value1 to value between 0-11, to display on LCD
  // Use Arduino built-in map and constrain functions
  int scaledValue = map(constrain(distance, highWater, lowWater), lowWater, highWater, 0, 11);  // =======  Pick this one or... ====
  lcd.clear();
  lcd.print("E    SENSOR 1    F");
  lcd.setCursor(0,1);
  while (scaledValue > 0) {
     lcd.print((char)0);
     scaledValue--;
  }
      
   delay(2000);  // Wait 2 seconds before printing the 2nd value.

  // Convert measured value2 to value between 0-11, to display on LCD
  // Use Arduino built-in map and constrain functions
  int scaledValue = map(constrain(distance2, highWater, lowWater), lowWater, highWater, 0, 11);  // ======= ...this one, not both ===
;//<<<<<<<<note distance2
  lcd.clear();
  lcd.print("E   SENSOR 2   F ");
  lcd.setCursor(0,1);
  while (scaledValue > 0) {
     lcd.print((char)0);
     scaledValue--;
  }
      
      delay(2000);  // Wait 2 seconds before measuring again. We're in no hurry!
}

You have define scaledValue twice. You can't use the same variable name twice at the same scope level.
28  Using Arduino / Programming Questions / Re: Cannot convert serial input from int to ASCII using atoi function on: August 26, 2014, 09:48:50 pm
Quote
I am trying  to convert int to ASCII.
If that's the case, then why are you reading the Serial object?

The following code waits for an integer to be entered from the keyboard and the user to click Send. It also takes the arbitrary value of 12345 and converts it to ASCII.
Code:
void setup() {
  Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}

void loop() {

  char buf[12];
  int byteReceived;
  //delay(5000);
  // send data only when you receive data:
  if (Serial.available() > 0) {
    // read the incoming byte:
    byteReceived = Serial.readBytesUntil('\n', buf, 11);   // Keep room for null
    buf[byteReceived] = '\0';   // At this point buf[] hold the ASCII for the int that was entered
    Serial.print("Received: ");
    Serial.println(buf);
    Serial.print("To make the ASCII value an int: ");
    Serial.println(atoi(buf));
  // If you want to convert an int to ASCII, why are you reading the Serial object?
  // To convert:
    int val = 12345;
   
    itoa(val, buf, 10);
    Serial.print("val = ");
    Serial.println(val);    // Done...
  }
}
29  Using Arduino / Programming Questions / Re: reconstructing 3 byte array to long after EEPROM read on: August 26, 2014, 09:26:35 pm
I still think a union offers an easy way to do this kind of stuff and allows you to ignore the endian problem.

Code:
union longConverter {
   char buff[4];
   long val;
} myData;

To write a long to EEPROM, use something like:

Code:
   myLong = 123456L;
   myData.val = myLong;   // Put the long into the union...
   for (int i = 0; i < sizeof(long); i++)
    EEPROM.write(i, myData.buff[i]);   // Write the data to EEPROM, starting at EEPROM address 0

To retrieve the value back from EEPROM:

Code:
   for (int i = 0; i < sizeof(long); i++)
     myData.buff[i] = EEPROM.read(i);   // Read data back from EEPROM, starting at EEPROM address 0
   myLong = myData.val;   // Retrieve the long from the union

The same approach can be done for other data types, too. It can be more portable, too. For example, doubles and floats are the same on an Uno, but doubles are 8 bytes on a Due. The code above, written for a double data type, would work without change if ported to a Due. Bit shifting would not.
30  Using Arduino / Programming Questions / Re: How would I include 10 functions into an array? on: August 26, 2014, 09:11:01 pm
You can use Purdum's Right-Left Rule to figure out what complex data definitions are, like BulldogLowell's:

void (*myFunction[2])(void);

Verbalizing: "myFunction is an array of two pointers to function with void arguments that return void". See:

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

Pages: 1 [2] 3 4 ... 55