Go Down

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

#### bam94

##### Jul 25, 2014, 01:25 pm
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 variableint sensorValue;int sensorHigh = 0;int sensorLow = 1023;// assign the variable to the different Pinconst 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

#1
##### Jul 25, 2014, 03:06 pm
Code: [Select]
`int pitch = map(sensorValue,sensorLow,sensorHigh,50,4000);`
How large a number +/- can an int hold?
Use long

#### bam94

#2
##### Jul 25, 2014, 03:34 pm

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 ^^.
Have a nice day

#3

#### Grumpy_Mike

#4
##### Aug 13, 2014, 04:22 am
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

#### RobRothman

#5
##### Aug 13, 2014, 01:43 pm
Quote
Posted by: steinie44

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

Rob Rothman

#### steinie44

#6
##### Aug 13, 2014, 02:12 pm
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