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);
}
}
}