I am having trouble controlling the pitch (frequency) coming out of a speaker. I can use a pot to control it but I want to use an IR controller to do the same thing.
What I'm trying to accomplish is to have the speaker output the range of 100 Hz to 1200 Hz and every time I push a button on the controller it would step up the frequency by 50 Hz. I have mapped the HEX code for my controller and input that into my code.
My problem is I think I have the right code but it won't compile for my Mega 2560 board. I will post my code below. Could anyone please help?
#include <IRremote.h>
int RECV_PIN = 11;
int speakerPin=0;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
pinMode(0,OUTPUT);
}
void translateIR()
{
int sensorValue=digitalRead(results.value);
int frequency=map(sensorValue, 0, 1023, 100, 1200);
tone(speakerPin, frequency);
switch(results.value){
case 0xFF629D: //Volume up Button
if (sensorValue==0){
Serial.println("Tone Up");
for(int i=100;i<=1200;i+50){
digitalWrite(speakerPin, i);
break;
}
}
case 0xFFA857: //Volume Down Button
if (sensorValue==0){
Serial.println("Tone Down");
for (int i=1200;i>=100;i-50){
digitalWrite(speakerPin,i);
break;
}
}
default:
Serial.println("Button not Mapped");
}
}
void loop() {
if (irrecv.decode(&results)) {
Serial.println(results.value,HEX);
translateIR();
irrecv.resume(); // Receive the next value
delay(200);
}
}