Go Down

Topic: Continual reading of analog input (Read 121 times) previous topic - next topic

jlangner

I have a relatively simple question for anyone who is competent with Arduinos. I have a piezo buzzer on pin 6 generating a tone of about 4166hz, every second. I have a microphone wired to analog pin (A0). I want to continually monitor the analog input...the code I came up with is below. The serial monitor/plotter are not displaying any data? Clearly my code is not working. Can someone give me any advice as to how to continually monitor the analog input while keeping the pin 6 routine the same? Thank you.
Code: [Select]

int analog = A0;
int output = 6;
int j = 4000;
int timerStop;
void setup() {
  Serial.begin(9600);
  DDRD = DDRB | B11111100;
  pinMode(output, OUTPUT);
}

void loop() {
  signalOutput();
  while(signalOn()){
    int value = analogRead(analog);
    Serial.print(value);
  }
  delay(1000);   
}

void signalOutput(){
  //signal frequency 4000Hz
  int timerStart = micros();
  timerStop = 0;
  while(timerStop <= 275){
    //digitalWrite(output, HIGH);
    PORTD = B01000000;
    delayMicroseconds(120);
    //digitalWrite(output, LOW);
    PORTD = B00000000;
    delayMicroseconds(120);
    timerStop = micros() - timerStart;
  }
}

boolean signalOn(){
  bool signalBoolean = true;
  if(timerStop <= 275){
    signalBoolean = true;
  }
  else{
    signalBoolean = false;
  }
  return signalBoolean;
}


aarg

#1
Nov 13, 2017, 03:21 am Last Edit: Nov 13, 2017, 03:24 am by aarg
There is no way to generate a tone in the brute force method that you are using, and cooperatively run other tasks. Certainly not enough time to do analog reads. You have to use some other approach to tone generation, probably using a processor timer.
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

jlangner

Would it be possible to read the analog input in an interrupt routine? Or would that not change anything

aarg

Would it be possible to read the analog input in an interrupt routine? Or would that not change anything
It's possible but it would pause the signal generation while the interrupt was being serviced. So there would be long discontinuities in the waveform.
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

jlangner

Thank you for all your help, I really do appreciate it!

Delta_G

Generate your tone with one of the hardware timers.  Then all your loop has left to do is read your input. 
If at first you don't succeed, up - home - sudo - enter.

Go Up