Go Down

Topic: Project 06 - Negative frequency ? (Read 3986 times) previous topic - next topic

bam94

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: [Select]

//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 ;-)

steinie44

Code: [Select]

int pitch = map(sensorValue,sensorLow,sensorHigh,50,4000);

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

bam94


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  ;)


Grumpy_Mike

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

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. :D

Rob Rothman

steinie44

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. :)

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy