Adding more code to loop contorts tone() of toy microwave! Why?

Hello,
I've built a DIY toy microwave for my daughter to play with (see picture below) and now I want to add two cooking plates that can be "heated" up and down via two potentiometers increasing/decreasing the brightness of two red LEDs. The code for both projects works fine when run alone, but once I insert the "cooking plate code" into the existing "microwave code", the microwave sounds generated by the passive buzzer are heavily contorted and also contain brief interruptions.

I don't think it's an issue of the wiring or of the parts I use, since this contortion happens even when I do not have any additional parts of the two cooking plates connected, but just add the code to the existing one. I guess it's an inherent problem of the tone() function, but I do not know an approach to tackle this.
Do you have any idea what's the issue here and how to solve it?

The microwave I built using:

  • Arduino Uno
  • 3x4 keypad matrix
  • one passive buzzer
  • 8x8 LED matrix (MAX7219)
  • one LED to light up the interior of the microwave

Obviously, the microwave doesn't really heat anything, but it "beeps" whenever my daughter presses a button on the keypad, shows this number on the LED matrix and does a countdown timer when she presses "*" on the keypad (i.e. imitates an engine sound and gives a final "Job's-done"-beep). Here's an image to give you an impression:

That's the (tiny bit of) code for one (!) cooking plate:

int potPin = A1; //Declare potPin to be analog pin A2
int LEDPin = 10; // Declare LEDPin to be arduino pin 10
int readValue;  // Use this variable to read Potentiometer
int writeValue; // Use this variable for writing to LED

void setup() {
  pinMode(potPin, INPUT);  //set potPin to be an input
  pinMode(LEDPin, OUTPUT); //set LEDPin to be an OUTPUT
  Serial.begin(9600);      // turn on Serial Port
}

void loop() {

  readValue = analogRead(potPin);  //Read the voltage on the Potentiometer
  Serial.print("Reading a value of ");  //for debugging print your values
  Serial.print(readValue);
  //writeValue = (255./1023.) * readValue; //Calculate Write Value for LED: when read is high, write is high.
  writeValue = 255. * ((1023. - readValue) / 1023.); //Calculate Write Value for LED: when read is high, write is low.
  analogWrite(LEDPin, writeValue);      //Write to the LED
  Serial.print(";    Writing a value of ");  //for debugging print your values
  Serial.println(writeValue);

}

And that's the code altogether, which works like a charm as long as the Part ".../////Herdplatten und -Knöpfe" of the loop is commented out:

/////////////////////////////////////////////////////////////////LEDmatrix
# include <LedControl.h>
LedControl LEDMatrix = LedControl(17, 18, 19, 1); //  Pin 17 →  DATA IN Pin, Pin 18 →  CLK Pin, Pin 19 →  CS Pin

/////////////////////////////////////////////////////////////////Lights
#define Backofenlicht 14
#define Mikrowellenlicht 12
#define HerdknopfEins A0
#define HerdknopfZwei A1
int readValueOne;  // Use this variable to read HerdknopfEins
int writeValueOne; // Use this variable for writing to HerdplatteEins
int readValueTwo;  // Use this variable to read HerdknopfZwei
int writeValueTwo; // Use this variable for writing to HerdplatteZwei
#define HerdplatteEins 10
#define HerdplatteZwei 11

/////////////////////////////////////////////////////////////////Keypad
#include <Keypad.h>
//Hier wird die größe des Keypads definiert
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
char customKey; //pressedKey entspricht in Zukunft den gedrĂĽckten Tasten
int lastNumber; //the key that was pressed before the recent one is saved if it's >0 and <10
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

/////////////////////////////////////////////////////////////////Buzzer without delay()
int buzzer_pin = 13;//the pin of the passive buzzer (be sure to ground the buzzer directly to arduino otherwise background noise will be an issue)

void Haus()
{ byte Zeichen[8] = {B00010000, B11111000, B11111100, B10011110, B10011110, B11111100, B11111000, B00010000};
  for (int zaehler = 0; zaehler < 8; zaehler ++)
  {
    LEDMatrix.setRow(0, zaehler, Zeichen[zaehler]);
  }
}

