Pages: [1]   Go Down
Author Topic: Project 06 - Negative frequency ?  (Read 614 times)
0 Members and 4 Guests 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 9 times.)
Logged

Shelby Township, Disco, Michigan
Offline Offline
Sr. Member
****
Karma: 15
Posts: 466
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
Sr. Member
****
Karma: 15
Posts: 466
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My bad, was thinking 40,000
Logged

Pages: [1]   Go Up
Jump to: