Averaging and finding the median
Here's how to find the average of 9 samples, or the median number of a sorted list of samples. Wiring and PicBasic Pro examples follow.
Written in Wiring, tested on an Arduino board:
/*
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 analogValues[in+1] ) { // out of order?
// swap them:
swapper = analogValues[in];
analogValues [in] = analogValues[in+1];
analogValues[in+1] = swapper;
}
}
}
}
Sending Mail from Processing
* mail_client
by Tom Igoe
A simple mail sender client
Created 21 January 2006
*/
import processing.net.*;
Client myClient;
int clicks;
String reply = null;
boolean sent = false;
void setup() {
// Connect:
myClient = new Client(this, "echonyc.com", 25);
delay(300);
}
void draw() {
if(!sent) {
waitForReply();
myClient.write("HELO echonyc.com\n");
waitForReply();
myClient.write("MAIL FROM:tigoe@echonyc.com\n");
waitForReply();
myClient.write("RCPT TO:tigoe@echonyc.com\n");
waitForReply();
myClient.write("DATA\n");
waitForReply();
myClient.write("Subject:Noodles\n");
myClient.write("From:tigoe@echonyc.com\n");
myClient.write("To:tigoe@tigoe.net\n");
myClient.write("\rHere's the body\n.\n");
waitForReply();
myClient.write("QUIT\n\r");
waitForReply();
}
sent = true;
}
void waitForReply() {
int newChar = 0;
while (newChar != 10) {
if(myClient.available() > 0) {
newChar = myClient.read();
reply += (char)newChar;
}
}
println(reply);
}Check your port number.
Arduino and iPod
Rosie Daniel wrote a nice piece of Arduino code to control an iPod.
Rosie used a hacked iPod remote
to connect her Arduino to the iPod. The remoteŐs AUD connection is its data in connection, which is connected to the Arduino/Wiring boardŐs data out. The iPodŐs power (VCC) and Ground are connected to the power and ground of the microcontroller. Then this code works.
//code that controls basic
//(play/pause, next, previous, volume up, and volume down) functions of ipod
//rosie daniel
int hits = 0;
int buttonStates[]={0,0,LOW,LOW,LOW,LOW,LOW};
int buttonPrevious[]={0,0,LOW,LOW,LOW,LOW,LOW};
int buttonRelease[] = {0xFF, 0x55, 0x03, 0x02, 0x00, 0x00,0xFB};
int commands[]={0,0,0x01,0x08,0x10,0x02,0x04};
int checkSum(int len, int mode, int command1, int command2, int parameter) {
int checksum = 0x100 - ((len + mode + command1 + command2+ parameter) & 0xFF);
return checksum;
}
void setup() {
Serial.begin(19200);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
}
void loop() {
for (int c=2; c<7; c++)
{
buttonStates[c] = digitalRead(c);
buttonStates[c] = digitalRead(c);
if (buttonStates[c] != buttonPrevious[c] ) {
delay(5); //helps avoid a 'double' press - check a second time to see if the button is still pressed after a delay
buttonStates[c] = digitalRead(c);
if (buttonStates[c] == HIGH) {
sendCommand(commands[c]);
//Serial.print(hits);
hits++;
}
buttonPrevious[c] = buttonStates[c];
}
}
}
void sendCommand(int cmd) {
int cs = checkSum(0x03, 0x02, 0x00, cmd, 0);
Serial.println(cs,HEX);
int bytes[] = {0xFF, 0x55, 0x03, 0x02, 0x00, cmd, cs};
for (int i = 0; i < 8; i++) {
Serial.print(bytes[i], BYTE);
}
for (int i = 0; i < 8; i++) {
Serial.print(buttonRelease[i],BYTE);
}
}
Source:ITP