void setup()
{
  Serial.begin(9600);

  LEDMatrix.shutdown(0, false); // Matrix "aufwecken"
  LEDMatrix.setIntensity(0, 8); // Matrix auf mittlere Helligkeit setzen
  LEDMatrix.clearDisplay(0);
  Haus();

  pinMode(Mikrowellenlicht, OUTPUT);
  digitalWrite(Mikrowellenlicht, LOW);
  pinMode(HerdplatteEins, OUTPUT);
  digitalWrite(HerdplatteEins, LOW);
  pinMode(HerdplatteZwei, OUTPUT);
  digitalWrite(HerdplatteZwei, LOW);
  pinMode(HerdknopfEins, INPUT);
  pinMode(HerdknopfZwei, INPUT);
}

void Tasse()
{ byte Zeichen[8] = {B00000000, B11111101, B10000110, B10000101, B10000110, B11111101, B01001000, B01111000};
  for (int zaehler = 0; zaehler < 8; zaehler ++)
  {
    LEDMatrix.setRow(0, zaehler, Zeichen[zaehler]);
  }
}

void Smiley()
{ byte Zeichen[8] = {B00000000, B00100110, B01000110, B01000000, B01000000, B01000110, B00100110, B00000000};
  for (int zaehler = 0; zaehler < 8; zaehler ++)
  {
    LEDMatrix.setRow(0, zaehler, Zeichen[zaehler]);
  }
}

void Herz()
{ byte Zeichen[8] = {B00011100, B00111110, B01111110, B11111100, B11111100, B01111110, B00111110, B00011100};
  for (int zaehler = 0; zaehler < 8; zaehler ++)
  {
    LEDMatrix.setRow(0, zaehler, Zeichen[zaehler]);
  }
}

void PfeilUnten()
{ byte Zeichen[8] = {B00010000, B00110000, B01111111, B11111111, B01111111, B00110000, B00010000, B00000000};
  for (int zaehler = 0; zaehler < 8; zaehler ++)
  {
    LEDMatrix.setRow(0, zaehler, Zeichen[zaehler]);
  }
}

void ZahlEins()
{ byte Zeichen[8] = {B00000000, B00000000, B00000100, B10000010, B11111111, B10000000, B00000000, B00000000};
  for (int zaehler = 0; zaehler < 8; zaehler ++)
  {
    LEDMatrix.setRow(0, zaehler, Zeichen[zaehler]);
  }
}

void ZahlZwei()
{ byte Zeichen[8] = {B00000000, B10000010, B11000001, B10100001, B10010001, B10001110, B00000000, B00000000};
  for (int zaehler = 0; zaehler < 8; zaehler ++)
  {
    LEDMatrix.setRow(0, zaehler, Zeichen[zaehler]);
  }
}

void ZahlDrei()
{ byte Zeichen[8] = {B00000000, B01000010, B10000001, B10001001, B10001001, B01110110, B00000000, B00000000};
  for (int zaehler = 0; zaehler < 8; zaehler ++)
  {
    LEDMatrix.setRow(0, zaehler, Zeichen[zaehler]);
  }
}

void ZahlVier()
{ byte Zeichen[8] = {B00000000, B00110000, B00101000, B00100100, B11110010, B00100001, B00000000, B00000000};
  for (int zaehler = 0; zaehler < 8; zaehler ++)
  {
    LEDMatrix.setRow(0, zaehler, Zeichen[zaehler]);
  }
}

void ZahlFuenf()
{ byte Zeichen[8] = {B00000000, B01001111, B10001001, B10001001, B10001001, B01110001, B00000000, B00000000};
  for (int zaehler = 0; zaehler < 8; zaehler ++)
  {
    LEDMatrix.setRow(0, zaehler, Zeichen[zaehler]);
  }
}

void ZahlSechs()
{ byte Zeichen[8] = {B00000000, B01111110, B10001001, B10001001, B10001001, B01110010, B00000000, B00000000};
  for (int zaehler = 0; zaehler < 8; zaehler ++)
  {
    LEDMatrix.setRow(0, zaehler, Zeichen[zaehler]);
  }
}

