Go Down

Topic: HWSerial Syntax -- puzzled! (Read 530 times) previous topic - next topic

brianbr

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: [Select]

/*  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;
     }
   }
 }
}

mellis

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.  

Go Up