Go Down

Topic: Please help with arduino digital read and analog read (Read 702 times) previous topic - next topic

hongp

Hello, People,

I set ADC prescale = 128 by using the following code:
Code: [Select]
void setup() {
  Serial.begin(9600); // use the serial port
  pinMode(refInput, INPUT);
  pinMode(finalInput, INPUT);
  pinMode(motorControl, OUTPUT);
  ADCSRA |= (1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); //set adc prescaler=128
  ADMUX = 0x40; // use adc0
  ADCSRA |= (1<<ADATE);  //enable auto trigger
  ADCSRA |= (1<<ADEN);  //enable ADC
  ADCSRA |= (1<<ADSC);  //start ADC measurements
}


Then I sample the sound with the following code:
Code: [Select]
    for (int i = 0 ; i < 512 ; i += 2) { // save 256 samples
      while(!(ADCSRA & 0x10)); // wait for adc to be ready
      ADCSRA |=(1<<ADSC); //start ADC measurements
      byte m = ADCL; // fetch adc data
      byte j = ADCH;
      int k = (j << 8) | m; // form into an int
      k -= 0x0200; // form into a signed int
      k <<= 6; // form into a 16b signed int
      fft_input[i] = k; // put real data into even bins
      fft_input[i+1] = 0; // set odd bins to 0
    }//end of for


Everything works in the way I want it to. But now I need at least two digital pins to use digitalRead(); I found out all the digital pins stopped reading, but digitalWrite() still works.

Do you guys have any idea what is going on?
If audio section is the right place to ask this type of question, please point me to the correct section.

Thank you guys for the help in advance; Any suggestion and reply is welcome and appreciated.

Nick Gammon

How did you find that out? Can you post a complete sketch that demonstrates this?

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

hongp


How did you find that out? Can you post a complete sketch that demonstrates this?


Yes, sir. Here is the complete sketch:
Code: [Select]
#define LOG_OUT 1 // use the log output function
#define FFT_N 256 // set to 256 point fft
#include <FFT.h> // include the library

int refFrequency = 0;
int finalFrequency = 0;
int errorFrequency = 0;
int refState = 0;
int finalState = 0;
const int refInput = 9;
const int finalInput = 10;
int bin = 0;
int frequency = 0;
int resolution = 9615 / 256;
int maxLogOut = 0;
int motorControl = 11;

void setup() {
  Serial.begin(9600); // use the serial port
  pinMode(refInput, INPUT);
  pinMode(finalInput, INPUT);
  pinMode(motorControl, OUTPUT);
  ADCSRA |= (1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); //set adc prescaler=128
  ADMUX = 0x40; // use adc0
  ADCSRA |= (1<<ADATE);  //enable auto trigger
  ADCSRA |= (1<<ADEN);  //enable ADC
  ADCSRA |= (1<<ADSC);  //start ADC measurements
}

void loop() {
  while(1) { // reduces jitter
    cli();  // UDRE interrupt slows this way down on arduino1.0
    //sampling
    for (int i = 0 ; i < 512 ; i += 2) { // save 256 samples
      while(!(ADCSRA & 0x10)); // wait for adc to be ready
      ADCSRA |=(1<<ADSC); //start ADC measurements
      byte m = ADCL; // fetch adc data
      byte j = ADCH;
      int k = (j << 8) | m; // form into an int
      k -= 0x0200; // form into a signed int
      k <<= 6; // form into a 16b signed int
      fft_input[i] = k; // put real data into even bins
      fft_input[i+1] = 0; // set odd bins to 0
    }//end of for
   
    //FFT
    fft_window(); // window the data for better frequency response
    fft_reorder(); // reorder the data before doing the fft
    fft_run(); // process the data in the fft
    fft_mag_log(); // take the output of the fft
    sei();
    maxLogOut = 0;
    for(int i = 1; i < 128; i++){
      if(fft_log_out[i] > maxLogOut && fft_log_out[i] > 150){
          maxLogOut = fft_log_out[i];
          bin = i;
      }//end of if
    }//end of for
    frequency = bin * resolution;
   
    //if digital pin 9 is high, set the reference frequency
    //if digital pin 10 is high, set the final frequency
    refState == digitalRead(refInput);
    finalState == digitalRead(finalInput);
    if(refState == HIGH){
      refFrequency = frequency;
      Serial.print("----------------------");
    } else if(finalState == HIGH){
      finalFrequency = frequency;
    } //end of else if
   
    //debug
    Serial.print("frequency: ");
    Serial.print(frequency);
    Serial.print(" | ");
    Serial.print("reference frequency: ");
    Serial.print(refFrequency);
    Serial.print(" | ");
    Serial.print("final frequency: ");
    Serial.print(finalFrequency);
    Serial.print(" | ");
    Serial.print("error frequency: ");
    Serial.println(errorFrequency);
   
    //motor control
   
  }//end of while
}//end of loop


It doesn't work because Serial.print("----------------------"); is not excuted after I connect digital pin 9 to 5V. I need to have a control over the reference and final frequency in order to have a feedback path, then do the motor control

Nick Gammon

Code: [Select]

    refState == digitalRead(refInput);
    finalState == digitalRead(finalInput);


That compares, not assigns.

You want "=".
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

hongp


Code: [Select]

    refState == digitalRead(refInput);
    finalState == digitalRead(finalInput);


That compares, not assigns.

You want "=".

Sorry about the silly question, and thank you for the help

Go Up