Show Posts
Pages: [1] 2
1  Using Arduino / Project Guidance / Re: frequency counter for Atmega2560 on: January 27, 2013, 09:08:09 am
hi  dhenry
thanks for helping.
I am not in the lab, I don't have signal generator my selv, I will test  after 1 hour, so instead of this
Code:
startCounting (500);  // how many mS to count for
shall I increase or decrease the counting number , I will test soon, but my problem is the code is counting either odd numbers or even numbers, and I want to count both,  like this way. 90, 91, 92, 93, 94,95,96 ....... and so on 
2  Using Arduino / Project Guidance / Re: frequency counter for Atmega2560 on: January 27, 2013, 04:07:31 am
Hi, Nick Gammon

thanks for quick reply

I am using Amega2560 - seeeduino,the code is yours:  the code is working perfectly,when I tested, the problem is jumping over one number.
here is the code:
Code:
// Timer and Counter example for Mega2560
// Author: Nick Gammon
// Date: 24th April 2012

// input on pin D47 (T5)

// these are checked for in the main program
volatile unsigned long timerCounts;
volatile boolean counterReady;

// internal to counting routine
unsigned long overflowCount;
unsigned int timerTicks;
unsigned int timerPeriod;

void startCounting (unsigned int ms)
  {

  counterReady = false;         // time not up yet
  timerPeriod = ms;             // how many 1 mS counts to do
  timerTicks = 0;               // reset interrupt counter
  overflowCount = 0;            // no overflows yet

  // reset Timer 2 and Timer 5
  TCCR2A = 0;
  TCCR2B = 0;
  TCCR5A = 0;            
  TCCR5B = 0;  

  // Timer 5 - counts events on pin D47
  TIMSK5 = _BV (TOIE1);   // interrupt on Timer 5 overflow

  // Timer 2 - gives us our 1 mS counting interval
  // 16 MHz clock (62.5 nS per tick) - prescaled by 128
  //  counter increments every 8 uS.
  // So we count 125 of them, giving exactly 1000 uS (1 mS)
  TCCR2A = _BV (WGM21) ;   // CTC mode
  OCR2A  = 124;            // count up to 125  (zero relative!!!!)

  // Timer 2 - interrupt on match (ie. every 1 mS)
  TIMSK2 = _BV (OCIE2A);   // enable Timer2 Interrupt

  TCNT2 = 0;    
  TCNT5 = 0;      // Both counters to zero

  // Reset prescalers
  GTCCR = _BV (PSRASY);        // reset prescaler now
  // start Timer 2
  TCCR2B =  _BV (CS20) | _BV (CS22) ;  // prescaler of 128
  // start Timer 5
  // External clock source on T4 pin (D47). Clock on rising edge.
  TCCR5B =  _BV (CS50) | _BV (CS51) | _BV (CS52);

}  // end of startCounting

ISR (TIMER5_OVF_vect)
{
  ++overflowCount;               // count number of Counter1 overflows  
}  // end of TIMER5_OVF_vect


//******************************************************************
//  Timer2 Interrupt Service is invoked by hardware Timer 2 every 1ms = 1000 Hz
//  16Mhz / 128 / 125 = 1000 Hz

ISR (TIMER2_COMPA_vect)
{
  // grab counter value before it changes any more
  unsigned int timer5CounterValue;
  timer5CounterValue = TCNT5;  // see datasheet, (accessing 16-bit registers)

  // see if we have reached timing period
  if (++timerTicks < timerPeriod)
    return;  // not yet

  // if just missed an overflow
  if (TIFR5 & TOV5)
    overflowCount++;

  // end of gate time, measurement ready

  TCCR5A = 0;    // stop timer 5
  TCCR5B = 0;    

  TCCR2A = 0;    // stop timer 2
  TCCR2B = 0;    

  TIMSK2 = 0;    // disable Timer2 Interrupt
  TIMSK5 = 0;    // disable Timer5 Interrupt

  // calculate total count
  timerCounts = (overflowCount << 16) + timer5CounterValue;  // each overflow is 65536 more
  counterReady = true;              // set global flag for end count period
}  // end of TIMER2_COMPA_vect


