Hi, I have a noob question.
I have this sketch, that allow me to turn on/off a led thru Telegram:
/*******************************************************************
Control an LED strip using Inline Keyboard on Telegram
By Brian Lough
YouTube: https://www.youtube.com/brianlough
Tindie: https://www.tindie.com/stores/brianlough/
Twitter: https://twitter.com/witnessmenow
*******************************************************************/
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
// Library for interacting with the Telegram API
// Search for "Telegegram" in the Library manager and install
// The universal Telegram library
// https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot
#include <ArduinoJson.h>
// Library used for parsing Json from the API responses
// NOTE: There is a breaking change in the 6.x.x version,
// install the 5.x.x version instead
// Search for "Arduino Json" in the Arduino Library manager
// https://github.com/bblanchon/ArduinoJson
//------- Replace the following! ------
char ssid[] = "xxxxxxx"; // your network SSID (name)
char password[] = "xxxxxxxx"; // your network password
#define TELEGRAM_BOT_TOKEN "xxxxxxx"
//------- ---------------------- ------
#include "pitches.h"
// This is the Wifi client that supports HTTPS
WiFiClientSecure client;
UniversalTelegramBot bot(TELEGRAM_BOT_TOKEN, client);
#define LED_PIN 2 // Same as D4 for Wemos
int delayBetweenChecks = 1000;
unsigned long lastTimeChecked; //last time messages' scan has been done
unsigned long lightTimerExpires;
boolean lightTimerActive = false;
void setup() {
Serial.begin(115200);
// Set WiFi to station mode and disconnect from an AP if it was Previously connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
pinMode(2, OUTPUT);
digitalWrite(2, LOW);
// attempt to connect to Wifi network:
Serial.print("Connecting Wifi: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Only required on 2.5 Beta
client.setInsecure();
// longPoll keeps the request to Telegram open for the given amount of seconds if there are no messages
// This hugely improves response time of the bot, but is only really suitable for projects
// where the the initial interaction comes from Telegram as the requests will block the loop for
// the length of the long poll
bot.longPoll = 60;
}
void handleNewMessages(int numNewMessages) {
for (int i = 0; i < numNewMessages; i++) {
// If the type is a "callback_query", a inline keyboard button was pressed
if (bot.messages[i].type == F("callback_query")) {
String text = bot.messages[i].text;
Serial.print("Call back button pressed with text: ");
Serial.println(text);
if (text == F("ON")) {
digitalWrite(LED_PIN, HIGH);
} else if (text == F("OFF")) {
digitalWrite(LED_PIN, LOW);
} else if (text.startsWith("TIME")) {
text.replace("TIME", "");
int timeRequested = text.toInt();
digitalWrite(LED_PIN, HIGH);
lightTimerActive = true;
lightTimerExpires = millis() + (timeRequested * 1000 * 60);
}
} else {
String chat_id = String(bot.messages[i].chat_id);
String text = bot.messages[i].text;
if (text == F("/options")) {
// Keyboard Json is an array of arrays.
// The size of the main array is how many row options the keyboard has
// The size of the sub arrays is how many coloums that row has
// "The Text" property is what shows up in the keyboard
// The "callback_data" property is the text that gets sent when pressed
String keyboardJson = F("[[{ \"text\" : \"ON\", \"callback_data\" : \"ON\" },{ \"text\" : \"OFF\", \"callback_data\" : \"OFF\" }],");
keyboardJson += F("[{ \"text\" : \"10 Mins\", \"callback_data\" : \"TIME10\" }, { \"text\" : \"20 Mins\", \"callback_data\" : \"TIME20\" }, { \"text\" : \"30 Mins\", \"callback_data\" : \"TIME30\" }]]");
bot.sendMessageWithInlineKeyboard(chat_id, "Sadbhs Stars", "", keyboardJson);
}
// When a user first uses a bot they will send a "/start" command
// So this is a good place to let the users know what commands are available
if (text == F("/start")) {
bot.sendMessage(chat_id, "/options : returns the inline keyboard\n", "Markdown");
}
}
}
}
void loop() {
if (millis() > lastTimeChecked + delayBetweenChecks) {
// getUpdates returns 1 if there is a new message from Telegram
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
if (numNewMessages) {
Serial.println("got response");
handleNewMessages(numNewMessages);
}
lastTimeChecked = millis();
if (lightTimerActive && millis() > lightTimerExpires) {
lightTimerActive = false;
digitalWrite(LED_PIN, LOW);
}
}
}
Ant then, I have this Imperial March + Buzzer sketch:
const int c = 261;
const int d = 294;
const int e = 329;
const int f = 349;
const int g = 391;
const int gS = 415;
const int a = 440;
const int aS = 455;
const int b = 466;
const int cH = 523;
const int cSH = 554;
const int dH = 587;
const int dSH = 622;
const int eH = 659;
const int fH = 698;
const int fSH = 740;
const int gH = 784;
const int gSH = 830;
const int aH = 880;
const int buzzerPin = 8;
const int ledPin1 = 12;
const int ledPin2 = 13;
//const int play = 3;
int counter = 0;
void setup()
{
//Setup pin modes
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
// pinMode(play, INPUT);
}
void loop()
{
// if (in == HIGH) {
//Play first section
firstSection();
//Play second section
secondSection();
//Variant 1
beep(f, 250);
beep(gS, 500);
beep(f, 350);
beep(a, 125);
beep(cH, 500);
beep(a, 375);
beep(cH, 125);
beep(eH, 650);
delay(500);
//Repeat second section
secondSection();
//Variant 2
beep(f, 250);
beep(gS, 500);
beep(f, 375);
beep(cH, 125);
beep(a, 500);
beep(f, 375);
beep(cH, 125);
beep(a, 650);
delay(650);
//}
}
void beep(int note, int duration)
{
//Play tone on buzzerPin
tone(buzzerPin, note, duration);
//Play different LED depending on value of 'counter'
if(counter % 2 == 0)
{
digitalWrite(ledPin1, HIGH);
delay(duration);
digitalWrite(ledPin1, LOW);
}else
{
digitalWrite(ledPin2, HIGH);
delay(duration);
digitalWrite(ledPin2, LOW);
}
//Stop tone on buzzerPin
noTone(buzzerPin);
delay(50);
//Increment counter
counter++;
}
void firstSection()
{
beep(a, 500);
beep(a, 500);
beep(a, 500);
beep(f, 350);
beep(cH, 150);
beep(a, 500);
beep(f, 350);
beep(cH, 150);
beep(a, 650);
delay(500);
beep(eH, 500);
beep(eH, 500);
beep(eH, 500);
beep(fH, 350);
beep(cH, 150);
beep(gS, 500);
beep(f, 350);
beep(cH, 150);
beep(a, 650);
delay(500);
}
void secondSection()
{
beep(aH, 500);
beep(a, 300);
beep(a, 150);
beep(aH, 500);
beep(gSH, 325);
beep(gH, 175);
beep(fSH, 125);
beep(fH, 125);
beep(fSH, 250);
delay(325);
beep(aS, 250);
beep(dSH, 500);
beep(dH, 325);
beep(cSH, 175);
beep(cH, 125);
beep(b, 125);
beep(cH, 250);
delay(350);
}
What's the proper way to mix these two in one, so when the led goes HIGH the Buzzer starts playing it?
I'm really confused.