# Simple Frequency Generator Issue

I'm new to arduino and coding in general so I'm sure the solution is quite simple. However I've had trouble finding anything to explain or help out with this problem. I've been trying to get a function I'd created to run a small Piezo buzzer through a range of frequencies based off of a function I'd made: F=f(2)^(n/12) where F = the output frequency, f = the open or 'starting' frequency, and n is the dependent value that can shoot off to whatever desired value. The issue is F always ends up being some wonky value that's nowhere relative to what it should be. I'd typed out whats below but it spits out a multiple lines of 440s, 441s, 442s, and so on. I'd also like to get the decimal place to the thousandths if possible:

int n; //Note above open frequency int F; //Product note

void setup() { Serial.begin(9600); n=0; }

void loop() { F=220*2^(n/12); //220 stands for open frequency value Serial.println(F); delay(500); n++; }

https://www.arduino.cc/en/Reference/HomePage

Look at the section on Data Types and look at the entry for int. Study up what kinds of numbers it can and cannot hold. Then look at some of the other data types and see if there might be one that's more appropriate.

``````void loop() {
F=220*2^(n/12);
``````

While you're there, find the entry for the '^' operator (under Bitwise Operators). It doesn't do what you apparently think it does. Next look under the Math section and see if you don't find something more appropriate for that.

And '^' is xor and not the power operator.

Finally, you need to change the name of at least one variable. F is a defined macro. You never ever ever want to use single capital letters for variable names because those are reserved for macros in the core. In your code you should try to use words, like 'frequency' instead of just single letters. But for something this simple I guess it is OK as long as they're all lower case.

Thanks Delta_G. Getting a good foot placement off of your replies. It never had even occurred to me that ^ wouldn't work as a power function or issues that may arise with capital letters. I'll make sure to study up on the language reference page you'd appointed me to. Good stuff, good stuff!