Hello everybody!
I'm pretty new to all this Arduino things, but I was looking to do a frequency detection and I found a code on Instructables.com The person who wrote it told there how it worked. I uploaded it on my Arduino Uno and indeed, it worked great! But I wanted to make the code bigger so I bought an Arduino Due (because he is a lot faster than the Uno), but I had to download the new beta-software (1.5 I think). I hooked up the Due and made exactly the same circuit (I only had to change 1 resistor because the Due works with 3.3V) and I took the same code but when I tried to upload it, I had a lot of errors...
This is the list of errors:
_1e_versie_Arduino_sketch.ino: In function 'void setup()':
_1e_versie_Arduino_sketch:34: error: 'cli' cannot be used as a function
_1e_versie_Arduino_sketch:39: error: 'ADCSRA' was not declared in this scope
_1e_versie_Arduino_sketch:40: error: 'ADCSRB' was not declared in this scope
_1e_versie_Arduino_sketch:42: error: 'ADMUX' was not declared in this scope
_1e_versie_Arduino_sketch:42: error: 'REFS0' was not declared in this scope
_1e_versie_Arduino_sketch:43: error: 'ADLAR' was not declared in this scope
_1e_versie_Arduino_sketch:45: error: 'ADPS2' was not declared in this scope
_1e_versie_Arduino_sketch:45: error: 'ADPS0' was not declared in this scope
_1e_versie_Arduino_sketch:46: error: 'ADATE' was not declared in this scope
_1e_versie_Arduino_sketch:47: error: 'ADIE' was not declared in this scope
_1e_versie_Arduino_sketch:48: error: 'ADEN' was not declared in this scope
_1e_versie_Arduino_sketch:49: error: 'ADSC' was not declared in this scope
_1e_versie_Arduino_sketch:51: error: 'sei' was not declared in this scope
_1e_versie_Arduino_sketch.ino: In function 'void ADC_vect()':
_1e_versie_Arduino_sketch:57: error: 'ADCH' was not declared in this scope
_1e_versie_Arduino_sketch:65: error: 'PORTB' was not declared in this scope
_1e_versie_Arduino_sketch.ino: In function 'void loop()':
_1e_versie_Arduino_sketch:74: error: 'PORTB' was not declared in this scope
This is the code by Amandaghaessei:
//sine wave freq detection with 38.5kHz sampling rate and interrupts
//by Amanda Ghassaei
//http://www.instructables.com/id/Arduino-Frequency-Detection/
//July 2012
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
*/
//clipping indicator variables
boolean clipping = 0;
//data storage variables
byte newData = 0;
byte prevData = 0;
//freq variables
unsigned int timer = 0;//counts period of wave
unsigned int period;
int frequency;
void setup(){
Serial.begin(9600);
pinMode(13,OUTPUT);//led indicator pin
cli();//diable interrupts
//set up continuous sampling of analog pin 0
//clear ADCSRA and ADCSRB registers
ADCSRA = 0;
ADCSRB = 0;
ADMUX |= (1 << REFS0); //set reference voltage
ADMUX |= (1 << ADLAR); //left align the ADC value- so we can read highest 8 bits from ADCH register only
ADCSRA |= (1 << ADPS2) | (1 << ADPS0); //set ADC clock with 32 prescaler- 16mHz/32=500kHz
ADCSRA |= (1 << ADATE); //enabble auto trigger
ADCSRA |= (1 << ADIE); //enable interrupts when measurement complete
ADCSRA |= (1 << ADEN); //enable ADC
ADCSRA |= (1 << ADSC); //start ADC measurements
sei();//enable interrupts
}
ISR(ADC_vect) {//when new ADC value ready
prevData = newData;//store previous value
newData = ADCH;//get value from A0
if (prevData < 127 && newData >=127){//if increasing and crossing midpoint
period = timer;//get period
timer = 0;//reset timer
}
if (newData == 0 || newData == 1023){//if clipping
PORTB |= B00100000;//set pin 13 high- turn on clipping indicator led
clipping = 1;//currently clipping
}
timer++;//increment timer at rate of 38.5kHz
}
void loop(){
if (clipping){//if currently clipping
PORTB &= B11011111;//turn off clippng indicator led
clipping = 0;
}
frequency = 38462/period;//timer rate/period
//print results
Serial.print(frequency);
Serial.println(" hz");
delay(100);
}
I hope some of you guys can help me out, I would be the happiest man in the world! ![]()
Thank you so much!!
Best regards,
RobinC,
Belgium