void ZahlSieben()
{ byte Zeichen[8] = {B00000000, B11100001, B00010001, B00001001, B00000101, B00000011, B00000000, B00000000};
  for (int zaehler = 0; zaehler < 8; zaehler ++)
  {
    LEDMatrix.setRow(0, zaehler, Zeichen[zaehler]);
  }
}

void ZahlAcht()
{ byte Zeichen[8] = {B00000000, B01110110, B10001001, B10001001, B10001001, B01110110, B00000000, B00000000};
  for (int zaehler = 0; zaehler < 8; zaehler ++)
  {
    LEDMatrix.setRow(0, zaehler, Zeichen[zaehler]);
  }
}

void ZahlNeun()
{ byte Zeichen[8] = {B00000000, B01001110, B10010001, B10010001, B10010001, B01111110, B00000000, B00000000};
  for (int zaehler = 0; zaehler < 8; zaehler ++)
  {
    LEDMatrix.setRow(0, zaehler, Zeichen[zaehler]);
  }
}

void ZahlNull()
{ byte Zeichen[8] = {B00000000, B01111110, B10000001, B10000001, B10000001, B01111110, B00000000, B00000000};
  for (int zaehler = 0; zaehler < 8; zaehler ++)
  {
    LEDMatrix.setRow(0, zaehler, Zeichen[zaehler]);
  }
}

void Kreuz()
{ byte Zeichen[8] = {B10000001, B01000010, B00100100, B00011000, B00011000, B00100100, B01000010, B10000001};
  for (int zaehler = 0; zaehler < 8; zaehler ++)
  {
    LEDMatrix.setRow(0, zaehler, Zeichen[zaehler]);
  }
}

//Array "Timer" for a count down timer function
void (*Timer[9])() { //Adaptiert von hier: https://forum.arduino.cc/t/array-of-functions/429541/3
  ZahlEins, ZahlZwei, ZahlDrei, ZahlVier, ZahlFuenf, ZahlSechs, ZahlSieben, ZahlAcht, ZahlNeun
};


void loop()
{
  //////////////////////////////////////////////////////////////Herdplatten und -Knöpfe
  readValueOne = analogRead(HerdknopfEins);  //Read the voltage on the Potentiometer
  //Serial.print("PlatteEins ReadValue = ");  //for debugging print your values
  //Serial.print(readValueOne);
  writeValueOne = 255. * ((1023. - readValueOne) / 1023.); //Calculate Write Value for LED: when read is high, write is low.
  analogWrite(HerdplatteEins, writeValueOne);      //Write to the LED
  //Serial.print(";    PlatteEins WriteValue = ");  //for debugging print your values
  //Serial.println(writeValueOne);
  readValueTwo = analogRead(HerdknopfZwei);  //Read the voltage on the Potentiometer
  //Serial.print("PlatteZwei ReadValue = ");  //for debugging print your values
  //Serial.print(readValueTwo);
  writeValueTwo = 255. * ((1023. - readValueTwo) / 1023.); //Calculate Write Value for LED: when read is high, write is low.
  analogWrite(HerdplatteZwei, writeValueTwo);      //Write to the LED
  //Serial.print(";    PlatteZwei WriteValue = ");  //for debugging print your values
  //Serial.println(writeValueTwo);

  /////////////////////////////////////////////////////////////Keypad, Timer und Buzzer
  customKey = customKeypad.getKey();//pressedKey entspricht der gedrĂĽckten Taste
  if (customKey) { //Wenn eine Taste gedrĂĽckt wurde
    Serial.print("GedrĂĽckte Taste: ");
    Serial.println(customKey);//Teile uns am Serial Monitor die gedrĂĽckte Taste mit und mach einen Zeilenumbruch
    if (customKey > 0 && customKey <= 9) {
      customKey = lastNumber;
    }
    tone(buzzer_pin, 3000, 100); //generates a 100ms beep at 300 Hz on a passive buzzer
    switch (customKey)
    {
      case '1':
        ZahlEins();
        break;
      case '2':
        ZahlZwei();
        break;
      case '3':
        ZahlDrei();
        break;
      case '4':
        ZahlVier();
        break;
      case '5':
        ZahlFuenf();
        break;
      case '6':
        ZahlSechs();
        break;
      case '7':
        ZahlSieben();
        break;
      case '8':
        ZahlAcht();
        break;
      case '9':
        ZahlNeun();
        break;
      case '0':
        ZahlNull();
        break;
      case 'A':
        Herz();
        break;
      case 'B':
        Haus();
        break;
      case 'C':
        Smiley();
        break;
      case 'D':
        if (digitalRead(Mikrowellenlicht) == LOW) {
          digitalWrite(Mikrowellenlicht, HIGH);
        }
        else {
          digitalWrite(Mikrowellenlicht, LOW);
        }
        break;
      case '#':
        LEDMatrix.clearDisplay(0);
        break;
      case '*':
        Serial.print("Vorige Nummer: ");
        Serial.println(lastNumber);
        tone(buzzer_pin, 40, lastNumber * 1000);
        for (lastNumber != 0; lastNumber > 0; lastNumber--) { //lastNumber ist ungleich Null
          Timer[lastNumber]();//recalls the element from the array "Timer" at position "lastNumber"
          delay(1000);
        }
        Tasse();
        tone(buzzer_pin, 3000, 500);
    }
  }
}

