Hello!
I am working on a project that will make stepper motors move at a certain frequency to generate sound.
I have the following two code snippets, both of which are functionally the same. One uses a class the other does not:
############NO CLASS###########
#include <Tone.h>
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, World!");
pinMode(38,OUTPUT);
pinMode(54,OUTPUT);
pinMode(61,OUTPUT);
digitalWrite(38,LOW);
Tone voice;
voice.begin(54);
for(uint16_t i=100;i<1000;i+=100){
voice.play(i);
delay(100);
voice.stop();
}
}
void loop() {
// put your main code here, to run repeatedly:
}
#############WITH CLASS############
#include <Tone.h>
class MotorChannel{
public:
Tone voice;
MotorChannel(){
pinMode(38,OUTPUT);
pinMode(54,OUTPUT);
pinMode(61,OUTPUT);
digitalWrite(38,LOW);
voice.begin(54);
}
void playFreq(uint16_t freq,uint16_t duration){
voice.play(freq);
delay(duration);
voice.stop();
}
};
MotorChannel lead(38,54,61);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, World!");
for(uint16_t i=100;i<1000;i+=100){
lead.playFreq(i,100);
}
}
void loop() {
// put your main code here, to run repeatedly:
}
*Please note the #include <Tone.h>. This is a library that has been discontinued, and will be attached below (hopefully).
*Hardware: ATMega2560 w/ RAMPS 1.4 board
The one with no classes runs fine. It plays a range of frequencies(expected).
The one that uses a class runs, but all frequencies get rounded. It ends up playing something like a low note twice, a mid note 7 times, then a high note. It should, however, play a range. Instead, it plays 3 frequencies.
What have I done wrong here? This is probably the weirdest issue I've ever had, and cannot wrap my head around it!
Thank you!
EDIT: I should note what the tone library does: It is a library that allows the use of multiple tones playing simultaneously using the different internal timers.
Tone.cpp (11.5 KB)
Tone.h (3.23 KB)