So heres the bulk of the code I've made, mind you I've wandered into most the correct solutions because Im not super well verse on whats happening behind the scenes in arduino
#include <Wire.h> // Enable this line if using Arduino Uno, Mega, etc.
#include <Adafruit_GFX.h>
#include "Adafruit_LEDBackpack.h"
#include <WiFi.h> // Include the Wi-Fi library
#include <PubSubClient.h>
#define BUTTON_PIN1 12 // GIOP12 pin connected to sleep button
#define BUTTON_PIN2 14 // GIOP14 pin connected to alarm button
#define BUTTON_PIN3 27 // GIOP27 pin connected to minute button
Adafruit_7segment matrix = Adafruit_7segment();
unsigned long minute = 59, second = 59;
unsigned long countdown_time = (minute * 60000) + second*1000;
unsigned long holdTime = 5000;
int buttonState1, prevState;
int buttonState2, buttonState3;
unsigned long buttonBias, countdownBias;
long countdowntime_milliseconds;
unsigned long countdown_minute, countdown_sec;
int digit1, digit2, digit3, digit4;
// WiFi
const char *ssid = ""; // Enter your WiFi name
const char *password = ""; // Enter WiFi password
// MQTT Broker
const char *mqtt_broker = "broker.mqttdashboard.com";
const char *topic = "esp32";
const char *mqtt_username = "";
const char *mqtt_password = "public";
const int mqtt_port = 1883;
int command;
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
matrix.begin(0x70);
pinMode(BUTTON_PIN1, INPUT_PULLUP); // config GIOP12 as input pin and enable the internal pull-up resistor
pinMode(BUTTON_PIN2, INPUT_PULLUP); // config GIOP14 as input pin and enable the internal pull-up resistor
pinMode(BUTTON_PIN3, INPUT_PULLUP); // config GIOP27 as input pin and enable the internal pull-up resistor
// Set software serial baud to 115200;
Serial.begin(115200);
// connecting to a WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
//connecting to a mqtt broker
client.setServer(mqtt_broker, mqtt_port);
client.setCallback(callback);
Serial.print("Calling back yall \n");
while (!client.connected()) {
String client_id = "esp32-client-";
// client_id += String(WiFi.macAddress());
// Serial.printf("The client %s connects to the public mqtt broker\n", client_id.c_str());
if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
Serial.println("Public emqx mqtt broker connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state() + "hmm");
delay(2000);
}
}
// publish and subscribe
client.subscribe(topic);
} // end of void setup()
void loop()
{
client.loop();
storyTime();
check_Reset();
}//End of void loop
void callback(char *topic, byte *payload, unsigned int length)
{
char test[length +1];
memcpy(test, payload, length);
test[length]= '\0';
for (int i = 0; i < length; i++)
{
// Serial.print((char)payload[i]);
}//end of loop
Serial.print("\n");
// Pass messages into desired functions
storyTime(test,length+1);
}//End of void callback
void storyTime(char Test [ArraySize]="default" , int ArraySize=7 )
{
if (strcmp(Test,"OFF")!= 0 && strcmp(Test,"ON")!= 0)
{
countdowntime_milliseconds = countdown_time - millis() + countdownBias;
// Serial.println(countdowntime_milliseconds);
if (countdowntime_milliseconds >= 0)
{
countdown_minute = ((countdowntime_milliseconds / 60000)%60);
countdown_sec = (countdowntime_milliseconds/1000) % 60;
if (digitalRead(BUTTON_PIN1) == LOW){
digit1 = 9;
} else {
digit1 = countdown_minute/10;
}
if (digitalRead(BUTTON_PIN2) == LOW){
digit2 = 9;
} else {
digit2 = countdown_minute%10;
}
if (digitalRead(BUTTON_PIN3) == LOW){
digit3 = 9;
digit4 = 9;
} else {
digit3 = countdown_sec/10;
digit4 = countdown_sec%10;
}
matrix.blinkRate(0);
matrix.writeDigitNum(0, digit1);
matrix.writeDigitNum(1, digit2);
matrix.writeDigitRaw(2, 0x02);
matrix.writeDigitNum(3, digit3);
matrix.writeDigitNum(4, digit4);
matrix.writeDisplay();
}// End of if (countdowntime_milliseconds >= 0)
} else if (strcmp(Test,"OFF")== 0)
{
Serial.print("Back to blinking tweleve for eternity\n");
} //End of if (strcmp(Test,"OFF")!= 0)
}//End of storyTime
type or paste code here
So basically I want to take the payload and pass its message into the function storyTime() but because its currently meant to count storytime needs to be called in the loop to update its current time on the display I just want to be able to update storytime with out having to pass any arguments
It seems storytime is doing 2 things. Sometimes you want both things done, sometimes you want only one thing.
A function should do 1 thing....
So you should split storytime in two parts.
Problem solved???
the storytime functions purpose rn is to test if stuff works most of this is going to be replaced and repurposed what I need story time to do right now is update in the loop in a way that doesnt require me pass arguments but still allows it, getting the payload out of the callback function has been really stumper for me, if you know of other techniques that would be helpful
The compiler needs to know some details of the functions that will be used in the program that is being compiled such as its name, return type and the type of parameters that will be passed to the function. These details must be available before the function is called and can be accomplished by putting the function declaration near the start of the program
However, to make things easier that is not necessary when using the Arduino IDE which adds the early function definitions, also known as function prototypes, as part of preparing the sketch for compilation. This is a hidden process
However, there was a bug in that process that caused it to fail when a function with optional parameters was used and I am not sure if it has been fixed so I put the function prototype in manually as a precaution. That is why the function appears to be defined twice in the sketch but note that the prototype ends with a semicolon and has no code associated with it. It just provides details to the compiler
Thank you for the explanation that helps and it works, But now when it displays it just displays the most recent value and does not update the count down. Which is weird but you all have been very helpful is solving the problem I did have