Your serial baud rate is unnecessarily low - the extra printing in the hotplate section may be causing your code to block.

analogRead takes about 100uS - it might be having an effect on timing. You can read it asynchronously if it really is a problem.

Start by commenting out all lines in that block except one. Problem persist? Focus on that particular line, what it does, and how it could be done quicker/better. Problem went away? Comment out that line, and uncomment another line to see if the problem emerges.
If not one single line is responsible for the problem, then start by only putting in certain kinds of lines, such as the print/println lines.
That way you can troubleshoot your issue in a more focused way.

Btw, that is a very nice toy you made there :slight_smile:

It isn't large compared to the slow baud printing, but instead of floating point math contortions consider using integer math with the multiplies before the divides:

  writeValue = (255 * (1023 - readValue)) / 1023; //Calculate Write Value for LED: when read is high, write is low.

Or even converting the resolution of the input to the resolution of the output:

  writeValue = 255 - readValue/4; //Calculate Write Value for LED: when read is high, write is low.

Hallo ceejay82
Take a view to the map() function to convert the analog values more handy.
Have a nice day and enjoy coding in C++.

Add a serial print statements after each call to tone() to see when it is being called and with what values.

And yes, crank up the baud to something 21st century like 115200!

a7

1 Like

you could use

+1.

The Arduino tone function does not block. It will play for the duration specified, or until noTone is called, either to cut short a timed tone or turn off a tone with no specific duration.

a7

Thanks to everyone for your replies and the time you took to help me out! I appreciate your efforts (and your compliments on the project :blush:)!

Just cranking up the baud rate (I tried 38400 and 115200 separately), did not resolve the issue. And I don't think the serial printing in the cooking plate section causes this problem, as the problem persists even when I comment out the serial printing lines.

I'll try your other suggestions (map() function, toneAC, ...) as soon as I can (which isn't too often the case with two little kids around :sweat_smile:) and I'll let you know about the outcome!

Hi wildbill, thanks for your suggestion!
I tried analogReadAsync, which works well for one cooking plate (at least in theory, meaning without having connected the cooking plates, i.e. the red LEDs, and their potentiometers), but as soon as I comply the code for two cooking plates, the issue is back...
Maybe that's beacuse I did something wrong when extending the analogReadAsync from one cooking plate to two... I'm not familiar with the analogReadAsync and do neither know whether to use polling or callback mode nor how to do so.

I've come up with this code:

  //////////////////////////////// HerdplatteEins
  analogReadAsync(HerdknopfEins);  //Read the voltage on the 1st Potentiometer asynchronously
  readValueOne = getAnalogReadValue();
  Serial.print("ReadValueOne = "); 
  Serial.print(readValueOne);
  writeValueOne = 255. * ((1023. - readValueOne)/1023.); //Calculate Write Value for LED: when read is high, write is low.
  analogWrite(HerdplatteEins, writeValueOne);      //Write to the LED
  Serial.print(";    WriteValueOne = ");
  Serial.println(writeValueOne);
  1. Does that make sense? Or would you propose something different?
  2. Where's the problem if I just repeat this section of code once more for cooking plate no. 2? I assume using "getAnalogReadValue()" twice without discriminating between them isn't good, but I don't know how to discriminate them from each other.

