Monday, September 20, 2010

Arduino Sketch, Phototheremin





//Example 07: Send to the computer the values read from
//analogue input 0
//Make sure you click on "Serial Monitor"
//after you upload

#define SENSOR 0 // select the input pin for the
// sensor resistor
const int numReadings = 10;

//int val = 0; //variable to store the value coming from the sensor
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average

int inputPin = SENSOR;


void setup() {

Serial.begin(9600); // open the serial port to send data back to the computer
// at 9600 bits per second
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;

}

void loop() {

// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = analogRead(inputPin);
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;

// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;

// calculate the average:
average = total / numReadings;
// send it to the computer (as ASCII digits)

//val = analogRead(SENSOR); //read the value from the sensor

Serial.println(average, DEC); //print the value to the serial port

//delay (10); // wait 10 ms between each send
}

No comments:

Post a Comment