I am making a phone to phone intercom system and everything works as it should. The last thing I am struggling with is when I press the call master button it turns on pinmode 10 which I have connected to a piezo. The piezo sounds just fine but it is a steady constant tone. I would like the piezo to pulse as close to a phone ring as possible. I'm new to programming so appreciate the advice.
const byte callMaster = A0; //Room call to game master button
const byte outringTrigger = 8; //Room call to game master ring tone
const byte callRoom = A3; //Game master call to room button
const byte outringTrigger2 = 9; //Master call to room ring tone
const byte answerRoom = A1; //Master handset switch
const byte answerMaster = A2; //Room handset switch
// variables will change:
int callbuttonState1 = 0; // variable for reading the callMaster pushbutton status
int callbuttonState2 = 0; // variable for reading the answerRoom pushbutton status
int callbuttonState3 = 0; // variable for reading the answerMaster pushbutton status
int callbuttonState4 = 0; // variable for reading the callRoom pushbutton status
void setup() {
pinMode(callMaster, INPUT_PULLUP); //Call game master
pinMode(callRoom, INPUT_PULLUP); //Call room
pinMode(answerRoom, INPUT_PULLUP); //Answer room call
pinMode(answerMaster, INPUT_PULLUP); //Answer game master call
pinMode(outringTrigger, OUTPUT); //Trigger sound board input #1 outgoing ring sound (room to master)
pinMode(outringTrigger2, OUTPUT); //Trigger sound board input #6 incoming ring sound (master to room)
pinMode(10, OUTPUT); //Trigger Master Ring output to sound piezo
}//Close setup
//This loop will run over and over.
void loop() {
// read the state of the pushbutton1 value:
callbuttonState1 = digitalRead(callMaster);
// check if the pushbutton1 is pressed.
// if it is, the callbuttonState1 is HIGH:
if (callbuttonState1 == LOW) {
// change the #8 output state:
digitalWrite(outringTrigger, HIGH);
digitalWrite(10,HIGH);
// debounce:
delay(500);
}
// read the state of the master handset button:
callbuttonState2 = digitalRead(answerRoom);
// check if the master handset button is pressed.
// if it is, the callbuttonState2 is LOW:
if (callbuttonState2 == HIGH) {
// change the #8 output state:
digitalWrite(outringTrigger, LOW);
digitalWrite(10,LOW);
// debounce:
delay(500);
}
// read the state of the call room button:
callbuttonState4 = digitalRead(callRoom);
// check if the call room button is pressed.
// if it is, the callbuttonState4 is LOW:
if (callbuttonState4 == LOW) {
// change the #9 output state:
digitalWrite(outringTrigger2, LOW);
// debounce:
delay(500);
}
// read the state of the room handset button:
callbuttonState3 = digitalRead(answerMaster);
// check if the room handset button is pressed.
// if it is, the callbuttonState3 is LOW:
if (callbuttonState3 == HIGH) {
// change the #9 output state:
digitalWrite(outringTrigger2, HIGH);
// debounce:
delay(500);
}
}//Close loop
Where in the code do I place tone(pin, frequency, duration)?
I tried putting tone(10, 1000, 500) in my loop right after I tell pin 10 to go high. Now it only plays once for 500 ms and stops.
const byte callMaster = A0; //Room call to game master button
const byte outringTrigger = 8; //Room call to game master ring tone
const byte callRoom = A3; //Game master call to room button
const byte outringTrigger2 = 9; //Master call to room ring tone
const byte answerRoom = A1; //Master handset switch
const byte answerMaster = A2; //Room handset switch
// variables will change:
int callbuttonState1 = 0; // variable for reading the callMaster pushbutton status
int callbuttonState2 = 0; // variable for reading the answerRoom pushbutton status
int callbuttonState3 = 0; // variable for reading the answerMaster pushbutton status
int callbuttonState4 = 0; // variable for reading the callRoom pushbutton status
void setup() {
pinMode(callMaster, INPUT_PULLUP); //Call game master
pinMode(callRoom, INPUT_PULLUP); //Call room
pinMode(answerRoom, INPUT_PULLUP); //Answer room call
pinMode(answerMaster, INPUT_PULLUP); //Answer game master call
pinMode(outringTrigger, OUTPUT); //Trigger sound board input #1 outgoing ring sound (room to master)
pinMode(outringTrigger2, OUTPUT); //Trigger sound board input #6 incoming ring sound (master to room)
pinMode(10, OUTPUT); //Trigger Master Ring output to sound piezo
}//Close setup
//This loop will run over and over.
void loop() {
// read the state of the pushbutton1 value:
callbuttonState1 = digitalRead(callMaster);
// check if the pushbutton1 is pressed.
// if it is, the callbuttonState1 is HIGH:
if (callbuttonState1 == LOW) {
// change the #8 output state:
digitalWrite(outringTrigger, HIGH);
digitalWrite(10,HIGH);
tone(10, 1000, 500)
// debounce:
delay(500);
}
// read the state of the master handset button:
callbuttonState2 = digitalRead(answerRoom);
// check if the master handset button is pressed.
// if it is, the callbuttonState2 is LOW:
if (callbuttonState2 == HIGH) {
// change the #8 output state:
digitalWrite(outringTrigger, LOW);
digitalWrite(10,LOW);
// debounce:
delay(500);
}
// read the state of the call room button:
callbuttonState4 = digitalRead(callRoom);
// check if the call room button is pressed.
// if it is, the callbuttonState4 is LOW:
if (callbuttonState4 == LOW) {
// change the #9 output state:
digitalWrite(outringTrigger2, LOW);
// debounce:
delay(500);
}
// read the state of the room handset button:
callbuttonState3 = digitalRead(answerMaster);
// check if the room handset button is pressed.
// if it is, the callbuttonState3 is LOW:
if (callbuttonState3 == HIGH) {
// change the #9 output state:
digitalWrite(outringTrigger2, HIGH);
// debounce:
delay(500);
}
}//Close loop
How long do you want the tone to play for ?
The duration is optional.
Syntax
tone(pin, frequency)
tone(pin, frequency, duration)
Parameters
pin: the pin on which to generate the tone
frequency: the frequency of the tone in hertz - unsigned int
duration: the duration of the tone in milliseconds (optional) - unsigned long
I want it to play until the phone is answered which is my A1 input. I am using a nano if it helps. Right now, with my original code (first post) it will play until I answer but it only plays a constant tone. I would like to be able to adjust the tone it plays so I can make it play a pulse tone and control the rate at which it pulses.
You won't be able to use delay() to control the timing, because delay() blocks all other actions.
You will need to learn how to time the tone pulses using millis() and the "blink without delay" approach, so that you can also monitor other inputs. See also this tutorial on doing several things at once.
I've used millis() once before in another code so I will try and apply it to this code.
My question is where do I insert it? Do I put it in the setup or the loop?
In loop(). Study the examples and all should become clear.