Thanks for your help!!

Hi alto777, thanks to you as well for your suggestions!
I really like this idea for troubleshooting:

Add a serial print statements after each call to tone() to see when it is being called and with what values.

But I have absolutely no clue how to Serial.print the tone() I'm generating?!? Can you help me out? Thanks very much!

Hi paulpaulson,
thanks for your suggestion. I tired using the map() function and I love it! :star_struck:

writeValueTwo = 255. * ((1023. - readValueTwo)/1023.); //Calculate Write Value for LED: when read is high, write is low.

The code above does the same as the code below, which is just great in terms of keeping things simple and in order!

writeValueTwo = map(readValueTwo, 0, 1023, 255, 0);

analogReadAsync starts the process of reading the analog value. It takes a little while so you can't get the value until it is complete. You can see examples in the library of the two methods.

I am a little surprised that the async method solves the problem as you've got it there. Can you post the whole thing?

A closer look at your code shows you are already. I changed you print statement and added another; I believe these are the only calls to tone() in you sketch.

If there are others or you add any, just Serial.print the arguments you are passing to tone(), then see what gets printed relative to what you hear.


        Serial.print("tone 40, ");
        Serial.print(lastNumber * 1000);
        Serial.println(“ now!”);


        tone(buzzer_pin, 40, lastNumber * 1000);
        for (lastNumber != 0; lastNumber > 0; lastNumber--) { //lastNumber ist ungleich Null
          Timer[lastNumber]();//recalls the element from the array "Timer" at position "lastNumber"
          delay(1000);
        }
        Tasse();
        tone(buzzer_pin, 3000, 500);

        Serial.println(“   tone 3000, 500 now…”);

HTH

a7

Hi wildbill,
thanks for the quick answer! I just found out that the sound contortion is not a problem as long as my code only contains one analogRead (and that observation is independent of using analogReadAsync). So in other words: as long as I only have one analogRead for one cooking plate (as provided in the code below), my sounds are as they should be. But as soon as I run the code for both cooking plates, the sounds are contorted again.
I'm confused, this is strange! :face_with_spiral_eyes:

Current code here:

/////////////////////////////////////////////////////////////////LEDmatrix
# include <LedControl.h> 
LedControl LEDMatrix = LedControl(17, 18, 19, 1); //  Pin 17 →  DATA IN Pin, Pin 18 →  CLK Pin, Pin 19 →  CS Pin 

/////////////////////////////////////////////////////////////////Lights
#define Backofenlicht 14
#define Mikrowellenlicht 12
#define HerdknopfEins A0
#define HerdknopfZwei A1
int readValueOne;  // Use this variable to read HerdknopfEins
int writeValueOne; // Use this variable for writing to HerdplatteEins
int readValueTwo;  // Use this variable to read HerdknopfZwei
int writeValueTwo; // Use this variable for writing to HerdplatteZwei
#define HerdplatteEins 10
#define HerdplatteZwei 11

/////////////////////////////////////////////////////////////////Keypad
#include <Keypad.h>
//Hier wird die größe des Keypads definiert
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
char customKey; //pressedKey entspricht in Zukunft den gedrĂĽckten Tasten
int lastNumber; //the key that was pressed before the recent one is saved if it's >0 and <10
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

/////////////////////////////////////////////////////////////////Buzzer without delay()
int buzzer_pin = 13;//the pin of the passive buzzer (be sure to ground the buzzer directly to arduino otherwise background noise will be an issue)

/*///////////////////////////////////////////////////////////////Countdown timer
unsigned long previousMillis = 0;     // will store last time stamp
unsigned long currentMillis = 0;
const long interval = 1000;           // interval at which to execute next iteration (milliseconds)*/

