hello
i'm trying to write a sound oscillator and i've written a method "pitch()" that gets an unsigned long for timing and a pin number, and then toggles that pin according to the timing. it works perfect.
but the problem is another method which is supposed to change pitch. it gets two pitches and the amount of time that it takes for changing. this is the code:
void changePitchAutiomation(int pitch1Pin, int pitch2Pin, int intervalPin) {
unsigned long prevState;
unsigned long currentState;
int pitch1 = analogRead(pitch1Pin);
int pitch2 = analogRead(pitch2Pin);
int interval = analogRead(intervalPin);
int pitchChanging = pitch2 - pitch1;
int eachLevelTime = interval / pitchChanging;
if (pitch2 >= pitch1) {
for (int i = 0; i <= pitchChanging; i++) {
prevState = micros();
currentState = micros();
while (currentState - prevState < eachLevelTime) {
pitch(pitch1 + i, 5);
currentState = micros();
}
}
}
else {
for (int i = 0; i <= pitchChanging * (-1); i++) {
prevState = micros();
currentState = micros();
while (currentState - prevState < eachLevelTime) {
pitch(pitch1 - i, 5);
currentState = micros();
}
}
}
}
this makes no sound. i've put the inside code within the loop() directly with actual numbers for pitch1, pitch2 and interval instead of analogRead(). still not working.
the only thing that came to my mind was that maybe the while() loop never runs because it takes much time just for running the instructions that the condition can't be true. i would be thankful if you help me correcting my code.
Your code snippet does not control any output pin. Is that the intention? You will have to show your complete code or a representative example that exhibits the behaviour.
sterretje:
Your code snippet does not control any output pin. Is that the intention? You will have to show your complete code or a representative example that exhibits the behaviour.
and then in the loop() method i call changePitchAutiomation(). pin 13 is not connected and is only for blinking the led on arduino. when i only call pitch() the led blinks but when i call the other method it remains on or maybe blinks very fast.
sterretje:
You specifically call pinMode(x, OUTPUT) in the function. Any reason? E.g. do you set it to INPUT at other places in your code.
no i don't. it's just because i put all the required code in one method so i don't need to do it in the setup() method.
sterretje:
Do you ever call changePitchAutiomation?
of course i do. i'm a newbie but not this much
sterretje:
And yes, you can hook up an oscilloscope and observe the data on the pin.
i mean without an oscilloscope. i'd heard about something called frequency meter, searched it on the google after my last post, and unfortunately they're even more expensive than oscilloscopes.
Add some Serial.print() statements to see what values the function changePitchAutiomation() sends to the pitch() function.
My wild guess is that your changePitchAutiomation() function should not be using micros() at all and should be relying on a return value from the pitch() function to measure time.
Or maybe the changePitchAutiomation() should only be doing part of the timing.
Or, another way to think about it, the timing in the two functions depends on each other. Maybe some of the timing variables should be global and shared by both functions.
Robin2:
Add some Serial.print() statements to see what values the function changePitchAutiomation() sends to the pitch() function.
in the pitch() method which is simpler and working, i used serial.println() after each toggling. but for some reason it didn't show any value on the serial monitor. i guess it's because of the speed of serial.
and actually i think it wouldn't be easy to change the entire approach, plus i want to find out the problem in this code which seems to be ok.
ardubeeg:
in the pitch() method which is simpler and working, i used serial.println() after each toggling. but for some reason it didn't show any value on the serial monitor. i guess it's because of the speed of serial.
You may indeed have to slow down the program - that is a normal debugging procedure.
and actually i think it wouldn't be easy to change the entire approach,
plus i want to find out the problem in this code which seems to be ok.
If it was OK you would not be asking questions here.
If it is not OK, something will have to be changed, even if it is difficult.
I doubt if the changes will be difficult, but they will need some careful thought to figure out what is required. I have made a few suggestions to stimulate the grey cells,