void setup () {
  Serial.begin(115200);      
  Serial.println("Frequency Counter");
} // end of setup



void loop () {

  // stop Timer 0 interrupts from throwing the count out
  byte oldTCCR0A = TCCR0A;
  byte oldTCCR0B = TCCR0B;
  TCCR0A = 0;    // stop timer 0
  TCCR0B = 0;    
  
  startCounting (500);  // how many mS to count for

  while (!counterReady)
     { }  // loop until count over

  // adjust counts by counting interval to give frequency in Hz
  float frq = (timerCounts *  1000.0) / timerPeriod;

  // restart timer 0
  TCCR0A = oldTCCR0A;
  TCCR0B = oldTCCR0B;

  Serial.print ("Frequency: ");
  Serial.println ((unsigned long) frq);
  
  // let serial stuff finish
  delay(200);

}   // end of loop
what I have to modify this code so it can count .. 1,2, 3, 4, 5, 6, 7, 8, instead of 1 , 3, 5, 7, 9 or 2, 4, 6, 8, 10 using signal generator.

thanks
3  Using Arduino / Project Guidance / frequency counter for Atmega2560 on: January 27, 2013, 03:07:00 am
Hi , Nick Gammon

first thanks for your tutorial on this link http://www.gammon.com.au/forum/?id=11504 it is very useful, tutorial
I want to use the code on "Frequency Counter sketch for Atmega2560" on your tutorial,  when I tested the code it is counting either  odd numbers or even numbers , I tested using sigal generator,

what I wanted is to count numbers for example:
 starting from:  80, 81, 82, 83, 84, 85, and so on
but the code is counting for example. 80, 82, 84, 86, 88, 90  and so on it is jumping over a one number
if I start for example  81,  it continued as  83, 85, 87, 89, 91

can you please help me by  telling what I have to change in your code to count - like . 60,61,62,63, 64,65,66,67,68,69,70  continued like that not jumping any number?

thanks

4  Using Arduino / Programming Questions / Re: guitar frequencies on: January 19, 2013, 01:03:36 pm
the code continued here

Code:
case 3:
 digitalWrite(led_e4, LOW); // sets the proper LED on, all else off
 digitalWrite(led_b3, LOW);
 digitalWrite(led_g3, HIGH);
 digitalWrite(led_d3, LOW);
 digitalWrite(led_a2, LOW);
 digitalWrite(led_e2, LOW);
 a = 0.0730;
 b = 0.8541;
 threshold = 130;
 upper_bound = 50;
 lower_bound = 15;
 avg_upper = 29;
 in_tune = 27; // This is the “in tune” average of cross counts for the string.
 avg_lower = 26;
 timer_divide = 500;
 divide_by = 4;
 break;
 case 4:
digitalWrite(led_e4, LOW); // sets the proper LED on, all else off
 digitalWrite(led_b3, HIGH);
 digitalWrite(led_g3, LOW);
 digitalWrite(led_d3, LOW);
 digitalWrite(led_a2, LOW);
 digitalWrite(led_e2, LOW);
a = 0.1270;
 b = 0.7459;
 threshold = 140;
 upper_bound = 50;
 lower_bound = 15;
 avg_upper = 35;
 in_tune = 34; // This is the “in tune” average of cross counts for the string.
 avg_lower = 33;
 timer_divide = 500;
 divide_by = 4;
 break;
case 5:
 digitalWrite(led_e4, HIGH); // sets the proper LED on, all else off
 digitalWrite(led_b3, LOW);
 digitalWrite(led_g3, LOW);
 digitalWrite(led_d3, LOW);
 digitalWrite(led_a2, LOW);
 digitalWrite(led_e2, LOW);
 a = 0.1648;
 b = 0.6705;
threshold = 150;
 upper_bound = 60;
 lower_bound = 20;
 avg_upper = 47;
 in_tune = 45; // This is the “in tune” average of cross counts for the string.
 avg_lower = 43;
 timer_divide = 500;
 divide_by = 4;
 break;
 }

 check_crossings();

 // After the string input has been idle for a while, we take the average of a number of cross counts that were in bound.
 if (idle_timer == 10000)
 {
 Serial.println("AVG AVG LOOK HERE AVG AVG");
 average_val = avg_cross / divide_by;
 Serial.println(average_val);

 // If else statements for tuner lights
 if ((average_val < avg_lower) && (average_val > 0))
 {
 // Turn off all string display lights to conserve power
 digitalWrite(led_e4, LOW);
 digitalWrite(led_b3, LOW);
 digitalWrite(led_g3, LOW);
 digitalWrite(led_d3, LOW);
 digitalWrite(led_a2, LOW);
 digitalWrite(led_e2, LOW);

 // Sets the proper tuning LED on, all else off
 digitalWrite(led_high, LOW);
 digitalWrite(led_ok, LOW);
 digitalWrite(led_low, HIGH);

 pitch_diff = in_tune - average_val;
 Serial.print("Pitch Difference Low: ");
 Serial.println(pitch_diff);

 // If the tuning is off by a questionably high amount, count it as an error in reading and do not turn the peg.
 // Otherwise tune the peg for a period of time. This time depends on how far off the read average is.
 if (pitch_diff < 20)
 for(long i = 0; i < pitch_diff * 36000; i++)
 {
 digitalWrite(servoPin, HIGH); // start the pulse
 delayMicroseconds(15); // pulse width
 digitalWrite(servoPin, LOW); // stop the pulse
 }
 }

 // Don't turn the peg if the guitar is in tune.
 else if ((average_val >= avg_lower && average_val <= avg_upper) || (average_val == 0))
 {
 // Sets the proper tuning LED on, all else off
 digitalWrite(led_high, LOW);
 digitalWrite(led_ok, HIGH);
 digitalWrite(led_low, LOW);
 }

 else if (average_val > avg_upper)
 {
 // Turn off all string display lights to conserve power
 digitalWrite(led_e4, LOW);
 digitalWrite(led_b3, LOW);
 digitalWrite(led_g3, LOW);
 digitalWrite(led_d3, LOW);
 digitalWrite(led_a2, LOW);
 digitalWrite(led_e2, LOW);

 // Sets the proper tuning LED on, all else off
 digitalWrite(led_high, HIGH);
 digitalWrite(led_ok, LOW);
 digitalWrite(led_low, LOW);

 pitch_diff = average_val - in_tune;
 Serial.print("Pitch Difference High: ");
 Serial.println(pitch_diff);

 // If the tuning is off by a questionably high amount, count it as an error in reading and do not turn the peg.
 // Otherwise tune the peg for a period of time. This time depends on how far off the read average is.
 if (pitch_diff < 20)
 for(long i = 0; i < pitch_diff * 270000; i++)
 {
 digitalWrite(servoPin, HIGH); // start the pulse
 delayMicroseconds(2); // pulse width
 digitalWrite(servoPin, LOW); // stop the pulse
 }
 }

 // Reset all variables used for pitch detection
 cross_count = 0;
 avg_cross = 0;
 avg_counter = 0;
 }

 // We take the average of cross counts after the first value in bound. We dismiss the first value since it is usually inaccurate
 // for finding a good average.
 if (timer % timer_divide == 0){
 if (cross_count > lower_bound && cross_count < upper_bound) {
 if (avg_counter >= 1 && avg_counter < (divide_by + 1)) {
 avg_cross = avg_cross + cross_count;
 Serial.print("Runnin Avg cross sum: ");
 Serial.println(avg_cross);
 }
avg_counter++;
}
Serial.print("cross_count: ");
 Serial.println(cross_count);
 cross_count = 0;
 }
 }

void check_crossings()
 {
 lastx = x;
 lasty = y;
 x = analogRead(analogPin); // Read the input pin
 y = a * x + a * lastx + b * lasty; // Apply Butterworth filter to eliminate high frequencies

 // If the string crosses it's set threshold, add it to the count. If there are no crossings, the idle timer will begin to run.
 if (lasty > threshold and y < threshold)
 {
 cross_count++;
 idle_timer = 0;
 }
}
5  Using Arduino / Programming Questions / Re: guitar frequencies on: January 19, 2013, 01:00:11 pm
Hi PaulS

thanks for help, may be  "a strang car again" sorry, I am new for using farum.
the hardware I am using, I explained before here: http://arduino.cc/forum/index.php/topic,142209.msg1075998.html#msg1075998
I get some code from this  public domain: http://www.tonychai.com/guitar.html wrote by Tony Chai
I don,t need to to use servo or tuning peg, indstead I am using electromegnet and coils to vibrate the string, just visualizing frequency. my vibration hardware circuit I put here
  
here is the code
Code:
/*
 * Magic_Tune
 *
 * This program will receive an amplified analog signal, compare the pitch, and control the servo motor while displaying output LEDs
 */


//Libraries required for servo control and usage of the Atmega’s internal timer
 #include
#include "WProgram.h"

//Global variables to be used
 int x;
 int y;
 int lastx;
 int lasty;
 long timer;
 int idle_timer;
 int threshold;
 int cross_count;
 int in_tune;
 int average_val;
 int pitch_diff;

int upper_bound;
 int lower_bound;
 int avg_cross;
 int avg_counter;
 int avg_upper;
 int avg_lower;
 float a, b;

int timer_divide;
 int divide_by;

int string_select;
 int select_pin_val;

int analogPin = 0; // analog to digital pin for signal input

int led_high = 13; // this LED will show user if a string's pitch is too high
 int led_ok = 12; // this LED will show user if a string's pitch is correct
 int led_low = 11; // this LED will show user if a string's pitch is too low

// these pins will light up to show which string the Atmega is comparing values for
 int led_e4 = 10;
 int led_b3 = 9;
 int led_g3 = 8;
 int led_d3 = 7;
 int led_a2 = 6;
 int led_e2 = 5;

int button_pin = 3; // input pin for user using a button to switch strings
 int servoPin = 2; // control pin for servo motor

void setup()
 {
 // Set up timer 1 to generate an interrupt every 1 microsecond
 TCCR1A = 0x00;
 TCCR1B = (_BV(WGM12) | _BV(CS12));
 OCR1A = .071;
 TIMSK1 = _BV(OCIE1A);

 x = 0;
 lastx = 0;
 y = 0;
 lasty = 0;

 timer = 0;
 cross_count = 0;
 avg_cross = 0;
 avg_counter = 0;
 string_select = 0;

 //Set the input and output pins
 pinMode(button_pin, INPUT);
 pinMode(servoPin, OUTPUT);
 pinMode(led_high, OUTPUT);
 pinMode(led_ok, OUTPUT);
 pinMode(led_low, OUTPUT);
 pinMode(led_e4, OUTPUT);
 pinMode(led_b3, OUTPUT);
 pinMode(led_g3, OUTPUT);
 pinMode(led_d3, OUTPUT);
 pinMode(led_a2, OUTPUT);
 pinMode(led_e2, OUTPUT);

 Serial.begin(9600); // Opens serial port, sets data rate to 9600 bps
 }

// Nothing is done in the Arduino loop, since timing is off.
 void loop()
 {
}

// Timer function running every microsecond
 ISR(TIMER1_COMPA_vect)
 {
 timer++;
 idle_timer++;

 // Read button press to determine which string is to be detected
 if (timer % 100 == 0)
 {
 select_pin_val = digitalRead(button_pin);
 if (select_pin_val == HIGH)
 {
 string_select = ((string_select + 1) % 6);
 Serial.print("string: ");
 Serial.println(string_select);
 }
 }


 // Depending on which string is selected, the proper variables are set
 switch (string_select)
 {
 case 0:
 digitalWrite(led_e4, LOW); // sets the proper LED on, all else off
 digitalWrite(led_b3, LOW);
 digitalWrite(led_g3, LOW);
 digitalWrite(led_d3, LOW);
 digitalWrite(led_a2, LOW);
 digitalWrite(led_e2, HIGH);
 a = 0.045;
 b = 0.9099;
 threshold = 150;
 upper_bound = 77;
 lower_bound = 33;
 avg_upper = 57;
 in_tune = 55; // This is the “in tune” average of cross counts for the string.
 avg_lower = 53;
 timer_divide= 2000;
 divide_by = 3;
 break;
 case 1:
 digitalWrite(led_e4, LOW); // sets the proper LED on, all else off
 digitalWrite(led_b3, LOW);
 digitalWrite(led_g3, LOW);
 digitalWrite(led_d3, LOW);
 digitalWrite(led_a2, HIGH);
 digitalWrite(led_e2, LOW);
 a = 0.0592;
 b = 0.8816;
 threshold = 150;
 upper_bound = 88;
 lower_bound = 44;
 avg_upper = 67;
 in_tune = 65; // This is the “in tune” average of cross counts for the string.
 avg_lower = 63;
 timer_divide = 2000;
 divide_by = 3;
 break;
 case 2:
 digitalWrite(led_e4, LOW); // sets the proper LED on, all else off
 digitalWrite(led_b3, LOW);
 digitalWrite(led_g3, LOW);
 digitalWrite(led_d3, HIGH);
 digitalWrite(led_a2, LOW);
 digitalWrite(led_e2, LOW);
 a = 0.0797;
 b = 0.8406;
 threshold = 150;
 upper_bound = 117;
 lower_bound = 63;
 avg_upper = 97;
 in_tune = 95; // This is the “in tune” average of cross counts for the string.
 avg_lower = 93;
 timer_divide = 2000;
 divide_by = 3;
 break;
 
 I have to divede the code two parts because I cant put here all- the next is ather part of the code-
see the circuit image
6  Using Arduino / Programming Questions / Re: guitar frequencies on: January 19, 2013, 05:38:50 am
Hi  GoForSmoke

I just want to visualize the string frequency(guitar string) on LCD.
7  Using Arduino / Programming Questions / Re: guitar frequencies on: January 18, 2013, 08:02:24 pm
HI,

is there any help about this thread,http://arduino.cc/forum/index.php/topic,142209.msg1075998.html#msg1075998 , still I coun't figur out , calculating the frequency of guitar strings???? any hints ?
thanks
8  Using Arduino / Programming Questions / Re: guitar frequencies on: January 17, 2013, 05:25:11 pm
thanks

I installed the library "frequency Library" and it complied well,

thank you PoulS and other guys in thread smiley
9  Using Arduino / Programming Questions / Re: guitar frequencies on: January 17, 2013, 04:35:25 pm
Thanks  PoulS

now I changed header file from  #include <FreqCounter.h> to 
#include "FreqCounter.h"

I can see that was a part of the compile error, but still the compile problem not disappear,
new one is her: on the piture
how to install the library as library ?, sorry my novice questions?
thanks again
10  Using Arduino / Programming Questions / Re: guitar frequencies on: January 17, 2013, 02:39:01 pm
sorry PaulS
you are  right, what I want to try is to re-use  the frequency counter library code/ the code on this public domain link:http://www.pjrc.com/teensy/td_libs_FreqCount.html#compare
because I think, it is relevant my project after modifying,. I compliled the code, there is always error like this one
thanks
the code is
Code:
// Frequency Counter Lib example

/*
  Martin Nawrath KHM LAB3
  Kunsthochschule f¸r Medien Kˆln
  Academy of Media Arts
  http://www.khm.de
  http://interface.khm.de/index.php/labor/experimente/
 */
#include <FreqCounter.h>


unsigned long frq;
int cnt;
int pinLed=13;

void setup() {
  pinMode(pinLed, OUTPUT);

  Serial.begin(115200);        // connect to the serial port

  Serial.println("Frequency Counter");

}



void loop() {

  // wait if any serial is going on
  FreqCounter::f_comp=10;   // Cal Value / Calibrate with professional Freq Counter
  FreqCounter::start(100);  // 100 ms Gate Time

  while (FreqCounter::f_ready == 0)

  frq=FreqCounter::f_freq;
  Serial.print(cnt++);
  Serial.print("  Freq: ");
  Serial.println(frq);
  delay(20);
  digitalWrite(pinLed,!digitalRead(pinLed));  // blink Led



header file

Code:
/*
  FreqCounter.h - Library for a Frequency Counter c.
  Created by Martin Nawrath, KHM Lab3, Dec. 2008
  Released into the public domain.
*/



#ifndef FreqCounter_h
#define FreqCounter_h


#include <avr/interrupt.h>
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

namespace FreqCounter {

extern unsigned long f_freq;
extern volatile unsigned char f_ready;
extern volatile unsigned char f_mlt;
extern volatile unsigned int f_tics;
extern volatile unsigned int f_period;
extern volatile unsigned int f_comp;

void start(int ms);


}

#endif

11  Using Arduino / Programming Questions / Re: guitar frequencies on: January 17, 2013, 11:23:56 am
Thanks PaulS and AWOL for the help,

indeed it is part of  our project.
the hardware I am using,is microcontroller seeeduino ADK Mega2560 and LMD386 amplifier - when the string is tuned the signal from the guitars string should be amplified and it passed to arduino  pin0 , the signal should be again filtered and finally  it should be shown the frequency on LCD. the input and the output of the amplifier  are connected two coils-with electro-magatics , one input couil and the other is output coil(one coil is as microphone the other coil is making vabrating the string) , the tuning  is happen outomatically when the guitars string come very close to the magnatics, one  string at a time. the idea is like this link:http://www.youtube.com/watch?v=cz2E9Ijz9_I
 I attached here the picture of the system.  I still need help about coding,  
 thanks

obs: is that line 87 of the frequency library - ?  
but it is behaving like my compiler does'nt recognize c++ code
I am using arduino IDE-
12  Using Arduino / Programming Questions / guitar string frequency on: January 17, 2013, 03:07:57 am
Hi   guys,
I need some help/hints  about  programming_ my guitar project- specifically  calculating frequency of a guitar strings and displaying it on LCD. the solution I have been required is like the picture of this link. http://www.pjrc.com/teensy/td_libs_FreqCount.html#compare
question is like:
1- write a progrogram that measuring/calculating frequency of a acoustic guitars string individually
2- when you tune one string it should be seen the frequency on lcd, one string at one time, use a zero-crossing filter/formula
is there any one of you guys can help me  on that, I am using seeeduino board , I tried to use frequency library on the above link, but I get error 

thanks
13  Using Arduino / Programming Questions / Re: guitar frequencies on: January 16, 2013, 07:46:46 pm
Hi   
 I am still waiting some help/hints  about  programming_ my guitar project- specifically  calculating frequency of a guitar strings and displaying it on LCD. the solution I have been required is like the picture of this link. http://www.pjrc.com/teensy/td_libs_FreqCount.html#compare
is there any one of you guys can help me  on that, I am using seeeduino board , I tried to use frequency library on the above link, but I get error 

thanks
14  Using Arduino / Programming Questions / Re: guitar frequencies on: January 15, 2013, 03:34:50 am
Thanks Valcarrara  for reply.
sorry, It was my fault by  asking wrong/weird question, because what I want to get solution is : calculation/programming frequency of the guitar strings individually, using arduino code:
question says:
when you tone f.eks. biggest string E nr.6, we have to see the frequency of E(82 Hz) on the LCD, but just only E, and if we tune again the smallest string (e) nr.1 , we have to see 329 Hz on LCD and so on, individually at a time.  we have to use zero crossing algorithm, PWM or ADC convertion using timers, but my programming knowledge is minimal , guys can you please help me how code that. Thanks
15  Using Arduino / Programming Questions / Re: guitar frequencies on: January 11, 2013, 06:26:06 pm
thanks MarT

I just download the one in this link http://interface.khm.de/index.php/lab/experiments/arduino-frequency-counter-library/

down the in the page: Download  >FreqCounter Library
Pages: [1] 2