void Haus()
{ byte Zeichen[8]= {B00010000,B11111000,B11111100,B10011110,B10011110,B11111100,B11111000,B00010000};
  for (int zaehler = 0; zaehler < 8; zaehler ++)
  {LEDMatrix.setRow(0, zaehler, Zeichen[zaehler]);}
}

void setup()  
{   
  Serial.begin(38400);
  
  LEDMatrix.shutdown(0, false); // Matrix "aufwecken" 
  LEDMatrix.setIntensity(0,0); // Matrix auf mittlere Helligkeit setzen 
  LEDMatrix.clearDisplay(0);
  Haus();

  pinMode(Mikrowellenlicht,OUTPUT);
  digitalWrite(Mikrowellenlicht,LOW);
  pinMode(HerdplatteEins,OUTPUT);
  digitalWrite(HerdplatteEins,LOW);
  pinMode(HerdplatteZwei,OUTPUT);
  digitalWrite(HerdplatteZwei,LOW);
  pinMode(HerdknopfEins,INPUT);
  pinMode(HerdknopfZwei,INPUT);
} 


void Tasse()
{ byte Zeichen[8]= {B00000000,B11111101,B10000110,B10000101,B10000110,B11111101,B01001000,B01111000};
  for (int zaehler = 0; zaehler < 8; zaehler ++)
  {LEDMatrix.setRow(0, zaehler, Zeichen[zaehler]);}
  }

void Smiley()
{ byte Zeichen[8]= {B00000000,B00100110,B01000110,B01000000,B01000000,B01000110,B00100110,B00000000};
  for (int zaehler = 0; zaehler < 8; zaehler ++)
  {LEDMatrix.setRow(0, zaehler, Zeichen[zaehler]);}
  }

void Herz()
{ byte Zeichen[8]= {B00011100,B00111110,B01111110,B11111100,B11111100,B01111110,B00111110,B00011100};
  for (int zaehler = 0; zaehler < 8; zaehler ++)
  {LEDMatrix.setRow(0, zaehler, Zeichen[zaehler]);}
}

void PfeilUnten()
{ byte Zeichen[8] = {B00010000,B00110000,B01111111,B11111111,B01111111,B00110000,B00010000,B00000000};
  for (int zaehler = 0; zaehler < 8; zaehler ++)
  {LEDMatrix.setRow(0, zaehler, Zeichen[zaehler]);}
}

void ZahlEins()
{ byte Zeichen[8] = {B00000000,B00000000,B00000100,B10000010,B11111111,B10000000,B00000000,B00000000};
  for (int zaehler = 0; zaehler < 8; zaehler ++)
  {LEDMatrix.setRow(0, zaehler, Zeichen[zaehler]);}
}

void ZahlZwei()
{ byte Zeichen[8] = {B00000000,B10000010,B11000001,B10100001,B10010001,B10001110,B00000000,B00000000};
  for (int zaehler = 0; zaehler < 8; zaehler ++)
  {LEDMatrix.setRow(0, zaehler, Zeichen[zaehler]);}
}

void ZahlDrei()
{ byte Zeichen[8] = {B00000000,B01000010,B10000001,B10001001,B10001001,B01110110,B00000000,B00000000};
  for (int zaehler = 0; zaehler < 8; zaehler ++)
  {LEDMatrix.setRow(0, zaehler, Zeichen[zaehler]);}
}

void ZahlVier()
{ byte Zeichen[8] = {B00000000,B00110000,B00101000,B00100100,B11110010,B00100001,B00000000,B00000000};
  for (int zaehler = 0; zaehler < 8; zaehler ++)
  {LEDMatrix.setRow(0, zaehler, Zeichen[zaehler]);}
}

void ZahlFuenf()
{ byte Zeichen[8] = {B00000000,B01001111,B10001001,B10001001,B10001001,B01110001,B00000000,B00000000};
  for (int zaehler = 0; zaehler < 8; zaehler ++)
  {LEDMatrix.setRow(0, zaehler, Zeichen[zaehler]);}
}

void ZahlSechs()
{ byte Zeichen[8] = {B00000000,B01111110,B10001001,B10001001,B10001001,B01110010,B00000000,B00000000};
  for (int zaehler = 0; zaehler < 8; zaehler ++)
  {LEDMatrix.setRow(0, zaehler, Zeichen[zaehler]);}
}

void ZahlSieben()
{ byte Zeichen[8] = {B00000000,B11100001,B00010001,B00001001,B00000101,B00000011,B00000000,B00000000};
  for (int zaehler = 0; zaehler < 8; zaehler ++)
  {LEDMatrix.setRow(0, zaehler, Zeichen[zaehler]);}
}

void ZahlAcht()
{ byte Zeichen[8] = {B00000000,B01110110,B10001001,B10001001,B10001001,B01110110,B00000000,B00000000};
  for (int zaehler = 0; zaehler < 8; zaehler ++)
  {LEDMatrix.setRow(0, zaehler, Zeichen[zaehler]);}
}

void ZahlNeun()
{ byte Zeichen[8] = {B00000000,B01001110,B10010001,B10010001,B10010001,B01111110,B00000000,B00000000};
  for (int zaehler = 0; zaehler < 8; zaehler ++)
  {LEDMatrix.setRow(0, zaehler, Zeichen[zaehler]);}
}

void ZahlNull()
{ byte Zeichen[8] = {B00000000,B01111110,B10000001,B10000001,B10000001,B01111110,B00000000,B00000000};
  for (int zaehler = 0; zaehler < 8; zaehler ++)
  {LEDMatrix.setRow(0, zaehler, Zeichen[zaehler]);}
}

void Kreuz()
{ byte Zeichen[8] = {B10000001,B01000010,B00100100,B00011000,B00011000,B00100100,B01000010,B10000001};
  for (int zaehler = 0; zaehler < 8; zaehler ++)
  {LEDMatrix.setRow(0, zaehler, Zeichen[zaehler]);}
}

//Array "Timer" for a count down timer function
void (*Timer[9])(){//Adaptiert von hier: https://forum.arduino.cc/t/array-of-functions/429541/3
  ZahlEins,ZahlZwei,ZahlDrei,ZahlVier,ZahlFuenf,ZahlSechs,ZahlSieben,ZahlAcht,ZahlNeun};


void loop()  
{ 
  //////////////////////////////////////////////////////////////Herdplatten und -Knöpfe
  readValueOne = analogRead(HerdknopfEins);  //Read the voltage on the Potentiometer
  Serial.print("ReadValueOne = ");  //for debugging print your values
  Serial.print(readValueOne);
  writeValueOne = map(readValueOne, 0, 1023, 255, 0);//Calculate Write Value for LED: when read is high, write is low.
  analogWrite(HerdplatteEins, writeValueOne);      //Write to the LED
  Serial.print(";    WriteValueOne = ");  //for debugging print your values
  Serial.print(writeValueOne);
  ////////////////////////////
  /*readValueTwo = analogRead(HerdknopfZwei);  //Read the voltage on the Potentiometer
  Serial.print(";    ReadValueTwo = ");  //for debugging print your values
  Serial.print(readValueTwo);
  writeValueTwo = map(readValueTwo, 0, 1023, 255, 0);//Calculate Write Value for LED: when read is high, write is low.
  analogWrite(HerdplatteZwei, writeValueTwo);      //Write to the LED
  Serial.print(";    WriteValueTwo = ");  //for debugging print your values
  Serial.println(writeValueTwo);//*/
  
  customKey = customKeypad.getKey();//pressedKey entspricht der gedrĂĽckten Taste    
  if (customKey){//Wenn eine Taste gedrĂĽckt wurde
    //Serial.print("GedrĂĽckte Taste: ");
    //Serial.println(customKey);//Teile uns am Serial Monitor die gedrĂĽckte Taste mit und mach einen Zeilenumbruch
    int Number = customKey - 48;// customKey is char, thus 0 is 48, 1 is 49, etc.
    if(Number > 0 && Number <= 9){
      lastNumber = Number;
    }
    tone(buzzer_pin,3000,100); //generates a 100ms beep at 300 Hz on a passive buzzer
    switch (customKey)
    {
      case '1':
      ZahlEins();
      break;
      case '2':
      ZahlZwei();
      break;
      case '3':
      ZahlDrei();
      break;
      case '4':
      ZahlVier();
      break;
      case '5':
      ZahlFuenf();
      break;
      case '6':
      ZahlSechs();
      break;
      case '7':
      ZahlSieben();
      break;
      case '8':
      ZahlAcht();
      break;
      case '9':
      ZahlNeun();
      break;
      case '0':
      ZahlNull();
      break;
      case 'A':
      Herz();
      break;
      case 'B':
      Haus();
      break;
      case 'C':
      Smiley();
      break;
      case 'D':
      if(digitalRead(Mikrowellenlicht) == LOW){
        digitalWrite(Mikrowellenlicht,HIGH);
      }
      else{
        digitalWrite(Mikrowellenlicht,LOW);
      }
      break;
      case '#':
      LEDMatrix.clearDisplay(0);
      break;
      case '*':
      //Serial.print("Vorige Nummer: ");
      //Serial.println(lastNumber);
      if(digitalRead(Mikrowellenlicht) == LOW){
        digitalWrite(Mikrowellenlicht,HIGH);
      }
      tone(buzzer_pin,40,lastNumber*1000);
      for(lastNumber!=0;lastNumber>0;lastNumber--){//lastNumber ist ungleich Null
        Timer[lastNumber-1]();//recalls the element from the array "Timer" at position "lastNumber"
        delay(1000);
        }
      digitalWrite(Mikrowellenlicht,LOW);
      Tasse();
      tone(buzzer_pin,3000,700);
    }
  }
}

Edit: this absolutely makes sense now, if you read post#18! It wasn't about having one or two analogReads! It was about using pin 11 for PWM that interferes with tone() !!!

From the documentation:

Use of the tone() function will interfere with PWM output on pins 3 and 11 (on boards other than the Mega).

I would not be surprised to learn that the reverse is true too. Might be worth switching HerdplatteZwei and Mikrowellenlicht.

Also, be aware that Backofenlicht is on pin 14, which is another name for A0. Since you never use it in code, it doesn't matter unless you have two things wired to that pin.

2 Likes

I tested

but it gives me the error message

exit status 1
stray '\342' in program

which I guess is the result of using “ instead of ", isn't it?

When I serial print the duration of my 40Hz buzzer tone, the serial monitor returns the right value! So that's fine.

Now the behaviour changed a bit: only the sounds at 3000Hz are contorted. The sound at 40Hz is not. Maybe because the latter is played during the for(){} statement in my code, which makes use of delay() and thus does not run any analogRead in the meantime.

I am blown off my feet! Honestly, I've read this sentence

in the doumentation, too, but I never thought it would affect my sounds, if I use pin 11 for PWM LED lighting, only.

I just tested it and by simply exchanging pins 11 and 12 in the code (i.e. Mikrowellenlicht is pin 11 and HerdplatteZwei is pin 12, now) the sounds are as perfect as they should be! I am amazed and very grateful for your support! That was so simple!

Now, I only need to rewire Mikrowellenlicht to the right pin, connect the cooking plate items and test it further, but I'm thrilled to see it works in the "dry test". Thanks very much! I'll let you know as soon as I've done the real test with all wires set correctly.

2 Likes

Yes, yes it is and I am surprise to find that I was able to even type those wrong quotes.

This must be the first time I wrote a code fragment on my new tablet… now I go turn off and test "smart quotes", <- now dumb.

a7

1 Like

Update: using toneAC solves the problem entirely! The only problem is that toneAC requires the buzzer to be connected via two (!) PWM pins. So if you can't spare those (as I), you're in a tight spot. I solved it by switching to an Arduino Mega.

Interestingly, when using tone (and not toneAC) the Arduino Mega also contorts the sounds of the buzzer regardless of the PWM pins used for other things (at least that was my impression after a bit of experimenting). Now, with the Mega, a baud rate of 74880, toneAC and the buzzer connected to pins 11 & 12, everything runs smoothly, despite me having three (!) potentiometers read analogously. So toneAC really is a great alternative!

Thanks to all commenters for your advice and patience!
Have a great time and take care! As soon as my kitchen is really complete (currently still lacks the LED cooking plates), I'll post a picture, so that you can see what you contributed to! Cheers!