/************************************************ * THIS USES A INFARED SENSOR ON PIN C2 TO MAKE * A SIMPLE THEREMIN-LIKE MUSICAL INSTRUMENT * SPEAKER IS ON B0 * * RYAN T ALLEY * JUNE 7, 2010 ************************************************/ #include "oulib.h" #include "oulib_serial_buffered.h" uint16_t getSensor(void); int main (void) { DDRB = 0b00000001; // OUTPUT on PIN B0 DDRC = 0b00000000; // INPUT on PIN C2 adc_set_reference(ADC_REF_AREF); // AREF pin must be connected to +5 V adc_set_adlar(0); // adc_set_prescalar(ADC_PRESCALAR_128); // adc_set_enable(ADC_ENABLE); // sei(); while(1) { PORTB = PORTB ^ 0x1; delay_us(8 * getSensor()); // 8 is a scalar (to lower pitch) }; return (0); } // STOLEN CODE: Supposed to: Read the analog value from the sensor (pin C2) and convert to digital uint16_t getSensor(void) { uint16_t dval; uint16_t x; uint16_t y; uint16_t z; adc_set_channel(ADC_CHANNEL_2); // read from pin C2 (ADC2), port 25 adc_start_conversion(); dval = adc_read(); if (dval < 35) { // This value cannot go below 34 or could divide by zero dval = 34; } x = dval-29; // Logarithmic model as described above y = x/4; z = 10230/y; return (z); // puts distances in mm }