Pages: [1]   Go Down
Author Topic: HWSerial Syntax -- puzzled!  (Read 428 times)
0 Members and 1 Guest are viewing this topic.
Underhill Center, Vermont, USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 71
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Below is some code I got from Tom Igoe's website ... it compiles clean under 0008 and 0009. I am puzzled by his manner of invoking the HW serial functions. I know what he is doing and it all works just fine, buttttttttttt where do these function names  ( Beginserial(), printInteger(), etc ) come from? I see no includes, no nothing to point to these alternate names ... what am I missing?

   cheers ... BBR

Code:
/*  Analog median and average by Tom Igoe
 
 This program reads an analog input and gives the average of 9 readings,
 and sorts the list of readings and delivers the median number.
 
 Created 17 October 2005 Updated
 
 */
int numReadings = 9; // number of samples to take
int median = 0;      // median of the sorted samples
int readingNumber;   // counter for the sample array

// variables for subroutines:
byte i = 0;
byte j = 0;
byte position = 0;
int analogValues[9];

//function prototypes:
void bubbleSort();
int  averageArray();

void setup() {
  beginSerial(9600);
}

void loop() {
  for (readingNumber = 0; readingNumber < numReadings; readingNumber++) {
    //get the reading:
    analogValues[readingNumber] = analogRead(0);
    // increment the counter:
    readingNumber++;
  }
  // sort the array using a bubble sort:
  bubbleSort();

  // get the middle element:
  median = analogValues[numReadings / 2];




  // print the results:
  // print the array, nicely ASCII-formatted:
  printString("Array: [");
  for (j = 0; j < numReadings; j++) {
    printInteger(analogValues[j]);
    printString (", ");
  }
  printString("]\r\n");
  // average the array:
  printString(" Average = ");
  printInteger(averageArray());
  printString("\tMedian = ");
  printInteger(median);
  printString("\r\n");
}

// average the values in the array:
int  averageArray() {
  int total = 0;
  int average = 0;
  for (i = 0; i< numReadings; i++) {
    total = total + analogValues[i];
  }
  average = total/(numReadings + 1);
  return average;
}

void bubbleSort() {
  int out, in, swapper;
  for(out=0 ; out < numReadings; out++) {  // outer loop
    for(in=out; in<numReadings; in++)  {  // inner loop
      if( analogValues[in] > analogValues[in+1] ) {   // out of order?
        // swap them:
        swapper = analogValues[in];
        analogValues [in] = analogValues[in+1];
        analogValues[in+1] = swapper;
      }
    }
  }
}
Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 9
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Those are the old names for the functions, so for compatibility, they're still included, but shouldn't be used any more.  They more or less do the same things as the Serial.begin(), Serial.print() functions, etc.  
Logged

Pages: [1]   Go Up
Jump to: