Pages: [1]   Go Down
Author Topic: Project 06 - Negative frequency ?  (Read 3549 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I began coding on arduino since last week, and today I'm working on project 06 of the starter kit. Everything is working, however I would like to know value of the frequency that the piezo is playing, that's why I put in the code the function serial.print(). However when I'm checking the value in the monitor, sometimes I have neagtive values of frequency. Do you know what does it mean ? Is there a  problem ?

Here is my code
Code:
//Play Theremin with a Piezo

// Set up the name of the different variable
int sensorValue;
int sensorHigh = 0;
int sensorLow = 1023;

// assign the variable to the different Pin
const int ledPin = 13;

void setup(){
  pinMode(ledPin,OUTPUT);
  digitalWrite(ledPin,HIGH);
  Serial.begin(9600);
 
  while(millis()<5000){
    sensorValue = analogRead(A0);
    if (sensorValue < sensorLow){
      sensorLow = sensorValue;
    }
    if (sensorValue > sensorHigh){
      sensorHigh = sensorValue;
    }
  }
 
  digitalWrite(ledPin, LOW);
}
void loop(){
  sensorValue=analogRead(A0);
  int pitch = map(sensorValue,sensorLow,sensorHigh,50,4000);
 
  Serial.print("frequency value: ");
  Serial.println(pitch);

  tone(8,pitch,20);
    delay(10);
}

I put a screenshot of the monitor at the of this post

Thank you ;-)


* frequency value.jpg (206.87 KB, 218x702 - viewed 37 times.)
Logged

Shelby Township, Disco, Michigan
Offline Offline
God Member
*****
Karma: 26
Posts: 766
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int pitch = map(sensorValue,sensorLow,sensorHigh,50,4000);
How large a number +/- can an int hold?
Use long
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How large a number +/- can an int hold?
Use long

According to Arduino website, " int" can hold  a range of -32,768 to +32,767, and "long" can hold a range of -2,147,483,648 to +2,147,483,647.
But I solve my problem, it was during the calibration of the sensor, the problem wasn't come from the code, it was me ^^.
Thank you for your answer.
Have a nice day  smiley-wink
Logged

Shelby Township, Disco, Michigan
Offline Offline
God Member
*****
Karma: 26
Posts: 766
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My bad, was thinking 40,000
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 634
Posts: 34528
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Any bit pattern with the most significant bit set will print out as negative.
Look up "two's complement" for exact details.
http://en.wikipedia.org/wiki/Two%27s_complement
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Posted by: steinie44

My bad, was thinking 40,000

If you can hear 40,000 Hz then you're either a dog or the world's only human with supersonic ears. smiley-grin

Rob Rothman
Logged

Shelby Township, Disco, Michigan
Offline Offline
God Member
*****
Karma: 26
Posts: 766
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
If you can hear 40,000 Hz then you're either a dog or the world's only human with supersonic ears. smiley-grin

Rob Rothman
My dog made me do it. Then he made me fess-up. smiley
Logged

Pages: [1]   Go Up
Jump to: